Skip to content

Commit

Permalink
Fixed tests and removed unused imports
Browse files Browse the repository at this point in the history
Signed-off-by: Krishna Kondaka <[email protected]>
  • Loading branch information
Krishna Kondaka committed Oct 2, 2023
1 parent d885508 commit 1f9fc36
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Timer;
import org.apache.commons.lang3.StringUtils;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.RestHighLevelClient;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch.core.BulkRequest;
import org.opensearch.client.opensearch.core.bulk.BulkOperation;
import org.opensearch.client.opensearch.core.bulk.CreateOperation;
import org.opensearch.client.opensearch.core.bulk.UpdateOperation;
import org.opensearch.client.opensearch.core.bulk.UpdateOperationData;
import com.fasterxml.jackson.databind.JsonNode;
import org.opensearch.client.opensearch.core.bulk.DeleteOperation;
import org.opensearch.client.opensearch.core.bulk.IndexOperation;
import org.opensearch.client.transport.TransportOptions;
Expand Down Expand Up @@ -64,13 +61,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;
import org.opensearch.client.json.jsonb.JsonbJsonpMapper;
import jakarta.json.stream.JsonGenerator;

import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -86,7 +77,6 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.Iterator;

import static org.opensearch.dataprepper.logging.DataPrepperMarkers.SENSITIVE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.opensearch.dataprepper.model.configuration.PluginSetting;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.EnumUtils;
import org.opensearch.dataprepper.model.plugin.InvalidPluginConfigurationException;
import org.opensearch.dataprepper.plugins.sink.opensearch.DistributionVersion;
import org.opensearch.dataprepper.plugins.sink.opensearch.bulk.BulkAction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

package org.opensearch.dataprepper.plugins.sink.opensearch;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.opensearch.client.opensearch.core.bulk.BulkOperation;
import org.opensearch.client.opensearch.core.bulk.CreateOperation;
import org.opensearch.client.opensearch.core.bulk.DeleteOperation;
import org.opensearch.client.opensearch.core.bulk.UpdateOperation;
import org.opensearch.client.opensearch.core.bulk.IndexOperation;
import org.opensearch.dataprepper.model.event.EventHandle;
import org.junit.jupiter.api.Test;
Expand All @@ -21,7 +24,6 @@

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;

public class BulkOperationWrapperTests {
Expand Down Expand Up @@ -61,38 +63,20 @@ public void testGetId(final BulkOperation bulkOperation) {
assertThat(bulkOperationWrapper.getId(), equalTo(ID));
}

@Test
public void testGetIdUnsupportedAction() {
final BulkOperationWrapper bulkOperationWrapper = createObjectUnderTest(null, getDeleteBulkOperation());
assertThrows(UnsupportedOperationException.class, bulkOperationWrapper::getId);
}

@ParameterizedTest
@MethodSource("bulkOperationProvider")
public void testGetIndex(final BulkOperation bulkOperation) {
final BulkOperationWrapper bulkOperationWrapper = createObjectUnderTest(null, bulkOperation);
assertThat(bulkOperationWrapper.getIndex(), equalTo(INDEX));
}

@Test
public void testGetIndexUnsupportedAction() {
final BulkOperationWrapper bulkOperationWrapper = createObjectUnderTest(null, getDeleteBulkOperation());
assertThrows(UnsupportedOperationException.class, bulkOperationWrapper::getIndex);
}

@ParameterizedTest
@MethodSource("bulkOperationProvider")
public void testGetDocument(final BulkOperation bulkOperation) {
final BulkOperationWrapper bulkOperationWrapper = createObjectUnderTest(null, bulkOperation);
assertThat(bulkOperationWrapper.getDocument(), equalTo(DOCUMENT));
}

@Test
public void testGetDocumentUnsupportedAction() {
final BulkOperationWrapper bulkOperationWrapper = createObjectUnderTest(null, getDeleteBulkOperation());
assertThrows(UnsupportedOperationException.class, bulkOperationWrapper::getDocument);
}

private static Stream<Arguments> bulkOperationProvider() {
final IndexOperation indexOperation = new IndexOperation.Builder<>()
.id(ID)
Expand All @@ -112,10 +96,34 @@ private static Stream<Arguments> bulkOperationProvider() {
.create(createOperation)
.build();

ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = null;
try {
jsonNode = objectMapper.readTree("{\"key\":\"value\"}");
} catch (Exception e){}

final UpdateOperation updateOperation = new UpdateOperation.Builder<>()
.id(ID)
.index(INDEX)
.document(DOCUMENT)
.build();
final BulkOperation updateBulkOperation = (BulkOperation) new BulkOperation.Builder()
.update(updateOperation)
.build();

final DeleteOperation deleteOperation = new DeleteOperation.Builder()
.id(ID)
.index(INDEX)
.build();
final BulkOperation deleteBulkOperation = (BulkOperation) new BulkOperation.Builder()
.delete(deleteOperation)
.build();
return Stream.of(
Arguments.of(
indexBulkOperation,
createBulkOperation
createBulkOperation,
updateBulkOperation,
deleteBulkOperation
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,6 @@ public void testReadESConfig() {
assertEquals(BulkAction.INDEX.toString(), openSearchSinkConfiguration.getIndexConfiguration().getAction());
}

@Test(expected = IllegalArgumentException.class)
public void testInvalidAction() {

final Map<String, Object> metadata = new HashMap<>();
metadata.put(IndexConfiguration.INDEX_TYPE, IndexType.TRACE_ANALYTICS_RAW.getValue());
metadata.put(IndexConfiguration.ACTION, "invalid");
metadata.put(ConnectionConfiguration.HOSTS, TEST_HOSTS);

final PluginSetting pluginSetting = new PluginSetting(PLUGIN_NAME, metadata);
pluginSetting.setPipelineName(PIPELINE_NAME);

OpenSearchSinkConfiguration.readESConfig(pluginSetting);

}

@Test
public void testReadESConfigWithBulkActionCreate() {

Expand Down

0 comments on commit 1f9fc36

Please sign in to comment.