Skip to content
Snippets Groups Projects
Commit 19c87b6b authored by Bruce Jin's avatar Bruce Jin
Browse files

Squashed commit of the following

commit 2c570c96 
Author: Bruce <jtyself@amazon.com> 
Date: Wed Aug 23 2023 19:49:08 GMT-0500 (Central Daylight Time) 

    Add more tests


commit 2fa75ae6 
Author: Bruce <jtyself@amazon.com> 
Date: Wed Aug 23 2023 18:45:06 GMT-0500 (Central Daylight Time) 

    Add tests


commit a319af22 
Author: Bruce <jtyself@amazon.com> 
Date: Wed Aug 23 2023 16:52:47 GMT-0500 (Central Daylight Time) 

    Add more tests


commit 96da5b43 
Author: Bruce <jtyself@amazon.com> 
Date: Wed Aug 23 2023 16:22:42 GMT-0500 (Central Daylight Time) 

    remove unnecessary lines


commit 4117922a 
Author: Bruce <jtyself@amazon.com> 
Date: Wed Aug 23 2023 16:02:53 GMT-0500 (Central Daylight Time) 

    Remove unnecessary file


commit b2f5cb9f 
Author: Bruce <jtyself@amazon.com> 
Date: Wed Aug 23 2023 15:38:22 GMT-0500 (Central Daylight Time) 

    Fix exceptions


commit c4491db6 
Author: Bruce <jtyself@amazon.com> 
Date: Wed Aug 23 2023 15:21:40 GMT-0500 (Central Daylight Time) 

    Add tests


commit 65fa0fc6 
Author: Bruce <jtyself@amazon.com> 
Date: Wed Aug 23 2023 15:17:46 GMT-0500 (Central Daylight Time) 

    Add more tests


commit eaa8fa9b 
Author: Bruce <jtyself@amazon.com> 
Date: Wed Aug 23 2023 12:06:31 GMT-0500 (Central Daylight Time) 

    Add more tests


commit 32142ff2 
Author: Bruce <jtyself@amazon.com> 
Date: Wed Aug 23 2023 11:41:54 GMT-0500 (Central Daylight Time) 

    Add more tests


commit 0894d1da 
Author: Bruce <jtyself@amazon.com> 
Date: Wed Aug 23 2023 10:49:30 GMT-0500 (Central Daylight Time) 

    Add more tests


commit 5aafa5f1 
Author: Bruce <jtyself@amazon.com> 
Date: Wed Aug 23 2023 10:24:08 GMT-0500 (Central Daylight Time) 

    add tests


commit 48a74a75 
Author: Bruce <jtyself@amazon.com> 
Date: Fri Aug 18 2023 16:13:44 GMT-0500 (Central Daylight Time) 

    Add more tests


commit 61863de6 
Author: Bruce <jtyself@amazon.com> 
Date: Fri Aug 18 2023 15:09:19 GMT-0500 (Central Daylight Time) 

    Add more tests


commit 963f194a 
Author: Bruce <jtyself@amazon.com> 
Date: Fri Aug 18 2023 14:44:19 GMT-0500 (Central Daylight Time) 

    Add test cases


commit 0bf18891 
Author: Bruce <jtyself@amazon.com> 
Date: Fri Aug 18 2023 01:05:06 GMT-0500 (Central Daylight Time) 

    Add copyright


commit 7726b997 
Author: Bruce <jtyself@amazon.com> 
Date: Fri Aug 18 2023 00:47:34 GMT-0500 (Central Daylight Time) 

    Add test cases, and use newer version of mockito


commit bcfb6fd7 
Author: Bruce <jtyself@amazon.com> 
Date: Thu Aug 17 2023 17:38:07 GMT-0500 (Central Daylight Time) 

    use lombok


commit c98b5b65 
Author: Bruce <jtyself@amazon.com> 
Date: Thu Aug 17 2023 13:08:48 GMT-0500 (Central Daylight Time) 

    Enable ssl open

commit 21c9e0e1 
Author: Bruce <jtyself@amazon.com> 
Date: Thu Aug 17 2023 12:58:02 GMT-0500 (Central Daylight Time) 

    Elastic client handler test


commit 030fb7a0 
Author: Bruce <jtyself@amazon.com> 
Date: Thu Aug 17 2023 12:47:26 GMT-0500 (Central Daylight Time) 

    put back jacoco


commit 36a9c095 
Author: Bruce <jtyself@amazon.com> 
Date: Thu Aug 17 2023 12:39:28 GMT-0500 (Central Daylight Time) 

    remove jacoco
parent 94a95e31
No related branches found
No related tags found
1 merge request!631merge code to gitlab
Showing
with 908 additions and 6 deletions
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
\ No newline at end of file
......@@ -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>
......
// 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()));
}
}
// 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);
}
}
// 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);
}
}
......@@ -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));
}
}
}
}
// 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);
}
}
// 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();
}
}
}
// 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));
}
}
}
}
// 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));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment