Skip to content

Commit

Permalink
feat: add year to operation
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Nov 22, 2024
1 parent b331bb4 commit feec0f6
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public String setOperation(String body) throws RmesException {
if(!stampsRestrictionsService.canCreateOperation(seriesURI)) {
throw new RmesUnauthorizedException(ErrorCodes.OPERATION_CREATION_RIGHTS_DENIED, "Only an admin or a series manager can create a new operation.");
}

operation.setCreated(DateUtils.getCurrentDate());
operation.setModified(DateUtils.getCurrentDate());

Expand All @@ -140,12 +141,10 @@ public String setOperation(String id, String body) throws RmesException {
throw new RmesUnauthorizedException(ErrorCodes.OPERATION_MODIFICATION_RIGHTS_DENIED, "Only authorized users can modify operations.");
}

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Operation operation = new Operation(id);
try {
operation = mapper.readerForUpdating(operation).readValue(body);
} catch (IOException e) {
operation = Deserializer.deserializeJsonString(body, Operation.class);
} catch (RmesException e) {
logger.error(e.getMessage());
}

Expand Down Expand Up @@ -179,6 +178,10 @@ private void createRdfOperation(Operation operation, IRI serieUri, ValidationSta
RdfUtils.addTripleDateTime(operationURI, DCTERMS.CREATED, operation.getCreated(), model, RdfUtils.operationsGraph());
RdfUtils.addTripleDateTime(operationURI, DCTERMS.MODIFIED, operation.getModified(), model, RdfUtils.operationsGraph());

if(operation.getYear() != null){
RdfUtils.addTripleInt(operationURI, INSEE.YEAR, operation.getYear().toString(), model, RdfUtils.operationsGraph());
}

if (serieUri != null) {
//case CREATION : link operation to series
RdfUtils.addTripleUri(operationURI, DCTERMS.IS_PART_OF, serieUri, model, RdfUtils.operationsGraph());
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/fr/insee/rmes/model/operations/Operation.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ public class Operation {
@Schema(description = "Validation State")
private String validationState;

@Schema(description = "millesime")
private Integer year;

public Operation(String id, String prefLabelLg1, String prefLabelLg2, String altLabelLg1, String altLabelLg2,
IdLabelTwoLangs series, String idSims, String validationState) {
IdLabelTwoLangs series, String idSims, String validationState, int year) {
super();
this.id = id;
this.prefLabelLg1 = prefLabelLg1;
Expand Down Expand Up @@ -145,4 +148,12 @@ public String getValidationState() {
public void setValidationState(String validationState) {
this.validationState = validationState;
}

public Integer getYear() {
return year;
}

public void setYear(Integer year) {
this.year = year;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;

public class INSEE {
private static final ValueFactory factory = SimpleValueFactory.getInstance();
private static final ValueFactory factory = SimpleValueFactory.getInstance();

private INSEE() {
throw new IllegalStateException("Utility class");
Expand All @@ -23,6 +23,8 @@ private INSEE() {
private static IRI createIRI(String suffix) {
return factory.createIRI(NAMESPACE, suffix);
}
public static final IRI YEAR = INSEE.createIRI("year");

public static final IRI LAST_CODE_URI_SEGMENT = INSEE.createIRI("lastCodeUriSegment");
public static final IRI DISSEMINATIONSTATUS = INSEE.createIRI("disseminationStatus");
public static final IRI ADDITIONALMATERIAL = INSEE.createIRI("additionalMaterial");
Expand Down
39 changes: 22 additions & 17 deletions src/main/resources/request/operations/getOperation.ftlh
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
SELECT ?id ?prefLabelLg1 ?prefLabelLg2 ?altLabelLg1 ?altLabelLg2 ?idSims ?validationState ?created ?modified
SELECT ?id ?prefLabelLg1 ?prefLabelLg2 ?altLabelLg1 ?altLabelLg2 ?idSims ?validationState ?created ?modified ?year
WHERE { GRAPH <${OPERATIONS_GRAPH}> {
?operation skos:prefLabel ?prefLabelLg1 .
FILTER(STRENDS(STR(?operation),'/operations/operation/${ID}')) .
BIND(STRAFTER(STR(?operation),'/operation/') AS ?id) .
FILTER (lang(?prefLabelLg1) = '${LG1}') .
OPTIONAL {?operation skos:prefLabel ?prefLabelLg2 .
FILTER (lang(?prefLabelLg2) = '${LG2}') } .
OPTIONAL {?operation skos:altLabel ?altLabelLg1 .
FILTER (lang(?altLabelLg1) = '${LG1}') } .
OPTIONAL {?operation skos:altLabel ?altLabelLg2 .
FILTER (lang(?altLabelLg2) = '${LG2}') } .
}OPTIONAL { ?operation dcterms:created ?created } .
OPTIONAL { ?operation dcterms:modified ?modified } .
OPTIONAL{ ?report rdf:type sdmx-mm:MetadataReport . ?report sdmx-mm:target ?operation BIND(STRAFTER(STR(?report),'/rapport/') AS ?idSims) .
}
OPTIONAL {?operation insee:validationState ?validationState .
}
?operation skos:prefLabel ?prefLabelLg1 .
FILTER(STRENDS(STR(?operation),'/operations/operation/${ID}')) .
BIND(STRAFTER(STR(?operation),'/operation/') AS ?id) .
FILTER (lang(?prefLabelLg1) = '${LG1}') .
OPTIONAL {
?operation skos:prefLabel ?prefLabelLg2 .
FILTER (lang(?prefLabelLg2) = '${LG2}')
} .
OPTIONAL {
?operation skos:altLabel ?altLabelLg1 .
FILTER (lang(?altLabelLg1) = '${LG1}')
} .
OPTIONAL {
?operation skos:altLabel ?altLabelLg2 .
FILTER (lang(?altLabelLg2) = '${LG2}') } .
}
OPTIONAL { ?operation dcterms:created ?created } .
OPTIONAL { ?operation dcterms:modified ?modified } .
OPTIONAL{ ?report rdf:type sdmx-mm:MetadataReport . ?report sdmx-mm:target ?operation BIND(STRAFTER(STR(?report),'/rapport/') AS ?idSims) . }
OPTIONAL {?operation insee:validationState ?validationState .}
OPTIONAL {?operation insee:year ?year .}
}
LIMIT 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package fr.insee.rmes.bauhaus_services.operations.operations;

import fr.insee.rmes.bauhaus_services.operations.ParentUtils;
import fr.insee.rmes.bauhaus_services.operations.famopeserind_utils.FamOpeSerIndUtils;
import fr.insee.rmes.bauhaus_services.rdf_utils.ObjectType;
import fr.insee.rmes.bauhaus_services.rdf_utils.RdfUtils;
import fr.insee.rmes.bauhaus_services.rdf_utils.RepositoryGestion;
import fr.insee.rmes.config.Config;
import fr.insee.rmes.config.auth.security.restrictions.StampsRestrictionsService;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.persistance.sparql_queries.operations.operations.OperationsQueries;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
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.*;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class OperationsUtilsTest {
@InjectMocks
OperationsUtils operationsUtils;

@Mock
FamOpeSerIndUtils famOpeSerIndUtils;

@Mock
ParentUtils parentUtils;

@Mock
StampsRestrictionsService stampsRestrictionsService;

@Mock
RepositoryGestion repositoryGestion;

@Mock
Config config;

@Test
void shouldStoreYearProperty() throws RmesException {

when(repositoryGestion.getResponseAsBoolean("unicity-labelLg1")).thenReturn(false);
when(repositoryGestion.getResponseAsBoolean("unicity-labelLg2")).thenReturn(false);
when(config.getLg1()).thenReturn("fr");
when(config.getLg2()).thenReturn("en");
when(famOpeSerIndUtils.createId()).thenReturn("1");
when(famOpeSerIndUtils.checkIfObjectExists(ObjectType.SERIES, "2")).thenReturn(true);
when(parentUtils.checkIfSeriesHasSims(anyString())).thenReturn(false);
when(stampsRestrictionsService.canCreateOperation(any(IRI.class))).thenReturn(true);

try (MockedStatic<RdfUtils> mockedFactory = Mockito.mockStatic(RdfUtils.class);
MockedStatic<OperationsQueries> operationsQueriesMockedStatic = Mockito.mockStatic(OperationsQueries.class)
) {
SimpleValueFactory valueFactory = SimpleValueFactory.getInstance();
IRI operationIRI = valueFactory.createIRI("http://operation/2");
mockedFactory.when(() -> RdfUtils.setLiteralInt(anyString())).thenCallRealMethod();
mockedFactory.when(() -> RdfUtils.addTripleInt(any(), any(), any(), any(), any())).thenCallRealMethod();
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.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");
operationsQueriesMockedStatic.when(() -> OperationsQueries.checkPrefLabelUnicity(eq("1"), eq("prefLabelLg2"), eq("en"))).thenReturn("unicity-labelLg2");

JSONObject operation = new JSONObject();
JSONObject series = new JSONObject()
.put("id", "2");
operation
.put("prefLabelLg1", "prefLabelLg1")
.put("prefLabelLg2", "prefLabelLg2")
.put("year", 2024)
.put("series", series);

operationsUtils.setOperation(operation.toString());

ArgumentCaptor<Model> model = ArgumentCaptor.forClass(Model.class);

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://rdf.insee.fr/def/base#year, \"2024\"^^<http://www.w3.org/2001/XMLSchema#int>, http://operations-graph/) [http://operations-graph/]]", model.getValue().toString());

}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import fr.insee.rmes.persistance.sparql_queries.operations.series.OpSeriesQueries;
import fr.insee.rmes.testcontainers.queries.WithGraphDBContainer;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -32,4 +33,11 @@ void should_return_all_operations() throws Exception {
assertNotNull(result.getJSONObject(i).getString("iri"));
}
}

@Test
void should_return_one_operation() throws Exception {
OpSeriesQueries.setConfig(new ConfigStub());
JSONObject result = repositoryGestion.getResponseAsObject(OperationsQueries.operationQuery("s1447"));
assertEquals("2024", result.getString("year"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class OperationsResourcesTest {

@CsvSource({
"1, '"+MediaType.APPLICATION_JSON_VALUE+"', '{\"id\":\"1\"}'",
"1, '"+MediaType.APPLICATION_XML_VALUE+"', '<Operation><id>1</id><prefLabelLg1/><prefLabelLg2/><altLabelLg1/><altLabelLg2/><series/><idSims/><created/><modified/><validationState/></Operation>'",
"1, '"+MediaType.APPLICATION_XML_VALUE+"', '<Operation><id>1</id><prefLabelLg1/><prefLabelLg2/><altLabelLg1/><altLabelLg2/><series/><idSims/><created/><modified/><validationState/><year/></Operation>'",
})
@ParameterizedTest
void getOperationByID_resultFormatFitAccessHeader(String id, String mediaType, String expected) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,8 @@
"2008" ;
<http://rdf.insee.fr/def/base#validationState>
"Validated" ;
<http://rdf.insee.fr/def/base#year>
"2024" ;
<http://www.w3.org/2004/02/skos/core#altLabel>
"EPCV scheme 2008"@en , "EPCV 2008"@fr ;
<http://www.w3.org/2004/02/skos/core#prefLabel>
Expand Down

0 comments on commit feec0f6

Please sign in to comment.