diff --git a/NOTICE b/NOTICE index 0c83edae70b1713c5c3058bb9f39f22c1090a187..898d373bc003636b1513f156c8750d7a37c3380b 100644 --- a/NOTICE +++ b/NOTICE @@ -326,7 +326,6 @@ The following software have components provided under the terms of this license: - Apache Ant Launcher (from https://ant.apache.org/) - Apache Commons BeanUtils (from http://commons.apache.org/proper/commons-beanutils/, https://commons.apache.org/proper/commons-beanutils/, https://repo1.maven.org/maven2/commons-beanutils/commons-beanutils) - Apache Commons Codec (from http://commons.apache.org/proper/commons-codec/, https://commons.apache.org/proper/commons-codec/) -- Apache Commons Collections (from https://commons.apache.org/proper/commons-collections/) - Apache Commons IO (from http://commons.apache.org/io/, https://commons.apache.org/proper/commons-io/, https://repo1.maven.org/maven2/commons-io/commons-io) - Apache Commons Lang (from https://commons.apache.org/proper/commons-lang/) - Apache Commons Logging (from http://commons.apache.org/logging/, http://commons.apache.org/proper/commons-logging/) @@ -452,7 +451,6 @@ The following software have components provided under the terms of this license: - Java Libraries for Amazon Simple WorkFlow (from https://github.com/aws/aws-swf-flow-library) - Java Native Access (from https://github.com/java-native-access/jna, https://github.com/twall/jna) - Java Native Access Platform (from https://github.com/java-native-access/jna) -- Java UUID Generator (from http://wiki.fasterxml.com/JugHome) - JavaBeans Activation Framework (from <http://java.sun.com/javase/technologies/desktop/javabeans/jaf/index.jsp>, http://java.sun.com/javase/technologies/desktop/javabeans/jaf/index.jsp, https://repo1.maven.org/maven2/com/sun/activation/javax.activation) - Javassist (from http://www.javassist.org/) - JetBrains Java Annotations (from https://github.com/JetBrains/java-annotations) @@ -501,7 +499,7 @@ The following software have components provided under the terms of this license: - Microsoft Azure Java Core Library (from https://github.com/Azure/azure-sdk-for-java) - Microsoft Azure Netty HTTP Client Library (from https://github.com/Azure/azure-sdk-for-java) - Microsoft Azure SDK for SQL API of Azure Cosmos DB Service (from https://github.com/Azure/azure-sdk-for-java) -- Mockito (from http://mockito.org, http://www.mockito.org, https://github.com/mockito/mockito) +- Mockito (from http://mockito.org, https://github.com/mockito/mockito) - Mojo's Maven plugin for Cobertura (from http://mojo.codehaus.org/cobertura-maven-plugin/) - MongoDB Driver (from https://www.mongodb.com/) - MongoDB Java Driver (from http://mongodb.org/, http://www.mongodb.org, https://www.mongodb.com/) @@ -610,7 +608,6 @@ The following software have components provided under the terms of this license: - aalto-xml (from https://github.com/FasterXML/aalto-xml, https://repo1.maven.org/maven2/com/fasterxml/aalto-xml) - aggs-matrix-stats (from https://github.com/elastic/elasticsearch, https://github.com/elastic/elasticsearch.git) - compiler (from http://github.com/spullara/mustache.java) -- documentdb-bulkexecutor (from http://azure.microsoft.com/en-us/services/documentdb/) - elasticsearch (from https://github.com/elastic/elasticsearch, https://repo1.maven.org/maven2/org/elasticsearch/elasticsearch) - elasticsearch-cli (from https://github.com/elastic/elasticsearch) - elasticsearch-core (from https://github.com/elastic/elasticsearch) @@ -759,7 +756,6 @@ The following software have components provided under the terms of this license: - Microsoft Application Insights Java SDK Spring Boot starter (from https://github.com/Microsoft/ApplicationInsights-Java) - Microsoft Application Insights Java SDK Web Module (from https://github.com/Microsoft/ApplicationInsights-Java) - Microsoft Application Insights Log4j 2 Appender (from https://github.com/Microsoft/ApplicationInsights-Java) -- Mockito (from http://mockito.org, http://www.mockito.org, https://github.com/mockito/mockito) - Netty/Codec/HTTP (from https://repo1.maven.org/maven2/io/netty/netty-codec-http) - Plexus Common Utilities (from http://plexus.codehaus.org/plexus-utils, https://repo1.maven.org/maven2/org/codehaus/plexus/plexus-utils) - Protocol Buffer Java API (from http://code.google.com/p/protobuf, https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java) @@ -1170,7 +1166,7 @@ The following software have components provided under the terms of this license: - Microsoft Azure client library for KeyVault Secrets (from https://github.com/Azure/azure-sdk-for-java) - Microsoft Azure common module for Storage (from https://github.com/Azure/azure-sdk-for-java) - Microsoft Azure internal Avro module for Storage (from https://github.com/Azure/azure-sdk-for-java) -- Mockito (from http://mockito.org, http://www.mockito.org, https://github.com/mockito/mockito) +- Mockito (from http://mockito.org, https://github.com/mockito/mockito) - MongoDB Java Driver (from http://mongodb.org/, http://www.mongodb.org, https://www.mongodb.com/) - Netty/Codec/HTTP (from https://repo1.maven.org/maven2/io/netty/netty-codec-http) - Netty/Common (from https://repo1.maven.org/maven2/io/netty/netty-common) @@ -1182,8 +1178,6 @@ The following software have components provided under the terms of this license: - Spring Data for Azure Cosmos DB SQL API (from https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/cosmos/azure-spring-data-cosmos) - ThreeTen backport (from https://github.com/ThreeTen/threetenbp, https://www.threeten.org/threetenbp) - adal4j (from https://github.com/AzureAD/azure-activedirectory-library-for-java) -- azure-documentdb (from http://azure.microsoft.com/en-us/services/documentdb/, https://azure.microsoft.com/en-us/services/cosmos-db/) -- documentdb-bulkexecutor (from http://azure.microsoft.com/en-us/services/documentdb/) - jersey-container-servlet (from https://repo1.maven.org/maven2/org/glassfish/jersey/containers/jersey-container-servlet) - jersey-container-servlet-core (from https://repo1.maven.org/maven2/org/glassfish/jersey/containers/jersey-container-servlet-core) - jersey-core-client (from https://repo1.maven.org/maven2/org/glassfish/jersey/core/jersey-client) @@ -1195,6 +1189,7 @@ The following software have components provided under the terms of this license: - jersey-media-json-jackson (from https://repo1.maven.org/maven2/org/glassfish/jersey/media/jersey-media-json-jackson) - jersey-spring5 (from https://repo1.maven.org/maven2/org/glassfish/jersey/ext/jersey-spring5) - micrometer-core (from https://github.com/micrometer-metrics/micrometer) +- mockito-inline (from http://mockito.org, https://github.com/mockito/mockito) - mockito-junit-jupiter (from https://github.com/mockito/mockito) - msal4j (from https://github.com/AzureAD/microsoft-authentication-library-for-java) - msal4j-persistence-extension (from https://github.com/AzureAD/microsoft-authentication-extensions-for-java) diff --git a/devops/gc/deploy/templates/deployment.yaml b/devops/gc/deploy/templates/deployment.yaml index 9df366d0c2d008a85fd5ea59606d2b5d6e40c670..67a8eff91033e08f587d7e4b756ee4dac1fc2771 100644 --- a/devops/gc/deploy/templates/deployment.yaml +++ b/devops/gc/deploy/templates/deployment.yaml @@ -9,7 +9,7 @@ spec: selector: matchLabels: app: {{ .Values.conf.appName | quote }} - replicas: 1 + replicas: {{ .Values.conf.replicas }} template: metadata: labels: diff --git a/devops/gc/deploy/values.yaml b/devops/gc/deploy/values.yaml index a5e0dd84cf454af10a67f9d29a73281d1da99315..5f5143d606ce596a0a681caf669909064b18c005 100644 --- a/devops/gc/deploy/values.yaml +++ b/devops/gc/deploy/values.yaml @@ -37,6 +37,7 @@ conf: keycloakSecretName: "indexer-keycloak-secret" rabbitmqSecretName: "rabbitmq-secret" indexerRedisSecretName: "indexer-redis-secret" + replicas: 1 istio: proxyCPU: "10m" diff --git a/indexer-core/pom.xml b/indexer-core/pom.xml index b1af3bf7c09b4873401f8f6815244c4abff7e6b3..7070d03050178c5ba6a576bfbcd0efe1a23c8650 100644 --- a/indexer-core/pom.xml +++ b/indexer-core/pom.xml @@ -4,12 +4,12 @@ <parent> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-service</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <artifactId>indexer-core</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <name>indexer-core</name> <description>Indexer Service Core</description> <packaging>jar</packaging> diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/model/SearchRequest.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/model/SearchRequest.java index 7af9c3089fc993054b851ada52fd1cac79aed410..11931468c14f583deda5b30dbd64c8b1573e9ace 100644 --- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/model/SearchRequest.java +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/model/SearchRequest.java @@ -33,5 +33,5 @@ public class SearchRequest { private String cursor; private List<String> returnedFields; private SortQuery sort; - private boolean trackTotalCount = true; + private boolean trackTotalCount; } diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/StorageServiceImpl.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/StorageServiceImpl.java index cfcf8ce96187e5918a133e31800ea6731f609263..fecc1082db1ee0103ac986bfd83a025831c6a68f 100644 --- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/StorageServiceImpl.java +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/StorageServiceImpl.java @@ -147,8 +147,9 @@ public class StorageServiceImpl implements StorageService { } try { Records records = this.objectMapper.readValue(response.getBody(), Records.class); - ids.removeAll(records.getRecords().stream().map(Records.Entity::getId).collect(Collectors.toList())); - records.setNotFound(ids); + List<String> notFoundRecordIds = new ArrayList<>(ids); + notFoundRecordIds.removeAll(records.getRecords().stream().map(Records.Entity::getId).collect(Collectors.toList())); + records.setNotFound(notFoundRecordIds); return records; } catch (JsonProcessingException e) { throw new AppException(RequestStatus.INVALID_RECORD, "Invalid request", "Successful Storage service response with wrong json", e); diff --git a/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/StorageServiceImplTest.java b/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/StorageServiceImplTest.java index 437f8714f53d299f09f6da9d80e5c3169edd77e6..07220cddc238631f73078ca3b455517ae0231947 100644 --- a/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/StorageServiceImplTest.java +++ b/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/StorageServiceImplTest.java @@ -190,6 +190,43 @@ public class StorageServiceImplTest { @Test public void should_returnStorageRecords_givenRecordIds_getValidStorageRecordsTest() throws URISyntaxException { + String validDataFromStorage = "{\"records\":[{\"id\":\"tenant1:doc:1dbf528e0e0549cab7a08f29fbfc8465\", \"version\":1, \"kind\":\"tenant:test:test:1.0.0\"}," + + "{\"id\":\"tenant1:doc:15e790a69beb4d789b1f979e2af2e813\", \"version\":1, \"kind\":\"tenant:test:test:1.0.0\"}]}"; + + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpResponse.getBody()).thenReturn(validDataFromStorage); + + when(configurationProperties.getStorageQueryRecordHost()).thenReturn("storageUrl"); + when(this.httpClientHandler.sendRequest(any(), any())).thenReturn(httpResponse); + List<String> idsCopy = new ArrayList<>(); + idsCopy.addAll(ids); + Records storageRecords = this.sut.getStorageRecords(idsCopy); + + assertEquals(2, storageRecords.getRecords().size()); + assertEquals(0, storageRecords.getNotFound().size()); + } + + @Test + public void should_returnStorageRecords_givenRecordIds_allFound_getValidStorageRecordsTest() throws URISyntaxException { + + String validDataFromStorage = "{\"records\":[]}"; + + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpResponse.getBody()).thenReturn(validDataFromStorage); + + when(configurationProperties.getStorageQueryRecordHost()).thenReturn("storageUrl"); + when(this.httpClientHandler.sendRequest(any(), any())).thenReturn(httpResponse); + List<String> idsCopy = new ArrayList<>(); + idsCopy.addAll(ids); + Records storageRecords = this.sut.getStorageRecords(idsCopy); + + assertEquals(0, storageRecords.getRecords().size()); + assertEquals(2, storageRecords.getNotFound().size()); + } + + @Test + public void should_returnStorageRecords_givenRecordIds_noneFound_getValidStorageRecordsTest() throws URISyntaxException { + String validDataFromStorage = "{\"records\":[{\"id\":\"tenant1:doc:1dbf528e0e0549cab7a08f29fbfc8465\", \"version\":1, \"kind\":\"tenant:test:test:1.0.0\"}]}"; HttpResponse httpResponse = mock(HttpResponse.class); diff --git a/pom.xml b/pom.xml index 21c223ae750b19cae51cafcf587be9271ddd6ea1..4d800718d45af75337c9fd7f044642fc2acf27e6 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-service</artifactId> <packaging>pom</packaging> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <description>Indexer Service</description> <properties> 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 d3fe0e5f2598592575121fa8ffc22e4305161bbd..085d67757b1d91ae814eaf5ced7954e2b8d13211 100644 --- a/provider/indexer-aws/pom.xml +++ b/provider/indexer-aws/pom.xml @@ -18,7 +18,7 @@ <parent> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-service</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <relativePath>../../pom.xml</relativePath> </parent> @@ -26,7 +26,7 @@ <artifactId>indexer-aws</artifactId> <description>Indexer service on AWS</description> <packaging>jar</packaging> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <properties> <aws.version>1.11.1018</aws.version> @@ -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> @@ -66,12 +67,12 @@ <dependency> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-core</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.opengroup.osdu.core.aws</groupId> <artifactId>os-core-lib-aws</artifactId> - <version>0.23.0</version> + <version>0.24.0</version> </dependency> <!-- AWS managed packages --> @@ -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/main/java/org/opengroup/osdu/indexer/aws/cache/ElasticCredentialsCacheImpl.java b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/ElasticCredentialsCacheImpl.java index 63b65bac319cfb8ae9f93bb3bd73d1646a2c087f..8ba83cfc4cc242bf9eab18333e7e8ca62683f21a 100644 --- a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/ElasticCredentialsCacheImpl.java +++ b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/ElasticCredentialsCacheImpl.java @@ -18,11 +18,11 @@ import org.opengroup.osdu.core.common.provider.interfaces.IElasticCredentialsCac import org.springframework.stereotype.Component; @Component -public class ElasticCredentialsCacheImpl implements IElasticCredentialsCache { +public class ElasticCredentialsCacheImpl implements IElasticCredentialsCache<Object, Object> { @Override public void put(Object o, Object o2) { - + //Do nothing currently } @Override @@ -32,11 +32,11 @@ public class ElasticCredentialsCacheImpl implements IElasticCredentialsCache { @Override public void delete(Object o) { - + //Do nothing currently } @Override public void clearAll() { - + //Do nothing currently } } diff --git a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImpl.java b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImpl.java index fc33e78a6275a8a4ef707b586021e8be9b86a40e..f6684e1333911da0195b97273129a18e9ae5ce6b 100644 --- a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImpl.java +++ b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImpl.java @@ -33,45 +33,42 @@ public class IndexCacheImpl implements IIndexCache<String, Boolean>, AutoCloseab private ICache<String, Boolean> cache; private Boolean local; @Value("${aws.elasticache.cluster.endpoint}") - String REDIS_SEARCH_HOST; + String redisSearchHost; @Value("${aws.elasticache.cluster.port}") - String REDIS_SEARCH_PORT; + String redisSearchPort; @Value("${aws.elasticache.cluster.key}") - String REDIS_SEARCH_KEY; + String redisSearchKey; @Value("${aws.elasticache.cluster.index.expiration}") - String INDEX_CACHE_EXPIRATION; + String indexCacheExpiration; public IndexCacheImpl() throws K8sParameterNotFoundException, JsonProcessingException { int expTimeSeconds = 60 * 60; K8sLocalParameterProvider provider = new K8sLocalParameterProvider(); - if (provider.getLocalMode()){ + local = provider.getLocalMode(); + if (local.booleanValue()){ if (Boolean.parseBoolean(System.getenv("DISABLE_CACHE"))){ cache = new DummyCache<>(); }else{ - cache = new VmCache<String,Boolean>(expTimeSeconds, 10); + cache = new VmCache<>(expTimeSeconds, 10); } }else { - String host = provider.getParameterAsStringOrDefault("CACHE_CLUSTER_ENDPOINT", REDIS_SEARCH_HOST); - int port = Integer.parseInt(provider.getParameterAsStringOrDefault("CACHE_CLUSTER_PORT", REDIS_SEARCH_PORT)); + String host = provider.getParameterAsStringOrDefault("CACHE_CLUSTER_ENDPOINT", redisSearchHost); + int port = Integer.parseInt(provider.getParameterAsStringOrDefault("CACHE_CLUSTER_PORT", redisSearchPort)); Map<String, String > credential =provider.getCredentialsAsMap("CACHE_CLUSTER_KEY"); String password; if (credential !=null){ password = credential.get("token"); }else{ - password = REDIS_SEARCH_KEY; + password = redisSearchKey; } - cache = new RedisCache<String, Boolean>(host, port, password, expTimeSeconds, String.class,Boolean.class); + cache = new RedisCache<>(host, port, password, expTimeSeconds, String.class,Boolean.class); } - local = cache instanceof AutoCloseable; } @Override public void close() throws Exception { - if (local){ - - }else{ + if (!local.booleanValue()){ ((AutoCloseable)this.cache).close(); } - } @Override diff --git a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImpl.java b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImpl.java index 27a1f3792a25a47a331117a85e52f55a62510efd..040407bf39f1ea8fd87e1556e96b12c2cf80a45d 100644 --- a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImpl.java +++ b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImpl.java @@ -30,43 +30,43 @@ import java.util.Map; @Component public class SchemaCacheImpl implements ISchemaCache<String, String>, AutoCloseable { @Value("${aws.elasticache.cluster.endpoint}") - String REDIS_SEARCH_HOST; + String redisSearchHost; @Value("${aws.elasticache.cluster.port}") - String REDIS_SEARCH_PORT; + String redisSearchPort; @Value("${aws.elasticache.cluster.key}") - String REDIS_SEARCH_KEY; + String redisSearchKey; @Value("${aws.elasticache.cluster.schema.expiration}") - String SCHEMA_CACHE_EXPIRATION; + String schemaCacheExpiration; private ICache<String, String> cache; private Boolean local = false; public SchemaCacheImpl() throws K8sParameterNotFoundException, JsonProcessingException { int expTimeSeconds = 60 * 60; K8sLocalParameterProvider provider = new K8sLocalParameterProvider(); - if (provider.getLocalMode()){ + local = provider.getLocalMode(); + if (local.booleanValue()){ if (Boolean.parseBoolean(System.getenv("DISABLE_CACHE"))){ cache = new DummyCache<>(); }else{ - cache = new VmCache<String,String>(expTimeSeconds, 10); + cache = new VmCache<>(expTimeSeconds, 10); } }else { - String host = provider.getParameterAsStringOrDefault("CACHE_CLUSTER_ENDPOINT", REDIS_SEARCH_HOST); - int port = Integer.parseInt(provider.getParameterAsStringOrDefault("CACHE_CLUSTER_PORT", REDIS_SEARCH_PORT)); + String host = provider.getParameterAsStringOrDefault("CACHE_CLUSTER_ENDPOINT", redisSearchHost); + int port = Integer.parseInt(provider.getParameterAsStringOrDefault("CACHE_CLUSTER_PORT", redisSearchPort)); Map<String, String > credential =provider.getCredentialsAsMap("CACHE_CLUSTER_KEY"); String password; if (credential !=null){ password = credential.get("token"); }else{ - password = REDIS_SEARCH_KEY; + password = redisSearchKey; } cache = new RedisCache<>(host, port, password, expTimeSeconds, String.class, String.class); } - local = cache instanceof AutoCloseable; } @Override public void close() throws Exception { - if (this.local){ - // do nothing, this is using local dummy cache + if (this.local.booleanValue()){ + // do nothing, this is using local dummy cache or vm cache }else { // cast to redis cache so it can be closed ((AutoCloseable)this.cache).close(); diff --git a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/di/EntitlementsClientFactory.java b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/di/EntitlementsClientFactory.java index dd7cbd1efa355aebf59aab8b148371afa29f8569..1c092c71ef6d5c1d3540a9f5a908086f7c3b3a81 100644 --- a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/di/EntitlementsClientFactory.java +++ b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/di/EntitlementsClientFactory.java @@ -32,10 +32,10 @@ import org.springframework.web.context.annotation.RequestScope; public class EntitlementsClientFactory extends AbstractFactoryBean<IEntitlementsFactory> { @Value("${AUTHORIZE_API}") - private String AUTHORIZE_API; + private String authorizeApi; @Value("${AUTHORIZE_API_KEY:}") - private String AUTHORIZE_API_KEY; + private String authorizeApiKey; @Autowired private HttpResponseBodyMapper mapper; @@ -45,8 +45,8 @@ public class EntitlementsClientFactory extends AbstractFactoryBean<IEntitlements return new EntitlementsFactory(EntitlementsAPIConfig .builder() - .rootUrl(AUTHORIZE_API) - .apiKey(AUTHORIZE_API_KEY) + .rootUrl(authorizeApiKey) + .apiKey(authorizeApiKey) .build(), mapper); } diff --git a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/publish/PublisherImpl.java b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/publish/PublisherImpl.java index fb81abbbbb8100094869e1d442b91d3bb6533ae5..c19b22b1718fc4eb262d748a084338ec6a20e58c 100644 --- a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/publish/PublisherImpl.java +++ b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/publish/PublisherImpl.java @@ -14,7 +14,6 @@ package org.opengroup.osdu.indexer.aws.publish; -import com.amazonaws.services.sns.model.MessageAttributeValue; import com.amazonaws.services.sns.model.PublishRequest; import com.amazonaws.services.sns.AmazonSNS; @@ -22,14 +21,13 @@ import org.opengroup.osdu.core.aws.ssm.K8sParameterNotFoundException; 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.common.model.indexer.RecordStatus; import org.opengroup.osdu.indexer.provider.interfaces.IPublisher; import org.opengroup.osdu.core.common.model.indexer.JobStatus; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.opengroup.osdu.core.aws.ssm.K8sLocalParameterProvider; import javax.inject.Inject; -import java.util.HashMap; -import java.util.Map; @Component public class PublisherImpl implements IPublisher { @@ -40,6 +38,9 @@ public class PublisherImpl implements IPublisher { @Value("${aws.region}") private String amazonSNSRegion; + @Value("${OSDU_TOPIC}") + private String osduIndexerTopic; + @Inject public void init() throws K8sParameterNotFoundException { AmazonSNSConfig snsConfig = new AmazonSNSConfig(amazonSNSRegion); @@ -50,20 +51,10 @@ public class PublisherImpl implements IPublisher { public void publishStatusChangedTagsToTopic(DpsHeaders headers, JobStatus indexerBatchStatus) throws Exception { - // Attributes - 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())); + PublishRequestBuilder<RecordStatus> publishRequestBuilder = new PublishRequestBuilder<>(); + publishRequestBuilder.setGeneralParametersFromHeaders(headers); - PublishRequest publishRequest = new PublishRequestBuilder().generatePublishRequest("data", indexerBatchStatus.getStatusesList(), messageAttributes, amazonSNSTopic); + PublishRequest publishRequest = publishRequestBuilder.generatePublishRequest(osduIndexerTopic, amazonSNSTopic, indexerBatchStatus.getStatusesList()); snsClient.publish(publishRequest); } diff --git a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/security/BasicAuthSecurityConfig.java b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/security/BasicAuthSecurityConfig.java index 6585ebc959b9fb878de01658d042c53bae1271e6..652f06f5d1bd8866c1d8ac14a465d17429efbc53 100644 --- a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/security/BasicAuthSecurityConfig.java +++ b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/security/BasicAuthSecurityConfig.java @@ -14,17 +14,21 @@ package org.opengroup.osdu.indexer.aws.security; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.web.SecurityFilterChain; @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) -public class BasicAuthSecurityConfig extends WebSecurityConfigurerAdapter { - @Override - protected void configure(HttpSecurity http) throws Exception { +@Configuration +public class BasicAuthSecurityConfig { + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.httpBasic().disable() .csrf().disable(); //disable default authN. AuthN handled by endpoints proxy + return http.build(); } } diff --git a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/service/ElasticClientHandlerAws.java b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/service/ElasticClientHandlerAws.java index 52943cebc267e8c03ffd6ab68e89948d28027c09..98c524854e644f678795cae59e08972d3420294d 100644 --- a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/service/ElasticClientHandlerAws.java +++ b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/service/ElasticClientHandlerAws.java @@ -44,13 +44,13 @@ public class ElasticClientHandlerAws extends ElasticClientHandler { private static final int REST_CLIENT_CONNECT_TIMEOUT = 60000; private static final int REST_CLIENT_SOCKET_TIMEOUT = 60000; - private static final int REST_CLIENT_RETRY_TIMEOUT = 60000; @Value("${aws.es.certificate.disableTrust:false}") // @Value("#{new Boolean('${aws.es.certificate.disableTrust:false}')}") private Boolean disableSslCertificateTrust; public ElasticClientHandlerAws() { + //DO nothing here, just a class constructor } @Override @@ -61,7 +61,7 @@ public class ElasticClientHandlerAws extends ElasticClientHandler { .setConnectTimeout(REST_CLIENT_CONNECT_TIMEOUT) .setSocketTimeout(REST_CLIENT_SOCKET_TIMEOUT)); - if(isLocalHost(host) || disableSslCertificateTrust) { + if(isLocalHost(host) || disableSslCertificateTrust.booleanValue()) { SSLContext sslContext; try { diff --git a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/service/UnsafeX509ExtendedTrustManager.java b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/service/UnsafeX509ExtendedTrustManager.java index c2a9af091ababbe359394cc1256eeb3071f2bfae..8d3a104fea2251ca6968ab367bc66a2823f2f6fd 100644 --- a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/service/UnsafeX509ExtendedTrustManager.java +++ b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/service/UnsafeX509ExtendedTrustManager.java @@ -34,42 +34,42 @@ class UnsafeX509ExtendedTrustManager extends X509ExtendedTrustManager { @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String authType) { if (LOGGER.isDebugEnabled()) { - LOGGER.debug(CLIENT_CERTIFICATE_LOG_MESSAGE, x509Certificates[0].getSubjectDN()); + LOGGER.debug(CLIENT_CERTIFICATE_LOG_MESSAGE, x509Certificates[0].getSubjectX500Principal()); } } @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String authType, Socket socket) { if (LOGGER.isDebugEnabled()) { - LOGGER.debug(CLIENT_CERTIFICATE_LOG_MESSAGE, x509Certificates[0].getSubjectDN()); + LOGGER.debug(CLIENT_CERTIFICATE_LOG_MESSAGE, x509Certificates[0].getSubjectX500Principal()); } } @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String authType, SSLEngine sslEngine) { if (LOGGER.isDebugEnabled()) { - LOGGER.debug(CLIENT_CERTIFICATE_LOG_MESSAGE, x509Certificates[0].getSubjectDN()); + LOGGER.debug(CLIENT_CERTIFICATE_LOG_MESSAGE, x509Certificates[0].getSubjectX500Principal()); } } @Override public void checkServerTrusted(X509Certificate[] x509Certificates, String authType) { if (LOGGER.isDebugEnabled()) { - LOGGER.debug(SERVER_CERTIFICATE_LOG_MESSAGE, x509Certificates[0].getSubjectDN()); + LOGGER.debug(SERVER_CERTIFICATE_LOG_MESSAGE, x509Certificates[0].getSubjectX500Principal()); } } @Override public void checkServerTrusted(X509Certificate[] x509Certificates, String authType, Socket socket) { if (LOGGER.isDebugEnabled()) { - LOGGER.debug(SERVER_CERTIFICATE_LOG_MESSAGE, x509Certificates[0].getSubjectDN()); + LOGGER.debug(SERVER_CERTIFICATE_LOG_MESSAGE, x509Certificates[0].getSubjectX500Principal()); } } @Override public void checkServerTrusted(X509Certificate[] x509Certificates, String authType, SSLEngine sslEngine) { if (LOGGER.isDebugEnabled()) { - LOGGER.debug(SERVER_CERTIFICATE_LOG_MESSAGE, x509Certificates[0].getSubjectDN()); + LOGGER.debug(SERVER_CERTIFICATE_LOG_MESSAGE, x509Certificates[0].getSubjectX500Principal()); } } diff --git a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/util/AwsServiceAccountAuthToken.java b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/util/AwsServiceAccountAuthToken.java index f562dd260aa0eab16bc24dfc3902d2363af82a65..a3406802bc0d1103e418ac32e06a482aef636c40 100644 --- a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/util/AwsServiceAccountAuthToken.java +++ b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/util/AwsServiceAccountAuthToken.java @@ -34,8 +34,8 @@ import java.util.Map; @Component public class AwsServiceAccountAuthToken { - private String client_credentials_secret; - private String client_credentials_clientid; + private String clientCredentialsSecret; + private String clientCredentialsClientId; private String tokenUrl; private String oauthCustomScope; private String token= null; @@ -45,8 +45,8 @@ public class AwsServiceAccountAuthToken { private void init() { K8sLocalParameterProvider provider = new K8sLocalParameterProvider(); try { - this.client_credentials_clientid = provider.getParameterAsString("CLIENT_CREDENTIALS_ID"); - this.client_credentials_secret = provider.getCredentialsAsMap("CLIENT_CREDENTIALS_SECRET").get("client_credentials_client_secret"); + this.clientCredentialsClientId = provider.getParameterAsString("CLIENT_CREDENTIALS_ID"); + this.clientCredentialsSecret = provider.getCredentialsAsMap("CLIENT_CREDENTIALS_SECRET").get("client_credentials_client_secret"); this.tokenUrl = provider.getParameterAsString("OAUTH_TOKEN_URI"); this.oauthCustomScope = provider.getParameterAsString("OAUTH_CUSTOM_SCOPE"); } catch (K8sParameterNotFoundException | JsonProcessingException e) { @@ -57,8 +57,8 @@ public class AwsServiceAccountAuthToken { public String getAuthToken() throws AppException { if (expiredToken()){ Map<String,String> headers = new HashMap<>(); - String authorizationHeaderContents=getEncodedAuthorization(this.client_credentials_clientid,this.client_credentials_secret); - headers.put("Authorization","Basic "+authorizationHeaderContents); + String authorizationHeaderContents=getEncodedAuthorization(this.clientCredentialsClientId, this.clientCredentialsSecret); + headers.put("Authorization","Basic "+ authorizationHeaderContents); headers.put("Content-Type", "application/x-www-form-urlencoded"); IHttpClient httpClient = new HttpClient(); diff --git a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/util/IndexerQueueTaskBuilderAws.java b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/util/IndexerQueueTaskBuilderAws.java index 3f96435d7bcff6ad54ecdd27d0eb11c826b080cb..de86fa2584a0e9f6581455528d2c383854788b81 100644 --- a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/util/IndexerQueueTaskBuilderAws.java +++ b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/util/IndexerQueueTaskBuilderAws.java @@ -37,6 +37,8 @@ import java.util.Map; @Component public class IndexerQueueTaskBuilderAws extends IndexerQueueTaskBuilder { + private static final String TYPE_STRING = "String"; + private static final String RETRY_STRING = "retry"; private static final int INITIAL_RETRY_DELAY_SECONDS = 5; private static final int MAX_RETRY_DELAY_SECONDS = 900; // 15 minutes (900 seconds) is the hard limit SQS sets of message delays @@ -44,7 +46,6 @@ public class IndexerQueueTaskBuilderAws extends IndexerQueueTaskBuilder { private String storageQueue; private String dlq; - private final String retryString = "retry"; private Gson gson; @@ -74,23 +75,23 @@ public class IndexerQueueTaskBuilderAws extends IndexerQueueTaskBuilder { public void createReIndexTask(String payload,DpsHeaders headers) { Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); messageAttributes.put(DpsHeaders.ACCOUNT_ID, new MessageAttributeValue() - .withDataType("String") + .withDataType(TYPE_STRING) .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); messageAttributes.put(DpsHeaders.DATA_PARTITION_ID, new MessageAttributeValue() - .withDataType("String") + .withDataType(TYPE_STRING) .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); headers.addCorrelationIdIfMissing(); messageAttributes.put(DpsHeaders.CORRELATION_ID, new MessageAttributeValue() - .withDataType("String") + .withDataType(TYPE_STRING) .withStringValue(headers.getCorrelationId())); messageAttributes.put(DpsHeaders.USER_EMAIL, new MessageAttributeValue() - .withDataType("String") + .withDataType(TYPE_STRING) .withStringValue(headers.getUserEmail())); messageAttributes.put(DpsHeaders.AUTHORIZATION, new MessageAttributeValue() - .withDataType("String") + .withDataType(TYPE_STRING) .withStringValue(headers.getAuthorization())); messageAttributes.put("ReIndexCursor", new MessageAttributeValue() - .withDataType("String") + .withDataType(TYPE_STRING) .withStringValue("True")); SendMessageRequest sendMessageRequest = new SendMessageRequest() .withQueueUrl(storageQueue) @@ -106,28 +107,28 @@ public class IndexerQueueTaskBuilderAws extends IndexerQueueTaskBuilder { private void createTask(String payload, DpsHeaders headers) { Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); messageAttributes.put(DpsHeaders.ACCOUNT_ID, new MessageAttributeValue() - .withDataType("String") + .withDataType(TYPE_STRING) .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); messageAttributes.put(DpsHeaders.DATA_PARTITION_ID, new MessageAttributeValue() - .withDataType("String") + .withDataType(TYPE_STRING) .withStringValue(headers.getPartitionIdWithFallbackToAccountId())); headers.addCorrelationIdIfMissing(); messageAttributes.put(DpsHeaders.CORRELATION_ID, new MessageAttributeValue() - .withDataType("String") + .withDataType(TYPE_STRING) .withStringValue(headers.getCorrelationId())); messageAttributes.put(DpsHeaders.USER_EMAIL, new MessageAttributeValue() - .withDataType("String") + .withDataType(TYPE_STRING) .withStringValue(headers.getUserEmail())); messageAttributes.put(DpsHeaders.AUTHORIZATION, new MessageAttributeValue() - .withDataType("String") + .withDataType(TYPE_STRING) .withStringValue(headers.getAuthorization())); RecordChangedMessages message = gson.fromJson(payload, RecordChangedMessages.class); int retryCount; int retryDelay; - if (message.getAttributes().containsKey(retryString)) { - retryCount = Integer.parseInt(message.getAttributes().get(retryString)); + if (message.getAttributes().containsKey(RETRY_STRING)) { + retryCount = Integer.parseInt(message.getAttributes().get(RETRY_STRING)); retryCount++; retryDelay = Math.min(getWaitTimeExp(retryCount), MAX_RETRY_DELAY_SECONDS); } else { @@ -140,14 +141,14 @@ public class IndexerQueueTaskBuilderAws extends IndexerQueueTaskBuilder { System.out.println("Delay (in seconds) before next retry: " + retryDelay); // Append the retry count to the message attributes - messageAttributes.put(retryString, new MessageAttributeValue() - .withDataType("String") + messageAttributes.put(RETRY_STRING, new MessageAttributeValue() + .withDataType(TYPE_STRING) .withStringValue(String.valueOf(retryCount)) ); // Append the ancestry kinds used to prevent circular chasing if(message.getAttributes().containsKey(Constants.ANCESTRY_KINDS)) { messageAttributes.put(Constants.ANCESTRY_KINDS, new MessageAttributeValue() - .withDataType("String") + .withDataType(TYPE_STRING) .withStringValue(message.getAttributes().get(Constants.ANCESTRY_KINDS))); } @@ -158,7 +159,7 @@ public class IndexerQueueTaskBuilderAws extends IndexerQueueTaskBuilder { sendMessageRequest = new SendMessageRequest() .withQueueUrl(storageQueue) .withMessageBody(message.getData()) - .withDelaySeconds(new Integer(retryDelay)) + .withDelaySeconds(Integer.valueOf(retryDelay)) .withMessageAttributes(messageAttributes); }else{ sendMessageRequest = new SendMessageRequest() diff --git a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/util/RequestInfoImpl.java b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/util/RequestInfoImpl.java index d352c5a7c03dd8864a5d6a18bfa5e782a6b4856e..c756f8c536dd0e8227e2c84653ad438eccdb5422 100644 --- a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/util/RequestInfoImpl.java +++ b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/util/RequestInfoImpl.java @@ -56,8 +56,7 @@ public class RequestInfoImpl implements IRequestInfo { // throw to prevent null reference exception below throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Invalid Headers", "Headers Map DpsHeaders is null"); } - DpsHeaders headers = this.getCoreServiceHeaders(headersMap.getHeaders()); - return headers; + return this.getCoreServiceHeaders(headersMap.getHeaders()); } /** @@ -94,9 +93,7 @@ public class RequestInfoImpl implements IRequestInfo { private DpsHeaders getCoreServiceHeaders(Map<String, String> input) { Preconditions.checkNotNull(input, "input headers cannot be null"); - DpsHeaders output = DpsHeaders.createFromMap(input); - - return output; + return DpsHeaders.createFromMap(input); } @Override diff --git a/provider/indexer-aws/src/main/resources/application.properties b/provider/indexer-aws/src/main/resources/application.properties index a0d1a419f6a6aa71128963495390ae748e7e9649..677c2536cde4d22eabf9794270c092ede9f7697d 100644 --- a/provider/indexer-aws/src/main/resources/application.properties +++ b/provider/indexer-aws/src/main/resources/application.properties @@ -35,6 +35,9 @@ STORAGE_QUERY_KINDS_HOST=${STORAGE_BASE_URL}/api/storage/v2/query/kinds STORAGE_QUERY_RECORD_FOR_CONVERSION_HOST=${STORAGE_BASE_URL}/api/storage/v2/query/records:batch STORAGE_RECORDS_BATCH_SIZE=20 INDEXER_QUEUE_HOST="" + +OSDU_TOPIC=${OSDU_INDEXER_TOPIC:indexer-status-changed} + ## AWS ElastiCache configuration aws.elasticache.cluster.endpoint=${CACHE_CLUSTER_ENDPOINT:null} aws.elasticache.cluster.port=${CACHE_CLUSTER_PORT:null} 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/cache/IndexCacheImplTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImplTest.java new file mode 100644 index 0000000000000000000000000000000000000000..5a354cfb802ec11fed8cdf70599d9650212330f7 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImplTest.java @@ -0,0 +1,114 @@ +// 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.opengroup.osdu.core.aws.ssm.K8sLocalParameterProvider; +import org.opengroup.osdu.core.common.cache.RedisCache; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; + +@RunWith(MockitoJUnitRunner.class) +public class IndexCacheImplTest { + + + private final String s = "s"; + private final boolean o = true; + private final String password = "password"; + private final String endpoint = "CACHE_CLUSTER_ENDPOINT"; + private final String port = "6369"; + + @Test + public void local_VmCache_test() throws Exception{ + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(true); + })) { + IndexCacheImpl cacheImpl = new IndexCacheImpl(); + cacheImpl.put(s, o); + assertEquals(o, cacheImpl.get(s)); + cacheImpl.delete(s); + assertNull(cacheImpl.get(s)); + cacheImpl.clearAll(); + cacheImpl.close(); + } + } + + @Test + public void non_local_Null_Credential_RedisCache_test() throws Exception{ + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(false); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_ENDPOINT"), any())).thenReturn(endpoint); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_PORT"), any())).thenReturn(port); + when(mockProvider.getCredentialsAsMap(eq("CACHE_CLUSTER_KEY"))).thenReturn(null); + })) { + try (MockedConstruction<RedisCache> cache = Mockito.mockConstruction(RedisCache.class, (mockCache, context) -> { + doNothing().when(mockCache).put(s,o); + when(mockCache.get(s)).thenReturn(o); + doNothing().when(mockCache).delete(s); + doNothing().when(mockCache).clearAll(); + doNothing().when(mockCache).close(); + })) { + IndexCacheImpl cacheImpl = new IndexCacheImpl(); + cacheImpl.put(s, o); + assertEquals(o, cacheImpl.get(s)); + cacheImpl.delete(s); + cacheImpl.clearAll(); + cacheImpl.close(); + } + } + } + + @Test + public void non_Local_notNull_Credential_RedisCache_test() throws Exception{ + + Map<String, String> map = new HashMap<String, String>(); + map.put("token", password); + + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(false); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_ENDPOINT"), any())).thenReturn(endpoint); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_PORT"), any())).thenReturn(port); + when(mockProvider.getCredentialsAsMap(eq("CACHE_CLUSTER_KEY"))).thenReturn(map); + })) { + try (MockedConstruction<RedisCache> cache = Mockito.mockConstruction(RedisCache.class, (mockCache, context) -> { + doNothing().when(mockCache).put(s,o); + when(mockCache.get(s)).thenReturn(o); + doNothing().when(mockCache).delete(s); + doNothing().when(mockCache).clearAll(); + doNothing().when(mockCache).close(); + })) { + IndexCacheImpl cacheImpl = new IndexCacheImpl(); + cacheImpl.put(s, o); + assertEquals(o, cacheImpl.get(s)); + cacheImpl.delete(s); + cacheImpl.clearAll(); + cacheImpl.close(); + } + } + } + +} diff --git a/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImplTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImplTest.java new file mode 100644 index 0000000000000000000000000000000000000000..61e3e73c7a02da8978db9663598ffd8ed3a6eed5 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImplTest.java @@ -0,0 +1,115 @@ +// 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.opengroup.osdu.core.aws.ssm.K8sLocalParameterProvider; +import org.opengroup.osdu.core.common.cache.RedisCache; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; + +@RunWith(MockitoJUnitRunner.class) +public class SchemaCacheImplTest { + + + private final String s = "s"; + private final String o = "o"; + private final String password = "password"; + private final String endpoint = "CACHE_CLUSTER_ENDPOINT"; + private final String port = "6369"; + + + @Test + public void local_VmCache_test() throws Exception{ + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(true); + })) { + SchemaCacheImpl cacheImpl = new SchemaCacheImpl(); + cacheImpl.put(s, o); + assertEquals(o, cacheImpl.get(s)); + cacheImpl.delete(s); + assertNull(cacheImpl.get(s)); + cacheImpl.clearAll(); + cacheImpl.close(); + } + } + + @Test + public void non_local_Null_Credential_RedisCache_test() throws Exception{ + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(false); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_ENDPOINT"), any())).thenReturn(endpoint); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_PORT"), any())).thenReturn(port); + when(mockProvider.getCredentialsAsMap(eq("CACHE_CLUSTER_KEY"))).thenReturn(null); + })) { + try (MockedConstruction<RedisCache> cache = Mockito.mockConstruction(RedisCache.class, (mockCache, context) -> { + doNothing().when(mockCache).put(s,o); + when(mockCache.get(s)).thenReturn(o); + doNothing().when(mockCache).delete(s); + doNothing().when(mockCache).clearAll(); + doNothing().when(mockCache).close(); + })) { + SchemaCacheImpl cacheImpl = new SchemaCacheImpl(); + cacheImpl.put(s, o); + assertEquals(o, cacheImpl.get(s)); + cacheImpl.delete(s); + cacheImpl.clearAll(); + cacheImpl.close(); + } + } + } + + @Test + public void non_Local_notNull_Credential_RedisCache_test() throws Exception{ + + Map<String, String> map = new HashMap<String, String>(); + map.put("token", password); + + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(false); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_ENDPOINT"), any())).thenReturn(endpoint); + when(mockProvider.getParameterAsStringOrDefault(eq("CACHE_CLUSTER_PORT"), any())).thenReturn(port); + when(mockProvider.getCredentialsAsMap(eq("CACHE_CLUSTER_KEY"))).thenReturn(map); + })) { + try (MockedConstruction<RedisCache> cache = Mockito.mockConstruction(RedisCache.class, (mockCache, context) -> { + doNothing().when(mockCache).put(s,o); + when(mockCache.get(s)).thenReturn(o); + doNothing().when(mockCache).delete(s); + doNothing().when(mockCache).clearAll(); + doNothing().when(mockCache).close(); + })) { + SchemaCacheImpl cacheImpl = new SchemaCacheImpl(); + cacheImpl.put(s, o); + assertEquals(o, cacheImpl.get(s)); + cacheImpl.delete(s); + cacheImpl.clearAll(); + cacheImpl.close(); + } + } + } + +} 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/persistence/ElasticRepositoryImplTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/persistence/ElasticRepositoryImplTest.java new file mode 100644 index 0000000000000000000000000000000000000000..c89cbb150983ce5bae6c5aec96344c766df24644 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/persistence/ElasticRepositoryImplTest.java @@ -0,0 +1,81 @@ +// 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.persistence; + + +import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; +import org.opengroup.osdu.core.aws.ssm.K8sLocalParameterProvider; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; + +@RunWith(SpringJUnit4ClassRunner.class) +public class ElasticRepositoryImplTest { + + private final String s = "s"; + private final String o = "o"; + private final String username = "username"; + private final String password = "password"; + private final String host = "host"; + private final String port = "6369"; + + + @Test + public void getElasticClusterSettings_Null_User_Test() { + + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(false); + when(mockProvider.getParameterAsStringOrDefault(eq("ELASTICSEARCH_HOST"), any())).thenReturn(host); + when(mockProvider.getParameterAsStringOrDefault(eq("ELASTICSEARCH_PORT"), any())).thenReturn(port); + when(mockProvider.getCredentialsAsMap(eq("ELASTICSEARCH_CREDENTIALS"))).thenReturn(null); + })) { + ElasticRepositoryImpl elasticImpl = new ElasticRepositoryImpl(); + + assertNotNull(elasticImpl.getElasticClusterSettings(new TenantInfo())); + + } + + } + @Test + public void getElasticClusterSettings_not_Null_User_Test() { + + Map<String, String> map = new HashMap<String, String>(); + map.put("username", username); + map.put("password", password); + + try (MockedConstruction<K8sLocalParameterProvider> provider = Mockito.mockConstruction(K8sLocalParameterProvider.class, (mockProvider, context) -> { + when(mockProvider.getLocalMode()).thenReturn(false); + when(mockProvider.getParameterAsStringOrDefault(eq("ELASTICSEARCH_HOST"), any())).thenReturn(host); + when(mockProvider.getParameterAsStringOrDefault(eq("ELASTICSEARCH_PORT"), any())).thenReturn(port); + when(mockProvider.getCredentialsAsMap(eq("ELASTICSEARCH_CREDENTIALS"))).thenReturn(map); + })) { + ElasticRepositoryImpl elasticImpl = new ElasticRepositoryImpl(); + elasticImpl.isHttps = true; + assertNotNull(elasticImpl.getElasticClusterSettings(new TenantInfo())); + + } + + } +} 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..6a85fb6215f2635e49b33c39fcb76dbd59f48a25 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,37 +18,42 @@ 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.common.model.indexer.RecordStatus; +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; import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.test.util.ReflectionTestUtils; + +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; import java.util.HashMap; import java.util.Map; -import static org.mockito.Mockito.mock; - @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(); @Mock AmazonSNS snsClient; - public void setUp() { - snsClient = mock(AmazonSNS.class); - } - @Test public void publishStatusChangedTagsToTopic() throws Exception { // Arrange @@ -57,23 +62,49 @@ public class PublisherImplTest { 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,null); + PublishRequestBuilder<RecordStatus> publishRequestBuilder = new PublishRequestBuilder<>(); + publishRequestBuilder.setGeneralParametersFromHeaders(headers); + + PublishRequest publishRequest = publishRequestBuilder.generatePublishRequest(null, null, jobStatus.getStatusesList()); // Act publisher.publishStatusChangedTagsToTopic(headers, jobStatus); // 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(); + JobStatus jobStatus = new JobStatus(); + + // Arrange + DpsHeaders headers = new DpsHeaders(); + + PublishRequestBuilder<RecordStatus> publishRequestBuilder = new PublishRequestBuilder<>(); + publishRequestBuilder.setGeneralParametersFromHeaders(headers); + + PublishRequest publishRequest = publishRequestBuilder.generatePublishRequest(null, indexer_sns_topic_arn, jobStatus.getStatusesList()); + // 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..104d016f9873b68ea1b4df7995172351d3801807 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/service/ElasticClientHandlerAwsTest.java @@ -0,0 +1,57 @@ +// 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 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; + + @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/service/UnsafeX509ExtendedTrustManagerTest.java b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/service/UnsafeX509ExtendedTrustManagerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..b250dda665f35b0475372a7ca5ab84217b910089 --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/service/UnsafeX509ExtendedTrustManagerTest.java @@ -0,0 +1,201 @@ +// 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.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +import java.math.BigInteger; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.Principal; +import java.security.PublicKey; +import java.security.SignatureException; +import java.security.cert.CertificateEncodingException; +import java.security.cert.CertificateException; +import java.security.cert.CertificateExpiredException; +import java.security.cert.CertificateNotYetValidException; +import java.security.cert.X509Certificate; +import java.util.Date; +import java.util.HashSet; +import java.util.Set; + +import javax.security.auth.x500.X500Principal; + +import org.springframework.boot.test.context.SpringBootTest; +import org.opengroup.osdu.indexer.aws.IndexerAwsApplication; +import java.net.Socket; + +@RunWith(MockitoJUnitRunner.class) +@SpringBootTest(classes = {IndexerAwsApplication.class}) +public class UnsafeX509ExtendedTrustManagerTest { + + + @Test + public void createClientBuilder() throws Exception { + + X509Certificate[] certificates = new X509Certificate[1]; + + certificates[0] = new Certificate(); + + UnsafeX509ExtendedTrustManager manager = UnsafeX509ExtendedTrustManager.INSTANCE; + + manager.checkClientTrusted(certificates, "auth"); + manager.checkClientTrusted(certificates, "auth", new Socket()); + manager.checkServerTrusted(certificates, "auth"); + manager.checkServerTrusted(certificates, "auth", new Socket()); + + } + +} + +class Certificate extends X509Certificate{ + + @Override + public boolean hasUnsupportedCriticalExtension() { + return true; + } + + @Override + public Set<String> getCriticalExtensionOIDs() { + return new HashSet<String>(); + } + + @Override + public Set<String> getNonCriticalExtensionOIDs() { + return new HashSet<String>(); + } + + @Override + public byte[] getExtensionValue(String oid) { + return new byte[0]; + } + + @Override + public void checkValidity() throws CertificateExpiredException, CertificateNotYetValidException { + } + + @Override + public void checkValidity(Date date) throws CertificateExpiredException, CertificateNotYetValidException { + } + + @Override + public int getVersion() { + return 0; + } + + @Override + public BigInteger getSerialNumber() { + return new BigInteger("0"); + } + + @Override + public Principal getIssuerDN() { + return null; + } + + @Override + public Principal getSubjectDN() { + return null; + } + + @Override + public X500Principal getSubjectX500Principal() { + return null; + } + + @Override + public Date getNotBefore() { + return null; + } + + @Override + public Date getNotAfter() { + return null; + } + + @Override + public byte[] getTBSCertificate() throws CertificateEncodingException { + return null; + } + + @Override + public byte[] getSignature() { + return null; + } + + @Override + public String getSigAlgName() { + return ""; + } + + @Override + public String getSigAlgOID() { + return ""; + } + + @Override + public byte[] getSigAlgParams() { + return null; + } + + @Override + public boolean[] getIssuerUniqueID() { + return null; + } + + @Override + public boolean[] getSubjectUniqueID() { + return null; + } + + @Override + public boolean[] getKeyUsage() { + return null; + } + + @Override + public int getBasicConstraints() { + return 0; + } + + @Override + public byte[] getEncoded() throws CertificateEncodingException { + return null; + } + + @Override + public void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, + NoSuchProviderException, SignatureException { + } + + @Override + public void verify(PublicKey key, String sigProvider) throws CertificateException, NoSuchAlgorithmException, + InvalidKeyException, NoSuchProviderException, SignatureException { + } + + @Override + public String toString() { + return ""; + } + + @Override + public PublicKey getPublicKey() { + return null; + } + +} \ No newline at end of file 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..3bc34c6b4155ccd952f2a4c88291684ddd47fe1a --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/util/AwsServiceAccountAuthTokenTest.java @@ -0,0 +1,159 @@ +// 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 static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertSame; +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()); + assertSame(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()); + assertNotSame(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..1c33fccd68bc4b0c492797a0ccb81b71b097e2bd --- /dev/null +++ b/provider/indexer-aws/src/test/java/org/opengroup/osdu/indexer/aws/util/RequestInfoImplTest.java @@ -0,0 +1,111 @@ +// 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.Assert.assertNull; +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(); + + assertNull(result); + } + + @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)); + } +} diff --git a/provider/indexer-azure/pom.xml b/provider/indexer-azure/pom.xml index 962f1dee216af8414b825030fcd32e2e57bff4a4..7f343df9081ffa38efbd34a3fcb9c2cceac18d0a 100644 --- a/provider/indexer-azure/pom.xml +++ b/provider/indexer-azure/pom.xml @@ -21,12 +21,12 @@ <parent> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-service</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <relativePath>../../pom.xml</relativePath> </parent> <artifactId>indexer-azure</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <name>indexer-azure</name> <description>Indexer Service Azure</description> <packaging>jar</packaging> @@ -39,7 +39,7 @@ <azure.appservice.subscription /> <log4j.version>2.17.1</log4j.version> <nimbus-jose-jwt.version>8.20.2</nimbus-jose-jwt.version> - <indexer-core.version>0.24.0-SNAPSHOT</indexer-core.version> + <indexer-core.version>0.25.0-SNAPSHOT</indexer-core.version> <spring-security-jwt.version>1.1.1.RELEASE</spring-security-jwt.version> <osdu.corelibazure.version>0.23.2</osdu.corelibazure.version> <os-core-common.version>0.23.3</os-core-common.version> @@ -48,7 +48,7 @@ <mockito.version>3.11.2</mockito.version> <cobertura-maven-plugin.version>2.7</cobertura-maven-plugin.version> <spring-security-oauth2.version>2.3.6.RELEASE</spring-security-oauth2.version> - <netty.version>4.1.96.Final</netty.version> + <netty.version>4.1.98.Final</netty.version> <reactor-netty.version>1.0.7</reactor-netty.version> <reactor-core.version>3.4.24</reactor-core.version> <oauth2-oidc-sdk.version>6.5</oauth2-oidc-sdk.version> @@ -198,6 +198,10 @@ <artifactId>azure-core</artifactId> <groupId>com.azure</groupId> </exclusion> + <exclusion> + <groupId>com.microsoft.azure</groupId> + <artifactId>documentdb-bulkexecutor</artifactId> + </exclusion> </exclusions> </dependency> diff --git a/provider/indexer-gc/pom.xml b/provider/indexer-gc/pom.xml index c0ca6dcf612e3c47d1ec0d650cf64754cb98c7b4..e8191402bf56a491f65f72ef1bff7ab87b4a6138 100644 --- a/provider/indexer-gc/pom.xml +++ b/provider/indexer-gc/pom.xml @@ -5,12 +5,12 @@ <parent> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-service</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <relativePath>../../pom.xml</relativePath> </parent> <artifactId>indexer-gc</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <name>indexer-gc</name> <description>Indexer Service Google Cloud</description> <packaging>jar</packaging> @@ -44,7 +44,7 @@ <dependency> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-core</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <!-- excluded due to runtime conflict with latest core-lib-gc transient dependencies --> <exclusions> <exclusion> diff --git a/provider/indexer-ibm/pom.xml b/provider/indexer-ibm/pom.xml index 027b98c91bc2d0d2f1bac58a9246034ec3811a7a..3c9e3b1b5faa916ad92c6b5d541537df63d5a30f 100644 --- a/provider/indexer-ibm/pom.xml +++ b/provider/indexer-ibm/pom.xml @@ -21,7 +21,7 @@ <parent> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-service</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <relativePath>../../pom.xml</relativePath> </parent> @@ -57,7 +57,7 @@ <dependency> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-core</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <exclusions> <exclusion> <groupId>io.netty</groupId> diff --git a/testing/indexer-test-aws/pom.xml b/testing/indexer-test-aws/pom.xml index 09555ebc2ec6a6f92b9b68702ac6adb0fbd64ba5..d4a94b588ee14be25c43b596855a43a131ad3b09 100644 --- a/testing/indexer-test-aws/pom.xml +++ b/testing/indexer-test-aws/pom.xml @@ -21,13 +21,13 @@ <parent> <groupId>org.opengroup.osdu</groupId> <artifactId>indexer-test</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-test-aws</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> @@ -51,7 +51,7 @@ <dependency> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-test-core</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> </dependency> <!-- AWS specific packages --> diff --git a/testing/indexer-test-azure/pom.xml b/testing/indexer-test-azure/pom.xml index a29aaaf76551b100869e984fef3837a95d0fc10c..4ba517d42e20eeab1c3a35d0ab3ff374bc550b22 100644 --- a/testing/indexer-test-azure/pom.xml +++ b/testing/indexer-test-azure/pom.xml @@ -21,13 +21,13 @@ <parent> <groupId>org.opengroup.osdu</groupId> <artifactId>indexer-test</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-test-azure</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> @@ -53,7 +53,7 @@ <dependency> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-test-core</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> diff --git a/testing/indexer-test-baremetal/pom.xml b/testing/indexer-test-baremetal/pom.xml index 56ebb186e8e1ace9fbadd0c30e8c87f85a1d108e..8c0027470cacac91af6c35348e77b6554a82130b 100644 --- a/testing/indexer-test-baremetal/pom.xml +++ b/testing/indexer-test-baremetal/pom.xml @@ -20,7 +20,7 @@ <parent> <artifactId>indexer-test</artifactId> <groupId>org.opengroup.osdu</groupId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> @@ -43,7 +43,7 @@ <dependency> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-test-core</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.nimbusds</groupId> diff --git a/testing/indexer-test-core/pom.xml b/testing/indexer-test-core/pom.xml index 73456b98a00642088f6e8e945ff80c1ee7e9553b..3f5e31cd78cdb83f36825ee921074695758ccbf8 100644 --- a/testing/indexer-test-core/pom.xml +++ b/testing/indexer-test-core/pom.xml @@ -5,13 +5,13 @@ <parent> <groupId>org.opengroup.osdu</groupId> <artifactId>indexer-test</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-test-core</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <properties> <maven.compiler.target>17</maven.compiler.target> diff --git a/testing/indexer-test-gc/pom.xml b/testing/indexer-test-gc/pom.xml index e2cf947ca5bbaccb24cf496f8522ca8c862d68d5..8b85bc93fd7e9cdd39e32741df07426bf38aabac 100644 --- a/testing/indexer-test-gc/pom.xml +++ b/testing/indexer-test-gc/pom.xml @@ -6,13 +6,13 @@ <parent> <groupId>org.opengroup.osdu</groupId> <artifactId>indexer-test</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-test-gc</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> @@ -44,7 +44,7 @@ <dependency> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-test-core</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> </dependency> <!-- Cucumber --> diff --git a/testing/indexer-test-ibm/pom.xml b/testing/indexer-test-ibm/pom.xml index e8e17a5f33344d2238d6ca94dbb9633cdceae2a9..a07fc080ccf6f552b7a767efe4a8891a6df23a92 100644 --- a/testing/indexer-test-ibm/pom.xml +++ b/testing/indexer-test-ibm/pom.xml @@ -6,13 +6,13 @@ <parent> <groupId>org.opengroup.osdu</groupId> <artifactId>indexer-test</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-test-ibm</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> @@ -45,7 +45,7 @@ <dependency> <groupId>org.opengroup.osdu.indexer</groupId> <artifactId>indexer-test-core</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> </dependency> <dependency> diff --git a/testing/pom.xml b/testing/pom.xml index e2b37af411dcfec1830c2b5d4b870e27697ce8c8..382be620df71984959592db29dfb84ec3cf335e9 100644 --- a/testing/pom.xml +++ b/testing/pom.xml @@ -18,7 +18,7 @@ <modelVersion>4.0.0</modelVersion> <groupId>org.opengroup.osdu</groupId> <artifactId>indexer-test</artifactId> - <version>0.24.0-SNAPSHOT</version> + <version>0.25.0-SNAPSHOT</version> <description>Indexer Service Integration Test Root Project</description> <properties> <spring.version>5.3.23</spring.version>