From e695e71c26a54c2ea115b124a851b7988f6b8241 Mon Sep 17 00:00:00 2001 From: Duvelis Carao <dcarao@slb.com> Date: Mon, 2 Nov 2020 18:01:17 -0500 Subject: [PATCH] New Partition API - list all partition id --- docs/api/partition_openapi.yaml | 27 +++++ docs/tutorial/Partition.md | 34 +++++- .../osdu/partition/api/PartitionApi.java | 8 ++ .../interfaces/IPartitionService.java | 4 + .../interfaces/IPartitionServiceCache.java | 2 +- .../service/CachedPartitionServiceImpl.java | 22 +++- .../osdu/partition/api/PartitionApiTest.java | 15 +++ .../CachedPartitionServiceImplTest.java | 18 +++ .../PartitionServiceDummyCacheImpl.java | 4 +- .../aws/service/PartitionServiceImpl.java | 8 +- .../azure/cache/PartitionListCacheImpl.java | 18 +++ .../cache/PartitionServiceCacheImpl.java | 6 +- .../persistence/PartitionTableStore.java | 16 ++- .../azure/service/PartitionServiceImpl.java | 7 ++ .../persistence/CloudTableStoreTest.java | 90 +++++++++++++++ .../persistence/PartitionTableStoreTest.java | 108 ++++++++++++++++++ .../service/PartitionServiceImplTest.java | 14 ++- .../partition/api/TestListPartitions.java | 53 +++++++++ .../partition/api/ListPartitionsApitTest.java | 62 ++++++++++ .../descriptor/ListPartitionDescriptor.java | 36 ++++++ 20 files changed, 539 insertions(+), 13 deletions(-) create mode 100644 provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/cache/PartitionListCacheImpl.java create mode 100644 provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/persistence/CloudTableStoreTest.java create mode 100644 provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/persistence/PartitionTableStoreTest.java create mode 100644 testing/partition-test-azure/src/test/java/org/opengroup/osdu/partition/api/TestListPartitions.java create mode 100644 testing/partition-test-core/src/main/java/org/opengroup/osdu/partition/api/ListPartitionsApitTest.java create mode 100644 testing/partition-test-core/src/main/java/org/opengroup/osdu/partition/api/descriptor/ListPartitionDescriptor.java diff --git a/docs/api/partition_openapi.yaml b/docs/api/partition_openapi.yaml index 0c7b31274..d6b077a2b 100644 --- a/docs/api/partition_openapi.yaml +++ b/docs/api/partition_openapi.yaml @@ -96,6 +96,33 @@ paths: security: - JWT: - global + '/partitions': + get: + tags: + - partition-api + summary: list + operationId: listUsingGET + consumes: + - application/json + produces: + - application/json + parameters: + responses: + '200': + description: OK + schema: + type: array + items: + type: string + '401': + description: Unauthorized + '403': + description: Forbidden + '404': + description: Not Found + security: + - JWT: + - global post: tags: - partition-api diff --git a/docs/tutorial/Partition.md b/docs/tutorial/Partition.md index 2c68480a6..e8e7b328c 100644 --- a/docs/tutorial/Partition.md +++ b/docs/tutorial/Partition.md @@ -8,6 +8,8 @@ * [Get partition details](#get-partition) * [Create a new partition](#create-partition) * [Delete an existing partition](#delete-partition) + * [List of partitions](#list-partition) + ## Introduction <a name="introduction"></a> Partition service is responsible for creating and retrieving the partition specific properties (secret and non-secret) on behalf of other services. @@ -139,4 +141,34 @@ curl --request DELETE \ --header 'Authorization: Bearer <JWT>' \ --header 'Content-Type: application/json' ``` -</details> \ No newline at end of file +</details> + + +### List partitions <a name="list-partition"></a> +Consuming services can use this API to list all partitions Id. +``` +GET api/partition/v1/partitions +``` +<details><summary>curl</summary> + +``` +curl --request GET \ + --url 'https://<base_url>/api/partition/v1/partitions' \ + --header 'Authorization: Bearer <JWT>' \ + --header 'Content-Type: application/json' +``` +</details> + +A sample output is shown below. +<details><summary>Sample response</summary> + +``` +[ + "default-dev", + "opendes" +] +``` + +</details> + +[Back to Table of Contents](#TOC) \ No newline at end of file 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 540da827e..0192cde50 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 @@ -27,6 +27,7 @@ import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import javax.validation.Valid; import java.net.URI; +import java.util.List; import java.util.Map; @RestController @@ -59,4 +60,11 @@ public class PartitionApi { this.partitionService.deletePartition(partitionId); return ResponseEntity.noContent().build(); } + + @GetMapping + @PreAuthorize("@authorizationFilter.hasPermissions()") + public List<String> list() { + List<String> partitions = this.partitionService.getAllPartitions(); + return partitions; + } } diff --git a/partition-core/src/main/java/org/opengroup/osdu/partition/provider/interfaces/IPartitionService.java b/partition-core/src/main/java/org/opengroup/osdu/partition/provider/interfaces/IPartitionService.java index b42583adb..8c56fa410 100644 --- a/partition-core/src/main/java/org/opengroup/osdu/partition/provider/interfaces/IPartitionService.java +++ b/partition-core/src/main/java/org/opengroup/osdu/partition/provider/interfaces/IPartitionService.java @@ -16,6 +16,8 @@ package org.opengroup.osdu.partition.provider.interfaces; import org.opengroup.osdu.partition.model.PartitionInfo; +import java.util.List; + public interface IPartitionService { PartitionInfo createPartition(String partitionId, PartitionInfo partitionInfo); @@ -23,4 +25,6 @@ public interface IPartitionService { PartitionInfo getPartition(String partitionId); boolean deletePartition(String partitionId); + + List<String> getAllPartitions(); } \ No newline at end of file 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 index d61d1c2ec..762074d0c 100644 --- 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 @@ -17,5 +17,5 @@ package org.opengroup.osdu.partition.provider.interfaces; import org.opengroup.osdu.core.common.cache.ICache; import org.opengroup.osdu.partition.model.PartitionInfo; -public interface IPartitionServiceCache extends ICache<String, PartitionInfo> { +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 index bf82d14b5..7cfd3e927 100644 --- 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 @@ -21,10 +21,13 @@ 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; @@ -32,6 +35,9 @@ public class CachedPartitionServiceImpl implements IPartitionService { @Inject private IPartitionServiceCache partitionServiceCache; + @Inject + private IPartitionServiceCache partitionListCache; + @Override public PartitionInfo createPartition(String partitionId, PartitionInfo partitionInfo) { PartitionInfo pi = partitionService.createPartition(partitionId, partitionInfo); @@ -45,7 +51,7 @@ public class CachedPartitionServiceImpl implements IPartitionService { @Override public PartitionInfo getPartition(String partitionId) { - PartitionInfo pi = partitionServiceCache.get(partitionId); + PartitionInfo pi = (PartitionInfo) partitionServiceCache.get(partitionId); if (pi == null) { pi = partitionService.getPartition(partitionId); @@ -70,4 +76,18 @@ public class CachedPartitionServiceImpl implements IPartitionService { 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/api/PartitionApiTest.java b/partition-core/src/test/java/org/opengroup/osdu/partition/api/PartitionApiTest.java index 565395e0b..af86e7402 100644 --- a/partition-core/src/test/java/org/opengroup/osdu/partition/api/PartitionApiTest.java +++ b/partition-core/src/test/java/org/opengroup/osdu/partition/api/PartitionApiTest.java @@ -33,7 +33,9 @@ import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import javax.servlet.http.HttpServletRequest; import java.net.URI; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import static org.junit.Assert.*; @@ -112,4 +114,17 @@ public class PartitionApiTest { throw ae; } } + + @Test + public void should_return200AndListAllPartition() { + List<String> partitions = new ArrayList<>(); + partitions.add("tenant1"); + partitions.add("tenant2"); + + when(partitionService.getAllPartitions()).thenReturn(partitions); + + List<String> result = this.sut.list(); + assertNotNull(result); + assertEquals(partitions.size(), result.size()); + } } \ No newline at end of file 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 index 0050f2563..7b83376d5 100644 --- 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 @@ -23,6 +23,9 @@ 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.*; @@ -35,6 +38,9 @@ public class CachedPartitionServiceImplTest { @Mock private IPartitionServiceCache partitionServiceCache; + @Mock + private IPartitionServiceCache partitionListCache; + @InjectMocks private CachedPartitionServiceImpl cachedPartitionServiceImpl; @@ -97,4 +103,16 @@ public class CachedPartitionServiceImplTest { verify(partitionServiceCache, times(1)).get(partId); } + @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/PartitionServiceDummyCacheImpl.java b/provider/partition-aws/src/main/java/org/opengroup/osdu/partition/provider/aws/service/PartitionServiceDummyCacheImpl.java index a5f93dda6..91e4dd90e 100644 --- a/provider/partition-aws/src/main/java/org/opengroup/osdu/partition/provider/aws/service/PartitionServiceDummyCacheImpl.java +++ b/provider/partition-aws/src/main/java/org/opengroup/osdu/partition/provider/aws/service/PartitionServiceDummyCacheImpl.java @@ -22,7 +22,7 @@ import org.springframework.stereotype.Service; * We don't want to use cache. Implement a dummy service to always return a cache miss. */ @Service -public class PartitionServiceDummyCacheImpl implements IPartitionServiceCache { +public class PartitionServiceDummyCacheImpl implements IPartitionServiceCache<String, Object> { public PartitionServiceDummyCacheImpl() { } @@ -43,7 +43,7 @@ public class PartitionServiceDummyCacheImpl implements IPartitionServiceCache { } @Override - public void put(String arg0, PartitionInfo arg1) { + public void put(String arg0, Object 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 e3f986a34..71250a42a 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 @@ -129,5 +129,11 @@ public class PartitionServiceImpl implements IPartitionService { return ssmHelper.deletePartitionSecrets(partitionId); } - + + @Override + public List<String> getAllPartitions() { + //TODO: Pending to be implemented + return null; + } + } 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 new file mode 100644 index 000000000..a8f722ce8 --- /dev/null +++ b/provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/cache/PartitionListCacheImpl.java @@ -0,0 +1,18 @@ +package org.opengroup.osdu.partition.provider.azure.cache; + +import org.opengroup.osdu.core.common.cache.VmCache; +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; + +@Service +@Qualifier("partitionListCache") +public class PartitionListCacheImpl extends VmCache<String, List<String>> implements IPartitionServiceCache<String, List<String>> { + + public PartitionListCacheImpl() { + super(5 * 60, 1000); + } +} 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 index c349b8d7e..07ed510f5 100644 --- 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 @@ -3,10 +3,14 @@ package org.opengroup.osdu.partition.provider.azure.cache; import org.opengroup.osdu.core.common.cache.VmCache; 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.context.annotation.Primary; import org.springframework.stereotype.Service; @Service -public class PartitionServiceCacheImpl extends VmCache<String, PartitionInfo> implements IPartitionServiceCache { +@Primary +@Qualifier("partitionServiceCache") +public class PartitionServiceCacheImpl extends VmCache<String, PartitionInfo> implements IPartitionServiceCache<String, PartitionInfo> { public PartitionServiceCacheImpl() { super(5 * 60, 1000); diff --git a/provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/persistence/PartitionTableStore.java b/provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/persistence/PartitionTableStore.java index c83f82e47..841b324e3 100644 --- a/provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/persistence/PartitionTableStore.java +++ b/provider/partition-azure/src/main/java/org/opengroup/osdu/partition/provider/azure/persistence/PartitionTableStore.java @@ -21,10 +21,7 @@ import org.opengroup.osdu.partition.model.Property; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; @Component public class PartitionTableStore { @@ -131,4 +128,15 @@ public class PartitionTableStore { private String getTenantSafeSecreteId(String partitionId, String secreteName) { return String.format("%s-%s", partitionId, secreteName); } + + public List<String> getAllPartitions() { + List<String> partitions = new ArrayList<>(); + Iterable<PartitionEntity> results = (Iterable<PartitionEntity>) + this.cloudTableStore.queryByKey(PartitionEntity.class, + ROW_KEY, ID); + for (PartitionEntity tableEntity : results) { + partitions.add(tableEntity.getPartitionKey()); + } + return partitions; + } } 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 f2036d261..fee469574 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 @@ -23,7 +23,9 @@ import org.opengroup.osdu.partition.provider.interfaces.IPartitionService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; @Service @@ -65,4 +67,9 @@ public class PartitionServiceImpl implements IPartitionService { return true; } + + @Override + public List<String> getAllPartitions() { + return this.tableStore.getAllPartitions(); + } } \ No newline at end of file diff --git a/provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/persistence/CloudTableStoreTest.java b/provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/persistence/CloudTableStoreTest.java new file mode 100644 index 000000000..41fa1ffea --- /dev/null +++ b/provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/persistence/CloudTableStoreTest.java @@ -0,0 +1,90 @@ +// 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, eitsher express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.partition.provider.azure.persistence; + +import com.microsoft.azure.storage.StorageException; +import com.microsoft.azure.storage.table.*; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.opengroup.osdu.core.common.model.http.AppException; +import org.powermock.modules.junit4.PowerMockRunner; + + +import static org.junit.Assert.*; +import static org.mockito.Mockito.when; + +@RunWith(PowerMockRunner.class) +public class CloudTableStoreTest { + + @Mock + private CloudTable cloudTableClient; + + @InjectMocks + private CloudTableStore sut; + + @Test + public void should_empty_whenRecordNotExists() { + Iterable<PartitionEntity> results = (Iterable<PartitionEntity>) sut.queryByKey(PartitionEntity.class, + "partitionKey", "partitionId"); + assertNotNull(results); + } + + @Test + public void should_empty_whenRecordExists() throws StorageException { + try { + TableBatchOperation tbOp = new TableBatchOperation(); + when(cloudTableClient.execute(new TableBatchOperation())).thenThrow(new StorageException("Error", "Error", null)); + sut.insertBatchEntities(tbOp); + fail("should not be here"); + } catch (AppException e) { + assertEquals(500, e.getError().getCode()); + assertEquals("error creating partition", e.getError().getReason()); + } + } + + @Test + public void when_call_queryByCompoundKey() { + Iterable<? extends TableEntity> result = sut.queryByCompoundKey(PartitionEntity.class, "RowKey", "id", "value", "partitionId"); + assertNotNull(result); + } + + @Test + public void when_wrongInput_ThrowException() { + try { + sut.queryByCompoundKey(PartitionEntity.class, null, null, null, null); + fail("Should not be here"); + } catch (Exception e) { + assertNotNull(e); + } + } + + @Test + public void when_call_queryByKey() { + Iterable<? extends TableEntity> result = sut.queryByKey(PartitionEntity.class, "PartitionKey", "partitionId"); + assertNotNull(result); + } + + @Test + public void when_call_queryByKey_wrongInput_ThrowException() { + try { + sut.queryByKey(PartitionEntity.class, null, null); + fail("Should not be here"); + } catch (Exception e) { + assertNotNull(e); + } + } +} diff --git a/provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/persistence/PartitionTableStoreTest.java b/provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/persistence/PartitionTableStoreTest.java new file mode 100644 index 000000000..73b19d87e --- /dev/null +++ b/provider/partition-azure/src/test/java/org/opengroup/osdu/partition/provider/azure/persistence/PartitionTableStoreTest.java @@ -0,0 +1,108 @@ +// 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.persistence; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.opengroup.osdu.core.common.model.http.AppException; +import org.opengroup.osdu.partition.model.PartitionInfo; +import org.opengroup.osdu.partition.model.Property; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.*; + +import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; + +@RunWith(PowerMockRunner.class) +public class PartitionTableStoreTest { + + @InjectMocks + private PartitionTableStore sut; + + @Mock + private CloudTableStore cloudTableStore; + + private static final String PARTITION_ID = "partitionId"; + private static final String PARTITION_KEY = "PartitionKey"; + + @Test + public void should_returnFalse_whenPartitionNotExists() { + boolean exist = sut.partitionExists(PARTITION_ID); + assertFalse(exist); + } + + @Test + public void should_get_partitionInfo() { + Collection<PartitionEntity> list = new ArrayList<>(); + PartitionEntity partitionEntity = new PartitionEntity(PARTITION_ID, "name"); + list.add(partitionEntity); + when(cloudTableStore.queryByKey(PartitionEntity.class, PARTITION_KEY, PARTITION_ID)).thenReturn((Iterable) list); + + Map<String, Property> partition = sut.getPartition(PARTITION_ID); + assertNotNull(partition); + assertEquals(1, partition.size()); + } + + @Test + public void should_returnEmpty_when_partitionNotFound() { + Map<String, Property> partition = sut.getPartition(PARTITION_ID); + assertNotNull(partition); + assertEquals(0, partition.size()); + } + + @Test + public void should_addPartiton_whenPartionProvided() { + sut.addPartition(PARTITION_ID, new PartitionInfo()); + } + + @Test + public void should_returnException_whenNoPartitionInfo() { + doThrow(new AppException(500, "Error", "error creating partition")).when(cloudTableStore).insertBatchEntities(any()); + try { + sut.addPartition(PARTITION_ID, new PartitionInfo()); + fail("Should not be here"); + } catch (AppException e) { + assertEquals(500, e.getError().getCode()); + assertEquals("error creating partition", e.getError().getMessage()); + } + } + + + @Test + public void should_getAll_partitions() { + Collection<PartitionEntity> list = new ArrayList<>(); + PartitionEntity partitionEntity = new PartitionEntity(PARTITION_ID, "name"); + list.add(partitionEntity); + when(cloudTableStore.queryByKey(PartitionEntity.class, "RowKey", "id")).thenReturn((Iterable) list); + + List<String> partitions = sut.getAllPartitions(); + assertNotNull(partitions); + assertEquals(1, partitions.size()); + } + + @Test + public void delete_partition() { + Collection<PartitionEntity> list = new ArrayList<>(); + PartitionEntity partitionEntity = new PartitionEntity(PARTITION_ID, "name"); + list.add(partitionEntity); + when(cloudTableStore.queryByKey(PartitionEntity.class, PARTITION_KEY, PARTITION_ID)).thenReturn((Iterable) list); + sut.deletePartition(PARTITION_ID); + } +} 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 f6cd49f40..7b42b2dc6 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 @@ -25,11 +25,12 @@ 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.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import static org.mockito.Mockito.when; @RunWith(PowerMockRunner.class) @@ -124,4 +125,13 @@ public class PartitionServiceImplTest { this.sut.deletePartition(null); } + + @Test + public void should_returnEmptyList_when_no_partitions() { + when(this.tableStore.getAllPartitions()).thenReturn(new ArrayList<>()); + List<String> partitions = sut.getAllPartitions(); + assertNotNull(partitions); + assertTrue(partitions.isEmpty()); + + } } \ No newline at end of file diff --git a/testing/partition-test-azure/src/test/java/org/opengroup/osdu/partition/api/TestListPartitions.java b/testing/partition-test-azure/src/test/java/org/opengroup/osdu/partition/api/TestListPartitions.java new file mode 100644 index 000000000..c21da9aaa --- /dev/null +++ b/testing/partition-test-azure/src/test/java/org/opengroup/osdu/partition/api/TestListPartitions.java @@ -0,0 +1,53 @@ +// 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.api; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.opengroup.osdu.partition.util.AzureTestUtils; + +public class TestListPartitions extends ListPartitionsApitTest { + + @Before + @Override + public void setup() { + this.testUtils = new AzureTestUtils(); + } + + @After + @Override + public void tearDown() { + this.testUtils = null; + } + + @Test + @Override + public void should_return401_when_noAccessToken() throws Exception { + // revisit this later -- Istio is changing the response code + } + + @Test + @Override + public void should_return401_when_accessingWithCredentialsWithoutPermission() throws Exception { + // revisit this later -- Istio is changing the response code + } + + @Test + @Override + public void should_return401_when_makingHttpRequestWithoutToken() throws Exception { + // revisit this later -- Istio is changing the response code + } +} diff --git a/testing/partition-test-core/src/main/java/org/opengroup/osdu/partition/api/ListPartitionsApitTest.java b/testing/partition-test-core/src/main/java/org/opengroup/osdu/partition/api/ListPartitionsApitTest.java new file mode 100644 index 000000000..c8cf51d1b --- /dev/null +++ b/testing/partition-test-core/src/main/java/org/opengroup/osdu/partition/api/ListPartitionsApitTest.java @@ -0,0 +1,62 @@ +// 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.api; + +import com.sun.jersey.api.client.ClientResponse; +import org.opengroup.osdu.partition.api.descriptor.CreatePartitionDescriptor; +import org.opengroup.osdu.partition.api.descriptor.DeletePartitionDescriptor; +import org.opengroup.osdu.partition.api.descriptor.GetPartitionDescriptor; +import org.opengroup.osdu.partition.api.descriptor.ListPartitionDescriptor; +import org.opengroup.osdu.partition.util.BaseTestTemplate; +import org.springframework.http.HttpStatus; + +import static org.junit.Assert.assertEquals; + +public abstract class ListPartitionsApitTest extends BaseTestTemplate { + + private String partitionId = getIntegrationTestPrefix() + System.currentTimeMillis(); + + @Override + protected String getId() { + return partitionId; + } + + @Override + protected void deleteResource() throws Exception { + DeletePartitionDescriptor deletePartitionDes = new DeletePartitionDescriptor(); + deletePartitionDes.setPartitionId(partitionId); + ClientResponse response = deletePartitionDes.run(this.getId(), this.testUtils.getAccessToken()); + assertEquals(this.error(""), HttpStatus.NO_CONTENT.value(), (long) response.getStatus()); + } + + @Override + protected void createResource() throws Exception { + CreatePartitionDescriptor createPartitionDescriptor = new CreatePartitionDescriptor(); + createPartitionDescriptor.setPartitionId(partitionId); + + ClientResponse createResponse = createPartitionDescriptor.run(this.getId(), this.testUtils.getAccessToken()); + assertEquals(this.error((String) createResponse.getEntity(String.class)) + , HttpStatus.CREATED.value(), (long) createResponse.getStatus()); + } + + public ListPartitionsApitTest() { + super(new ListPartitionDescriptor()); + } + + @Override + protected int expectedOkResponseCode() { + return HttpStatus.OK.value(); + } +} diff --git a/testing/partition-test-core/src/main/java/org/opengroup/osdu/partition/api/descriptor/ListPartitionDescriptor.java b/testing/partition-test-core/src/main/java/org/opengroup/osdu/partition/api/descriptor/ListPartitionDescriptor.java new file mode 100644 index 000000000..29838f60b --- /dev/null +++ b/testing/partition-test-core/src/main/java/org/opengroup/osdu/partition/api/descriptor/ListPartitionDescriptor.java @@ -0,0 +1,36 @@ +// 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.api.descriptor; + +import org.opengroup.osdu.partition.util.RestDescriptor; +import org.springframework.web.bind.annotation.RequestMethod; + +public class ListPartitionDescriptor extends RestDescriptor { + + @Override + public String getPath() { + return "api/partition/v1/partitions"; + } + + @Override + public String getHttpMethod() { + return RequestMethod.GET.toString(); + } + + @Override + public String getValidBody() { + return ""; + } +} -- GitLab