Skip to content

Commit

Permalink
feat: geenralize exception handler for the operations module (#851)
Browse files Browse the repository at this point in the history
* feat: geenralize exception handler for the operations module

* fix: merge

* feat: add test and remove @MockBean

* feat: add unit test

* feat: finalize generic error handler

* fix: solve unit test

* fix: solve sonar issue
  • Loading branch information
EmmanuelDemey authored Jan 29, 2025
1 parent 4cc87d7 commit 43b350d
Show file tree
Hide file tree
Showing 79 changed files with 736 additions and 978 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@

public interface ClassificationsService {

public String getFamilies() throws RmesException;
String getFamilies() throws RmesException;

public String getFamily(String id) throws RmesException;
String getFamily(String id) throws RmesException;

public String getFamilyMembers(String id) throws RmesException;
String getFamilyMembers(String id) throws RmesException;

public String getSeries() throws RmesException;
String getSeries() throws RmesException;

public String getOneSeries(String id) throws RmesException;
String getOneSeries(String id) throws RmesException;

public String getSeriesMembers(String id) throws RmesException;
String getSeriesMembers(String id) throws RmesException;

public String getClassifications() throws RmesException;
String getClassifications() throws RmesException;

public String getClassification(String id) throws RmesException;
String getClassification(String id) throws RmesException;

public void updateClassification(String id, String body) throws RmesException;
void updateClassification(String id, String body) throws RmesException;

public String getClassificationLevels(String id) throws RmesException;
String getClassificationLevels(String id) throws RmesException;

public String getClassificationLevel(String classificationid, String levelId) throws RmesException;
String getClassificationLevel(String classificationid, String levelId) throws RmesException;

public String getClassificationLevelMembers(String classificationid, String levelId) throws RmesException;
String getClassificationLevelMembers(String classificationid, String levelId) throws RmesException;

public String getCorrespondences() throws RmesException;
String getCorrespondences() throws RmesException;

public String getCorrespondence(String id) throws RmesException;
String getCorrespondence(String id) throws RmesException;

public String getCorrespondenceAssociations(String id) throws RmesException;
String getCorrespondenceAssociations(String id) throws RmesException;

public String getCorrespondenceAssociation(String correspondenceId, String associationId) throws RmesException;
String getCorrespondenceAssociation(String correspondenceId, String associationId) throws RmesException;

public String setClassificationValidation(String id) throws RmesException;
String setClassificationValidation(String id) throws RmesException;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.insee.rmes.bauhaus_services;

import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.webservice.ConceptsCollectionsResources;
import fr.insee.rmes.webservice.concepts.ConceptsCollectionsResources;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;

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

import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.model.concepts.MembersLg;
import fr.insee.rmes.webservice.ConceptsCollectionsResources;
import fr.insee.rmes.webservice.concepts.ConceptsCollectionsResources;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public String setClassificationValidation(String classificationId) throws RmesEx
//GET graph
JSONObject listGraph = repoGestion.getResponseAsObject(ClassificationsQueries.getGraphUriById(classificationId));
logger.debug("JSON for listGraph id : {}", listGraph);
if (listGraph.length()==0) {throw new RmesNotFoundException(ErrorCodes.CLASSIFICATION_UNKNOWN_ID, "Classification not found", classificationId);}
if (listGraph.isEmpty()) {throw new RmesNotFoundException(ErrorCodes.CLASSIFICATION_UNKNOWN_ID, "Classification not found", classificationId);}
String graph = listGraph.getString("graph");
String classifUriString = listGraph.getString(Constants.URI);
Resource graphIri = RdfUtils.createIRI(graph);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public String getClassificationItem(String classificationId, String itemId) thro
logger.info("Starting to get classification item {} from {}", itemId, classificationId);
JSONObject item = repoGestion.getResponseAsObject(ItemsQueries.itemQuery(classificationId, itemId));
JSONArray altLabels = repoGestion.getResponseAsArray(ItemsQueries.itemAltQuery(classificationId, itemId));
if(altLabels.length() != 0) {
if(!altLabels.isEmpty()) {
item.put("altLabels", altLabels);
}
return item.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private JSONObject groupByBroaderNarrowerCloseMatchByCode(String notation) throw
}

private void addLinkCodeToItem(JSONObject item, String key, JSONObject broaderNarrowerCloseMatchForCode){
if(broaderNarrowerCloseMatchForCode.getJSONArray(key).length() > 0){
if(!broaderNarrowerCloseMatchForCode.getJSONArray(key).isEmpty()){
item.put(key, broaderNarrowerCloseMatchForCode.getJSONArray(key));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import fr.insee.rmes.persistance.sparql_queries.concepts.CollectionsQueries;
import fr.insee.rmes.utils.FilesUtils;
import fr.insee.rmes.utils.XMLUtils;
import fr.insee.rmes.webservice.ConceptsCollectionsResources;
import fr.insee.rmes.webservice.concepts.ConceptsCollectionsResources;
import jakarta.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import fr.insee.rmes.persistance.sparql_queries.concepts.ConceptsQueries;
import fr.insee.rmes.utils.FilesUtils;
import fr.insee.rmes.utils.XMLUtils;
import fr.insee.rmes.webservice.ConceptsCollectionsResources;
import fr.insee.rmes.webservice.concepts.ConceptsCollectionsResources;
import jakarta.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import fr.insee.rmes.utils.ExportUtils;
import fr.insee.rmes.utils.FilesUtils;
import fr.insee.rmes.utils.XsltUtils;
import fr.insee.rmes.webservice.ConceptsCollectionsResources;
import fr.insee.rmes.webservice.concepts.ConceptsCollectionsResources;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.http.HttpStatus;
import org.json.JSONArray;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import fr.insee.rmes.persistance.sparql_queries.concepts.ConceptsQueries;
import fr.insee.rmes.utils.FilesUtils;
import fr.insee.rmes.utils.JSONUtils;
import fr.insee.rmes.webservice.ConceptsCollectionsResources;
import fr.insee.rmes.webservice.concepts.ConceptsCollectionsResources;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.impl.LinkedHashModel;
Expand Down Expand Up @@ -86,10 +86,10 @@ public JSONObject getConceptById(String id) throws RmesException{
JSONObject concept = repoGestion.getResponseAsObject(ConceptsQueries.conceptQuery(id));
JSONArray altLabelLg1 = repoGestion.getResponseAsArray(ConceptsQueries.altLabel(id, config.getLg1()));
JSONArray altLabelLg2 = repoGestion.getResponseAsArray(ConceptsQueries.altLabel(id, config.getLg2()));
if(altLabelLg1.length() != 0) {
if(!altLabelLg1.isEmpty()) {
concept.put(Constants.ALT_LABEL_LG1, JSONUtils.extractFieldToArray(altLabelLg1, "altLabel"));
}
if(altLabelLg2.length() != 0) {
if(!altLabelLg2.isEmpty()) {
concept.put(Constants.ALT_LABEL_LG2, JSONUtils.extractFieldToArray(altLabelLg2, "altLabel"));
}
return concept;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ private String persist(Distribution distribution, boolean creation) throws RmesE
private void validate(Distribution distribution) throws RmesException {
if (distribution.getIdDataset() == null) {
throw new RmesBadRequestException("The property idDataset is required");

}
if (distribution.getLabelLg1() == null) {
throw new RmesBadRequestException("The property labelLg1 is required");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class GeographyServiceImpl extends RdfService implements GeographyService
public String getGeoFeatures() throws RmesException {
logger.info("Starting to get geo features");
JSONArray resQuery = repoGestion.getResponseAsArray(GeoQueries.getFeaturesQuery());
if (resQuery.length() != 0) {
if (!resQuery.isEmpty()) {
for (int i = resQuery.length() - 1; i >= 0; i--) {
JSONObject feature = resQuery.getJSONObject(i);
if (feature.has(HAS_COMPOSITION)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void createRdfDatableNote(String conceptId, DatableNote datableNote, Mode

public void deleteDatableNote(String conceptId, DatableNote datableNote, List<IRI> notesToDelete) throws RmesException{
JSONObject noteToDelete = repoGestion.getResponseAsObject(NotesQueries.getChangeNoteToDelete(conceptId, datableNote));
if (noteToDelete.length() != 0) {
if (!noteToDelete.isEmpty()) {
notesToDelete.add(RdfUtils.toURI(noteToDelete.getString("changeNoteURI")));
}
}
Expand All @@ -117,7 +117,7 @@ public String getLastVersion(Concept concept, VersionableNote note, String defau
} else {
JSONObject jsonVersion = repoGestion.getResponseAsObject(
NotesQueries.getLastVersionnableNoteVersion(concept.getId(), note.getPredicat()));
if (jsonVersion.length() == 0) {
if (jsonVersion.isEmpty()) {
return defaultVersion;
}
return jsonVersion.getString("version");
Expand All @@ -127,7 +127,7 @@ public String getLastVersion(Concept concept, VersionableNote note, String defau
public String getConceptVersion(Concept concept) throws RmesException{
String conceptVersion = ONE;
JSONObject jsonConceptVersion = repoGestion.getResponseAsObject(NotesQueries.getConceptVersion(concept.getId()));
if (jsonConceptVersion.length() == 0) {
if (jsonConceptVersion.isEmpty()) {
return conceptVersion;
}
conceptVersion = jsonConceptVersion.getString("conceptVersion");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ public void setSeries(String id, String body) throws RmesException {
@Override
public String getOperationsWithoutReport(String idSeries) throws RmesException {
JSONArray resQuery = repoGestion.getResponseAsArray(OperationsQueries.operationsWithoutSimsQuery(idSeries));
if (resQuery.length()==1 &&resQuery.getJSONObject(0).length()==0) {resQuery.remove(0);}
if (resQuery.length()==1 && resQuery.getJSONObject(0).isEmpty()) {resQuery.remove(0);}
return QueryUtils.correctEmptyGroupConcat(resQuery.toString());
}

@Override
public String getOperationsWithReport(String idSeries) throws RmesException {
JSONArray resQuery = repoGestion.getResponseAsArray(OperationsQueries.operationsWithSimsQuery(idSeries));
if (resQuery.length()==1 &&resQuery.getJSONObject(0).length()==0) {resQuery.remove(0);}
if (resQuery.length()==1 && resQuery.getJSONObject(0).isEmpty()) {resQuery.remove(0);}
return QueryUtils.correctEmptyGroupConcat(resQuery.toString());
}

Expand Down Expand Up @@ -217,7 +217,7 @@ public String setFamilyValidation(String id) throws RmesException{

public String getSeriesWithReport(String idFamily) throws RmesException {
JSONArray resQuery = repoGestion.getResponseAsArray(OperationsQueries.seriesWithSimsQuery(idFamily));
if (resQuery.length()==1 &&resQuery.getJSONObject(0).length()==0) {resQuery.remove(0);}
if (resQuery.length()==1 && resQuery.getJSONObject(0).isEmpty()) {resQuery.remove(0);}
return QueryUtils.correctEmptyGroupConcat(resQuery.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
public void getAllRubricsJson(String idSims, JSONObject jsonSims) throws RmesException {
JSONArray docRubrics = repoGestion
.getResponseAsArray(DocumentationsQueries.getDocumentationRubricsQuery(idSims, langService.getLanguage1(), langService.getLanguage2()));
if (docRubrics.length() != 0) {
if (!docRubrics.isEmpty()) {
clearRubrics(idSims, docRubrics);
jsonSims.put("rubrics", docRubrics);
}
Expand Down Expand Up @@ -116,7 +116,7 @@ else if (rubric.get(Constants.RANGE_TYPE).equals(RangeType.GEOGRAPHY.name())) {
clearGeographyRubric(rubric);
}
}
if (tempMultipleCodeList.size() != 0) {
if (!tempMultipleCodeList.isEmpty()) {
tempMultipleCodeList.forEach((k, v) -> docRubrics.put(v));
}
return docRubrics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public JSONObject getDocumentationByIdSims(String idSims) throws RmesException {

// Get general informations
JSONObject doc = repoGestion.getResponseAsObject(DocumentationsQueries.getDocumentationTitleQuery(idSims));
if (doc.length() == 0) {
if (doc.isEmpty()) {
throw new RmesNotFoundException(ErrorCodes.SIMS_UNKNOWN_ID, "Documentation not found", idSims);
}
doc.put(Constants.ID, idSims);
Expand Down Expand Up @@ -404,7 +404,7 @@ private String createSimsID() throws RmesException {
logger.info("Generate documentation id");
JSONObject json = repoGestion.getResponseAsObject(DocumentationsQueries.lastID());
logger.debug("JSON for documentation id : {}", json);
if (json.length() == 0) {
if (json.isEmpty()) {
return "1000";
}
String id = json.getString(Constants.ID_SIMS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class MetadataStructureDefUtils extends RdfService {

public JSONObject getMetadataAttributeById(String id) throws RmesException{
JSONObject mas = repoGestion.getResponseAsObject(DocumentationsQueries.getAttributeSpecificationQuery(id));
if (mas.length()==0) {throw new RmesException(HttpStatus.SC_BAD_REQUEST, "Attribute not found", "id doesn't exist"+id);}
if (mas.isEmpty()) {throw new RmesException(HttpStatus.SC_BAD_REQUEST, "Attribute not found", "id doesn't exist"+id);}
transformRangeType(mas);
mas.put(Constants.ID, id);
return mas;
Expand All @@ -48,7 +48,7 @@ public void transformRangeType(JSONObject mas) throws RmesException {

public JSONArray getMetadataAttributes() throws RmesException {
JSONArray attributesList = repoGestion.getResponseAsArray(DocumentationsQueries.getAttributesQuery());
if (attributesList.length() != 0) {
if (!attributesList.isEmpty()) {
for (int i = 0; i < attributesList.length(); i++) {
JSONObject attribute = attributesList.getJSONObject(i);
transformRangeType(attribute);
Expand All @@ -60,7 +60,7 @@ public JSONArray getMetadataAttributes() throws RmesException {
public Map<String,String> getMetadataAttributesUri() throws RmesException {
Map<String,String> attributes = new HashMap<>();
JSONArray attributesList = repoGestion.getResponseAsArray(DocumentationsQueries.getAttributesUriQuery());
if (attributesList.length() != 0) {
if (!attributesList.isEmpty()) {
for (int i = 0; i < attributesList.length(); i++) {
JSONObject attribute = attributesList.getJSONObject(i);
if (attribute.has(Constants.ID)&& attribute.has(Constants.URI)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public JSONArray getAllDocuments() throws RmesException {
}

private void formatDateInJsonArray(JSONArray allDocs) {
if (allDocs.length() != 0) {
if (!allDocs.isEmpty()) {
for (int i = 0; i < allDocs.length(); i++) {
JSONObject doc = allDocs.getJSONObject(i);
formatDateInJsonObject(doc);
Expand Down Expand Up @@ -177,7 +177,7 @@ protected String createDocumentID() throws RmesException {
}

public Integer getIdFromJson(JSONObject json) {
if (json.length() == 0) {
if (json.isEmpty()) {
return null;
} else {
String id = json.getString(Constants.ID);
Expand Down Expand Up @@ -279,7 +279,7 @@ private void checkDocumentDoesNotExist(String id, String url) throws RmesExcepti

private void checkUrlDoesNotExist(String id, String url, int errorCode, String errorMessage) throws RmesException {
JSONObject existingUriJson = repoGestion.getResponseAsObject(DocumentsQueries.getDocumentUriQuery(url));
if (existingUriJson.length() > 0) {
if (!existingUriJson.isEmpty()) {
String uri = existingUriJson.getString(Constants.DOCUMENT);
String existingId = getDocumentNameFromUrl(uri);
if (!existingId.equals(id)) {
Expand Down Expand Up @@ -382,7 +382,7 @@ public HttpStatus deleteDocument(String docId, boolean isLink) throws RmesExcept
// Check that the document is not referred to by any sims
private void checkDocumentReference(String docId, String uri) throws RmesException {
JSONArray jsonResultat = repoGestion.getResponseAsArray(DocumentsQueries.getLinksToDocumentQuery(docId));
if (jsonResultat.length() > 0) {
if (!jsonResultat.isEmpty()) {
throw new RmesBadRequestException(ErrorCodes.DOCUMENT_DELETION_LINKED,
"The document " + uri + "cannot be deleted because it is referred to by " + jsonResultat.length()
+ " sims, including: " + ((JSONObject) jsonResultat.get(0)).get(Constants.TEXT).toString(),
Expand All @@ -398,7 +398,7 @@ private void checkDocumentReference(String docId, String uri) throws RmesExcepti
*/
private void checkRightsToModifyFile(String docId) throws RmesException {
JSONArray sims = repoGestion.getResponseAsArray(DocumentsQueries.getLinksToDocumentQuery(docId));
if (sims.length() == 0) return; //document's file isn't linked to a sims
if (sims.isEmpty()) return; //document's file isn't linked to a sims
for (int i = 0; i < sims.length(); i++) {
String simsUri = ((JSONObject) sims.get(i)).get(Constants.TEXT).toString();

Expand Down Expand Up @@ -587,7 +587,7 @@ private String createFileUrl(String name) throws RmesException {
private IRI getDocumentUri(IRI url) throws RmesException {
JSONObject uri = repoGestion
.getResponseAsObject(DocumentsQueries.getDocumentUriQuery(getDocumentNameFromUrl(url.stringValue())));
if (uri.length() == 0 || !uri.has(Constants.DOCUMENT)) {
if (uri.isEmpty() || !uri.has(Constants.DOCUMENT)) {
String id = createDocumentID();
return RdfUtils.objectIRI(ObjectType.DOCUMENT, id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public String createId() throws RmesException {
logger.info("Generate famOpeSer id");
JSONObject json = repoGestion.getResponseAsObject(FamOpeSerQueries.lastId());
logger.debug("JSON for famOpeSer id : {}", json);
if (json.length()==0) {return "1000";}
if (json.isEmpty()) {return "1000";}
String id = json.getString(Constants.ID);
if (id.equals(Constants.UNDEFINED)) {return "1000";}
return "s" + (Integer.parseInt(id)+1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,15 @@ private void addLinks(String idIndic, JSONObject indicator) throws RmesException

private void addOneTypeOfLink(String id, JSONObject object, IRI predicate) throws RmesException {
JSONArray links = repositoryGestion.getResponseAsArray(IndicatorsQueries.indicatorLinks(id, predicate));
if (links.length() != 0) {
if (!links.isEmpty()) {
links = QueryUtils.transformRdfTypeInString(links);
object.put(predicate.getLocalName(), links);
}
}

private void addOneOrganizationLink(String id, JSONObject object, IRI predicate) throws RmesException {
JSONArray organizations = repositoryGestion.getResponseAsArray(IndicatorsQueries.getMultipleOrganizations(id, predicate));
if (organizations.length() != 0) {
if (!organizations.isEmpty()) {
for (int i = 0; i < organizations.length(); i++) {
JSONObject orga = organizations.getJSONObject(i);
orga.put("type", ObjectType.ORGANIZATION.labelType());
Expand Down Expand Up @@ -423,7 +423,7 @@ public String createID() throws RmesException {
logger.info("Generate indicator id");
JSONObject json = repositoryGestion.getResponseAsObject(IndicatorsQueries.lastID());
logger.debug("JSON for indicator id : {}" , json);
if (json.length()==0) {return null;}
if (json.isEmpty()) {return null;}
String id = json.getString(Constants.ID);
if (id.equals(Constants.UNDEFINED)) {return null;}
int idInt = Integer.parseInt(id.substring(1))+1;
Expand Down
Loading

0 comments on commit 43b350d

Please sign in to comment.