Skip to content

Commit

Permalink
Merge branch 'main' into fix/solve-sonar-issues
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey authored Dec 6, 2024
2 parents f597e43 + 4e4ff63 commit 467de66
Show file tree
Hide file tree
Showing 21 changed files with 493 additions and 64 deletions.
3 changes: 3 additions & 0 deletions src/main/java/fr/insee/rmes/bauhaus_services/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public class Constants {


/*A*/
public static final String ACCRUAL_PERIODICITY_LIST ="accrualPeriodicityList";
public static final String ALT_LABEL_LG1 = "altLabelLg1";
Expand Down Expand Up @@ -108,8 +109,10 @@ public class Constants {
public static final String TEXT_LG1 = "texte";
public static final String TEXT_LG2 = "text";
public static final String TYPE_OF_OBJECT = "typeOfObject";
public static final String TYPE_STRING = "String";
public static final String TYPELIST = "typeList";


/*U*/
public static final String UNDEFINED = "undefined";
public static final String UPDATED_DATE = "updatedDate";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public JSONObject getDocument(String id, boolean isLink) throws RmesException {

if (jsonDocs.isNull(Constants.URI)) {
logger.error("Error with the document {}. It looks like it does not have an uri", id);
throw new RmesNotFoundException(ErrorCodes.DOCUMENT_UNKNOWN_ID, "Cannot find " + (isLink ? "Link" : "Document") + " with id: ", id);
throw new RmesNotFoundException(ErrorCodes.DOCUMENT_UNKNOWN_ID, "Cannot find " + (isLink ? "Link" : "Document") + " with id : " + id, id);
}
formatDateInJsonObject(jsonDocs);
jsonDocs.put("sims", this.getSimsByDocument(id, isLink));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.model.vocabulary.SKOS;
import org.eclipse.rdf4j.model.vocabulary.XSD;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
Expand Down Expand Up @@ -179,7 +180,7 @@ private void createRdfOperation(Operation operation, IRI serieUri, ValidationSta
RdfUtils.addTripleDateTime(operationURI, DCTERMS.MODIFIED, operation.getModified(), model, RdfUtils.operationsGraph());

if(operation.getYear() != null){
RdfUtils.addTripleInt(operationURI, DCTERMS.TEMPORAL, operation.getYear().toString(), model, RdfUtils.operationsGraph());
model.add(operationURI, DCTERMS.TEMPORAL, RdfUtils.createLiteral(operation.getYear().toString(), XSD.GYEAR), RdfUtils.operationsGraph());
}

if (serieUri != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,15 @@ public static Literal setLiteralString(String string, String language) {
public static Literal setLiteralBoolean(Boolean bool) {
return factory.createLiteral(bool);
}


public static Literal createLiteral(String value, IRI datatype){
return factory.createLiteral(value, datatype);
}

public static Literal setLiteralInt(String number) {
return factory.createLiteral(number, XSD.INT);
}

public static Literal setLiteralDateTime(String date) {
String parsedDate = DateTimeFormatter.ISO_DATE_TIME.format(DateUtils.parseDateTime(date));
return factory.createLiteral(parsedDate, XSD.DATETIME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fr.insee.rmes.config.auth.roles.Roles;
import fr.insee.rmes.config.auth.user.Stamp;
import fr.insee.rmes.exceptions.RmesRuntimeBadRequestException;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -173,6 +174,9 @@ private boolean checkStampIsContributor(String body) {
private static @Nullable String extractContributorStampFromBody(String body) {
return (new JSONObject(body)).optString("contributor");
}
private static @Nullable JSONArray extractContributorStampsFromBody(String body) {
return (new JSONObject(body)).optJSONArray("contributor");
}

//for PUT and DELETE structure
public boolean isStructureContributor(String structureId){
Expand All @@ -183,7 +187,13 @@ public boolean isStructureContributor(String structureId){
// for POST structure or component
public boolean isStructureAndComponentContributor(String body) {
logger.trace("Check if {} can create the structure or component", methodSecurityExpressionRoot.getPrincipal());
return hasRole(Roles.STRUCTURES_CONTRIBUTOR)&& checkStampIsContributor(body);
Optional<Stamp> stamp = getStamp();
JSONArray contributors = extractContributorStampsFromBody(body);

if(contributors == null){
return false;
}
return hasRole(Roles.STRUCTURES_CONTRIBUTOR) && contributors.toList().stream().anyMatch(s -> ((String) s).equalsIgnoreCase(stamp.get().stamp()));
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fr.insee.rmes.config.swagger.model.operations.documentation;

public class DocumentId {
private String id;

public DocumentId(String id) {
this.id = id;
}

public String getDocumentId() {
return id;
}

public String getString() {
if (id != null && !id.isEmpty()) {
return id;
}
else{
return null; //without this it might cause some trouble to test with new DocumentID(null)
}
}

}
2 changes: 1 addition & 1 deletion src/main/java/fr/insee/rmes/utils/FilesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ private static String reduceFileNameSize(String fileName, int maxLength) {

private static String removeAsciiCharacters(String fileName) {
return Normalizer.normalize(fileName, Normalizer.Form.NFD)
.replaceAll("\\p{M}+", "")
.replace("œ", "oe")
.replace("Œ", "OE")
.replaceAll("[-_]", " ")
.replaceAll("\\p{M}+", " ")
.replaceAll("\\p{Punct}", "")
.replace(":", "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public String getDistributions() throws RmesException {
}

@GetMapping("/{id}")
@Operation(operationId = "getDistribution", summary = "Get a distributions",
@Operation(operationId = "getDistribution", summary = "Get a distribution",
responses = {@ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Distribution.class))))})
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 @@ -2,6 +2,7 @@

import fr.insee.rmes.bauhaus_services.Constants;
import fr.insee.rmes.bauhaus_services.DocumentsService;
import fr.insee.rmes.config.swagger.model.operations.documentation.DocumentId;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.model.operations.documentations.Document;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -107,15 +108,28 @@ public ResponseEntity<String> setDocument(
@PutMapping("/document/{id}")
@Operation(operationId = "setDocumentById", summary = "Update document ")
public ResponseEntity<String> setDocument(
@Parameter(description = "Id", required = true) @PathVariable(Constants.ID) String id,
@Parameter(description = Constants.DOCUMENT, required = true, schema = @Schema(implementation = Document.class)) @RequestBody String body) throws RmesException {
documentsService.setDocument(id, body);
@Parameter(
description = "Id",
required = true,
schema = @Schema (type=Constants.TYPE_STRING)
)
@PathVariable(Constants.ID) DocumentId id,
@Parameter(
description = Constants.DOCUMENT,
required = true,
schema = @Schema(implementation = Document.class)
)
@RequestBody String body) throws RmesException {
String documentIdString = (id.getDocumentId() != null) ? sanitizeDocumentId(id.getDocumentId()) : null;
documentsService.setDocument(documentIdString, body);
logger.info("Update document : {}", id);
return ResponseEntity.ok(id);
return ResponseEntity.ok(documentIdString);
}


@PreAuthorize("hasAnyRole(T(fr.insee.rmes.config.auth.roles.Roles).ADMIN "


@PreAuthorize("hasAnyRole(T(fr.insee.rmes.config.auth.roles.Roles).ADMIN "
+ ", T(fr.insee.rmes.config.auth.roles.Roles).INDICATOR_CONTRIBUTOR "
+ ", T(fr.insee.rmes.config.auth.roles.Roles).SERIES_CONTRIBUTOR)")
@Operation(operationId = "changeDocument", summary = "Change document file")
Expand All @@ -140,8 +154,15 @@ public ResponseEntity<String> changeDocument(
+ ", T(fr.insee.rmes.config.auth.roles.Roles).SERIES_CONTRIBUTOR)")
@DeleteMapping("/document/{id}")
@Operation(operationId = "deleteDocument", summary = "Delete a document")
public ResponseEntity<Object> deleteDocument(@PathVariable(Constants.ID) String id) throws RmesException {
return ResponseEntity.status(documentsService.deleteDocument(id)).body(id);
public ResponseEntity<Object> deleteDocument(
@Parameter(
required = true,
schema = @Schema (type=Constants.TYPE_STRING)
)
@PathVariable(Constants.ID) DocumentId id)
throws RmesException {
String documentIdString = (id.getDocumentId() != null) ? sanitizeDocumentId(id.getDocumentId()) : null;
return ResponseEntity.status(documentsService.deleteDocument(documentIdString)).body(documentIdString);
}


Expand Down Expand Up @@ -176,17 +197,48 @@ public ResponseEntity<Object> setLink(
@PutMapping("/link/{id}")
@Operation(operationId = "setLinkById", summary = "Update link")
public ResponseEntity<Object> setLink(
@Parameter(description = "Id", required = true) @PathVariable(Constants.ID) String id,
@Parameter(description = "Link", required = true, schema = @Schema(implementation = Document.class)) @RequestBody String body) throws RmesException {
return ResponseEntity.ok(documentsService.setLink(id, body));
@Parameter(
description = "Id",
required = true,
schema = @Schema (type=Constants.TYPE_STRING)
)
@PathVariable(Constants.ID) DocumentId id,
@Parameter(
required = true,
schema = @Schema(implementation = Document.class)
)
@RequestBody String body
)
throws RmesException {
String documentIdString = (id.getDocumentId() != null) ? sanitizeDocumentId(id.getDocumentId()) : null;
return ResponseEntity.ok(documentsService.setLink(documentIdString, body));
}

@PreAuthorize("hasAnyRole(T(fr.insee.rmes.config.auth.roles.Roles).ADMIN "
+ ", T(fr.insee.rmes.config.auth.roles.Roles).INDICATOR_CONTRIBUTOR "
+ ", T(fr.insee.rmes.config.auth.roles.Roles).SERIES_CONTRIBUTOR)")
@DeleteMapping("/link/{id}")
@Operation(operationId = "deleteLink", summary = "Delete a link")
public ResponseEntity<Object> deleteLink(@PathVariable(Constants.ID) String id) throws RmesException {
return ResponseEntity.status(documentsService.deleteLink(id)).body(id);
public ResponseEntity<Object> deleteLink(
@Parameter(
required = true,
schema = @Schema (type=Constants.TYPE_STRING)
)
@PathVariable(Constants.ID) DocumentId id
) throws RmesException {
String documentIdString = (id.getDocumentId() != null) ? sanitizeDocumentId(id.getDocumentId()) : null;
return ResponseEntity.status(documentsService.deleteLink(documentIdString)).body(documentIdString);
}


// Méthode pour encoder et valider le DocumentID
private String sanitizeDocumentId(String documentIdString) {
if (documentIdString == null || documentIdString.isEmpty()) {
return null;
}
//on peut ajouter d'autres contrôles
return documentIdString.replaceAll("[/<>:\"]", "");
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static org.mockito.ArgumentMatchers.eq;

@SpringBootTest(properties = { "fr.insee.rmes.bauhaus.lg1=fr", "fr.insee.rmes.bauhaus.lg2=en"})
public class DatasetQueriesTest {
class DatasetQueriesTest {

@Autowired
Config config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static org.mockito.ArgumentMatchers.eq;

@SpringBootTest(properties = { "fr.insee.rmes.bauhaus.lg1=fr", "fr.insee.rmes.bauhaus.lg2=en"})
public class DistributionQueriesTest {
class DistributionQueriesTest {
@Autowired
Config config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class OperationsDocumentationsImplTest {
private OperationsDocumentationsImpl metadataReportService;

@Test
public void testExportMetadataReport_Success() throws RmesException {
void testExportMetadataReport_Success() throws RmesException {
String id = "1234";
boolean includeEmptyMas = true;
boolean lg1 = true;
Expand All @@ -45,7 +45,7 @@ public void testExportMetadataReport_Success() throws RmesException {
}

@Test
public void testExportMetadataReport_Failure_NoLanguageSelected() {
void testExportMetadataReport_Failure_NoLanguageSelected() {
String id = "1234";
boolean includeEmptyMas = true;
boolean lg1 = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class DocumentationExportTest {
private DocumentsUtils documentsUtils;

@Test
public void testExportAsZip_success() throws Exception {
void testExportAsZip_success() throws Exception {
JSONObject document = new JSONObject();
document.put("url", "file://doc.doc");
document.put("id", "1");
Expand Down Expand Up @@ -98,7 +98,7 @@ public void testExportAsZip_success() throws Exception {
}

@Test
public void testExportMetadataReport_Success_WithoutDocuments_Label() throws RmesException {
void testExportMetadataReport_Success_WithoutDocuments_Label() throws RmesException {
DocumentationExport documentationExport = new DocumentationExport(50, documentsUtils, exportUtils, seriesUtils, operationsUtils, indicatorsUtils, parentUtils, codeListService, organizationsService, documentationsUtils );

String id = "1234";
Expand All @@ -121,7 +121,7 @@ public void testExportMetadataReport_Success_WithoutDocuments_Label() throws Rm
}

@Test
public void testExportMetadataReport_Failure_UnknownGoal() throws RmesException {
void testExportMetadataReport_Failure_UnknownGoal() throws RmesException {
DocumentationExport documentationExport = new DocumentationExport(50, documentsUtils, exportUtils, seriesUtils, operationsUtils, indicatorsUtils, parentUtils, codeListService, organizationsService, documentationsUtils );

String id = "1234";
Expand All @@ -142,7 +142,7 @@ public void testExportMetadataReport_Failure_UnknownGoal() throws RmesException
}

@Test
public void testExportXmlFiles_Success() throws RmesException {
void testExportXmlFiles_Success() throws RmesException {
DocumentationExport documentationExport = new DocumentationExport(50, documentsUtils, exportUtils, seriesUtils, operationsUtils, indicatorsUtils, parentUtils, codeListService, organizationsService, documentationsUtils );

Map<String, String> xmlContent = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.model.vocabulary.XSD;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -66,6 +67,8 @@ void shouldStoreYearProperty() throws RmesException {
mockedFactory.when(() -> RdfUtils.setLiteralString(anyString(), anyString())).thenCallRealMethod();
mockedFactory.when(() -> RdfUtils.setLiteralString(anyString())).thenCallRealMethod();
mockedFactory.when(RdfUtils::operationsGraph).thenReturn(valueFactory.createIRI("http://operations-graph/"));
mockedFactory.when(() -> RdfUtils.createLiteral(anyString(), eq(XSD.GYEAR))).thenCallRealMethod();
mockedFactory.when(() -> RdfUtils.operationsGraph()).thenReturn(valueFactory.createIRI("http://operations-graph/"));
mockedFactory.when(() -> RdfUtils.objectIRI(eq(ObjectType.SERIES), eq("2"))).thenReturn(valueFactory.createIRI("http://series/2"));
mockedFactory.when(() -> RdfUtils.objectIRI(eq(ObjectType.OPERATION), eq("1"))).thenReturn(operationIRI);
operationsQueriesMockedStatic.when(() -> OperationsQueries.checkPrefLabelUnicity(eq("1"), eq("prefLabelLg1"), eq("fr"))).thenReturn("unicity-labelLg1");
Expand All @@ -88,7 +91,7 @@ void shouldStoreYearProperty() throws RmesException {

verify(repositoryGestion, times(1)).loadSimpleObject(eq(operationIRI), model.capture());

Assertions.assertEquals("[(http://operation/2, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://rdf.insee.fr/def/base#StatisticalOperation, http://operations-graph/) [http://operations-graph/], (http://operation/2, http://www.w3.org/2004/02/skos/core#prefLabel, \"prefLabelLg1\"@fr, http://operations-graph/) [http://operations-graph/], (http://operation/2, http://rdf.insee.fr/def/base#validationState, \"Unpublished\", http://operations-graph/) [http://operations-graph/], (http://operation/2, http://www.w3.org/2004/02/skos/core#prefLabel, \"prefLabelLg2\"@en, http://operations-graph/) [http://operations-graph/], (http://operation/2, http://www.w3.org/2004/02/skos/core#altLabel, \"altLabelLg1\"@fr, http://operations-graph/) [http://operations-graph/], (http://operation/2, http://www.w3.org/2004/02/skos/core#altLabel, \"altLabelLg2\"@en, http://operations-graph/) [http://operations-graph/], (http://operation/2, http://purl.org/dc/terms/temporal, \"2024\"^^<http://www.w3.org/2001/XMLSchema#int>, http://operations-graph/) [http://operations-graph/]]", model.getValue().toString());
Assertions.assertEquals("[(http://operation/2, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://rdf.insee.fr/def/base#StatisticalOperation, http://operations-graph/) [http://operations-graph/], (http://operation/2, http://www.w3.org/2004/02/skos/core#prefLabel, \"prefLabelLg1\"@fr, http://operations-graph/) [http://operations-graph/], (http://operation/2, http://rdf.insee.fr/def/base#validationState, \"Unpublished\", http://operations-graph/) [http://operations-graph/], (http://operation/2, http://www.w3.org/2004/02/skos/core#prefLabel, \"prefLabelLg2\"@en, http://operations-graph/) [http://operations-graph/], (http://operation/2, http://www.w3.org/2004/02/skos/core#altLabel, \"altLabelLg1\"@fr, http://operations-graph/) [http://operations-graph/], (http://operation/2, http://www.w3.org/2004/02/skos/core#altLabel, \"altLabelLg2\"@en, http://operations-graph/) [http://operations-graph/], (http://operation/2, http://purl.org/dc/terms/temporal, \"2024\"^^<http://www.w3.org/2001/XMLSchema#gYear>, http://operations-graph/) [http://operations-graph/]]", model.getValue().toString());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@SpringBootTest(properties = "spring.config.additional-location=classpath:rbac.yml")
@EnableConfigurationProperties(RBACConfiguration.class)
public class RBACConfigurationTest {
class RBACConfigurationTest {

@Autowired
private RBACConfiguration rbacConfiguration;
Expand Down
Loading

0 comments on commit 467de66

Please sign in to comment.