From 56336e9d7523eeb1b99b9af9472ca6ff54d2414f Mon Sep 17 00:00:00 2001 From: Dmitriy Gerashchenko <dgerashchenko@slb.com> Date: Fri, 5 Nov 2021 15:07:07 +0000 Subject: [PATCH] partition-core shouldn't contain SPI implementations --- NOTICE | 64 ++-- .../osdu/partition/api/PartitionApi.java | 14 +- .../interfaces/IPartitionServiceCache.java | 20 -- .../service/CachedPartitionServiceImpl.java | 111 ------- .../CachedPartitionServiceImplTest.java | 150 --------- .../PartitionServiceDummyListCacheImpl.java | 52 --- ...ionServiceDummyPartitionInfoCacheImpl.java | 51 --- .../aws/service/PartitionServiceImpl.java | 9 +- .../azure/cache/PartitionListCacheImpl.java | 38 --- .../cache/PartitionServiceCacheImpl.java | 37 --- .../azure/service/PartitionServiceImpl.java | 75 ++++- .../PartitionServiceImplCacheTest.java | 146 +++++++++ .../service/PartitionServiceImplTest.java | 31 +- .../gcp/cache/PartitionListCacheImpl.java | 53 --- .../gcp/cache/PartitionServiceCacheImpl.java | 52 --- .../gcp/service/PartitionServiceImpl.java | 91 +++-- .../PartitionServiceImplCacheTest.java | 200 +++++++++++ .../gcp/service/PartitionServiceImplTest.java | 310 ++++++++++-------- .../ibm/cache/PartitionListCacheImpl.java | 45 --- .../ibm/cache/PartitionServiceCacheImpl.java | 42 --- .../ibm/service/PartitionServiceImpl.java | 125 ++++--- .../cache/PartitionListCacheImpl.java | 53 --- .../cache/PartitionServiceCacheImpl.java | 52 --- .../service/PartitionServiceImpl.java | 87 +++-- 24 files changed, 860 insertions(+), 1048 deletions(-) delete mode 100644 partition-core/src/main/java/org/opengroup/osdu/partition/provider/interfaces/IPartitionServiceCache.java delete mode 100644 partition-core/src/main/java/org/opengroup/osdu/partition/service/CachedPartitionServiceImpl.java delete mode 100644 partition-core/src/test/java/org/opengroup/osdu/partition/service/CachedPartitionServiceImplTest.java delete mode 100644 provider/partition-aws/src/main/java/org/opengroup/osdu/partition/provider/aws/service/PartitionServiceDummyListCacheImpl.java delete mode 100644 provider/partition-aws/src/main/java/org/opengroup/osdu/partition/provider/aws/service/PartitionServiceDummyPartitionInfoCacheImpl.java delete mode 100644 provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/cache/PartitionListCacheImpl.java delete mode 100644 provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/cache/PartitionServiceCacheImpl.java create mode 100644 provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/service/PartitionServiceImplCacheTest.java delete mode 100644 provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/cache/PartitionListCacheImpl.java delete mode 100644 provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/cache/PartitionServiceCacheImpl.java create mode 100644 provider/partition-gcp/src/test/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImplCacheTest.java delete mode 100644 provider/partition-ibm/src/main/java/org/opengroup/osdu/partition/provider/ibm/cache/PartitionListCacheImpl.java delete mode 100644 provider/partition-ibm/src/main/java/org/opengroup/osdu/partition/provider/ibm/cache/PartitionServiceCacheImpl.java delete mode 100644 provider/partition-reference/src/main/java/org/opengroup/osdu/partition/provider/reference/cache/PartitionListCacheImpl.java delete mode 100644 provider/partition-reference/src/main/java/org/opengroup/osdu/partition/provider/reference/cache/PartitionServiceCacheImpl.java diff --git a/NOTICE b/NOTICE index 597ebbb9d..dbcc3b72f 100644 --- a/NOTICE +++ b/NOTICE @@ -289,8 +289,8 @@ The following software have components provided under the terms of this license: - AWS SDK for Java - Models (from https://aws.amazon.com/sdkforjava) - Adapter: RxJava (from https://repo1.maven.org/maven2/com/squareup/retrofit2/adapter-rxjava) - Apache Ant + JUnit (from http://ant.apache.org/) -- Apache Ant Core (from https://ant.apache.org/) -- Apache Ant Launcher (from https://ant.apache.org/) +- Apache Ant Core (from http://ant.apache.org/) +- Apache Ant Launcher (from http://ant.apache.org/) - Apache Commons BeanUtils (from http://commons.apache.org/proper/commons-beanutils/) - Apache Commons CLI (from https://commons.apache.org/proper/commons-cli/) - Apache Commons Codec (from https://commons.apache.org/proper/commons-codec/) @@ -304,9 +304,11 @@ The following software have components provided under the terms of this license: - Apache Log4j JUL Adapter (from https://repo1.maven.org/maven2/org/apache/logging/log4j/log4j-jul) - Apache Log4j SLF4J Binding (from https://repo1.maven.org/maven2/org/apache/logging/log4j/log4j-slf4j-impl) - Apache Log4j to SLF4J Adapter (from https://repo1.maven.org/maven2/org/apache/logging/log4j/log4j-to-slf4j) +- Apache Maven Invoker (from https://repo1.maven.org/maven2/org/apache/maven/shared/maven-invoker) - Apache Maven Wagon :: API (from https://repo1.maven.org/maven2/org/apache/maven/wagon/wagon-provider-api) - Apache Maven Wagon :: Providers :: File Provider (from https://repo1.maven.org/maven2/org/apache/maven/wagon/wagon-file) - Apache Maven Wagon :: Providers :: HTTP Shared Library (from https://repo1.maven.org/maven2/org/apache/maven/wagon/wagon-http-shared) +- Apache Maven Wagon :: Providers :: Lightweight HTTP Provider (from https://repo1.maven.org/maven2/org/apache/maven/wagon/wagon-http-lightweight) - Apache Maven Wagon :: Providers :: SSH Common Library (from https://repo1.maven.org/maven2/org/apache/maven/wagon/wagon-ssh-common) - Apache Maven Wagon :: Providers :: SSH External Provider (from https://repo1.maven.org/maven2/org/apache/maven/wagon/wagon-ssh-external) - Apache Maven Wagon :: Providers :: SSH Provider (from https://repo1.maven.org/maven2/org/apache/maven/wagon/wagon-ssh) @@ -323,7 +325,7 @@ The following software have components provided under the terms of this license: - Brave Instrumentation: Http Adapters (from https://repo1.maven.org/maven2/io/zipkin/brave/brave-instrumentation-http) - Brave instrumentation for Reactor Netty HTTP (from https://github.com/reactor/reactor-netty) - Byte Buddy (without dependencies) (from https://repo1.maven.org/maven2/net/bytebuddy/byte-buddy) -- Byte Buddy agent (from https://repo1.maven.org/maven2/net/bytebuddy/byte-buddy-agent) +- Byte Buddy Java agent (from https://repo1.maven.org/maven2/net/bytebuddy/byte-buddy-agent) - ClassMate (from http://github.com/cowtowncoder/java-classmate) - Cloud Key Management Service (KMS) API v1-rev20210820-1.32.1 (from https://repo1.maven.org/maven2/com/google/apis/google-api-services-cloudkms) - Cloud Storage JSON API v1-rev20210918-1.32.1 (from https://repo1.maven.org/maven2/com/google/apis/google-api-services-storage) @@ -389,15 +391,15 @@ The following software have components provided under the terms of this license: - Jackson datatype: JSR310 (from https://repo1.maven.org/maven2/com/fasterxml/jackson/datatype/jackson-datatype-jsr310) - Jackson datatype: jdk8 (from https://repo1.maven.org/maven2/com/fasterxml/jackson/datatype/jackson-datatype-jdk8) - Jackson extensions to the Google HTTP Client Library for Java. (from https://repo1.maven.org/maven2/com/google/http-client/google-http-client-jackson) -- Jackson module: Afterburner (from https://github.com/FasterXML/jackson-modules-base) -- Jackson module: Old JAXB Annotations (javax.xml.bind) (from https://github.com/FasterXML/jackson-modules-base) - Jackson-annotations (from http://github.com/FasterXML/jackson) - Jackson-annotations (from http://github.com/FasterXML/jackson) - Jackson-core (from https://github.com/FasterXML/jackson) - Jackson-core (from https://github.com/FasterXML/jackson) - Jackson-dataformat-XML (from http://wiki.fasterxml.com/JacksonExtensionXmlDataBinding) -- Jackson-dataformat-YAML (from https://github.com/FasterXML/jackson) +- Jackson-dataformat-YAML (from http://wiki.fasterxml.com/JacksonExtensionYAML) - Jackson-datatype-Joda (from http://wiki.fasterxml.com/JacksonModuleJoda) +- Jackson-module-Afterburner (from http://wiki.fasterxml.com/JacksonHome) +- Jackson-module-JAXB-annotations (from http://wiki.fasterxml.com/JacksonJAXBAnnotations) - Jackson-module-parameter-names (from https://repo1.maven.org/maven2/com/fasterxml/jackson/module/jackson-module-parameter-names) - Jakarta Bean Validation API (from https://beanvalidation.org) - Jakarta Expression Language Implementation (from https://projects.eclipse.org/projects/ee4j.el) @@ -428,7 +430,6 @@ The following software have components provided under the terms of this license: - Maven Artifact Manager (from https://repo1.maven.org/maven2/org/apache/maven/maven-artifact-manager) - Maven Core (from https://repo1.maven.org/maven2/org/apache/maven/maven-core) - Maven Error Diagnostics (from https://repo1.maven.org/maven2/org/apache/maven/maven-error-diagnostics) -- Maven Invoker (from https://repo1.maven.org/maven2/org/apache/maven/shared/maven-invoker) - Maven Model (from https://repo1.maven.org/maven2/org/apache/maven/maven-model) - Maven Monitor (from https://repo1.maven.org/maven2/org/apache/maven/maven-monitor) - Maven Plugin API (from https://repo1.maven.org/maven2/org/apache/maven/maven-plugin-api) @@ -441,7 +442,6 @@ The following software have components provided under the terms of this license: - Maven Reporting Implementation (from https://repo1.maven.org/maven2/org/apache/maven/reporting/maven-reporting-impl) - Maven Repository Metadata Model (from https://repo1.maven.org/maven2/org/apache/maven/maven-repository-metadata) - Maven Settings (from https://repo1.maven.org/maven2/org/apache/maven/maven-settings) -- Maven Wagon Lightweight HTTP Provider (from https://repo1.maven.org/maven2/org/apache/maven/wagon/wagon-http-lightweight) - Metrics Core (from https://repo1.maven.org/maven2/io/dropwizard/metrics/metrics-core) - Microsoft Application Insights Java SDK Core (from https://github.com/Microsoft/ApplicationInsights-Java) - Microsoft Application Insights Java SDK Spring Boot starter (from https://github.com/Microsoft/ApplicationInsights-Java) @@ -478,6 +478,7 @@ The following software have components provided under the terms of this license: - OkHttp (from https://repo1.maven.org/maven2/com/squareup/okhttp3/okhttp) - OkHttp Logging Interceptor (from https://repo1.maven.org/maven2/com/squareup/okhttp3/logging-interceptor) - OkHttp URLConnection (from https://repo1.maven.org/maven2/com/squareup/okhttp3/okhttp-urlconnection) +- Okio (from https://repo1.maven.org/maven2/com/squareup/okio/okio) - OpenCensus (from https://github.com/census-instrumentation/opencensus-java) - OpenCensus (from https://github.com/census-instrumentation/opencensus-java) - OpenCensus (from https://github.com/census-instrumentation/opencensus-java) @@ -508,17 +509,19 @@ The following software have components provided under the terms of this license: - SnakeYAML (from http://www.snakeyaml.org) - Spring AOP (from https://github.com/spring-projects/spring-framework) - Spring Beans (from https://github.com/spring-projects/spring-framework) -- Spring Boot (from http://projects.spring.io/spring-boot/) +- Spring Boot (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot) +- Spring Boot AOP Starter (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-aop) - Spring Boot Actuator (from http://projects.spring.io/spring-boot/) -- Spring Boot AutoConfigure (from http://projects.spring.io/spring-boot/) -- Spring Boot Logging Starter (from http://projects.spring.io/spring-boot/) +- Spring Boot AutoConfigure (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-autoconfigure) +- Spring Boot Json Starter (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-json) +- Spring Boot Logging Starter (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-logging) - Spring Boot Security Starter (from http://projects.spring.io/spring-boot/) -- Spring Boot Starter (from http://projects.spring.io/spring-boot/) -- Spring Boot Test (from http://projects.spring.io/spring-boot/) -- Spring Boot Test Auto-Configure (from http://projects.spring.io/spring-boot/) -- Spring Boot Test Starter (from http://projects.spring.io/spring-boot/) -- Spring Boot Tomcat Starter (from http://projects.spring.io/spring-boot/) -- Spring Boot Web Starter (from http://projects.spring.io/spring-boot/) +- Spring Boot Starter (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter) +- Spring Boot Test (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-test) +- Spring Boot Test Auto-Configure (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-test-autoconfigure) +- Spring Boot Test Starter (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-test) +- Spring Boot Tomcat Starter (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-tomcat) +- Spring Boot Web Starter (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-web) - Spring Cloud GCP Autoconfigure Module (from https://repo1.maven.org/maven2/org/springframework/cloud/spring-cloud-gcp-autoconfigure) - Spring Cloud GCP Core Module (from https://repo1.maven.org/maven2/org/springframework/cloud/spring-cloud-gcp-core) - Spring Cloud GCP Datastore Module (from https://repo1.maven.org/maven2/org/springframework/cloud/spring-cloud-gcp-data-datastore) @@ -586,7 +589,6 @@ The following software have components provided under the terms of this license: - mockito-core (from http://mockito.org) - mockito-core (from http://mockito.org) - mockito-core (from http://mockito.org) -- okio (from https://github.com/square/okio/) - org.apiguardian:apiguardian-api (from https://github.com/apiguardian-team/apiguardian) - org.conscrypt:conscrypt-openjdk-uber (from https://conscrypt.org/) - org.jetbrains.kotlin:kotlin-stdlib-common (from https://kotlinlang.org/) @@ -601,33 +603,31 @@ The following software have components provided under the terms of this license: - proto-google-common-protos (from https://github.com/googleapis/java-iam/proto-google-common-protos) - proto-google-common-protos (from https://github.com/googleapis/java-iam/proto-google-common-protos) - proto-google-iam-v1 (from https://github.com/googleapis/java-iam/proto-google-iam-v1) -- resilience4j (from https://github.com/resilience4j/resilience4j) - resilience4j (from https://resilience4j.readme.io) - resilience4j (from https://github.com/resilience4j/resilience4j) - resilience4j (from https://github.com/resilience4j/resilience4j) - resilience4j (from https://github.com/resilience4j/resilience4j) -- resilience4j (from https://resilience4j.readme.io) - resilience4j (from https://github.com/resilience4j/resilience4j) - resilience4j (from https://github.com/resilience4j/resilience4j) +- resilience4j (from https://resilience4j.readme.io) +- resilience4j (from https://github.com/resilience4j/resilience4j) - rxjava (from https://github.com/ReactiveX/RxJava) - spring-boot-actuator-autoconfigure (from https://spring.io/projects/spring-boot) - spring-boot-dependencies (from https://spring.io/projects/spring-boot) - spring-boot-starter-actuator (from https://spring.io/projects/spring-boot) -- spring-boot-starter-aop (from https://spring.io/projects/spring-boot) - spring-boot-starter-data-mongodb (from https://spring.io/projects/spring-boot) -- spring-boot-starter-json (from https://spring.io/projects/spring-boot) - spring-boot-starter-log4j2 (from https://spring.io/projects/spring-boot) - spring-boot-starter-reactor-netty (from https://spring.io/projects/spring-boot) - spring-boot-starter-validation (from https://spring.io/projects/spring-boot) - spring-boot-starter-webflux (from https://spring.io/projects/spring-boot) -- spring-security-config (from https://spring.io/spring-security) -- spring-security-core (from https://spring.io/spring-security) -- spring-security-oauth2-client (from https://spring.io/spring-security) -- spring-security-oauth2-core (from https://spring.io/spring-security) -- spring-security-oauth2-jose (from https://spring.io/spring-security) +- spring-security-config (from http://spring.io/spring-security) +- spring-security-core (from http://spring.io/spring-security) +- spring-security-oauth2-client (from http://spring.io/spring-security) +- spring-security-oauth2-core (from http://spring.io/spring-security) +- spring-security-oauth2-jose (from http://spring.io/spring-security) - spring-security-oauth2-resource-server (from https://spring.io/spring-security) -- spring-security-test (from https://spring.io/spring-security) -- spring-security-web (from https://spring.io/spring-security) +- spring-security-test (from http://spring.io/spring-security) +- spring-security-web (from http://spring.io/spring-security) - springfox-core (from https://github.com/springfox/springfox) - springfox-schema (from https://github.com/springfox/springfox) - springfox-spi (from https://github.com/springfox/springfox) @@ -950,7 +950,7 @@ The following software have components provided under the terms of this license: - 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) -- spring-security-core (from https://spring.io/spring-security) +- spring-security-core (from http://spring.io/spring-security) ======================================================================== MPL-1.1 @@ -991,7 +991,7 @@ SAX-PD ======================================================================== The following software have components provided under the terms of this license: -- Apache Ant Core (from https://ant.apache.org/) +- Apache Ant Core (from http://ant.apache.org/) ======================================================================== SPL-1.0 @@ -1005,7 +1005,7 @@ W3C ======================================================================== The following software have components provided under the terms of this license: -- Apache Ant Core (from https://ant.apache.org/) +- Apache Ant Core (from http://ant.apache.org/) - JTidy (from http://jtidy.sourceforge.net) - Xerces2-j (from https://xerces.apache.org/xerces2-j/) - xml-apis (from ) diff --git a/partition-core/src/main/java/org/opengroup/osdu/partition/api/PartitionApi.java b/partition-core/src/main/java/org/opengroup/osdu/partition/api/PartitionApi.java index 860086cf9..886d4da0a 100644 --- a/partition-core/src/main/java/org/opengroup/osdu/partition/api/PartitionApi.java +++ b/partition-core/src/main/java/org/opengroup/osdu/partition/api/PartitionApi.java @@ -14,7 +14,6 @@ package org.opengroup.osdu.partition.api; -import java.util.Collections; import org.opengroup.osdu.partition.logging.AuditLogger; import org.opengroup.osdu.partition.model.PartitionInfo; import org.opengroup.osdu.partition.model.Property; @@ -24,12 +23,21 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.annotation.RequestScope; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import javax.validation.Valid; import java.net.URI; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -39,7 +47,7 @@ import java.util.Map; public class PartitionApi { @Autowired - @Qualifier("cachedPartitionServiceImpl") + @Qualifier("partitionServiceImpl") private IPartitionService partitionService; @Autowired diff --git a/partition-core/src/main/java/org/opengroup/osdu/partition/provider/interfaces/IPartitionServiceCache.java b/partition-core/src/main/java/org/opengroup/osdu/partition/provider/interfaces/IPartitionServiceCache.java deleted file mode 100644 index 593d67c65..000000000 --- a/partition-core/src/main/java/org/opengroup/osdu/partition/provider/interfaces/IPartitionServiceCache.java +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2017-2020, Schlumberger -// -// 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.partition.provider.interfaces; - -import org.opengroup.osdu.core.common.cache.ICache; - -public interface IPartitionServiceCache<String, V> extends ICache<String, V> { -} diff --git a/partition-core/src/main/java/org/opengroup/osdu/partition/service/CachedPartitionServiceImpl.java b/partition-core/src/main/java/org/opengroup/osdu/partition/service/CachedPartitionServiceImpl.java deleted file mode 100644 index a06090238..000000000 --- a/partition-core/src/main/java/org/opengroup/osdu/partition/service/CachedPartitionServiceImpl.java +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright 2017-2020, Schlumberger -// -// 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.partition.service; - -import org.apache.http.HttpStatus; -import org.opengroup.osdu.core.common.model.http.AppException; -import org.opengroup.osdu.partition.model.PartitionInfo; -import org.opengroup.osdu.partition.provider.interfaces.IPartitionService; -import org.opengroup.osdu.partition.provider.interfaces.IPartitionServiceCache; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Service; - -import javax.inject.Inject; -import java.util.List; - -@Service -public class CachedPartitionServiceImpl implements IPartitionService { - - private static final String PARTITION_LIST_KEY = "getAllPartitions"; - - @Inject - @Qualifier("partitionServiceImpl") - private IPartitionService partitionService; - - @Inject - @Qualifier("partitionServiceCache") - private IPartitionServiceCache<String, PartitionInfo> partitionServiceCache; - - @Inject - @Qualifier("partitionListCache") - private IPartitionServiceCache<String, List<String>> partitionListCache; - - @Override - public PartitionInfo createPartition(String partitionId, PartitionInfo partitionInfo) { - if (partitionServiceCache.get(partitionId) != null) - throw new AppException(HttpStatus.SC_CONFLICT, "partition exist", "Partition with same id exist"); - PartitionInfo pi = partitionService.createPartition(partitionId, partitionInfo); - - if (pi != null) { - partitionServiceCache.put(partitionId, pi); - partitionListCache.clearAll(); - } - - return pi; - } - - @Override - public PartitionInfo updatePartition(String partitionId, PartitionInfo partitionInfo) { - PartitionInfo pi = partitionService.updatePartition(partitionId, partitionInfo); - - if(pi != null) { - partitionServiceCache.put(partitionId, pi); - } - - return pi; - } - - @Override - public PartitionInfo getPartition(String partitionId) { - PartitionInfo pi = (PartitionInfo) partitionServiceCache.get(partitionId); - - if (pi == null) { - pi = partitionService.getPartition(partitionId); - - if (pi != null) { - partitionServiceCache.put(partitionId, pi); - } - } - - return pi; - } - - @Override - public boolean deletePartition(String partitionId) { - if (partitionService.deletePartition(partitionId)) { - if (partitionServiceCache.get(partitionId) != null) { - partitionServiceCache.delete(partitionId); - } - partitionListCache.clearAll(); - return true; - } - - return false; - } - - @Override - public List<String> getAllPartitions() { - List<String> partitions = (List<String>)partitionListCache.get(PARTITION_LIST_KEY); - - if (partitions == null) { - partitions = partitionService.getAllPartitions(); - - if (partitions != null) { - partitionListCache.put(PARTITION_LIST_KEY, partitions); - } - } - return partitions; - } -} diff --git a/partition-core/src/test/java/org/opengroup/osdu/partition/service/CachedPartitionServiceImplTest.java b/partition-core/src/test/java/org/opengroup/osdu/partition/service/CachedPartitionServiceImplTest.java deleted file mode 100644 index 7d2680573..000000000 --- a/partition-core/src/test/java/org/opengroup/osdu/partition/service/CachedPartitionServiceImplTest.java +++ /dev/null @@ -1,150 +0,0 @@ -// Copyright 2017-2020, Schlumberger -// -// 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.partition.service; - -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.partition.model.PartitionInfo; -import org.opengroup.osdu.partition.provider.interfaces.IPartitionService; -import org.opengroup.osdu.partition.provider.interfaces.IPartitionServiceCache; - -import java.util.ArrayList; -import java.util.List; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; - -@RunWith(MockitoJUnitRunner.class) -public class CachedPartitionServiceImplTest { - - @Mock - private IPartitionService partitionServiceImpl; - - @Mock - private IPartitionServiceCache<String, PartitionInfo> partitionServiceCache; - - @Mock - private IPartitionServiceCache<String, List<String>> partitionListCache; - - @InjectMocks - private CachedPartitionServiceImpl cachedPartitionServiceImpl; - - @Test - public void createPartitionSucceed() { - String partId = "key"; - - PartitionInfo newPi = PartitionInfo.builder().build(); - PartitionInfo retPi = PartitionInfo.builder().build(); - - when(partitionServiceImpl.createPartition(partId, newPi)).thenReturn(retPi); - cachedPartitionServiceImpl.createPartition(partId, newPi); - - verify(partitionServiceImpl, times(1)).createPartition(partId, newPi); - verify(partitionServiceCache, times(1)).put(partId, retPi); - verify(partitionListCache, times(1)).clearAll(); - } - - @Test - public void createPartitionFailed() { - String partId = "key"; - PartitionInfo newPi = PartitionInfo.builder().build(); - - when(partitionServiceCache.get(partId)).thenReturn(null); - when(partitionServiceImpl.createPartition(partId, newPi)).thenReturn(null); - - cachedPartitionServiceImpl.createPartition(partId, newPi); - - verify(partitionServiceImpl, times(1)).createPartition(partId, newPi); - verify(partitionServiceCache, times(0)).put(any(), any()); - verify(partitionListCache, times(0)).clearAll(); - verify(partitionServiceCache, times(1)).get(any()); - } - - @Test - public void updatePartitionSucceed() { - String partId = "key"; - - PartitionInfo newPi = PartitionInfo.builder().build(); - PartitionInfo retPi = PartitionInfo.builder().build(); - - when(partitionServiceImpl.updatePartition(partId, newPi)).thenReturn(retPi); - - cachedPartitionServiceImpl.updatePartition(partId, newPi); - - verify(partitionServiceImpl, times(1)).updatePartition(partId, newPi); - verify(partitionServiceCache, times(1)).put(partId, retPi); - } - - @Test - public void updatePartitionFailed() { - String partId = "key"; - PartitionInfo newPi = PartitionInfo.builder().build(); - - when(partitionServiceImpl.updatePartition(partId, newPi)).thenReturn(null); - - cachedPartitionServiceImpl.updatePartition(partId, newPi); - - verify(partitionServiceImpl, times(1)).updatePartition(partId, newPi); - verify(partitionServiceCache, times(0)).put(any(), any()); - verify(partitionServiceCache, times(0)).get(any()); - } - - @Test - public void getPartition() { - String partId = "key"; - - PartitionInfo retPi = PartitionInfo.builder().build(); - - when(partitionServiceImpl.getPartition(partId)).thenReturn(retPi); - - cachedPartitionServiceImpl.getPartition(partId); - - verify(partitionServiceCache, times(1)).get(partId); - verify(partitionServiceImpl, times(1)).getPartition(partId); - verify(partitionServiceCache, times(1)).put(partId, retPi); - } - - @Test - public void deletePartition() { - String partId = "key"; - PartitionInfo retPi = PartitionInfo.builder().build(); - - when(partitionServiceImpl.deletePartition(partId)).thenReturn(true); - when(partitionServiceCache.get(partId)).thenReturn(retPi); - - cachedPartitionServiceImpl.deletePartition(partId); - - verify(partitionServiceImpl, times(1)).deletePartition(partId); - verify(partitionServiceCache, times(1)).delete(partId); - verify(partitionServiceCache, times(1)).get(partId); - verify(partitionListCache, times(1)).clearAll(); - } - - @Test - public void getAllPartitions() { - List<String> partitions = new ArrayList<>(); - - when(partitionServiceImpl.getAllPartitions()).thenReturn(partitions); - cachedPartitionServiceImpl.getAllPartitions(); - String partKey = "getAllPartitions"; - verify(partitionListCache, times(1)).get(partKey); - verify(partitionServiceImpl, times(1)).getAllPartitions(); - verify(partitionListCache, times(1)).put(partKey, partitions); - } - -} \ No newline at end of file diff --git a/provider/partition-aws/src/main/java/org/opengroup/osdu/partition/provider/aws/service/PartitionServiceDummyListCacheImpl.java b/provider/partition-aws/src/main/java/org/opengroup/osdu/partition/provider/aws/service/PartitionServiceDummyListCacheImpl.java deleted file mode 100644 index 6065c9027..000000000 --- a/provider/partition-aws/src/main/java/org/opengroup/osdu/partition/provider/aws/service/PartitionServiceDummyListCacheImpl.java +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright © 2020 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.partition.provider.aws.service; - -import org.opengroup.osdu.partition.model.PartitionInfo; -import org.opengroup.osdu.partition.provider.interfaces.IPartitionServiceCache; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Service; -import java.util.List; - -/*** - * We don't want to use cache. Implement a dummy service to always return a cache miss. - */ -@Service -@Qualifier("partitionListCache") -public class PartitionServiceDummyListCacheImpl implements IPartitionServiceCache<String, List<String>> { - public PartitionServiceDummyListCacheImpl() { - - } - - @Override - public void clearAll() { - return; - } - - @Override - public void delete(String arg0) { - return; - } - - @Override - public List<String> get(String arg0) { - return null; - } - - @Override - public void put(String arg0, List<String> arg1) { - return; - } -} diff --git a/provider/partition-aws/src/main/java/org/opengroup/osdu/partition/provider/aws/service/PartitionServiceDummyPartitionInfoCacheImpl.java b/provider/partition-aws/src/main/java/org/opengroup/osdu/partition/provider/aws/service/PartitionServiceDummyPartitionInfoCacheImpl.java deleted file mode 100644 index 615ddef17..000000000 --- a/provider/partition-aws/src/main/java/org/opengroup/osdu/partition/provider/aws/service/PartitionServiceDummyPartitionInfoCacheImpl.java +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright © 2020 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.partition.provider.aws.service; - -import org.opengroup.osdu.partition.model.PartitionInfo; -import org.opengroup.osdu.partition.provider.interfaces.IPartitionServiceCache; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Service; - -/*** - * We don't want to use cache. Implement a dummy service to always return a cache miss. - */ -@Service -@Qualifier("partitionServiceCache") -public class PartitionServiceDummyPartitionInfoCacheImpl implements IPartitionServiceCache<String, PartitionInfo> { - public PartitionServiceDummyPartitionInfoCacheImpl() { - - } - - @Override - public void clearAll() { - return; - } - - @Override - public void delete(String arg0) { - return; - } - - @Override - public PartitionInfo get(String arg0) { - return null; - } - - @Override - public void put(String arg0, PartitionInfo arg1) { - return; - } -} diff --git a/provider/partition-aws/src/main/java/org/opengroup/osdu/partition/provider/aws/service/PartitionServiceImpl.java b/provider/partition-aws/src/main/java/org/opengroup/osdu/partition/provider/aws/service/PartitionServiceImpl.java index ce1433df3..ad69c012f 100644 --- a/provider/partition-aws/src/main/java/org/opengroup/osdu/partition/provider/aws/service/PartitionServiceImpl.java +++ b/provider/partition-aws/src/main/java/org/opengroup/osdu/partition/provider/aws/service/PartitionServiceImpl.java @@ -14,9 +14,6 @@ package org.opengroup.osdu.partition.provider.aws.service; -import java.util.List; -import java.util.Map; - import org.apache.http.HttpStatus; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; import org.opengroup.osdu.core.common.model.http.AppException; @@ -27,6 +24,12 @@ import org.opengroup.osdu.partition.provider.interfaces.IPartitionService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; +import java.util.Map; + +/** + * AWS implementation doesn't use cache. + */ @Service public class PartitionServiceImpl implements IPartitionService { diff --git a/provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/cache/PartitionListCacheImpl.java b/provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/cache/PartitionListCacheImpl.java deleted file mode 100644 index 11a6a7061..000000000 --- a/provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/cache/PartitionListCacheImpl.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.opengroup.osdu.partition.provider.azure.cache; - -import org.opengroup.osdu.core.common.cache.ICache; -import org.opengroup.osdu.partition.provider.interfaces.IPartitionServiceCache; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Service; - -import javax.annotation.Resource; -import java.util.List; - -@Service -@Qualifier("partitionListCache") -public class PartitionListCacheImpl implements IPartitionServiceCache<String, List<String>> { - - @Resource(name="partitionListCache") - private ICache<String, List<String>> cache; - - @Override - public void put(String s, List<String> o) { - this.cache.put(s, o); - } - - @Override - public List<String> get(String s) { - return this.cache.get(s); - } - - @Override - public void delete(String s) { - this.cache.delete(s); - } - - @Override - public void clearAll() { - this.cache.clearAll(); - } - -} diff --git a/provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/cache/PartitionServiceCacheImpl.java b/provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/cache/PartitionServiceCacheImpl.java deleted file mode 100644 index 372432c05..000000000 --- a/provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/cache/PartitionServiceCacheImpl.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.opengroup.osdu.partition.provider.azure.cache; - -import org.opengroup.osdu.core.common.cache.ICache; -import org.opengroup.osdu.partition.model.PartitionInfo; -import org.opengroup.osdu.partition.provider.interfaces.IPartitionServiceCache; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Service; - -import javax.annotation.Resource; - -@Service -@Qualifier("partitionServiceCache") -public class PartitionServiceCacheImpl implements IPartitionServiceCache<String, PartitionInfo> { - - @Resource(name="partitionServiceCache") - private ICache<String, PartitionInfo> cache; - - @Override - public void put(String s, PartitionInfo o) { - this.cache.put(s, o); - } - - @Override - public PartitionInfo get(String s) { - return this.cache.get(s); - } - - @Override - public void delete(String s) { - this.cache.delete(s); - } - - @Override - public void clearAll() { - this.cache.clearAll(); - } -} diff --git a/provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/service/PartitionServiceImpl.java b/provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/service/PartitionServiceImpl.java index ec96e47aa..b1ae9b1c1 100644 --- a/provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/service/PartitionServiceImpl.java +++ b/provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/service/PartitionServiceImpl.java @@ -15,41 +15,56 @@ package org.opengroup.osdu.partition.provider.azure.service; import org.apache.http.HttpStatus; +import org.opengroup.osdu.core.common.cache.ICache; import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.partition.model.PartitionInfo; import org.opengroup.osdu.partition.model.Property; import org.opengroup.osdu.partition.provider.azure.persistence.PartitionTableStore; import org.opengroup.osdu.partition.provider.interfaces.IPartitionService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; -import java.util.ArrayList; +import javax.inject.Inject; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; @Service public class PartitionServiceImpl implements IPartitionService { - private final String PARTITION_NOT_FOUND = "partition not found"; + static final String PARTITION_LIST_KEY = "getAllPartitions"; + static final String PARTITION_NOT_FOUND = "partition not found"; @Autowired private PartitionTableStore tableStore; + @Inject + @Qualifier("partitionServiceCache") + private ICache<String, PartitionInfo> partitionServiceCache; + + @Inject + @Qualifier("partitionListCache") + private ICache<String, List<String>> partitionListCache; + @Override public PartitionInfo createPartition(String partitionId, PartitionInfo partitionInfo) { - if (this.tableStore.partitionExists(partitionId)) { + if (partitionServiceCache.get(partitionId) != null || tableStore.partitionExists(partitionId)) { throw new AppException(HttpStatus.SC_CONFLICT, "partition exist", "Partition with same id exist"); } - this.tableStore.addPartition(partitionId, partitionInfo); + tableStore.addPartition(partitionId, partitionInfo); + + partitionServiceCache.put(partitionId, partitionInfo); + partitionListCache.clearAll(); return partitionInfo; } @Override public PartitionInfo updatePartition(String partitionId, PartitionInfo partitionInfo) { - if (!this.tableStore.partitionExists(partitionId)) { + if (!tableStore.partitionExists(partitionId)) { throw new AppException(HttpStatus.SC_NOT_FOUND, PARTITION_NOT_FOUND, String.format("%s partition not found", partitionId)); } @@ -57,35 +72,65 @@ public class PartitionServiceImpl implements IPartitionService { throw new AppException(HttpStatus.SC_BAD_REQUEST, "can not update id", "the field id can not be updated"); } - this.tableStore.addPartition(partitionId, partitionInfo); - return PartitionInfo.builder().properties(this.tableStore.getPartition(partitionId)).build(); + tableStore.addPartition(partitionId, partitionInfo); + PartitionInfo pi = Optional.ofNullable(tableStore.getPartition(partitionId)) + .map(map -> PartitionInfo.builder().properties(map).build()) + .orElse(null); + + if(pi != null) { + partitionServiceCache.put(partitionId, pi); + } + + return pi; } @Override public PartitionInfo getPartition(String partitionId) { - Map<String, Property> out = new HashMap<>(); - out.putAll(this.tableStore.getPartition(partitionId)); + PartitionInfo pi = partitionServiceCache.get(partitionId); - if (out.isEmpty()) { - throw new AppException(HttpStatus.SC_NOT_FOUND, PARTITION_NOT_FOUND, String.format("%s partition not found", partitionId)); + if (pi == null) { + Map<String, Property> out = new HashMap<>(tableStore.getPartition(partitionId)); + + if (out.isEmpty()) { + throw new AppException(HttpStatus.SC_NOT_FOUND, PARTITION_NOT_FOUND, String.format("%s partition not found", partitionId)); + } + + pi = PartitionInfo.builder().properties(out).build(); + + if (pi != null) { + partitionServiceCache.put(partitionId, pi); + } } - return PartitionInfo.builder().properties(out).build(); + return pi; } @Override public boolean deletePartition(String partitionId) { - if (!this.tableStore.partitionExists(partitionId)) { + if (!tableStore.partitionExists(partitionId)) { throw new AppException(HttpStatus.SC_NOT_FOUND, PARTITION_NOT_FOUND, String.format("%s partition not found", partitionId)); } - this.tableStore.deletePartition(partitionId); + tableStore.deletePartition(partitionId); + if (partitionServiceCache.get(partitionId) != null) { + partitionServiceCache.delete(partitionId); + } + partitionListCache.clearAll(); return true; } @Override public List<String> getAllPartitions() { - return this.tableStore.getAllPartitions(); + List<String> partitions = partitionListCache.get(PARTITION_LIST_KEY); + + if (partitions == null) { + partitions = tableStore.getAllPartitions(); + + if (partitions != null) { + partitionListCache.put(PARTITION_LIST_KEY, partitions); + } + } + return partitions; } } \ No newline at end of file diff --git a/provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/service/PartitionServiceImplCacheTest.java b/provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/service/PartitionServiceImplCacheTest.java new file mode 100644 index 000000000..6c5b7800e --- /dev/null +++ b/provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/service/PartitionServiceImplCacheTest.java @@ -0,0 +1,146 @@ +// Copyright 2017-2020, Schlumberger +// +// 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.partition.provider.azure.service; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opengroup.osdu.core.common.cache.ICache; +import org.opengroup.osdu.partition.model.PartitionInfo; +import org.opengroup.osdu.partition.model.Property; +import org.opengroup.osdu.partition.provider.azure.persistence.PartitionTableStore; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class PartitionServiceImplCacheTest { + + @Mock + private PartitionTableStore tableStore; + + @Mock + private ICache<String, PartitionInfo> partitionServiceCache; + + @Mock + private ICache<String, List<String>> partitionListCache; + + @InjectMocks + private PartitionServiceImpl partitionService; + + @Test + public void createPartitionSucceed() { + String partId = "key"; + + PartitionInfo newPi = PartitionInfo.builder().build(); + PartitionInfo retPi = PartitionInfo.builder().build(); + + PartitionInfo partition = partitionService.createPartition(partId, newPi); + + assertEquals(newPi, partition); + verify(tableStore, times(1)).addPartition(partId, newPi); + verify(partitionServiceCache, times(1)).put(partId, retPi); + verify(partitionListCache, times(1)).clearAll(); + } + + @Test + public void updatePartitionSucceed() { + String partId = "key"; + + PartitionInfo newPi = PartitionInfo.builder().build(); + Map<String, Property> retPiProps = new HashMap<>(); + PartitionInfo retPi = PartitionInfo.builder().properties(retPiProps).build(); + + when(tableStore.partitionExists(partId)).thenReturn(true); + when(tableStore.getPartition(partId)).thenReturn(retPiProps); + + partitionService.updatePartition(partId, newPi); + + verify(tableStore, times(1)).addPartition(partId, newPi); + verify(partitionServiceCache, times(1)).put(partId, retPi); + } + + @Test + public void updatePartitionFailed() { + String partId = "key"; + PartitionInfo newPi = PartitionInfo.builder().build(); + + when(tableStore.partitionExists(partId)).thenReturn(true); + when(tableStore.getPartition(partId)).thenReturn(null); + + partitionService.updatePartition(partId, newPi); + + verify(tableStore, times(1)).addPartition(partId, newPi); + verify(partitionServiceCache, times(0)).put(any(), any()); + verify(partitionServiceCache, times(0)).get(any()); + } + + @Test + public void getPartition() { + String partId = "key"; + + Map<String, Property> retPiProps = new HashMap<String, Property>() {{ + put("1", mock(Property.class)); + }}; + PartitionInfo retPi = PartitionInfo.builder().properties(retPiProps).build(); + + when(tableStore.getPartition(partId)).thenReturn(retPiProps); + + partitionService.getPartition(partId); + + verify(partitionServiceCache, times(1)).get(partId); + verify(tableStore, times(1)).getPartition(partId); + verify(partitionServiceCache, times(1)).put(partId, retPi); + } + + @Test + public void deletePartition() { + String partId = "key"; + PartitionInfo retPi = PartitionInfo.builder().build(); + + when(tableStore.partitionExists(partId)).thenReturn(true); + when(partitionServiceCache.get(partId)).thenReturn(retPi); + + partitionService.deletePartition(partId); + + verify(tableStore, times(1)).deletePartition(partId); + verify(partitionServiceCache, times(1)).delete(partId); + verify(partitionServiceCache, times(1)).get(partId); + verify(partitionListCache, times(1)).clearAll(); + } + + @Test + public void getAllPartitions() { + List<String> partitions = new ArrayList<>(); + + when(tableStore.getAllPartitions()).thenReturn(partitions); + partitionService.getAllPartitions(); + String partKey = "getAllPartitions"; + verify(partitionListCache, times(1)).get(partKey); + verify(tableStore, times(1)).getAllPartitions(); + verify(partitionListCache, times(1)).put(partKey, partitions); + } +} \ No newline at end of file diff --git a/provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/service/PartitionServiceImplTest.java b/provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/service/PartitionServiceImplTest.java index 8643ebee2..2521a6a6f 100644 --- a/provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/service/PartitionServiceImplTest.java +++ b/provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/service/PartitionServiceImplTest.java @@ -14,28 +14,38 @@ package org.opengroup.osdu.partition.provider.azure.service; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opengroup.osdu.core.common.cache.ICache; import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.partition.model.PartitionInfo; import org.opengroup.osdu.partition.model.Property; import org.opengroup.osdu.partition.provider.azure.persistence.PartitionTableStore; -import org.powermock.modules.junit4.PowerMockRunner; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.when; -@RunWith(PowerMockRunner.class) +@ExtendWith(MockitoExtension.class) public class PartitionServiceImplTest { + @Mock + private ICache<String, PartitionInfo> partitionServiceCache; + + @Mock + private ICache<String, List<String>> partitionListCache; + @Mock private PartitionTableStore tableStore; @InjectMocks @@ -46,7 +56,7 @@ public class PartitionServiceImplTest { private final static String PARTITION_ID = "my-tenant"; private final Map<String, Property> properties = new HashMap<>(); - @Before + @BeforeEach public void setup() { properties.put("id", Property.builder().value(PARTITION_ID).build()); properties.put("storageAccount", Property.builder().value("storage-account").sensitive(true).build()); @@ -136,8 +146,6 @@ public class PartitionServiceImplTest { @Test public void should_throwException_when_deletingNonExistentPartition() { - when(this.tableStore.partitionExists(PARTITION_ID)).thenReturn(false); - try { this.sut.deletePartition("test-partition"); } catch (AppException ae) { @@ -146,10 +154,9 @@ public class PartitionServiceImplTest { } } - @Test(expected = AppException.class) + @Test public void should_throwException_when_deletingInvalidPartition() { - - this.sut.deletePartition(null); + assertThrows(AppException.class, () -> sut.deletePartition(null)); } @Test diff --git a/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/cache/PartitionListCacheImpl.java b/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/cache/PartitionListCacheImpl.java deleted file mode 100644 index 366bb47ab..000000000 --- a/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/cache/PartitionListCacheImpl.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - Copyright 2002-2021 Google LLC - Copyright 2002-2021 EPAM Systems, Inc - - 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.partition.provider.gcp.cache; - -import java.util.List; -import lombok.RequiredArgsConstructor; -import org.opengroup.osdu.core.common.cache.ICache; -import org.opengroup.osdu.partition.provider.interfaces.IPartitionServiceCache; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Service; - -@Service -@Qualifier("partitionListCache") -@RequiredArgsConstructor -public class PartitionListCacheImpl implements IPartitionServiceCache<String, List<String>> { - - private final ICache<String, List<String>> cache; - - @Override - public void put(String s, List<String> o) { - this.cache.put(s, o); - } - - @Override - public List<String> get(String s) { - return this.cache.get(s); - } - - @Override - public void delete(String s) { - this.cache.delete(s); - } - - @Override - public void clearAll() { - this.cache.clearAll(); - } -} diff --git a/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/cache/PartitionServiceCacheImpl.java b/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/cache/PartitionServiceCacheImpl.java deleted file mode 100644 index af35fb943..000000000 --- a/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/cache/PartitionServiceCacheImpl.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - Copyright 2002-2021 Google LLC - Copyright 2002-2021 EPAM Systems, Inc - - 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.partition.provider.gcp.cache; - -import lombok.RequiredArgsConstructor; -import org.opengroup.osdu.core.common.cache.ICache; -import org.opengroup.osdu.partition.model.PartitionInfo; -import org.opengroup.osdu.partition.provider.interfaces.IPartitionServiceCache; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Service; - -@Service -@Qualifier("partitionServiceCache") -@RequiredArgsConstructor -public class PartitionServiceCacheImpl implements IPartitionServiceCache<String, PartitionInfo> { - - private final ICache<String, PartitionInfo> cache; - - @Override - public void put(String s, PartitionInfo o) { - this.cache.put(s, o); - } - - @Override - public PartitionInfo get(String s) { - return this.cache.get(s); - } - - @Override - public void delete(String s) { - this.cache.delete(s); - } - - @Override - public void clearAll() { - this.cache.clearAll(); - } -} diff --git a/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImpl.java b/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImpl.java index 1c82f49c7..226e29490 100644 --- a/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImpl.java +++ b/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImpl.java @@ -17,15 +17,9 @@ package org.opengroup.osdu.partition.provider.gcp.service; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; import lombok.RequiredArgsConstructor; import org.apache.http.HttpStatus; +import org.opengroup.osdu.core.common.cache.ICache; import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.common.provider.interfaces.IKmsClient; import org.opengroup.osdu.partition.logging.AuditLogger; @@ -37,12 +31,21 @@ import org.opengroup.osdu.partition.provider.interfaces.IPartitionService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + @Service @RequiredArgsConstructor public class PartitionServiceImpl implements IPartitionService { - private static final String UNKNOWN_ERROR_REASON = "unknown error"; - + static final String UNKNOWN_ERROR_REASON = "unknown error"; + static final String PARTITION_LIST_KEY = "getAllPartitions"; private final PartitionPropertyEntityRepository partitionPropertyEntityRepository; @@ -50,17 +53,24 @@ public class PartitionServiceImpl implements IPartitionService { private final AuditLogger auditLogger; + private final ICache<String, PartitionInfo> partitionServiceCache; + + private final ICache<String, List<String>> partitionListCache; + @Override public PartitionInfo createPartition(String partitionId, PartitionInfo partitionInfo) { + if (partitionServiceCache.get(partitionId) != null) + throw new AppException(HttpStatus.SC_CONFLICT, "partition exist", "Partition with same id exist"); + if (this.partitionPropertyEntityRepository.findByPartitionId(partitionId).isPresent()) { this.auditLogger.createPartitionFailure(Collections.singletonList(partitionId)); throw new AppException(HttpStatus.SC_CONFLICT, UNKNOWN_ERROR_REASON, - "Partition already exists."); + "Partition already exists."); } List<PartitionPropertyEntity> partitionProperties = new ArrayList<>(); for (Map.Entry<String, Property> entry : partitionInfo.getProperties().entrySet()) { PartitionPropertyEntity entity = new PartitionPropertyEntity(partitionId, - entry.getKey(), entry.getValue()); + entry.getKey(), entry.getValue()); encryptPartitionPropertyEntityIfNeeded(entity); partitionProperties.add(entity); } @@ -68,7 +78,13 @@ public class PartitionServiceImpl implements IPartitionService { repository.saveAll(partitionProperties); return true; }); - return getPartition(partitionId); + PartitionInfo pi = getPartition(partitionId); + + if (pi != null) { + partitionListCache.clearAll(); + } + + return pi; } private void encryptPartitionPropertyEntityIfNeeded(PartitionPropertyEntity entity) { @@ -88,19 +104,19 @@ public class PartitionServiceImpl implements IPartitionService { if (partitionInfo.getProperties().containsKey("id")) { this.auditLogger.updatePartitionSecretFailure(Collections.singletonList(partitionId)); throw new AppException(HttpStatus.SC_BAD_REQUEST, "can not update id", - "the field id can not be updated"); + "the field id can not be updated"); } if (!this.partitionPropertyEntityRepository.findByPartitionId(partitionId).isPresent()) { this.auditLogger.updatePartitionSecretFailure(Collections.singletonList(partitionId)); throw new AppException(HttpStatus.SC_NOT_FOUND, UNKNOWN_ERROR_REASON, - "An attempt to update not existing partition."); + "An attempt to update not existing partition."); } List<PartitionPropertyEntity> partitionProperties = new ArrayList<>(); for (Map.Entry<String, Property> entry : partitionInfo.getProperties().entrySet()) { PartitionPropertyEntity entity = this.partitionPropertyEntityRepository - .findByPartitionIdAndName(partitionId, entry.getKey()); + .findByPartitionIdAndName(partitionId, entry.getKey()); if (Objects.nonNull(entity)) { entity.setSensitive(entry.getValue().isSensitive()); entity.setValue(entry.getValue().getValue()); @@ -114,26 +130,37 @@ public class PartitionServiceImpl implements IPartitionService { repository.saveAll(partitionProperties); return true; }); + return getPartition(partitionId); } @Override public PartitionInfo getPartition(String partitionId) { - PartitionInfo partitionInfo = getEncryptedPartition(partitionId); - for (Property property : partitionInfo.getProperties().values()) { - decryptPartitionPropertyIfNeeded(property); + PartitionInfo pi = partitionServiceCache.get(partitionId); + + if (pi == null) { + pi = getEncryptedPartition(partitionId); + for (Property property : pi.getProperties().values()) { + decryptPartitionPropertyIfNeeded(property); + } + + if (pi != null) { + partitionServiceCache.put(partitionId, pi); + } } - return partitionInfo; + + return pi; } private PartitionInfo getEncryptedPartition(String partitionId) { - if (!this.partitionPropertyEntityRepository.findByPartitionId(partitionId).isPresent()) { + Optional<List<PartitionPropertyEntity>> partitionPropertyEntitiesOptional = partitionPropertyEntityRepository + .findByPartitionId(partitionId); + if (!partitionPropertyEntitiesOptional.isPresent()) { this.auditLogger.readPartitionFailure(Collections.singletonList(partitionId)); throw new AppException(HttpStatus.SC_NOT_FOUND, UNKNOWN_ERROR_REASON, "Partition does not exist."); } - List<PartitionPropertyEntity> partitionPropertiesList = this.partitionPropertyEntityRepository - .findByPartitionId(partitionId).get(); + List<PartitionPropertyEntity> partitionPropertiesList = partitionPropertyEntitiesOptional.get(); PartitionInfo partitionInfo = new PartitionInfo(); Map<String, Property> partitionInfoProperties = new HashMap<>(); for (PartitionPropertyEntity entity : partitionPropertiesList) { @@ -163,16 +190,30 @@ public class PartitionServiceImpl implements IPartitionService { if (!this.partitionPropertyEntityRepository.findByPartitionId(partitionId).isPresent()) { this.auditLogger.deletePartitionFailure(Collections.singletonList(partitionId)); throw new AppException(HttpStatus.SC_NOT_FOUND, UNKNOWN_ERROR_REASON, - "An attempt to delete not existing partition."); + "An attempt to delete not existing partition."); } this.partitionPropertyEntityRepository.deleteByPartitionId(partitionId); + + if (partitionServiceCache.get(partitionId) != null) { + partitionServiceCache.delete(partitionId); + } + partitionListCache.clearAll(); return true; } @Transactional @Override public List<String> getAllPartitions() { - List<String> allPartitions = this.partitionPropertyEntityRepository.getAllPartitions(); - return (allPartitions.isEmpty() ? null : allPartitions); + List<String> partitions = partitionListCache.get(PARTITION_LIST_KEY); + + if (partitions == null) { + List<String> allPartitions = this.partitionPropertyEntityRepository.getAllPartitions(); + partitions = (allPartitions.isEmpty() ? null : allPartitions); + + if (partitions != null) { + partitionListCache.put(PARTITION_LIST_KEY, partitions); + } + } + return partitions; } } diff --git a/provider/partition-gcp/src/test/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImplCacheTest.java b/provider/partition-gcp/src/test/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImplCacheTest.java new file mode 100644 index 000000000..388f56f28 --- /dev/null +++ b/provider/partition-gcp/src/test/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImplCacheTest.java @@ -0,0 +1,200 @@ +/* + Copyright 2002-2021 Google LLC + Copyright 2002-2021 EPAM Systems, Inc + + 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.partition.provider.gcp.service; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opengroup.osdu.core.common.cache.ICache; +import org.opengroup.osdu.core.common.model.http.AppException; +import org.opengroup.osdu.core.common.provider.interfaces.IKmsClient; +import org.opengroup.osdu.partition.logging.AuditLogger; +import org.opengroup.osdu.partition.model.PartitionInfo; +import org.opengroup.osdu.partition.model.Property; +import org.opengroup.osdu.partition.provider.gcp.model.PartitionPropertyEntity; +import org.opengroup.osdu.partition.provider.gcp.repository.PartitionPropertyEntityRepository; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.argThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class PartitionServiceImplCacheTest { + + @Mock + private ICache<String, PartitionInfo> partitionServiceCache; + + @Mock + private ICache<String, List<String>> partitionListCache; + + @Mock + private PartitionPropertyEntityRepository partitionPropertyEntityRepository; + + @Mock + private AuditLogger auditLogger; + + private PartitionServiceImpl partitionServiceImpl; + + @BeforeEach + public void setup() { + partitionServiceImpl = new PartitionServiceImpl( + partitionPropertyEntityRepository, + mock(IKmsClient.class), + auditLogger, + partitionServiceCache, + partitionListCache + ); + } + + private List<PartitionPropertyEntity> partitionInfoToEntity(String partitionId, PartitionInfo partitionInfo) { + return partitionInfo.getProperties().entrySet().stream() + .map(entry -> new PartitionPropertyEntity(partitionId, entry.getKey(), entry.getValue())) + .collect(Collectors.toList()); + } + + @Test + public void createPartitionSucceed() { + String partId = "key"; + + PartitionInfo newPi = PartitionInfo.builder().build(); + PartitionInfo retPi = PartitionInfo.builder().build(); + String propKey = "123987123498"; + retPi.getProperties().put(propKey, new Property()); + + doReturn(Optional.empty(), Optional.of(partitionInfoToEntity(partId, retPi))) + .when(partitionPropertyEntityRepository).findByPartitionId(partId); + partitionServiceImpl.createPartition(partId, newPi); + + verify(partitionPropertyEntityRepository, times(1)).performTransaction(any()); + verify(partitionServiceCache, times(1)) + .put(any(), argThat(argument -> argument.getProperties().containsKey(propKey))); + verify(partitionListCache).clearAll(); + } + + @Test + public void createPartitionFailed() { + String partId = "key"; + PartitionInfo newPi = PartitionInfo.builder().build(); + + when(partitionServiceCache.get(partId)).thenReturn(null); + doReturn(Optional.empty()) + .when(partitionPropertyEntityRepository).findByPartitionId(partId); + + assertThrows(AppException.class, () -> partitionServiceImpl.createPartition(partId, newPi)); + + verify(partitionServiceCache, times(0)).put(any(), any()); + verify(partitionListCache, times(0)).clearAll(); + verify(partitionServiceCache, times(2)).get(any()); + } + + @Test + public void updatePartitionSucceed() { + String partId = "key"; + + PartitionInfo newPi = PartitionInfo.builder().build(); + PartitionInfo retPi = PartitionInfo.builder().build(); + String propKey = "123987123498"; + retPi.getProperties().put(propKey, new Property()); + + doReturn(Optional.of(partitionInfoToEntity(partId, retPi))) + .when(partitionPropertyEntityRepository).findByPartitionId(partId); + + partitionServiceImpl.updatePartition(partId, newPi); + + verify(partitionPropertyEntityRepository, times(1)).performTransaction(any()); + verify(partitionServiceCache, times(1)) + .put(any(), argThat(argument -> argument.getProperties().containsKey(propKey))); + } + + @Test + public void updatePartitionFailed() { + String partId = "key"; + PartitionInfo newPi = PartitionInfo.builder().build(); + + doReturn(Optional.empty()) + .when(partitionPropertyEntityRepository).findByPartitionId(partId); + + assertThrows(AppException.class, () -> partitionServiceImpl.updatePartition(partId, newPi)); + + verify(partitionServiceCache, times(0)).put(any(), any()); + verify(partitionServiceCache, times(0)).get(any()); + } + + + @Test + public void getPartition() { + String partId = "key"; + + PartitionInfo retPi = PartitionInfo.builder().build(); + String propKey = "123987123498"; + retPi.getProperties().put(propKey, new Property()); + + doReturn(Optional.of(partitionInfoToEntity(partId, retPi))) + .when(partitionPropertyEntityRepository).findByPartitionId(partId); + + partitionServiceImpl.getPartition(partId); + + verify(partitionServiceCache, times(1)).get(partId); + verify(partitionPropertyEntityRepository, times(1)).findByPartitionId(partId); + verify(partitionServiceCache, times(1)).put(partId, retPi); + } + + @Test + public void deletePartition() { + String partId = "key"; + PartitionInfo retPi = PartitionInfo.builder().build(); + + doReturn(Optional.of(partitionInfoToEntity(partId, retPi))) + .when(partitionPropertyEntityRepository).findByPartitionId(partId); + when(partitionServiceCache.get(partId)).thenReturn(retPi); + + partitionServiceImpl.deletePartition(partId); + + verify(partitionPropertyEntityRepository, times(1)).deleteByPartitionId(partId); + verify(partitionServiceCache, times(1)).delete(partId); + verify(partitionServiceCache, times(1)).get(partId); + verify(partitionListCache, times(1)).clearAll(); + } + + @Test + public void getAllPartitions() { + List<String> partitions = new ArrayList<>(); + partitions.add("1"); + + doReturn(partitions) + .when(partitionPropertyEntityRepository).getAllPartitions(); + + partitionServiceImpl.getAllPartitions(); + + verify(partitionListCache, times(1)).get(PartitionServiceImpl.PARTITION_LIST_KEY); + verify(partitionPropertyEntityRepository, times(1)).getAllPartitions(); + verify(partitionListCache, times(1)).put(PartitionServiceImpl.PARTITION_LIST_KEY, partitions); + } +} \ No newline at end of file diff --git a/provider/partition-gcp/src/test/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImplTest.java b/provider/partition-gcp/src/test/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImplTest.java index fadb1f153..a583242da 100644 --- a/provider/partition-gcp/src/test/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImplTest.java +++ b/provider/partition-gcp/src/test/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImplTest.java @@ -17,161 +17,185 @@ package org.opengroup.osdu.partition.provider.gcp.service; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opengroup.osdu.core.common.cache.ICache; import org.opengroup.osdu.core.common.model.http.AppException; +import org.opengroup.osdu.core.common.provider.interfaces.IKmsClient; import org.opengroup.osdu.partition.logging.AuditLogger; import org.opengroup.osdu.partition.model.PartitionInfo; import org.opengroup.osdu.partition.model.Property; import org.opengroup.osdu.partition.provider.gcp.model.PartitionPropertyEntity; import org.opengroup.osdu.partition.provider.gcp.repository.PartitionPropertyEntityRepository; -@RunWith(MockitoJUnitRunner.class) -public class PartitionServiceImplTest { - - private static final String PARTITION_ID = "newPartition"; - private static final boolean SENSITIVE = false; - private static final String NAME = "new-key"; - private static final String VALUE = "new-value"; - - - @Mock - private PartitionPropertyEntityRepository partitionPropertyEntityRepository; - - @Mock - private AuditLogger auditLogger; - - @InjectMocks - private PartitionServiceImpl partitionServiceImpl; - - private PartitionInfo expectedPartitionInfo; - private PartitionPropertyEntity partitionPropertyEntity; - private Optional<List<PartitionPropertyEntity>> partitionPropertyEntityList; - private Optional<List<PartitionPropertyEntity>> emptyList; - - @Before - public void setup() { - this.expectedPartitionInfo = new PartitionInfo(); - - Property property = new Property(); - property.setSensitive(SENSITIVE); - property.setValue(VALUE); - - Map<String, Property> properties = new HashMap<>(); - properties.put(NAME, property); - - this.expectedPartitionInfo.setProperties(properties); - - partitionPropertyEntity = new PartitionPropertyEntity(); - partitionPropertyEntity.setPartitionId(PARTITION_ID); - partitionPropertyEntity.setName(NAME); - partitionPropertyEntity.setSensitive(SENSITIVE); - partitionPropertyEntity.setValue(VALUE); - - List<PartitionPropertyEntity> entities = new ArrayList<>(); - entities.add(partitionPropertyEntity); - this.partitionPropertyEntityList = Optional.of(entities); - - this.emptyList = Optional.empty(); - } - - @Test - public void should_createPartition_when_partitionDoesNotExist() { - when(this.partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) - .thenReturn(this.emptyList, this.partitionPropertyEntityList); - - PartitionInfo actualPartitionInfo = this.partitionServiceImpl - .createPartition(PARTITION_ID, this.expectedPartitionInfo); - - assertEquals(this.expectedPartitionInfo, actualPartitionInfo); - } - - @Test(expected = AppException.class) - public void should_throwAnException_when_createPartitionWhichAlreadyExists() { - when(this.partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) - .thenReturn(this.emptyList); - - this.partitionServiceImpl.createPartition(PARTITION_ID, new PartitionInfo()); - } - - @Test - public void should_updatePartition_when_partitionExists() { - when(this.partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) - .thenReturn(this.partitionPropertyEntityList); - when(this.partitionPropertyEntityRepository.findByPartitionIdAndName(PARTITION_ID, NAME)) - .thenReturn(null); - - PartitionInfo actualPartitionInfo = this.partitionServiceImpl - .updatePartition(PARTITION_ID, this.expectedPartitionInfo); - - assertEquals(this.expectedPartitionInfo, actualPartitionInfo); - } - - @Test(expected = AppException.class) - public void should_throwAnException_when_updatePartitionWhichDoesNotExist() { - when(this.partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) - .thenReturn(this.emptyList); - this.partitionServiceImpl.createPartition(PARTITION_ID, new PartitionInfo()); - } - - @Test - public void should_getPartition_when_partitionExists() { - when(this.partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) - .thenReturn(this.partitionPropertyEntityList); - - PartitionInfo actualPartitionInfo = this.partitionServiceImpl.getPartition(PARTITION_ID); - - assertEquals(this.expectedPartitionInfo, actualPartitionInfo); - } - - @Test(expected = AppException.class) - public void should_throwAnException_when_getPartitionWhichDoesNotExist() { - when(this.partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) - .thenReturn(this.emptyList); - this.partitionServiceImpl.getPartition(PARTITION_ID); - } - - @Test - public void should_deletePartition_when_partitionExists() { - when(this.partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) - .thenReturn(this.partitionPropertyEntityList); - doNothing().when(this.partitionPropertyEntityRepository).deleteByPartitionId(PARTITION_ID); +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; - boolean expected = true; - boolean actual = partitionServiceImpl.deletePartition(PARTITION_ID); +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; - assertEquals(expected, actual); - } +@ExtendWith(MockitoExtension.class) +public class PartitionServiceImplTest { - @Test(expected = AppException.class) - public void should_throwAnException_when_deletePartitionWhichDoesNotExist() { - when(this.partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) - .thenReturn(this.emptyList); - this.partitionServiceImpl.deletePartition(PARTITION_ID); - } - - @Test - public void should_getAllPartitions() { - List<String> expectedPartitions = new ArrayList<>(); - expectedPartitions.add(PARTITION_ID); + private static final String PARTITION_ID = "newPartition"; + private static final boolean SENSITIVE = false; + private static final String NAME = "new-key"; + private static final String VALUE = "new-value"; + + @Mock + private ICache<String, PartitionInfo> partitionServiceCache; - when(this.partitionPropertyEntityRepository.getAllPartitions()).thenReturn(expectedPartitions); + @Mock + private ICache<String, List<String>> partitionListCache; - List<String> actualPartitions = this.partitionServiceImpl.getAllPartitions(); + @Mock + private PartitionPropertyEntityRepository partitionPropertyEntityRepository; - assertEquals(expectedPartitions, actualPartitions); - } + @Mock + private AuditLogger auditLogger; + + private PartitionServiceImpl partitionServiceImpl; + + private PartitionInfo expectedPartitionInfo; + private PartitionPropertyEntity partitionPropertyEntity; + private Optional<List<PartitionPropertyEntity>> partitionPropertyEntityList; + private Optional<List<PartitionPropertyEntity>> emptyList; + + @BeforeEach + public void setup() { + partitionServiceImpl = new PartitionServiceImpl( + partitionPropertyEntityRepository, + mock(IKmsClient.class), + auditLogger, + partitionServiceCache, + partitionListCache + ); + + expectedPartitionInfo = new PartitionInfo(); + + Property property = new Property(); + property.setSensitive(SENSITIVE); + property.setValue(VALUE); + + Map<String, Property> properties = new HashMap<>(); + properties.put(NAME, property); + + expectedPartitionInfo.setProperties(properties); + + partitionPropertyEntity = new PartitionPropertyEntity(); + partitionPropertyEntity.setPartitionId(PARTITION_ID); + partitionPropertyEntity.setName(NAME); + partitionPropertyEntity.setSensitive(SENSITIVE); + partitionPropertyEntity.setValue(VALUE); + + List<PartitionPropertyEntity> entities = new ArrayList<>(); + entities.add(partitionPropertyEntity); + partitionPropertyEntityList = Optional.of(entities); + + emptyList = Optional.empty(); + } + + @Test + public void should_createPartition_when_partitionDoesNotExist() { + when(partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) + .thenReturn(emptyList, partitionPropertyEntityList); + + PartitionInfo actualPartitionInfo = partitionServiceImpl + .createPartition(PARTITION_ID, expectedPartitionInfo); + + assertEquals(expectedPartitionInfo, actualPartitionInfo); + } + + @Test + public void should_throwAnException_when_createPartitionWhichAlreadyExists() { + assertThrows(AppException.class, () -> { + when(partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) + .thenReturn(emptyList); + + partitionServiceImpl.createPartition(PARTITION_ID, new PartitionInfo()); + }); + } + + @Test + public void should_updatePartition_when_partitionExists() { + when(partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) + .thenReturn(partitionPropertyEntityList); + when(partitionPropertyEntityRepository.findByPartitionIdAndName(PARTITION_ID, NAME)) + .thenReturn(null); + + PartitionInfo actualPartitionInfo = partitionServiceImpl + .updatePartition(PARTITION_ID, expectedPartitionInfo); + + assertEquals(expectedPartitionInfo, actualPartitionInfo); + } + + @Test + public void should_throwAnException_when_updatePartitionWhichDoesNotExist() { + assertThrows(AppException.class, () -> { + when(partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) + .thenReturn(emptyList); + partitionServiceImpl.createPartition(PARTITION_ID, new PartitionInfo()); + }); + } + + @Test + public void should_getPartition_when_partitionExists() { + when(partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) + .thenReturn(partitionPropertyEntityList); + + PartitionInfo actualPartitionInfo = partitionServiceImpl.getPartition(PARTITION_ID); + + assertEquals(expectedPartitionInfo, actualPartitionInfo); + } + + @Test + public void should_throwAnException_when_getPartitionWhichDoesNotExist() { + assertThrows(AppException.class, () -> { + when(partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) + .thenReturn(emptyList); + partitionServiceImpl.getPartition(PARTITION_ID); + }); + } + + @Test + public void should_deletePartition_when_partitionExists() { + when(partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) + .thenReturn(partitionPropertyEntityList); + doNothing().when(partitionPropertyEntityRepository).deleteByPartitionId(PARTITION_ID); + + boolean expected = true; + boolean actual = partitionServiceImpl.deletePartition(PARTITION_ID); + + assertEquals(expected, actual); + } + + @Test + public void should_throwAnException_when_deletePartitionWhichDoesNotExist() { + assertThrows(AppException.class, () -> { + when(partitionPropertyEntityRepository.findByPartitionId(PARTITION_ID)) + .thenReturn(emptyList); + partitionServiceImpl.deletePartition(PARTITION_ID); + }); + } + + @Test + public void should_getAllPartitions() { + List<String> expectedPartitions = new ArrayList<>(); + expectedPartitions.add(PARTITION_ID); + + when(partitionPropertyEntityRepository.getAllPartitions()).thenReturn(expectedPartitions); + + List<String> actualPartitions = partitionServiceImpl.getAllPartitions(); + + assertEquals(expectedPartitions, actualPartitions); + } } \ No newline at end of file diff --git a/provider/partition-ibm/src/main/java/org/opengroup/osdu/partition/provider/ibm/cache/PartitionListCacheImpl.java b/provider/partition-ibm/src/main/java/org/opengroup/osdu/partition/provider/ibm/cache/PartitionListCacheImpl.java deleted file mode 100644 index a1dc12327..000000000 --- a/provider/partition-ibm/src/main/java/org/opengroup/osdu/partition/provider/ibm/cache/PartitionListCacheImpl.java +++ /dev/null @@ -1,45 +0,0 @@ -/* Licensed Materials - Property of IBM */ -/* (c) Copyright IBM Corp. 2020. All Rights Reserved.*/ - -package org.opengroup.osdu.partition.provider.ibm.cache; - -import java.util.List; - -import javax.annotation.Resource; - -import org.opengroup.osdu.core.common.cache.ICache; -import org.opengroup.osdu.partition.provider.interfaces.IPartitionServiceCache; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Service; - -@Service -@Qualifier("partitionListCache") -public class PartitionListCacheImpl implements IPartitionServiceCache<String, List<String>> { - - - @Autowired - @Qualifier("partitionListCache") - private ICache<String, List<String>> cache; - - @Override - public void put(String s, List<String> o) { - this.cache.put(s, o); - } - - @Override - public List<String> get(String s) { - return this.cache.get(s); - } - - @Override - public void delete(String s) { - this.cache.delete(s); - } - - @Override - public void clearAll() { - this.cache.clearAll(); - } - -} diff --git a/provider/partition-ibm/src/main/java/org/opengroup/osdu/partition/provider/ibm/cache/PartitionServiceCacheImpl.java b/provider/partition-ibm/src/main/java/org/opengroup/osdu/partition/provider/ibm/cache/PartitionServiceCacheImpl.java deleted file mode 100644 index 4afd7cd1f..000000000 --- a/provider/partition-ibm/src/main/java/org/opengroup/osdu/partition/provider/ibm/cache/PartitionServiceCacheImpl.java +++ /dev/null @@ -1,42 +0,0 @@ -/* Licensed Materials - Property of IBM */ -/* (c) Copyright IBM Corp. 2020. All Rights Reserved.*/ - -package org.opengroup.osdu.partition.provider.ibm.cache; - -import org.opengroup.osdu.core.common.cache.ICache; -import org.opengroup.osdu.partition.model.PartitionInfo; -import org.opengroup.osdu.partition.provider.interfaces.IPartitionServiceCache; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Service; - -import javax.annotation.Resource; - -@Service -@Qualifier("partitionServiceCache") -public class PartitionServiceCacheImpl implements IPartitionServiceCache<String, PartitionInfo> { - - @Autowired - @Qualifier("partitionServiceCache") - private ICache<String, PartitionInfo> cache; - - @Override - public void put(String s, PartitionInfo o) { - this.cache.put(s, o); - } - - @Override - public PartitionInfo get(String s) { - return this.cache.get(s); - } - - @Override - public void delete(String s) { - this.cache.delete(s); - } - - @Override - public void clearAll() { - this.cache.clearAll(); - } -} diff --git a/provider/partition-ibm/src/main/java/org/opengroup/osdu/partition/provider/ibm/service/PartitionServiceImpl.java b/provider/partition-ibm/src/main/java/org/opengroup/osdu/partition/provider/ibm/service/PartitionServiceImpl.java index 247f48402..5545607a0 100644 --- a/provider/partition-ibm/src/main/java/org/opengroup/osdu/partition/provider/ibm/service/PartitionServiceImpl.java +++ b/provider/partition-ibm/src/main/java/org/opengroup/osdu/partition/provider/ibm/service/PartitionServiceImpl.java @@ -3,13 +3,13 @@ package org.opengroup.osdu.partition.provider.ibm.service; -import java.io.IOException; -import java.net.MalformedURLException; -import java.util.List; - -import javax.annotation.PostConstruct; - +import com.cloudant.client.api.Database; +import com.cloudant.client.api.model.Response; +import com.cloudant.client.org.lightcouch.DocumentConflictException; +import com.cloudant.client.org.lightcouch.NoDocumentException; +import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpStatus; +import org.opengroup.osdu.core.common.cache.ICache; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.ibm.auth.ServiceCredentials; @@ -18,25 +18,33 @@ import org.opengroup.osdu.partition.model.PartitionInfo; import org.opengroup.osdu.partition.provider.ibm.model.PartitionDoc; import org.opengroup.osdu.partition.provider.interfaces.IPartitionService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import com.cloudant.client.api.Database; -import com.cloudant.client.api.model.Response; -import com.cloudant.client.org.lightcouch.DocumentConflictException; -import com.cloudant.client.org.lightcouch.NoDocumentException; - -import lombok.extern.slf4j.Slf4j; +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.net.MalformedURLException; +import java.util.List; @Service @Slf4j public class PartitionServiceImpl implements IPartitionService { private static final String PARTITION_DATABASE = "partition"; + private static final String PARTITION_LIST_KEY = "getAllPartitions"; @Autowired private JaxRsDpsLog logger; + @Autowired + @Qualifier("partitionServiceCache") + private ICache<String, PartitionInfo> partitionServiceCache; + + @Autowired + @Qualifier("partitionListCache") + private ICache<String, List<String>> partitionListCache; + Database db; private IBMCloudantClientFactory cloudantFactory; @@ -69,10 +77,15 @@ public class PartitionServiceImpl implements IPartitionService { @Override public PartitionInfo createPartition(String partitionId, PartitionInfo partitionInfo) { + if (partitionServiceCache.get(partitionId) != null) { + throw new AppException(HttpStatus.SC_CONFLICT, "partition exist", "Partition with same id exist"); + } + + PartitionInfo pi; PartitionDoc partitionDoc = new PartitionDoc(partitionId, partitionInfo); try { db.save(partitionDoc); - return partitionInfo; + pi = partitionInfo; } catch (DocumentConflictException e) { log.error("Partition already exists"); throw new AppException(e.getStatusCode(), "Conflict", "partition already exists", e); @@ -81,11 +94,18 @@ public class PartitionServiceImpl implements IPartitionService { e.printStackTrace(); throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), "Partition creation failed", e); } - + + if (pi != null) { + partitionServiceCache.put(partitionId, pi); + partitionListCache.clearAll(); + } + + return pi; } @Override public PartitionInfo updatePartition(String partitionId, PartitionInfo partitionInfo) { + PartitionInfo pi; if (partitionInfo.getProperties().containsKey("id")) { throw new AppException(HttpStatus.SC_BAD_REQUEST, "can not update id", "the field id can not be updated"); } @@ -93,7 +113,7 @@ public class PartitionServiceImpl implements IPartitionService { PartitionDoc partitionDoc = db.find(PartitionDoc.class, partitionId); partitionDoc.getPartitionInfo().getProperties().putAll(partitionInfo.getProperties()); Response update = db.update(partitionDoc); - return partitionDoc.getPartitionInfo(); + pi = partitionDoc.getPartitionInfo(); } catch (NoDocumentException e) { log.error(String.format("%s partition does not exists", partitionId)); e.printStackTrace(); @@ -110,23 +130,38 @@ public class PartitionServiceImpl implements IPartitionService { throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Partition update failed", e.getMessage(), e); } + if(pi != null) { + partitionServiceCache.put(partitionId, pi); + } + + return pi; } @Override public PartitionInfo getPartition(String partitionId) { - PartitionDoc partitionDoc = null; - try { - partitionDoc = db.find(PartitionDoc.class, partitionId); - } catch (NoDocumentException e) { - log.error(String.format("%s partition does not exists", partitionId)); - e.printStackTrace(); - throw new AppException(e.getStatusCode(), e.getReason(), String.format("%s partition does not exists", partitionId), e); - } catch (Exception e) { - log.error("Partition could not found"); - e.printStackTrace(); - throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "unknown error", "Partition could not found", e ); + PartitionInfo pi = partitionServiceCache.get(partitionId); + + if (pi == null) { + PartitionDoc partitionDoc = null; + try { + partitionDoc = db.find(PartitionDoc.class, partitionId); + } catch (NoDocumentException e) { + log.error(String.format("%s partition does not exists", partitionId)); + e.printStackTrace(); + throw new AppException(e.getStatusCode(), e.getReason(), String.format("%s partition does not exists", partitionId), e); + } catch (Exception e) { + log.error("Partition could not found"); + e.printStackTrace(); + throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "unknown error", "Partition could not found", e ); + } + pi = partitionDoc.getPartitionInfo(); + + if (pi != null) { + partitionServiceCache.put(partitionId, pi); + } } - return partitionDoc.getPartitionInfo(); + + return pi; } @Override @@ -143,26 +178,38 @@ public class PartitionServiceImpl implements IPartitionService { log.error("Deletion Failed. Unexpected error"); e.printStackTrace(); } + if(deleteStatus.getStatusCode() == 200) { + if (partitionServiceCache.get(partitionId) != null) { + partitionServiceCache.delete(partitionId); + } + partitionListCache.clearAll(); return true; - } + } + return false; - } @Override public List<String> getAllPartitions() { - List<String> partitionList = null; - try { - partitionList = db.getAllDocsRequestBuilder().includeDocs(true).build().getResponse().getDocIds(); - } catch (IOException e) { - log.error("Partitions could not found. IOException occurred", e); - e.printStackTrace(); - } catch (Exception e) { - log.error("Partition could not found.", e); - e.printStackTrace(); + List<String> partitions = partitionListCache.get(PARTITION_LIST_KEY); + + if (partitions == null) { + try { + partitions = db.getAllDocsRequestBuilder().includeDocs(true).build().getResponse().getDocIds(); + } catch (IOException e) { + log.error("Partitions could not found. IOException occurred", e); + e.printStackTrace(); + } catch (Exception e) { + log.error("Partition could not found.", e); + e.printStackTrace(); + } + + if (partitions != null) { + partitionListCache.put(PARTITION_LIST_KEY, partitions); + } } - return partitionList; + return partitions; } } diff --git a/provider/partition-reference/src/main/java/org/opengroup/osdu/partition/provider/reference/cache/PartitionListCacheImpl.java b/provider/partition-reference/src/main/java/org/opengroup/osdu/partition/provider/reference/cache/PartitionListCacheImpl.java deleted file mode 100644 index 83853f6c9..000000000 --- a/provider/partition-reference/src/main/java/org/opengroup/osdu/partition/provider/reference/cache/PartitionListCacheImpl.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - Copyright 2002-2021 Google LLC - Copyright 2002-2021 EPAM Systems, Inc - - 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.partition.provider.reference.cache; - -import java.util.List; -import lombok.RequiredArgsConstructor; -import org.opengroup.osdu.core.common.cache.ICache; -import org.opengroup.osdu.partition.provider.interfaces.IPartitionServiceCache; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Service; - -@Service -@Qualifier("partitionListCache") -@RequiredArgsConstructor -public class PartitionListCacheImpl implements IPartitionServiceCache<String, List<String>> { - - private final ICache<String, List<String>> cache; - - @Override - public void put(String s, List<String> o) { - this.cache.put(s, o); - } - - @Override - public List<String> get(String s) { - return this.cache.get(s); - } - - @Override - public void delete(String s) { - this.cache.delete(s); - } - - @Override - public void clearAll() { - this.cache.clearAll(); - } -} diff --git a/provider/partition-reference/src/main/java/org/opengroup/osdu/partition/provider/reference/cache/PartitionServiceCacheImpl.java b/provider/partition-reference/src/main/java/org/opengroup/osdu/partition/provider/reference/cache/PartitionServiceCacheImpl.java deleted file mode 100644 index 55a0c33ea..000000000 --- a/provider/partition-reference/src/main/java/org/opengroup/osdu/partition/provider/reference/cache/PartitionServiceCacheImpl.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - Copyright 2002-2021 Google LLC - Copyright 2002-2021 EPAM Systems, Inc - - 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.partition.provider.reference.cache; - -import lombok.RequiredArgsConstructor; -import org.opengroup.osdu.core.common.cache.ICache; -import org.opengroup.osdu.partition.model.PartitionInfo; -import org.opengroup.osdu.partition.provider.interfaces.IPartitionServiceCache; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Service; - -@Service -@Qualifier("partitionServiceCache") -@RequiredArgsConstructor -public class PartitionServiceCacheImpl implements IPartitionServiceCache<String, PartitionInfo> { - - private final ICache<String, PartitionInfo> cache; - - @Override - public void put(String s, PartitionInfo o) { - this.cache.put(s, o); - } - - @Override - public PartitionInfo get(String s) { - return this.cache.get(s); - } - - @Override - public void delete(String s) { - this.cache.delete(s); - } - - @Override - public void clearAll() { - this.cache.clearAll(); - } -} diff --git a/provider/partition-reference/src/main/java/org/opengroup/osdu/partition/provider/reference/service/PartitionServiceImpl.java b/provider/partition-reference/src/main/java/org/opengroup/osdu/partition/provider/reference/service/PartitionServiceImpl.java index b6f7aa58f..0757dae4d 100644 --- a/provider/partition-reference/src/main/java/org/opengroup/osdu/partition/provider/reference/service/PartitionServiceImpl.java +++ b/provider/partition-reference/src/main/java/org/opengroup/osdu/partition/provider/reference/service/PartitionServiceImpl.java @@ -17,11 +17,9 @@ package org.opengroup.osdu.partition.provider.reference.service; -import java.util.Collections; -import java.util.List; -import java.util.Optional; import lombok.RequiredArgsConstructor; import org.apache.http.HttpStatus; +import org.opengroup.osdu.core.common.cache.ICache; import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.common.provider.interfaces.IKmsClient; import org.opengroup.osdu.partition.logging.AuditLogger; @@ -31,28 +29,48 @@ import org.opengroup.osdu.partition.provider.interfaces.IPartitionService; import org.opengroup.osdu.partition.provider.reference.repository.PartitionPropertyEntityRepository; import org.springframework.stereotype.Service; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + @Service @RequiredArgsConstructor public class PartitionServiceImpl implements IPartitionService { private static final String UNKNOWN_ERROR_REASON = "unknown error"; + private static final String PARTITION_LIST_KEY = "getAllPartitions"; + private final PartitionPropertyEntityRepository partitionPropertyEntityRepository; private final IKmsClient kmsClient; private final AuditLogger auditLogger; + private final ICache<String, PartitionInfo> partitionServiceCache; + + private final ICache<String, List<String>> partitionListCache; + @Override public PartitionInfo createPartition(String partitionId, PartitionInfo partitionInfo) { + if (partitionServiceCache.get(partitionId) != null) + throw new AppException(HttpStatus.SC_CONFLICT, "partition exist", "Partition with same id exist"); + if (partitionPropertyEntityRepository.findByPartitionId(partitionId).isPresent()) { throw new AppException(HttpStatus.SC_CONFLICT, UNKNOWN_ERROR_REASON, - "Partition already exists."); + "Partition already exists."); } partitionInfo.getProperties() - .forEach((key, property) -> encryptPartitionPropertyEntityIfNeeded(property)); + .forEach((key, property) -> encryptPartitionPropertyEntityIfNeeded(property)); partitionPropertyEntityRepository.createPartition(partitionId, partitionInfo); - return getPartition(partitionId); + PartitionInfo pi = getPartition(partitionId); + + if (pi != null) { + partitionServiceCache.put(partitionId, pi); + partitionListCache.clearAll(); + } + + return pi; } @Override @@ -60,16 +78,16 @@ public class PartitionServiceImpl implements IPartitionService { if (partitionInfo.getProperties().containsKey("id")) { this.auditLogger.updatePartitionSecretFailure(Collections.singletonList(partitionId)); throw new AppException(HttpStatus.SC_BAD_REQUEST, "can not update id", - "the field id can not be updated"); + "the field id can not be updated"); } if (!partitionPropertyEntityRepository.findByPartitionId(partitionId).isPresent()) { this.auditLogger.updatePartitionSecretFailure(Collections.singletonList(partitionId)); throw new AppException(HttpStatus.SC_NOT_FOUND, UNKNOWN_ERROR_REASON, - "An attempt to update not existing partition."); + "An attempt to update not existing partition."); } partitionInfo.getProperties().forEach((key, value) -> { Optional<Property> property = this.partitionPropertyEntityRepository - .findByPartitionIdAndName(partitionId, key); + .findByPartitionIdAndName(partitionId, key); if (property.isPresent()) { property.get().setSensitive(value.isSensitive()); property.get().setValue(value.getValue()); @@ -78,19 +96,31 @@ public class PartitionServiceImpl implements IPartitionService { encryptPartitionPropertyEntityIfNeeded(value); }); partitionPropertyEntityRepository.updatePartition(partitionId, partitionInfo); - return getPartition(partitionId); + PartitionInfo pi = getPartition(partitionId); + + if(pi != null) { + partitionServiceCache.put(partitionId, pi); + } + + return pi; } @Override public PartitionInfo getPartition(String partitionId) { - Optional<PartitionInfo> result = partitionPropertyEntityRepository.findByPartitionId( - partitionId); - if (!result.isPresent()) { - throw new AppException(HttpStatus.SC_NOT_FOUND, UNKNOWN_ERROR_REASON, "Partition does not exist."); + PartitionInfo pi = (PartitionInfo) partitionServiceCache.get(partitionId); + + if (pi == null) { + Optional<PartitionInfo> result = partitionPropertyEntityRepository.findByPartitionId( + partitionId); + if (!result.isPresent()) { + throw new AppException(HttpStatus.SC_NOT_FOUND, UNKNOWN_ERROR_REASON, "Partition does not exist."); + } + result.get().getProperties() + .forEach((key, property) -> decryptPartitionPropertyIfNeeded(property)); + pi = result.get(); + partitionServiceCache.put(partitionId, pi); } - result.get().getProperties() - .forEach((key, property) -> decryptPartitionPropertyIfNeeded(property)); - return result.get(); + return pi; } @Override @@ -98,14 +128,31 @@ public class PartitionServiceImpl implements IPartitionService { if (!partitionPropertyEntityRepository.findByPartitionId(partitionId).isPresent()) { this.auditLogger.deletePartitionFailure(Collections.singletonList(partitionId)); throw new AppException(HttpStatus.SC_NOT_FOUND, UNKNOWN_ERROR_REASON, - "An attempt to delete not existing partition."); + "An attempt to delete not existing partition."); } - return partitionPropertyEntityRepository.isDeletedPartitionInfoByPartitionId(partitionId); + if (partitionPropertyEntityRepository.isDeletedPartitionInfoByPartitionId(partitionId)) { + if (partitionServiceCache.get(partitionId) != null) { + partitionServiceCache.delete(partitionId); + } + partitionListCache.clearAll(); + return true; + } + + return false; } @Override public List<String> getAllPartitions() { - return partitionPropertyEntityRepository.getAllPartitions(); + List<String> partitions = (List<String>)partitionListCache.get(PARTITION_LIST_KEY); + + if (partitions == null) { + partitions = partitionPropertyEntityRepository.getAllPartitions(); + + if (partitions != null) { + partitionListCache.put(PARTITION_LIST_KEY, partitions); + } + } + return partitions; } private void encryptPartitionPropertyEntityIfNeeded(Property property) { -- GitLab