diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/ConceptsCollectionService.java b/src/main/java/fr/insee/rmes/bauhaus_services/ConceptsCollectionService.java index 4d29856cf..a9d81329a 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/ConceptsCollectionService.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/ConceptsCollectionService.java @@ -16,7 +16,7 @@ public interface ConceptsCollectionService { ResponseEntity getCollectionExportODT(String id, String accept, ConceptsCollectionsResources.Language lg, boolean withConcepts, HttpServletResponse response) throws RmesException; - ResponseEntity getCollectionExportODS(String id, String accept, boolean withConcepts, HttpServletResponse response) throws RmesException; + ResponseEntity getCollectionExportODS(String id, String accept, boolean withConcepts, HttpServletResponse response) throws RmesException; void exportZipCollection(String id, String accept, HttpServletResponse response, ConceptsCollectionsResources.Language lg, String type, boolean withConcepts) throws RmesException; } diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/ConceptsService.java b/src/main/java/fr/insee/rmes/bauhaus_services/ConceptsService.java index 149b5c399..56e69e629 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/ConceptsService.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/ConceptsService.java @@ -4,6 +4,7 @@ import fr.insee.rmes.model.concepts.MembersLg; import fr.insee.rmes.webservice.ConceptsCollectionsResources; import jakarta.servlet.http.HttpServletResponse; +import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; import java.io.InputStream; @@ -39,7 +40,7 @@ public interface ConceptsService { void setConceptsValidation(String body) throws RmesException ; - ResponseEntity exportConcept(String id, String acceptHeader) throws RmesException; + ResponseEntity exportConcept(String id, String acceptHeader) throws RmesException; void exportZipConcept(String id, String acceptHeader, HttpServletResponse response, ConceptsCollectionsResources.Language lg, String type, boolean withConcepts) throws RmesException; @@ -49,11 +50,11 @@ public interface ConceptsService { void setCollectionsValidation(String body) throws RmesException ; - ResponseEntity getCollectionExport(String id, String acceptHeader) throws RmesException ; + ResponseEntity getCollectionExport(String id, String acceptHeader) throws RmesException ; String getRelatedConcepts(String id) throws RmesException; - String deleteConcept(String id) throws RmesException; + void deleteConcept(String id) throws RmesException; Map getConceptsExportIS(List ids, List members) throws RmesException; } diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/OperationsService.java b/src/main/java/fr/insee/rmes/bauhaus_services/OperationsService.java index 7504fbd91..a0dcac566 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/OperationsService.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/OperationsService.java @@ -86,27 +86,16 @@ public interface OperationsService { /** * UPDATE - * @param id - * @param body - * @throws RmesException */ void setIndicator(String id, String body) throws RmesException; - - /** - * PUBLISH - * @param body - * @throws RmesException - */ - - String setIndicatorValidation(String body) throws RmesException; /** * CREATE - * @param body - * @return - * @throws RmesException */ String setIndicator(String body) throws RmesException; - + /** + * PUBLISH + */ + String setIndicatorValidation(String body) throws RmesException; } diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/StampAuthorizationChecker.java b/src/main/java/fr/insee/rmes/bauhaus_services/StampAuthorizationChecker.java index 8bda2da36..4c08bbde3 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/StampAuthorizationChecker.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/StampAuthorizationChecker.java @@ -28,12 +28,10 @@ public class StampAuthorizationChecker extends StampsRestrictionServiceImpl { private static final Logger logger = LoggerFactory.getLogger(StampAuthorizationChecker.class); public static final String CHECKING_AUTHORIZATION_ERROR_MESSAGE = "Error while checking authorization for user with stamp {} to modify or delete {}"; public static final String ERROR_AUTHORIZATION = "Error while checking authorization for user with stamp {} to modify {}"; - private final String baseInternalUri; @Autowired - public StampAuthorizationChecker(RepositoryGestion repoGestion, AuthorizeMethodDecider authorizeMethodDecider, UserProvider userProvider, @Value("${fr.insee.rmes.bauhaus.sesame.gestion.baseInternalURI}") String baseInternalUri) { + public StampAuthorizationChecker(RepositoryGestion repoGestion, AuthorizeMethodDecider authorizeMethodDecider, UserProvider userProvider) { super(repoGestion, authorizeMethodDecider, userProvider); - this.baseInternalUri = baseInternalUri; } public boolean isSeriesManagerWithStamp(String seriesId, Stamp stamp) { diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsCollectionServiceImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsCollectionServiceImpl.java index 6ccb4e9e5..d050e77b5 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsCollectionServiceImpl.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsCollectionServiceImpl.java @@ -11,7 +11,6 @@ import fr.insee.rmes.utils.XMLUtils; import fr.insee.rmes.webservice.ConceptsCollectionsResources; import jakarta.servlet.http.HttpServletResponse; -import org.apache.commons.text.CaseUtils; import org.json.JSONArray; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -89,7 +88,7 @@ public ResponseEntity getCollectionExportODT(String id, String accept, Concep String fileName = getFileNameForExport(collection, lg); if(conceptsIds.isEmpty()){ - return collectionExport.exportAsResponseODT(fileName, xmlContent,true,true,true, lg); + return collectionExport.exportAsResponseODT(fileName, xmlContent,true, lg); } Map concepts = conceptsService.getConceptsExportIS(conceptsIds, null); diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsImpl.java index 12c6b8873..5972b31ad 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsImpl.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsImpl.java @@ -28,6 +28,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.Resource; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -101,7 +102,7 @@ public String getRelatedConcepts(String id) throws RmesException{ * @throws RmesException */ @Override - public String deleteConcept(String id) throws RmesException { + public void deleteConcept(String id) throws RmesException { String uriConcept = RdfUtils.toString(RdfUtils.objectIRI(ObjectType.CONCEPT,id)); JSONArray graphArray = conceptsUtils.getGraphsWithConcept(uriConcept); @@ -139,8 +140,6 @@ public String deleteConcept(String id) throws RmesException { String successMessage=THE_CONCEPT+id+" has been deleted from graph "+RdfUtils.conceptGraph(); if (result!= HttpStatus.OK) { throw new RmesException(HttpStatus.INTERNAL_SERVER_ERROR.value(),"Unexpected return message: ",result.toString()); - } else { - return successMessage; } } @@ -214,14 +213,8 @@ public void setConceptsValidation(String body) throws RmesException { * Export concept(s) */ @Override - public ResponseEntity exportConcept(String id, String acceptHeader) throws RmesException { - ConceptForExport concept; - try { - concept = conceptsExport.getConceptData(id); - } catch (RmesException e) { - return ResponseEntity.status(e.getStatus()).contentType(MediaType.TEXT_PLAIN).body(e.getDetails()); - } - + public ResponseEntity exportConcept(String id, String acceptHeader) throws RmesException { + ConceptForExport concept = conceptsExport.getConceptData(id); Map xmlContent = convertConceptInXml(concept); String fileName = getFileNameForExport(concept); return conceptsExport.exportAsResponse(fileName,xmlContent,true,true,true); @@ -339,13 +332,8 @@ public void setCollectionsValidation(String body) throws RmesException { } @Override - public ResponseEntity getCollectionExport(String id, String acceptHeader) throws RmesException{ - CollectionForExportOld collection; - try { - collection = collectionExport.getCollectionDataOld(id); - } catch (RmesException e) { - return ResponseEntity.status(e.getStatus()).contentType(MediaType.TEXT_PLAIN).body(e.getDetails()); - } + public ResponseEntity getCollectionExport(String id, String acceptHeader) throws RmesException{ + CollectionForExportOld collection= collectionExport.getCollectionDataOld(id); Map xmlContent = convertCollectionInXml(collection); String fileName = FilesUtils.generateFinalFileNameWithoutExtension(collection.getId() + "-" + collection.getPrefLabelLg1(), maxLength); return collectionExport.exportAsResponse(fileName,xmlContent,true,true,true); diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/collections/CollectionExportBuilder.java b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/collections/CollectionExportBuilder.java index e378e70a4..fbd5d670b 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/collections/CollectionExportBuilder.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/collections/CollectionExportBuilder.java @@ -109,7 +109,7 @@ public ResponseEntity exportAsResponse(String fileName, Map exportAsResponseODT(String fileName, Map xmlContent, boolean lg1, boolean lg2, boolean includeEmptyFields, ConceptsCollectionsResources.Language lg) throws RmesException { + public ResponseEntity exportAsResponseODT(String fileName, Map xmlContent, boolean includeEmptyFields, ConceptsCollectionsResources.Language lg) throws RmesException { String parametersXML = XsltUtils.buildParams(true, true, includeEmptyFields, Constants.COLLECTION); xmlContent.put(Constants.PARAMETERS_FILE, parametersXML); String xmlPattern = lg == ConceptsCollectionsResources.Language.lg1 ? xmlPatternFR : xmlPatternEN; diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/indicators/IndicatorsUtils.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/indicators/IndicatorsUtils.java index 156e436ea..b7ece6be1 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/indicators/IndicatorsUtils.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/indicators/IndicatorsUtils.java @@ -12,6 +12,7 @@ import fr.insee.rmes.bauhaus_services.rdf_utils.*; import fr.insee.rmes.config.auth.security.restrictions.StampsRestrictionsService; import fr.insee.rmes.exceptions.*; +import fr.insee.rmes.exceptions.errors.IndicatorErrorCodes; import fr.insee.rmes.model.ValidationStatus; import fr.insee.rmes.model.links.OperationsLink; import fr.insee.rmes.model.operations.Indicator; @@ -89,18 +90,17 @@ public IndicatorsUtils( } private void validate(Indicator indicator) throws RmesException { + if(indicator.isWasGenerattedByEmpty()){ + throw new RmesBadRequestException(IndicatorErrorCodes.EMPTY_WAS_GENERATED_BY, "An indicator should be linked to a series."); + } if(repositoryGestion.getResponseAsBoolean(IndicatorsQueries.checkPrefLabelUnicity(indicator.getId(), indicator.getPrefLabelLg1(), lg1))){ - throw new RmesBadRequestException(ErrorCodes.OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG1, "This prefLabelLg1 is already used by another indicator."); + throw new RmesBadRequestException(IndicatorErrorCodes.EXISTING_PREF_LABEL_LG1, "This prefLabelLg1 is already used by another indicator."); } if(repositoryGestion.getResponseAsBoolean(IndicatorsQueries.checkPrefLabelUnicity(indicator.getId(), indicator.getPrefLabelLg2(), lg2))){ - throw new RmesBadRequestException(ErrorCodes.OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG2, "This prefLabelLg2 is already used by another indicator."); + throw new RmesBadRequestException(IndicatorErrorCodes.EXISTING_PREF_LABEL_LG2, "This prefLabelLg2 is already used by another indicator."); } } - public Indicator getIndicatorById(String id) throws RmesException{ - return buildIndicatorFromJson(getIndicatorJsonById(id), false); - } - public Indicator getIndicatorById(String id, boolean forXML) throws RmesException{ return buildIndicatorFromJson(getIndicatorJsonById(id), forXML); } @@ -434,7 +434,4 @@ public String createID() throws RmesException { public boolean checkIfIndicatorExists(String id) throws RmesException { return repositoryGestion.getResponseAsBoolean(IndicatorsQueries.checkIfExists(id)); } - - - } diff --git a/src/main/java/fr/insee/rmes/exceptions/ErrorCodes.java b/src/main/java/fr/insee/rmes/exceptions/ErrorCodes.java index 24a1bc74b..bf4488f13 100644 --- a/src/main/java/fr/insee/rmes/exceptions/ErrorCodes.java +++ b/src/main/java/fr/insee/rmes/exceptions/ErrorCodes.java @@ -143,9 +143,6 @@ public class ErrorCodes { public static final String OPERATION_OPERATION_EXISTING_PREF_LABEL_LG1 = "406_OPERATION_OPERATION_OPERATION_OPERATION_EXISTING_PREF_LABEL_LG1"; public static final String OPERATION_OPERATION_EXISTING_PREF_LABEL_LG2 = "406_OPERATION_OPERATION_OPERATION_OPERATION_EXISTING_PREF_LABEL_LG2"; - public static final String OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG1 = "406_OPERATION_INDICATOR_OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG1"; - public static final String OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG2 = "406_OPERATION_INDICATOR_OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG2"; - // SIMS public static final int SIMS_INCORRECT = 861; public static final int SIMS_DELETION_FOR_NON_SERIES = 862; diff --git a/src/main/java/fr/insee/rmes/exceptions/RmesExceptionHandler.java b/src/main/java/fr/insee/rmes/exceptions/RmesExceptionHandler.java index aade896a6..6fa9f7e5c 100644 --- a/src/main/java/fr/insee/rmes/exceptions/RmesExceptionHandler.java +++ b/src/main/java/fr/insee/rmes/exceptions/RmesExceptionHandler.java @@ -1,5 +1,7 @@ package fr.insee.rmes.exceptions; +import fr.insee.rmes.webservice.ConceptsCollectionsResources; +import fr.insee.rmes.webservice.ConceptsResources; import fr.insee.rmes.webservice.UserResources; import fr.insee.rmes.webservice.codesLists.CodeListsResources; import fr.insee.rmes.webservice.dataset.DatasetResources; @@ -23,6 +25,8 @@ OperationsResources.class, DocumentsResources.class, UserResources.class, + ConceptsResources.class, + ConceptsCollectionsResources.class }) public class RmesExceptionHandler extends ResponseEntityExceptionHandler { diff --git a/src/main/java/fr/insee/rmes/exceptions/errors/IndicatorErrorCodes.java b/src/main/java/fr/insee/rmes/exceptions/errors/IndicatorErrorCodes.java new file mode 100644 index 000000000..7b2978ae0 --- /dev/null +++ b/src/main/java/fr/insee/rmes/exceptions/errors/IndicatorErrorCodes.java @@ -0,0 +1,11 @@ +package fr.insee.rmes.exceptions.errors; + +public class IndicatorErrorCodes { + + private IndicatorErrorCodes() { + } + + public static final String EMPTY_WAS_GENERATED_BY = "406_OPERATION_INDICATOR_OPERATION_INDICATOR_EMPTY_WAS_GENERATED_BY"; + public static final String EXISTING_PREF_LABEL_LG1 = "406_OPERATION_INDICATOR_OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG1"; + public static final String EXISTING_PREF_LABEL_LG2 = "406_OPERATION_INDICATOR_OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG2"; +} diff --git a/src/main/java/fr/insee/rmes/model/operations/Indicator.java b/src/main/java/fr/insee/rmes/model/operations/Indicator.java index 0e657a303..c0e1ce0fb 100644 --- a/src/main/java/fr/insee/rmes/model/operations/Indicator.java +++ b/src/main/java/fr/insee/rmes/model/operations/Indicator.java @@ -176,6 +176,10 @@ public List getWasGeneratedBy() { return wasGeneratedBy; } + public boolean isWasGenerattedByEmpty() { + return this.wasGeneratedBy == null || this.wasGeneratedBy.isEmpty(); + } + public String getIdSims() { return idSims; } diff --git a/src/main/java/fr/insee/rmes/webservice/ConceptsResources.java b/src/main/java/fr/insee/rmes/webservice/ConceptsResources.java index d6f8b5362..9d02c16f9 100644 --- a/src/main/java/fr/insee/rmes/webservice/ConceptsResources.java +++ b/src/main/java/fr/insee/rmes/webservice/ConceptsResources.java @@ -20,6 +20,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.core.io.Resource; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -256,7 +257,7 @@ public ResponseEntity getCollectionExport(@PathVariable(Constants.ID) String @GetMapping(value = "/concept/export/{id}", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE, "application/zip" }) @Operation(operationId = "exportConcept", summary = "Blob of concept") - public ResponseEntity exportConcept(@PathVariable(Constants.ID) String id, @RequestHeader(required=false) String accept) throws RmesException { + public ResponseEntity exportConcept(@PathVariable(Constants.ID) String id, @RequestHeader(required=false) String accept) throws RmesException { return conceptsService.exportConcept(id, accept); } diff --git a/src/test/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsImplTest.java b/src/test/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsImplTest.java index c115a0436..81db68efd 100644 --- a/src/test/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsImplTest.java +++ b/src/test/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsImplTest.java @@ -20,6 +20,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.Resource; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -135,13 +136,13 @@ void exportConceptTest() throws RmesException, IOException, URISyntaxException { .reduce((a,b)->a+"\n"+b).get(); // WHEN - ResponseEntity result = (ResponseEntity) conceptsImpl.exportConcept(idConcept, MediaType.APPLICATION_OCTET_STREAM_VALUE); + ResponseEntity result = conceptsImpl.exportConcept(idConcept, MediaType.APPLICATION_OCTET_STREAM_VALUE); // THEN assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(result.getHeaders().getContentDisposition().getFilename()).isEqualTo("c1116accid.odt"); Diff inputsDiffs = DiffBuilder.compare(expectedXmlForOdtContent) - .withTest(getOdtContent(result.getBody().getByteArray())) + .withTest(getOdtContent(((ByteArrayResource)result.getBody()).getByteArray())) .ignoreWhitespace() .ignoreComments() .build(); diff --git a/src/test/java/fr/insee/rmes/bauhaus_services/operations/indicators/IndicatorsUtilsTest.java b/src/test/java/fr/insee/rmes/bauhaus_services/operations/indicators/IndicatorsUtilsTest.java index 1772fed85..60e7554eb 100644 --- a/src/test/java/fr/insee/rmes/bauhaus_services/operations/indicators/IndicatorsUtilsTest.java +++ b/src/test/java/fr/insee/rmes/bauhaus_services/operations/indicators/IndicatorsUtilsTest.java @@ -1,20 +1,26 @@ package fr.insee.rmes.bauhaus_services.operations.indicators; +import fr.insee.rmes.bauhaus_services.Constants; import fr.insee.rmes.bauhaus_services.operations.famopeserind_utils.FamOpeSerIndUtils; import fr.insee.rmes.bauhaus_services.rdf_utils.RepositoryGestion; +import fr.insee.rmes.exceptions.RmesBadRequestException; import fr.insee.rmes.exceptions.RmesException; import fr.insee.rmes.model.links.OperationsLink; import fr.insee.rmes.model.operations.Indicator; +import fr.insee.rmes.persistance.sparql_queries.operations.indicators.IndicatorsQueries; import org.eclipse.rdf4j.model.IRI; import org.eclipse.rdf4j.model.Model; import org.eclipse.rdf4j.model.impl.LinkedHashModel; import org.eclipse.rdf4j.model.impl.SimpleValueFactory; import org.eclipse.rdf4j.model.vocabulary.DCTERMS; +import org.json.JSONArray; import org.json.JSONObject; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -22,6 +28,8 @@ import java.util.ArrayList; import java.util.List; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; @@ -34,6 +42,67 @@ class IndicatorsUtilsTest { @Autowired private FamOpeSerIndUtils famOpeSerIndUtils; + @Test + void shouldThrowExceptionIfWasGeneratedByNull() throws RmesException { + JSONObject indicator = new JSONObject(); + + IndicatorsUtils indicatorsUtils = new IndicatorsUtils(true, repositoryGestion, null, null, null, famOpeSerIndUtils, null, null, null, null, "fr", "en"); + when(repositoryGestion.getResponseAsObject(any())).thenReturn(new JSONObject().put(Constants.ID, "p1000")); + RmesException exception = assertThrows(RmesBadRequestException.class, () -> indicatorsUtils.setIndicator(indicator.toString())); + assertThat(exception.getDetails()).contains("An indicator should be linked to a series."); + } + + @Test + void shouldThrowExceptionIfWasGeneratedByEmpty() throws RmesException { + JSONObject indicator = new JSONObject().put("wasGeneratedBy", new JSONArray()); + + IndicatorsUtils indicatorsUtils = new IndicatorsUtils(true, repositoryGestion, null, null, null, famOpeSerIndUtils, null, null, null, null, "fr", "en"); + when(repositoryGestion.getResponseAsObject(any())).thenReturn(new JSONObject().put(Constants.ID, "p1000")); + RmesException exception = assertThrows(RmesBadRequestException.class, () -> indicatorsUtils.setIndicator(indicator.toString())); + assertThat(exception.getDetails()).contains("An indicator should be linked to a series."); + } + + @Test + void shouldThrowExceptionIfLabelLg1Exist() throws RmesException { + JSONObject indicator = new JSONObject() + .put("id", "1") + .put("wasGeneratedBy", new JSONArray().put(new JSONObject())) + .put("prefLabelLg1", "prefLabelLg1") + .put("prefLabelLg2", "prefLabelLg2"); + + try (MockedStatic mockedFactory = Mockito.mockStatic(IndicatorsQueries.class)) { + mockedFactory.when(() -> IndicatorsQueries.checkPrefLabelUnicity(eq("p1001"), eq("prefLabelLg1"), eq("fr"))).thenReturn("query"); + + when(repositoryGestion.getResponseAsBoolean("query")).thenReturn(true); + when(repositoryGestion.getResponseAsObject(any())).thenReturn(new JSONObject().put(Constants.ID, "p1000")); + + IndicatorsUtils indicatorsUtils = new IndicatorsUtils(true, repositoryGestion, null, null, null, famOpeSerIndUtils, null, null, null, null, "fr", "en"); + RmesException exception = assertThrows(RmesBadRequestException.class, () -> indicatorsUtils.setIndicator(indicator.toString())); + assertThat(exception.getDetails()).contains("This prefLabelLg1 is already used by another indicator."); + } + } + + @Test + void shouldThrowExceptionIfLabelLg2Exist() throws RmesException { + JSONObject indicator = new JSONObject() + .put("id", "1") + .put("wasGeneratedBy", new JSONArray().put(new JSONObject())) + .put("prefLabelLg1", "prefLabelLg1") + .put("prefLabelLg2", "prefLabelLg2"); + + try (MockedStatic mockedFactory = Mockito.mockStatic(IndicatorsQueries.class)) { + mockedFactory.when(() -> IndicatorsQueries.checkPrefLabelUnicity(eq("p1001"), eq("prefLabelLg1"), eq("fr"))).thenReturn("query1"); + mockedFactory.when(() -> IndicatorsQueries.checkPrefLabelUnicity(eq("p1001"), eq("prefLabelLg2"), eq("en"))).thenReturn("query2"); + + when(repositoryGestion.getResponseAsBoolean("query1")).thenReturn(false); + when(repositoryGestion.getResponseAsBoolean("query2")).thenReturn(true); + when(repositoryGestion.getResponseAsObject(any())).thenReturn(new JSONObject().put(Constants.ID, "p1000")); + + IndicatorsUtils indicatorsUtils = new IndicatorsUtils(true, repositoryGestion, null, null, null, famOpeSerIndUtils, null, null, null, null, "fr", "en"); + RmesException exception = assertThrows(RmesBadRequestException.class, () -> indicatorsUtils.setIndicator(indicator.toString())); + assertThat(exception.getDetails()).contains("This prefLabelLg2 is already used by another indicator."); + } + } @Test void shouldAddAbstractPropertyWithNewSyntaxIfFeatureFlagTrue() throws RmesException { doNothing().when(repositoryGestion).deleteObject(any(), any()); diff --git a/src/test/java/fr/insee/rmes/utils/DateUtilsTest.java b/src/test/java/fr/insee/rmes/utils/DateUtilsTest.java index 4e6c64d7f..bacc88746 100644 --- a/src/test/java/fr/insee/rmes/utils/DateUtilsTest.java +++ b/src/test/java/fr/insee/rmes/utils/DateUtilsTest.java @@ -1,22 +1,65 @@ package fr.insee.rmes.utils; -import fr.insee.rmes.exceptions.RmesException; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import java.time.LocalDateTime; + +import static org.junit.jupiter.api.Assertions.*; class DateUtilsTest { @Test - void should_return_date_if_good_format() { - String date = DateUtils.getDate("2018-12-17"); - assertEquals("2018-12-17", date); + void getCurrentDate_ShouldReturnNonNullValue() { + String currentDate = DateUtils.getCurrentDate(); + assertNotNull(currentDate); + } + + @Test + void parseDateTime_ShouldParseValidZonedDateTime() { + String dateStr = "2024-12-09T12:30:45+01:00"; + LocalDateTime result = DateUtils.parseDateTime(dateStr); + assertEquals(LocalDateTime.of(2024, 12, 9, 12, 30, 45), result); + } + + @Test + void parseDateTime_ShouldParseValidDateWithoutTime() { + String dateStr = "2024-12-09"; + LocalDateTime result = DateUtils.parseDateTime(dateStr); + assertEquals(LocalDateTime.of(2024, 12, 9, 0, 0), result); + } + + @Test + void parseDateTime_ShouldReturnNullForInvalidDate() { + String invalidDate = "invalid-date"; + assertThrows(Exception.class, () -> DateUtils.parseDateTime(invalidDate)); + } + + @Test + void getDate_ShouldConvertToISODateFormat() { + String isoDateTime = "2024-12-09T15:45:30"; + String result = DateUtils.getDate(isoDateTime); + assertEquals("2024-12-09", result); + } + + @Test + void getDate_ShouldReturnOriginalForInvalidDate() { + String invalidDate = "invalid-date"; + String result = DateUtils.getDate(invalidDate); + assertEquals(invalidDate, result); + } + + @Test + void toDate_ShouldFormatToDDMMYYYY() { + String dateTime = "2024-12-09T15:45:30"; + String result = DateUtils.toDate(dateTime); + assertEquals("09/12/2024", result); } @Test - void should_return_date_if_bad_format() { - String date = DateUtils.getDate("12-17"); - assertEquals("12-17", date); + void toDate_ShouldReturnOriginalStringForShortString() { + String shortString = "2024"; + String result = DateUtils.toDate(shortString); + assertEquals(shortString, result); } }