Skip to content

Commit

Permalink
feat: add unit to double check when we verify if a parent object is a…
Browse files Browse the repository at this point in the history
…lready published
  • Loading branch information
EmmanuelDemey committed Nov 28, 2024
1 parent e216b1e commit 406ca97
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ private String getDocumentationValidationStatus(String id) throws RmesException
* @throws RmesException
*/
public String publishMetadataReport(String id) throws RmesException {
Model model = new LinkedHashModel();
Resource graph = RdfUtils.simsGraph(id);

// Find target
String[] target = parentUtils.getDocumentationTargetTypeAndId(id);
Expand All @@ -233,6 +231,14 @@ public String publishMetadataReport(String id) throws RmesException {
throw new RmesNotFoundException(ErrorCodes.SIMS_UNKNOWN_TARGET, "target not found for this Sims", id);
}

/* Check if the target is already published - otherwise an unauthorizedException is thrown. */
String status = parentUtils.getValidationStatus(targetId);
if (PublicationUtils.isUnublished(status)) {
throw new RmesBadRequestException(ErrorCodes.OPERATION_VALIDATION_UNPUBLISHED_PARENT,
"This metadataReport cannot be published before its target is published. ",
"MetadataReport: " + id + " ; Indicator/Series/Operation: " + targetId);
}

switch(targetType) {
case Constants.OPERATION_UP : targetUri = RdfUtils.objectIRI(ObjectType.OPERATION, targetId); break;
case Constants.SERIES_UP : targetUri = RdfUtils.objectIRI(ObjectType.SERIES, targetId); break;
Expand All @@ -250,16 +256,11 @@ public String publishMetadataReport(String id) throws RmesException {
"Only an admin or a manager can create a new sims.");
}

/* Check if the target is already published - otherwise an unauthorizedException is thrown. */
String status = parentUtils.getValidationStatus(targetId);
if (PublicationUtils.isPublished(status)) {
throw new RmesBadRequestException(ErrorCodes.OPERATION_VALIDATION_UNPUBLISHED_PARENT,
"This metadataReport cannot be published before its target is published. ",
"MetadataReport: " + id + " ; Indicator/Series/Operation: " + targetId);
}

documentationPublication.publishSims(id);

Model model = new LinkedHashModel();
Resource graph = RdfUtils.simsGraph(id);
IRI simsURI = RdfUtils.objectIRI(ObjectType.DOCUMENTATION, id);
model.add(simsURI, INSEE.VALIDATION_STATE, RdfUtils.setLiteralString(ValidationStatus.VALIDATED), graph);
model.remove(simsURI, INSEE.VALIDATION_STATE, RdfUtils.setLiteralString(ValidationStatus.UNPUBLISHED), graph);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ public class OperationPublication extends RdfService{
String[] ignoredAttrs = { "validationState", "hasPart", Constants.PUBLISHER, Constants.CONTRIBUTOR };

public void publishOperation(String operationId, JSONObject operationJson) throws RmesException {
checkSeriesIsPublished(operationId, operationJson);

Model model = new LinkedHashModel();

Resource operation = RdfUtils.operationIRI(operationId);

checkSeriesIsPublished(operationId, operationJson);

RepositoryConnection con = repoGestion.getConnection();
RepositoryResult<Statement> statements = repoGestion.getStatements(con, operation);

Expand Down Expand Up @@ -78,7 +77,7 @@ private void checkSeriesIsPublished(String operationId, JSONObject operationJson
String seriesId = operationJson.getJSONObject("series").getString(Constants.ID);
String status = ownersUtils.getValidationStatus(seriesId);

if (PublicationUtils.isPublished(status)) {
if (PublicationUtils.isUnublished(status)) {
throw new RmesBadRequestException(ErrorCodes.OPERATION_VALIDATION_UNPUBLISHED_PARENT,
"This operation cannot be published before its series is published",
"Operation: " + operationId + " ; Series: " + seriesId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
import org.eclipse.rdf4j.repository.RepositoryResult;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class SeriesPublication extends RdfService {

final
@Autowired
ParentUtils ownersUtils;

public SeriesPublication(ParentUtils ownersUtils) {
Expand All @@ -41,7 +42,7 @@ public void publishSeries(String id, JSONObject series) throws RmesException {
String familyId = series.getJSONObject(Constants.FAMILY).getString(Constants.ID);
String status = ownersUtils.getValidationStatus(familyId);

if (PublicationUtils.isPublished(status)) {
if (PublicationUtils.isUnublished(status)) {
throw new RmesBadRequestException(
ErrorCodes.SERIES_VALIDATION_UNPUBLISHED_FAMILY,
"This Series cannot be published before its family is published",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static boolean stringEndsWithItemFromList(@NotNull String inputStr, @NotN
return Arrays.stream(items).parallel().anyMatch(requireNonNull(inputStr)::endsWith);
}

public static boolean isPublished(String status) {
public static boolean isUnublished(String status) {
return ValidationStatus.UNPUBLISHED.getValue().equals(status) || Constants.UNDEFINED.equals(status);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import fr.insee.rmes.bauhaus_services.Constants;
import fr.insee.rmes.bauhaus_services.operations.ParentUtils;
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.ValidationStatus;
import fr.insee.rmes.model.operations.documentations.Documentation;
import fr.insee.rmes.model.operations.documentations.DocumentationRubric;
import org.apache.http.HttpStatus;
import org.json.JSONException;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.*;

import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

Expand All @@ -28,43 +29,51 @@ class DocumentationsUtilsTest {

@Mock
protected ParentUtils mockParentUtils;

@Mock
private RepositoryGestion repoGestion;

@BeforeEach
public void init() {
MockitoAnnotations.openMocks(this);
}

@Test
void shouldThrowExceptionIfParentTargetIsUnpublished() throws RmesException {
JSONObject operation = new JSONObject();
operation.put(Constants.ID, "1");
JSONObject series = new JSONObject();
series.put("id", "2");
operation.put("series", series);

String[] target = {"series", "2"};
when(mockParentUtils.getDocumentationTargetTypeAndId("1")).thenReturn(target);
when(mockParentUtils.getValidationStatus("2")).thenReturn(ValidationStatus.UNPUBLISHED.toString());

assertThrows(
RmesBadRequestException.class,
() -> documentationsUtils.publishMetadataReport("1")
);
}

@Test
void buildDocumentationFromJsonTest() throws RmesException{

String[] st = new String[] { Constants.OPERATION_UP, "s8888" };

// Mocker les méthodes de buildDocumentationFromJson qui font appel à d'autres classes
when(mockDocumentationsRubricsUtils.buildRubricFromJson(Mockito.any(),Mockito.anyBoolean())).thenReturn(new DocumentationRubric());
when(mockParentUtils.getDocumentationTargetTypeAndId(anyString())).thenReturn(st);

String source="{\"rubrics\":[],\"idSeries\":\"\",\"labelLg2\":\"Metadata report 9999\",\"labelLg1\":\"Rapport de métadonnées 9999\",\"idOperation\":\"s8888\",\"idIndicator\":\"\",\"id\":\"9999\"}";
JSONObject jsonDocumentation;
try {
jsonDocumentation = new JSONObject(source);
} catch (JSONException e) {
throw new RmesException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(),
"JSONException");
}
Documentation sims = documentationsUtils.buildDocumentationFromJson(jsonDocumentation,true);
if (!sims.getId().equals("9999")) {
fail("false id");
}
if (!sims.getIdOperation().equals("s8888")) {
fail("false idOperation");
}
if (!sims.getLabelLg1().equals("Rapport de métadonnées 9999")) {
fail("false labelLg1");
}
if (!sims.getLabelLg2().equals("Metadata report 9999")) {
fail("false labelLg2");
}
JSONObject documentation = new JSONObject()
.put("rubrics", new JSONArray())
.put("idSeries", "")
.put("id", "9999")
.put("idOperation", "s8888")
.put("labelLg1", "Rapport de métadonnées 9999")
.put("labelLg2", "Metadata report 9999");

Documentation sims = documentationsUtils.buildDocumentationFromJson(documentation,true);

assertEquals("9999", sims.getId());
assertEquals("s8888", sims.getIdOperation());
assertEquals("Rapport de métadonnées 9999", sims.getLabelLg1());
assertEquals("Metadata report 9999", sims.getLabelLg2());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package fr.insee.rmes.bauhaus_services.operations.operations;

import fr.insee.rmes.bauhaus_services.Constants;
import fr.insee.rmes.bauhaus_services.operations.ParentUtils;
import fr.insee.rmes.exceptions.RmesBadRequestException;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.model.ValidationStatus;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class OperationPublicationTest {

@InjectMocks
OperationPublication operationPublication;

@Mock
ParentUtils ownerUtils;

@Test
void shouldThrowExceptionIfParentSeriesIsUnpublished() throws RmesException {
JSONObject operation = new JSONObject();
operation.put(Constants.ID, "1");
JSONObject series = new JSONObject();
series.put("id", "2");
operation.put("series", series);

when(ownerUtils.getValidationStatus("2")).thenReturn(ValidationStatus.UNPUBLISHED.toString());
assertThrows(
RmesBadRequestException.class,
() -> operationPublication.publishOperation("1", operation)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fr.insee.rmes.bauhaus_services.operations.series;

import fr.insee.rmes.bauhaus_services.Constants;
import fr.insee.rmes.bauhaus_services.operations.ParentUtils;
import fr.insee.rmes.exceptions.RmesBadRequestException;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.model.ValidationStatus;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class SeriesPublicationTest {
@InjectMocks
SeriesPublication seriesPublication;

@Mock
ParentUtils ownerUtils;

@Test
void shouldThrowExceptionIfParentFamilyIsUnpublished() throws RmesException {
JSONObject series = new JSONObject();
series.put(Constants.ID, "1");
JSONObject family = new JSONObject();
family.put("id", "2");
series.put("family", family);

when(ownerUtils.getValidationStatus("2")).thenReturn(ValidationStatus.UNPUBLISHED.toString());
assertThrows(
RmesBadRequestException.class,
() -> seriesPublication.publishSeries("1", series)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fr.insee.rmes.bauhaus_services.rdf_utils;

import fr.insee.rmes.bauhaus_services.Constants;
import fr.insee.rmes.model.ValidationStatus;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class PublicationUtilsTest {
@Test
void testIsUnpublished() {
String unpublishedStatus = ValidationStatus.UNPUBLISHED.getValue();
assertTrue(PublicationUtils.isUnublished(unpublishedStatus));

String undefinedStatus = Constants.UNDEFINED;
assertTrue(PublicationUtils.isUnublished(undefinedStatus));

String otherStatus = "OTHER_STATUS";
assertFalse(PublicationUtils.isUnublished(otherStatus));

assertFalse(PublicationUtils.isUnublished(null));
}
}

0 comments on commit 406ca97

Please sign in to comment.