Skip to content

Commit

Permalink
fix: return a Distribution object (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey authored Jul 10, 2024
1 parent cddac6f commit 685a432
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,8 +82,8 @@ void shouldReturnDataset() throws RmesException, JSONException {
when(repositoryGestion.getResponseAsObject("query")).thenReturn(object);
try (MockedStatic<DistributionQueries> 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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 685a432

Please sign in to comment.