From 685a432d67084a03bed27b7282d7f3c589dc89fd Mon Sep 17 00:00:00 2001 From: Emmanuel DEMEY Date: Wed, 10 Jul 2024 15:22:33 +0200 Subject: [PATCH] fix: return a Distribution object (#687) --- .../distribution/DistributionService.java | 3 ++- .../distribution/DistributionServiceImpl.java | 15 +++++++-------- .../distribution/DistributionResources.java | 2 +- .../distribution/DistributionServiceImplTest.java | 5 +++-- .../webservice/DistributionResourcesTest.java | 8 ++++++-- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/distribution/DistributionService.java b/src/main/java/fr/insee/rmes/bauhaus_services/distribution/DistributionService.java index debaaeef7..ed2c8a41b 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/distribution/DistributionService.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/distribution/DistributionService.java @@ -1,13 +1,14 @@ package fr.insee.rmes.bauhaus_services.distribution; import fr.insee.rmes.exceptions.RmesException; +import fr.insee.rmes.model.dataset.Distribution; import fr.insee.rmes.model.dataset.PatchDistribution; public interface DistributionService { String getDistributions() throws RmesException; - String getDistributionByID(String id) throws RmesException; + Distribution getDistributionByID(String id) throws RmesException; String create(String body) throws RmesException; diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/distribution/DistributionServiceImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/distribution/DistributionServiceImpl.java index 473fb8329..56e57ed65 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/distribution/DistributionServiceImpl.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/distribution/DistributionServiceImpl.java @@ -70,14 +70,15 @@ public String getDistributions() throws RmesException { } @Override - public String getDistributionByID(String id) throws RmesException { - JSONObject distribution = repoGestion.getResponseAsObject(DistributionQueries.getDistribution(id, getDistributionGraph())); + public Distribution getDistributionByID(String id) throws RmesException { + JSONObject distribution_raw = repoGestion.getResponseAsObject(DistributionQueries.getDistribution(id, getDistributionGraph())); - if (distribution.isEmpty()){ + if (distribution_raw.isEmpty()){ throw new RmesNotFoundException("This distribution does not exist"); } - return distribution.toString(); + Distribution distribution = Deserializer.deserializeBody(distribution_raw.toString(), Distribution.class); + return distribution; } @Override @@ -129,8 +130,7 @@ public String publishDistribution(String id) throws RmesException { @Override public void deleteDistributionId(String distributionId) throws RmesException{ - String distributionString = getDistributionByID(distributionId); - Distribution distribution = Deserializer.deserializeBody(distributionString, Distribution.class); + Distribution distribution = getDistributionByID(distributionId); if (isPublished(distribution)){ throw new RmesBadRequestException(ErrorCodes.DISTRIBUTION_DELETE_ONLY_UNPUBLISHED, "Only unpublished distributions can be deleted"); } @@ -194,8 +194,7 @@ private void validate(Distribution distribution) throws RmesException { @Override public void patchDistribution(String distributionId, PatchDistribution patchDistribution) throws RmesException { - String distributionByID = getDistributionByID(distributionId); - Distribution distribution = Deserializer.deserializeBody(distributionByID, Distribution.class); + Distribution distribution = getDistributionByID(distributionId); if (patchDistribution.getUpdated() == null && patchDistribution.getByteSize() == null && patchDistribution.getUrl() == null){ throw new RmesBadRequestException(DISTRIUBTION_PATCH_INCORRECT_BODY,"One of these attributes is required : updated, byteSize or url"); } diff --git a/src/main/java/fr/insee/rmes/webservice/distribution/DistributionResources.java b/src/main/java/fr/insee/rmes/webservice/distribution/DistributionResources.java index e5f4d6599..51c0779a9 100644 --- a/src/main/java/fr/insee/rmes/webservice/distribution/DistributionResources.java +++ b/src/main/java/fr/insee/rmes/webservice/distribution/DistributionResources.java @@ -56,7 +56,7 @@ public String getDistributions() throws RmesException { @GetMapping("/{id}") @Operation(operationId = "getDistribution", summary = "Get a distributions", responses = {@ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Distribution.class))))}) - public String getDistribution(@PathVariable(Constants.ID) String id) throws RmesException { + public Distribution getDistribution(@PathVariable(Constants.ID) String id) throws RmesException { return this.distributionService.getDistributionByID(id); } diff --git a/src/test/java/fr/insee/rmes/bauhaus_services/distribution/DistributionServiceImplTest.java b/src/test/java/fr/insee/rmes/bauhaus_services/distribution/DistributionServiceImplTest.java index 439f417b0..bbe53e64d 100644 --- a/src/test/java/fr/insee/rmes/bauhaus_services/distribution/DistributionServiceImplTest.java +++ b/src/test/java/fr/insee/rmes/bauhaus_services/distribution/DistributionServiceImplTest.java @@ -6,6 +6,7 @@ import fr.insee.rmes.exceptions.RmesBadRequestException; import fr.insee.rmes.exceptions.RmesException; import fr.insee.rmes.exceptions.RmesNotFoundException; +import fr.insee.rmes.model.dataset.Distribution; import fr.insee.rmes.model.dataset.PatchDistribution; import fr.insee.rmes.utils.DateUtils; import fr.insee.rmes.utils.IdGenerator; @@ -81,8 +82,8 @@ void shouldReturnDataset() throws RmesException, JSONException { when(repositoryGestion.getResponseAsObject("query")).thenReturn(object); try (MockedStatic mockedFactory = Mockito.mockStatic(DistributionQueries.class)) { mockedFactory.when(() -> DistributionQueries.getDistribution(eq("1"), any())).thenReturn("query"); - String query = distributionService.getDistributionByID("1"); - Assertions.assertEquals("{\"id\":\"1\"}", query); + Distribution distribution = distributionService.getDistributionByID("1"); + Assertions.assertEquals("1", distribution.getId()); } } diff --git a/src/test/java/fr/insee/rmes/webservice/DistributionResourcesTest.java b/src/test/java/fr/insee/rmes/webservice/DistributionResourcesTest.java index f4af86353..e3af3987c 100644 --- a/src/test/java/fr/insee/rmes/webservice/DistributionResourcesTest.java +++ b/src/test/java/fr/insee/rmes/webservice/DistributionResourcesTest.java @@ -5,6 +5,7 @@ import fr.insee.rmes.config.auth.security.UserDecoder; import fr.insee.rmes.config.auth.user.User; import fr.insee.rmes.exceptions.RmesException; +import fr.insee.rmes.model.dataset.Distribution; import fr.insee.rmes.webservice.distribution.DistributionResources; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -41,8 +42,11 @@ void shouldReturn200IfRmesExceptionWhenFetchingDatasets() throws RmesException { @Test void shouldReturn200IfRmesExceptionWhenFetchingDistributionById() throws RmesException { - when(distributionService.getDistributionByID(anyString())).thenReturn("result"); - Assertions.assertEquals("result", distributionResources.getDistribution("")); + Distribution distribution = new Distribution(); + distribution.setId("1"); + + when(distributionService.getDistributionByID("1")).thenReturn(distribution); + Assertions.assertEquals(distribution, distributionResources.getDistribution("1")); } @Test