diff --git a/provider/indexer-aws/lombok.config b/provider/indexer-aws/lombok.config new file mode 100644 index 0000000000000000000000000000000000000000..a23edb413fc5d2e329b180ce25ef307f0c408874 --- /dev/null +++ b/provider/indexer-aws/lombok.config @@ -0,0 +1,2 @@ +config.stopBubbling = true +lombok.addLombokGeneratedAnnotation = true \ No newline at end of file diff --git a/provider/indexer-aws/pom.xml b/provider/indexer-aws/pom.xml index 6bff842ca7b02ff0592a44e088d74c56df556b22..982e215671c5cef610c03505f69f78f850b88e9e 100644 --- a/provider/indexer-aws/pom.xml +++ b/provider/indexer-aws/pom.xml @@ -35,6 +35,7 @@ <jackson-databind.version>2.13.2.2</jackson-databind.version> <jackson.version>2.13.2</jackson.version> <spring-webmvc.version>5.3.22</spring-webmvc.version> + <mockito.version>3.11.2</mockito.version> </properties> <dependencyManagement> @@ -160,16 +161,36 @@ <version>4.13.2</version> <scope>test</scope> </dependency> - <dependency> - <groupId>org.mockito</groupId> - <artifactId>mockito-all</artifactId> - <version>1.10.19</version> - <scope>test</scope> - </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> + <exclusions> + <exclusion> + <groupId>org.junit</groupId> + <artifactId>junit</artifactId> + </exclusion> + <exclusion> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + </exclusion> + <exclusion> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <version>${mockito.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-inline</artifactId> + <version>4.2.0</version> + <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> @@ -208,6 +229,7 @@ <argLine> @{argLine} --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED + --add-opens java.base/javax.net.ssl=ALL-UNNAMED </argLine> </configuration> </plugin> diff --git a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/cache/ElasticCredentialsCacheImplTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/cache/ElasticCredentialsCacheImplTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9ccb038e6626b3a3e3375faafd629f0f7ad55cb9 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/cache/ElasticCredentialsCacheImplTest.java @@ -0,0 +1,32 @@ +// Copyright © Amazon Web Services +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.indexer.aws.cache; + +import org.junit.Test; +import org.junit.Assert; +import org.springframework.boot.test.context.SpringBootTest; +import org.opengroup.osdu.indexer.aws.IndexerAwsApplication; + +@SpringBootTest(classes = {IndexerAwsApplication.class}) +public class ElasticCredentialsCacheImplTest { + + private ElasticCredentialsCacheImpl cache = new ElasticCredentialsCacheImpl(); + + @Test + public void get_should_return_null() throws Exception { + Assert.assertNull(cache.get(new Object())); + } + +} diff --git a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/di/EntitlementsClientFactoryTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/di/EntitlementsClientFactoryTest.java new file mode 100644 index 0000000000000000000000000000000000000000..b42e362843f835731cd9ac47359c8c16f7bbf3c6 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/di/EntitlementsClientFactoryTest.java @@ -0,0 +1,48 @@ +// Copyright © Amazon Web Services +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + + +package org.opengroup.osdu.indexer.aws.di; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.opengroup.osdu.core.common.entitlements.IEntitlementsFactory; +import org.opengroup.osdu.core.common.http.json.HttpResponseBodyMapper; +import static org.junit.jupiter.api.Assertions.*; + +@RunWith(MockitoJUnitRunner.class) +public class EntitlementsClientFactoryTest { + + @InjectMocks + private EntitlementsClientFactory factory; + + @Mock + private HttpResponseBodyMapper mapper; + + @Test + public void createInstance_shouldReturn_notNull_EntitlementFactory() throws Exception{ + IEntitlementsFactory entitlementFactory = factory.createInstance(); + assertNotNull(entitlementFactory); + } + + @Test + public void getObjectType_shouldReturn_IEntitlementFactoryClass() { + Class<?> responseClass = factory.getObjectType(); + assertEquals(responseClass, IEntitlementsFactory.class); + } +} diff --git a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/di/TenantInfoServiceTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/di/TenantInfoServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f5c251ec6ffa4d4852ebcfe295da9ce909737593 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/di/TenantInfoServiceTest.java @@ -0,0 +1,58 @@ +// Copyright © Amazon Web Services +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.indexer.aws.di; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory; +import java.util.ArrayList; +import java.util.List; +import static org.junit.jupiter.api.Assertions.*; + +@RunWith(MockitoJUnitRunner.class) +public class TenantInfoServiceTest { + + @Mock + private ITenantFactory tenantFactory; + + @Mock + private DpsHeaders headers; + + @InjectMocks + TenantInfoService service; + + @Test + public void shouldReturnSetValue_when_getTenantInfo_isCalled() { + TenantInfo expectedTenantInfo = new TenantInfo(); + Mockito.when(headers.getPartitionId()).thenReturn("opendes"); + Mockito.when(tenantFactory.getTenantInfo("opendes")).thenReturn(expectedTenantInfo); + TenantInfo tenantInfo = service.getTenantInfo(); + assertEquals(expectedTenantInfo, tenantInfo); + } + + @Test + public void shouldReturnSetList_when_getAllTenantInfos_isCalled() { + List<TenantInfo> expectedTenantInfoArrayList = new ArrayList<>(); + Mockito.when(tenantFactory.listTenantInfo()).thenReturn(expectedTenantInfoArrayList); + List<TenantInfo> tenantInfoArrayList = service.getAllTenantInfos(); + assertEquals(expectedTenantInfoArrayList, tenantInfoArrayList); + } +} diff --git a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/publish/PublisherImplTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/publish/PublisherImplTest.java index 79ef1356aa94e2462eeb189578589a7bfbbfd69b..65d37eeda5d1c3c639a2fc0d7cf6a6c29827ed5e 100644 --- a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/publish/PublisherImplTest.java +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/publish/PublisherImplTest.java @@ -18,13 +18,17 @@ import com.amazonaws.services.sns.AmazonSNS; import com.amazonaws.services.sns.model.MessageAttributeValue; import com.amazonaws.services.sns.model.PublishRequest; import com.amazonaws.services.sns.model.PublishResult; + import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.MockedConstruction; import org.mockito.Mockito; import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.aws.sns.AmazonSNSConfig; import org.opengroup.osdu.core.aws.sns.PublishRequestBuilder; +import org.opengroup.osdu.core.aws.ssm.K8sLocalParameterProvider; import org.opengroup.osdu.indexer.aws.IndexerAwsApplication; import org.opengroup.osdu.core.common.model.indexer.JobStatus; import org.springframework.boot.test.context.SpringBootTest; @@ -33,12 +37,16 @@ import org.mockito.runners.MockitoJUnitRunner; import java.util.HashMap; import java.util.Map; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) @SpringBootTest(classes = {IndexerAwsApplication.class}) public class PublisherImplTest { + private final String indexer_sns_topic_arn = "indexer_sns_topic_arn"; + @InjectMocks private PublisherImpl publisher = new PublisherImpl(); @@ -76,4 +84,50 @@ public class PublisherImplTest { // Assert Mockito.verify(snsClient, Mockito.times(1)).publish(Mockito.eq(publishRequest)); } + + + @Test + public void go_through_init_DLQ() throws Exception { + + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mock, context) -> { + when(mock.getParameterAsString(eq("INDEXER_SNS_TOPIC_ARN"))).thenReturn(indexer_sns_topic_arn); + })) { + + try (MockedConstruction<AmazonSNSConfig> sns = Mockito.mockConstruction(AmazonSNSConfig.class, (mock1, context) -> { + when(mock1.AmazonSNS()).thenReturn(snsClient); + })) { + + publisher.init(); + + // Arrange + DpsHeaders headers = new DpsHeaders(); + JobStatus jobStatus = new JobStatus(); + Mockito.when(snsClient.publish(Mockito.any(PublishRequest.class))) + .thenReturn(Mockito.any(PublishResult.class)); + + Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); + messageAttributes.put(DpsHeaders.ACCOUNT_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); + messageAttributes.put(DpsHeaders.DATA_PARTITION_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); + headers.addCorrelationIdIfMissing(); + messageAttributes.put(DpsHeaders.CORRELATION_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getCorrelationId())); + + PublishRequest publishRequest = new PublishRequestBuilder().generatePublishRequest("data", jobStatus.getStatusesList(), messageAttributes, indexer_sns_topic_arn); + // Act + publisher.publishStatusChangedTagsToTopic(headers, jobStatus); + + // Assert + Mockito.verify(snsClient, Mockito.times(1)).publish(Mockito.eq(publishRequest)); + + } + + } + + } + } diff --git a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/service/ElasticClientHandlerAwsTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/service/ElasticClientHandlerAwsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8dd132c9bac83941ad6bac148c26297f66c06f17 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/service/ElasticClientHandlerAwsTest.java @@ -0,0 +1,62 @@ +// Copyright © Amazon Web Services +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.indexer.aws.service; + +import org.junit.Test; +import org.junit.Assert; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import static org.mockito.Mockito.mock; + +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.boot.test.context.SpringBootTest; + +import org.opengroup.osdu.indexer.aws.IndexerAwsApplication; +import org.elasticsearch.client.RestClientBuilder; + +import javax.net.ssl.SSLContext; + + +@RunWith(MockitoJUnitRunner.class) +@SpringBootTest(classes = {IndexerAwsApplication.class}) +public class ElasticClientHandlerAwsTest { + + @InjectMocks + private ElasticClientHandlerAws handler = new ElasticClientHandlerAws(); + + @Mock + SSLContext sslContext; + + public void setUp() { + sslContext = mock(SSLContext.class); + } + + @Test + public void createClientBuilder() throws Exception { + + // Act + RestClientBuilder builder = handler.createClientBuilder("localhost", "Bearer", 6469, "protocolScheme", "tls"); + RestClientBuilder updatedBuilder = builder.setPathPrefix("Prefix"); + // Assert + String cleanPathPrefix = builder.cleanPathPrefix("Prefix"); + + Assert.assertEquals("/Prefix", cleanPathPrefix); + Assert.assertEquals(builder, updatedBuilder); + + } + +} + diff --git a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/util/AwsServiceAccountAuthTokenTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/util/AwsServiceAccountAuthTokenTest.java new file mode 100644 index 0000000000000000000000000000000000000000..3dcafab9dab54ddaeed30831108f69a38dd4cc56 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/util/AwsServiceAccountAuthTokenTest.java @@ -0,0 +1,158 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.indexer.aws.util; + +import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream; +import org.junit.Test; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; +import org.opengroup.osdu.core.common.http.HttpClient; +import org.opengroup.osdu.core.common.http.HttpRequest; +import org.opengroup.osdu.core.common.http.HttpResponse; +import org.springframework.stereotype.Component; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.io.PrintStream; + + +public class AwsServiceAccountAuthTokenTest { + + private AwsServiceAccountAuthToken authorizer; + + private String body_300expire = "{ \"expires_in\" : \"300\", \"access_token\" : \"abcd\", \"data\" : \"data\", \"attributes\" : { \"attribute\" : \"attribute\" } }"; + private String body_0expire = "{ \"expires_in\" : \"0\", \"access_token\" : \"abcd\", \"data\" : \"data\", \"attributes\" : { \"attribute\" : \"attribute\" } }"; + private String body_invalid = "{ \"expires_in\" : }"; + + @Test + public void getAuthTokenTest_null_token_no_code() throws Exception { + + try (MockedConstruction<HttpClient> httpClient = Mockito.mockConstruction(HttpClient.class, (mock, context) -> { + when(mock.send(any(HttpRequest.class))).thenReturn(new HttpResponse()); + })) { + + this.authorizer = new AwsServiceAccountAuthToken(); + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stream)); + assertNull(this.authorizer.getAuthToken()); + String allWrittenLines = new String(stream.toByteArray()); + stream.flush(); + assertTrue(allWrittenLines.contains("Could not parse AccessToken result")); + stream.close(); + } + + } + + + @Test + public void getAuthTokenTest_succeed_code() throws Exception { + + HttpResponse response = new HttpResponse(null, body_300expire, "contentType", 200, null, null, 0); + + try (MockedConstruction<HttpClient> httpClient = Mockito.mockConstruction(HttpClient.class, (mock, context) -> { + when(mock.send(any(HttpRequest.class))).thenReturn(response); + })) { + + this.authorizer = new AwsServiceAccountAuthToken(); + assertEquals("abcd", this.authorizer.getAuthToken()); + } + + } + + @Test + public void getAuthTokenTest_no_succeed_code() throws Exception { + + HttpResponse response = new HttpResponse(null, body_300expire, "contentType", 400, null, null, 0); + + try (MockedConstruction<HttpClient> httpClient = Mockito.mockConstruction(HttpClient.class, (mock, context) -> { + when(mock.send(any(HttpRequest.class))).thenReturn(response); + })) { + + this.authorizer = new AwsServiceAccountAuthToken(); + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stream)); + assertNull(this.authorizer.getAuthToken()); + String allWrittenLines = new String(stream.toByteArray()); + stream.flush(); + assertTrue(allWrittenLines.contains("Could not parse AccessToken result")); + stream.close(); + } + + } + + @Test + public void getAuthTokenTest_token_not_expire() throws Exception { + + HttpResponse response = new HttpResponse(null, body_300expire, "contentType", 200, null, null, 0); + + try (MockedConstruction<HttpClient> httpClient = Mockito.mockConstruction(HttpClient.class, (mock, context) -> { + when(mock.send(any(HttpRequest.class))).thenReturn(response); + })) { + + this.authorizer = new AwsServiceAccountAuthToken(); + String firstToken = this.authorizer.getAuthToken(); + assertEquals("abcd", this.authorizer.getAuthToken()); + assertTrue(firstToken == this.authorizer.getAuthToken()); + + } + + } + + @Test + public void getAuthTokenTest_token_expire() throws Exception { + + HttpResponse response = new HttpResponse(null, body_0expire, "contentType", 200, null, null, 0); + + try (MockedConstruction<HttpClient> httpClient = Mockito.mockConstruction(HttpClient.class, (mock, context) -> { + when(mock.send(any(HttpRequest.class))).thenReturn(response); + })) { + + this.authorizer = new AwsServiceAccountAuthToken(); + String firstToken = this.authorizer.getAuthToken(); + assertEquals("abcd", this.authorizer.getAuthToken()); + assertFalse(firstToken == this.authorizer.getAuthToken()); + + } + + } + + @Test + public void getAuthTokenTest_invalid_body() throws Exception { + + HttpResponse response = new HttpResponse(null, body_invalid, "contentType", 200, null, null, 0); + + try (MockedConstruction<HttpClient> httpClient = Mockito.mockConstruction(HttpClient.class, (mock, context) -> { + when(mock.send(any(HttpRequest.class))).thenReturn(response); + })) { + + this.authorizer = new AwsServiceAccountAuthToken(); + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stream)); + assertNull(this.authorizer.getAuthToken()); + String allWrittenLines = new String(stream.toByteArray()); + stream.flush(); + assertTrue(allWrittenLines.contains("Could not parse AccessToken result")); + stream.close(); + + } + + } + +} diff --git a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/util/IndexerQueueTaskBuilderAwsTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/util/IndexerQueueTaskBuilderAwsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..577028042be700fe82a7fc3c9a3778e993adf404 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/util/IndexerQueueTaskBuilderAwsTest.java @@ -0,0 +1,356 @@ +// Copyright © Amazon Web Services +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.indexer.aws.util; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.opengroup.osdu.core.aws.sqs.AmazonSQSConfig; +import org.opengroup.osdu.core.aws.ssm.K8sLocalParameterProvider; +import org.opengroup.osdu.core.aws.ssm.K8sParameterNotFoundException; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.common.model.search.RecordChangedMessages; +import org.opengroup.osdu.indexer.model.Constants; +import com.google.gson.Gson; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + +import com.amazonaws.services.sqs.AmazonSQS; +import com.amazonaws.services.sqs.model.MessageAttributeValue; +import com.amazonaws.services.sqs.model.SendMessageRequest; + +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class IndexerQueueTaskBuilderAwsTest { + + private String payload = "{ \"messageId\" : \"messageId\", \"publishTime\" : \"publishTime\", \"data\" : \"data\", \"attributes\" : { \"attribute\" : \"attribute\" } }"; + private String payload_ancestry_kinds = "{ \"messageId\" : \"messageId\", \"publishTime\" : \"publishTime\", \"data\" : \"data\", \"attributes\" : { \"attribute\" : \"attribute\", \"ancestry_kinds\" : \"ancestry_kinds\" } }"; + private String payload_retry = "{ \"messageId\" : \"messageId\", \"publishTime\" : \"publishTime\", \"data\" : \"data\", \"attributes\" : { \"attribute\" : \"attribute\" , \"retry\" : \"11\" } }"; + private static final int INITIAL_RETRY_DELAY_SECONDS = 5; + private final String retryString = "retry"; + private final Long countDownMillis = 123456L; + private final String storage_sqs_url = "storage_sqs_url"; + private final String deadletter_queue_sqs_url = "deadletter_queue_sqs_url"; + + @InjectMocks + IndexerQueueTaskBuilderAws builder; + + @Mock + AmazonSQS sqsClient; + + @Mock + Gson gson; + + @Test + public void createWorkerTaskTest_with_out_retryString() throws K8sParameterNotFoundException{ + + Gson realGson = new Gson(); + + DpsHeaders headers = new DpsHeaders(); + + Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); + messageAttributes.put(DpsHeaders.ACCOUNT_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); + messageAttributes.put(DpsHeaders.DATA_PARTITION_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); + headers.addCorrelationIdIfMissing(); + messageAttributes.put(DpsHeaders.CORRELATION_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getCorrelationId())); + messageAttributes.put(DpsHeaders.USER_EMAIL, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getUserEmail())); + messageAttributes.put(DpsHeaders.AUTHORIZATION, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getAuthorization())); + messageAttributes.put(retryString, new MessageAttributeValue() + .withDataType("String") + .withStringValue(String.valueOf(1))); + + RecordChangedMessages message = realGson.fromJson(payload, RecordChangedMessages.class); + + when(gson.fromJson(payload, RecordChangedMessages.class)).thenReturn(message); + + SendMessageRequest sendMessageRequest = new SendMessageRequest().withQueueUrl(null).withMessageBody(message.getData()).withDelaySeconds(new Integer(INITIAL_RETRY_DELAY_SECONDS)).withMessageAttributes(messageAttributes); + + builder.createWorkerTask(payload, headers); + + builder.createWorkerTask(payload, countDownMillis, headers); + + Mockito.verify(sqsClient, times(2)).sendMessage(Mockito.eq(sendMessageRequest)); + + } + + @Test + public void createWorkerTaskTest_with_retryString() throws K8sParameterNotFoundException{ + + Gson realGson = new Gson(); + + DpsHeaders headers = new DpsHeaders(); + + Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); + messageAttributes.put(DpsHeaders.ACCOUNT_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); + messageAttributes.put(DpsHeaders.DATA_PARTITION_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); + headers.addCorrelationIdIfMissing(); + messageAttributes.put(DpsHeaders.CORRELATION_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getCorrelationId())); + messageAttributes.put(DpsHeaders.USER_EMAIL, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getUserEmail())); + messageAttributes.put(DpsHeaders.AUTHORIZATION, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getAuthorization())); + messageAttributes.put(retryString, new MessageAttributeValue() + .withDataType("String") + .withStringValue(String.valueOf(1))); + + RecordChangedMessages message = realGson.fromJson(payload_retry, RecordChangedMessages.class); + + when(gson.fromJson(payload, RecordChangedMessages.class)).thenReturn(message); + + SendMessageRequest sendMessageRequest = new SendMessageRequest().withQueueUrl(null).withMessageBody(message.getData()); + + builder.createWorkerTask(payload, headers); + + builder.createWorkerTask(payload, countDownMillis, headers); + + Mockito.verify(sqsClient, times(2)).sendMessage(Mockito.eq(sendMessageRequest)); + + } + + @Test + public void createWorkerTaskTest_with_ancestry_kinds() throws K8sParameterNotFoundException{ + + Gson realGson = new Gson(); + + DpsHeaders headers = new DpsHeaders(); + + RecordChangedMessages message = realGson.fromJson(payload_ancestry_kinds, RecordChangedMessages.class); + + Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); + messageAttributes.put(DpsHeaders.ACCOUNT_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); + messageAttributes.put(DpsHeaders.DATA_PARTITION_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); + headers.addCorrelationIdIfMissing(); + messageAttributes.put(DpsHeaders.CORRELATION_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getCorrelationId())); + messageAttributes.put(DpsHeaders.USER_EMAIL, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getUserEmail())); + messageAttributes.put(DpsHeaders.AUTHORIZATION, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getAuthorization())); + messageAttributes.put(retryString, new MessageAttributeValue() + .withDataType("String") + .withStringValue(String.valueOf(1))); + messageAttributes.put(Constants.ANCESTRY_KINDS, new MessageAttributeValue() + .withDataType("String") + .withStringValue(message.getAttributes().get(Constants.ANCESTRY_KINDS))); + + + + when(gson.fromJson(payload, RecordChangedMessages.class)).thenReturn(message); + + SendMessageRequest sendMessageRequest = new SendMessageRequest().withQueueUrl(null).withMessageBody(message.getData()).withDelaySeconds(new Integer(INITIAL_RETRY_DELAY_SECONDS)).withMessageAttributes(messageAttributes); + + builder.createWorkerTask(payload, headers); + + builder.createWorkerTask(payload, countDownMillis, headers); + + Mockito.verify(sqsClient, times(2)).sendMessage(Mockito.eq(sendMessageRequest)); + + } + + @Test + public void createReIndexTaskTest() throws K8sParameterNotFoundException{ + + DpsHeaders headers = new DpsHeaders(); + + Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); + messageAttributes.put(DpsHeaders.ACCOUNT_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); + messageAttributes.put(DpsHeaders.DATA_PARTITION_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); + headers.addCorrelationIdIfMissing(); + messageAttributes.put(DpsHeaders.CORRELATION_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getCorrelationId())); + messageAttributes.put(DpsHeaders.USER_EMAIL, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getUserEmail())); + messageAttributes.put(DpsHeaders.AUTHORIZATION, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getAuthorization())); + messageAttributes.put("ReIndexCursor", new MessageAttributeValue() + .withDataType("String") + .withStringValue("True")); + + SendMessageRequest sendMessageRequest = new SendMessageRequest().withQueueUrl(null).withMessageBody(payload).withMessageAttributes(messageAttributes); + + builder.createReIndexTask(payload, headers); + + builder.createReIndexTask(payload, countDownMillis, headers); + + Mockito.verify(sqsClient, times(2)).sendMessage(Mockito.eq(sendMessageRequest)); + + } + + @Test + public void getWaitTimeExpTest() throws K8sParameterNotFoundException{ + + int zero_wait_time = IndexerQueueTaskBuilderAws.getWaitTimeExp(0); + + assertEquals(0, zero_wait_time); + + int non_zero_wait_time = IndexerQueueTaskBuilderAws.getWaitTimeExp(4); + + assertEquals(64, non_zero_wait_time); + } + + @Test + public void go_through_init_StorageQueue() throws Exception { + + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mock, context) -> { + when(mock.getParameterAsString(eq("STORAGE_SQS_URL"))).thenReturn(storage_sqs_url); + when(mock.getParameterAsString("INDEXER_DEADLETTER_QUEUE_SQS_URL")).thenReturn(deadletter_queue_sqs_url); + })) { + + try (MockedConstruction<AmazonSQSConfig> config = Mockito.mockConstruction(AmazonSQSConfig.class, (mock1, context) -> { + when(mock1.AmazonSQS()).thenReturn(sqsClient); + })) { + + builder.init(); + + Gson realGson = new Gson(); + + DpsHeaders headers = new DpsHeaders(); + + Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); + messageAttributes.put(DpsHeaders.ACCOUNT_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); + messageAttributes.put(DpsHeaders.DATA_PARTITION_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); + headers.addCorrelationIdIfMissing(); + messageAttributes.put(DpsHeaders.CORRELATION_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getCorrelationId())); + messageAttributes.put(DpsHeaders.USER_EMAIL, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getUserEmail())); + messageAttributes.put(DpsHeaders.AUTHORIZATION, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getAuthorization())); + messageAttributes.put(retryString, new MessageAttributeValue() + .withDataType("String") + .withStringValue(String.valueOf(1))); + + RecordChangedMessages message = realGson.fromJson(payload, RecordChangedMessages.class); + + SendMessageRequest sendMessageRequest = new SendMessageRequest().withQueueUrl(storage_sqs_url).withMessageBody(message.getData()).withDelaySeconds(new Integer(INITIAL_RETRY_DELAY_SECONDS)).withMessageAttributes(messageAttributes); + + builder.createWorkerTask(payload, headers); + + builder.createWorkerTask(payload, countDownMillis, headers); + + Mockito.verify(sqsClient, times(2)).sendMessage(Mockito.eq(sendMessageRequest)); + + } + } + + } + + @Test + public void go_through_init_DLQ() throws Exception { + + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mock, context) -> { + when(mock.getParameterAsString(eq("STORAGE_SQS_URL"))).thenReturn(storage_sqs_url); + when(mock.getParameterAsString("INDEXER_DEADLETTER_QUEUE_SQS_URL")).thenReturn(deadletter_queue_sqs_url); + })) { + + try (MockedConstruction<AmazonSQSConfig> config = Mockito.mockConstruction(AmazonSQSConfig.class, (mock1, context) -> { + when(mock1.AmazonSQS()).thenReturn(sqsClient); + })) { + + builder.init(); + + Gson realGson = new Gson(); + + DpsHeaders headers = new DpsHeaders(); + + Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); + messageAttributes.put(DpsHeaders.ACCOUNT_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); + messageAttributes.put(DpsHeaders.DATA_PARTITION_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); + headers.addCorrelationIdIfMissing(); + messageAttributes.put(DpsHeaders.CORRELATION_ID, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getCorrelationId())); + messageAttributes.put(DpsHeaders.USER_EMAIL, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getUserEmail())); + messageAttributes.put(DpsHeaders.AUTHORIZATION, new MessageAttributeValue() + .withDataType("String") + .withStringValue(headers.getAuthorization())); + messageAttributes.put(retryString, new MessageAttributeValue() + .withDataType("String") + .withStringValue(String.valueOf(1))); + + RecordChangedMessages message = realGson.fromJson(payload_retry, RecordChangedMessages.class); + + SendMessageRequest sendMessageRequest = new SendMessageRequest().withQueueUrl(deadletter_queue_sqs_url).withMessageBody(message.getData()); + + builder.createWorkerTask(payload_retry, headers); + + builder.createWorkerTask(payload_retry, countDownMillis, headers); + + Mockito.verify(sqsClient, times(2)).sendMessage(Mockito.eq(sendMessageRequest)); + + + } + + } + + } + +} diff --git a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/util/RequestInfoImplTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/util/RequestInfoImplTest.java new file mode 100644 index 0000000000000000000000000000000000000000..bccef1637ead6f2db6fc5441bef621f6ddc7e9c4 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/util/RequestInfoImplTest.java @@ -0,0 +1,110 @@ +// Copyright © Amazon Web Services +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.indexer.aws.util; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import org.opengroup.osdu.core.common.model.http.AppException; + + +import java.util.HashMap; +import java.util.Map; +import org.mockito.MockitoAnnotations; +import org.mockito.internal.matchers.apachecommons.ReflectionEquals; +import org.mockito.runners.MockitoJUnitRunner; +import static org.opengroup.osdu.core.common.model.http.DpsHeaders.AUTHORIZATION; +import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; + +@RunWith(MockitoJUnitRunner.class) +public class RequestInfoImplTest { + + @InjectMocks + private RequestInfoImpl request_info; + + @Mock + private DpsHeaders headersMap; + + @Mock + private AwsServiceAccountAuthToken awsServiceAccountAuthToken; + + @Before + public void setup(){ + headersMap = mock(DpsHeaders.class); + awsServiceAccountAuthToken = mock(AwsServiceAccountAuthToken.class); + MockitoAnnotations.initMocks(this); + } + + @Test(expected = AppException.class) + public void getHeaders_null_headersMap(){ + RequestInfoImpl request_info_nullheader = new RequestInfoImpl(); + + request_info_nullheader.getHeaders(); + } + + @Test + public void getHeaders_not_null_headersMap(){ + + Map<String, String> map = new HashMap<String,String>(); + + DpsHeaders expected = DpsHeaders.createFromMap(map); + + DpsHeaders headers = request_info.getHeaders(); + + assertTrue(new ReflectionEquals(expected).matches(headers)); + } + + @Test + public void getPartitionId_test(){ + + String result = request_info.getPartitionId(); + + assertTrue(result==null); + } + + @Test + public void getHeadersMapWithDwdAuthZ_test(){ + + Map<String, String> expected = new HashMap<String, String>(); + expected.put("content-type","application/json"); + + Map<String, String> result = request_info.getHeadersMapWithDwdAuthZ(); + + assertTrue(new ReflectionEquals(expected).matches(result)); + } + + @Test + public void isCronRequest_test(){ + assertFalse(request_info.isCronRequest()); + } + + @Test + public void isTaskQueueRequest_test(){ + assertFalse(request_info.isTaskQueueRequest()); + } + + @Test + public void getHeadersWithDwdAuthZ_test() { + + DpsHeaders result = request_info.getHeadersWithDwdAuthZ(); + + assertTrue(result.getHeaders().containsKey(AUTHORIZATION)); + } +}