From bfbb666d9097f8db87c8db153d9fe80f64797249 Mon Sep 17 00:00:00 2001 From: Alok Joshi Date: Mon, 16 Nov 2020 16:58:13 -0600 Subject: [PATCH] add partition patch api client --- pom.xml | 2 +- .../common/partition/IPartitionProvider.java | 2 + .../common/partition/PartitionService.java | 19 ++++++- .../partition/PartitionServiceTest.java | 51 +++++++++++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index c80ad32..86aca45 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.opengroup.osdu os-core-common - 0.3.18 + 0.3.19 jar diff --git a/src/main/java/org/opengroup/osdu/core/common/partition/IPartitionProvider.java b/src/main/java/org/opengroup/osdu/core/common/partition/IPartitionProvider.java index 54a09db..f97195a 100644 --- a/src/main/java/org/opengroup/osdu/core/common/partition/IPartitionProvider.java +++ b/src/main/java/org/opengroup/osdu/core/common/partition/IPartitionProvider.java @@ -22,6 +22,8 @@ public interface IPartitionProvider { PartitionInfo create(String partitionId, PartitionInfo partitionInfo) throws PartitionException; + void update(String partitionId, PartitionInfo partitionInfo) throws PartitionException; + void delete(String partitionId) throws PartitionException; List list() throws PartitionException; diff --git a/src/main/java/org/opengroup/osdu/core/common/partition/PartitionService.java b/src/main/java/org/opengroup/osdu/core/common/partition/PartitionService.java index 2944b98..7cc7b30 100644 --- a/src/main/java/org/opengroup/osdu/core/common/partition/PartitionService.java +++ b/src/main/java/org/opengroup/osdu/core/common/partition/PartitionService.java @@ -72,7 +72,7 @@ public class PartitionService implements IPartitionProvider { entity = new StringEntity(jsonString); } catch (UnsupportedEncodingException e) { throw new PartitionException( - String.format("Error making request to Partition service, error: %s", e.getMessage()), null); + String.format("Error making request to Partition service create api, error: %s", e.getMessage()), null); } httpPost.setEntity(entity); HttpResponse response = send(httpPost); @@ -83,6 +83,23 @@ public class PartitionService implements IPartitionProvider { .build(); } + @Override + public void update(String partitionId, PartitionInfo partitionInfo) throws PartitionException { + String url = this.createUrl(String.format("/partitions/%s", partitionId)); + HttpPatch httpPatch = new HttpPatch(url); + StringEntity entity; + try { + String jsonString = this.gson.toJson(partitionInfo); + entity = new StringEntity(jsonString); + } catch (UnsupportedEncodingException e) { + throw new PartitionException( + String.format("Error making request to Partition service update api, error: %s", e.getMessage()), null); + } + httpPatch.setEntity(entity); + HttpResponse response = send(httpPatch); + getResult(response, String.class); + } + @Override public void delete(String partitionId) throws PartitionException { String url = this.createUrl(String.format("/partitions/%s", partitionId)); diff --git a/src/test/java/org/opengroup/osdu/core/common/partition/PartitionServiceTest.java b/src/test/java/org/opengroup/osdu/core/common/partition/PartitionServiceTest.java index a7c1dbd..2f567f0 100644 --- a/src/test/java/org/opengroup/osdu/core/common/partition/PartitionServiceTest.java +++ b/src/test/java/org/opengroup/osdu/core/common/partition/PartitionServiceTest.java @@ -113,6 +113,57 @@ public class PartitionServiceTest { } } + @Test + public void should_return_null_when_updating_partition() throws IOException, PartitionException { + PartitionAPIConfig config = PartitionAPIConfig.builder().rootUrl("http://localhost").build(); + DpsHeaders headers = new DpsHeaders(); + CloseableHttpResponse mockResponse = getResponse(204, null); + when(cacheHttpClient.execute(any())).thenReturn(mockResponse); + + PartitionService sut = new PartitionService(config, headers, cacheHttpClient); + PartitionInfo input = PartitionInfo.builder() + .properties(new HashMap<>()) + .build(); + sut.update(PARTITION_ID, input); + } + + @Test + public void should_return_exception_when_updating_with_bad_partitionId() throws IOException { + PartitionAPIConfig config = PartitionAPIConfig.builder().rootUrl("http://localhost").build(); + DpsHeaders headers = new DpsHeaders(); + CloseableHttpResponse mockResponse = getResponse(400, BAD_REQUEST); + when(cacheHttpClient.execute(any())).thenReturn(mockResponse); + + PartitionService sut = new PartitionService(config, headers, cacheHttpClient); + try { + PartitionInfo input = PartitionInfo.builder() + .properties(new HashMap<>()) + .build(); + sut.update(null, input); + fail("should not be here"); + } catch (PartitionException e) { + assertNotNull(e); + assertEquals(BAD_REQUEST, e.getHttpResponse().getBody()); + } + } + + @Test + public void should_return_exception_when_updating_with_bad_partitionInfo() throws IOException { + PartitionAPIConfig config = PartitionAPIConfig.builder().rootUrl("http://localhost").build(); + DpsHeaders headers = new DpsHeaders(); + CloseableHttpResponse mockResponse = getResponse(400, BAD_REQUEST); + when(cacheHttpClient.execute(any())).thenReturn(mockResponse); + + PartitionService sut = new PartitionService(config, headers, cacheHttpClient); + try { + sut.update(PARTITION_ID, null); + fail("should not be here"); + } catch (PartitionException e) { + assertNotNull(e); + assertEquals(BAD_REQUEST, e.getHttpResponse().getBody()); + } + } + @Test public void should_return_partition_204_after_deleting() throws IOException, PartitionException { PartitionAPIConfig config = PartitionAPIConfig.builder().rootUrl("http://localhost").build(); -- GitLab