Skip to content

Commit

Permalink
feat: rewrite testcontainer (#688)
Browse files Browse the repository at this point in the history
* feat: rewrite testcontainer

* refactor(Use RdfConnnectionDetails and remove useless Sring context for testcontainers)

* refactor(unused imports)

* fix: review testcontainer

---------

Co-authored-by: Fabrice Bibonne <[email protected]>
  • Loading branch information
EmmanuelDemey and Fabrice Bibonne authored Jul 10, 2024
1 parent 70e4165 commit 27d28cb
Show file tree
Hide file tree
Showing 32 changed files with 215 additions and 142 deletions.
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,11 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.17.5</version>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import fr.insee.rmes.exceptions.ErrorCodes;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.exceptions.RmesNotAcceptableException;
import fr.insee.rmes.external_services.export.XDocReport;
import fr.insee.rmes.external.services.export.XDocReport;
import fr.insee.rmes.model.operations.documentations.Documentation;
import fr.insee.rmes.model.operations.documentations.MSD;
import fr.insee.rmes.persistance.sparql_queries.operations.documentations.DocumentationsQueries;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import fr.insee.rmes.bauhaus_services.rdf_utils.RdfService;
import fr.insee.rmes.config.swagger.model.IdLabelTwoLangs;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.external_services.export.XDocReport;
import fr.insee.rmes.external.services.export.XDocReport;
import fr.insee.rmes.model.operations.Indicator;
import fr.insee.rmes.model.operations.Operation;
import fr.insee.rmes.model.operations.Series;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.insee.rmes.bauhaus_services.rdf_utils;

public interface RdfConnectionDetails {
String getUrlServer();

String repositoryId();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.insee.rmes.bauhaus_services.rdf_utils;

import fr.insee.rmes.config.Config;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.persistance.ontologies.EVOC;
import fr.insee.rmes.persistance.ontologies.INSEE;
Expand All @@ -17,6 +16,7 @@
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

Expand All @@ -31,26 +31,24 @@ public class RepositoryGestion {

private static final String FAILURE_LOAD_OBJECT = "Failure load object : {}";
private static final String FAILURE_REPLACE_GRAPH = "Failure replace graph : ";
private static final String FAILURE_DELETE_OBJECT = "Failure delete object";

static final Logger logger = LoggerFactory.getLogger(RepositoryGestion.class);

private final Config config;
private final RepositoryUtils repositoryUtils;
private final RdfConnectionDetails rdfGestion;

private final RepositoryUtils repositoryUtils;

public RepositoryGestion(Config config, RepositoryUtils repositoryUtils) {
public RepositoryGestion(@Qualifier("rdfGestionConnectionDetails") RdfConnectionDetails rdfGestion, RepositoryUtils repositoryUtils) {
this.rdfGestion = rdfGestion;
this.repositoryUtils = repositoryUtils;
this.config = config;
}
}

@PostConstruct
public void init(){
repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion());
repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId());
}


/**
* Method which aims to produce response from a sparql query
*
Expand All @@ -59,8 +57,8 @@ public void init(){
* @throws RmesException
*/
public String getResponse(String query) throws RmesException {
return repositoryUtils.getResponse(query, repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()));
return repositoryUtils.getResponse(query, repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()));
}

/**
Expand All @@ -71,8 +69,8 @@ public String getResponse(String query) throws RmesException {
* @throws RmesException
*/
public HttpStatus executeUpdate(String updateQuery) throws RmesException {
return repositoryUtils.executeUpdate(updateQuery, repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()));
return repositoryUtils.executeUpdate(updateQuery, repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()));
}

/**
Expand All @@ -83,18 +81,18 @@ public HttpStatus executeUpdate(String updateQuery) throws RmesException {
* @throws RmesException
*/
public JSONObject getResponseAsObject(String query) throws RmesException {
return repositoryUtils.getResponseAsObject(query, repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()));
return repositoryUtils.getResponseAsObject(query, repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()));
}

public JSONArray getResponseAsArray(String query) throws RmesException {
return repositoryUtils.getResponseAsArray(query, repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()));
return repositoryUtils.getResponseAsArray(query, repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()));
}

public JSONArray getResponseAsJSONList(String query) throws RmesException {
return repositoryUtils.getResponseAsJSONList(query, repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()));
return repositoryUtils.getResponseAsJSONList(query, repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()));
}

/**
Expand All @@ -106,16 +104,16 @@ public JSONArray getResponseAsJSONList(String query) throws RmesException {
* @throws JSONException
*/
public boolean getResponseAsBoolean(String query) throws RmesException {
return repositoryUtils.getResponseForAskQuery(query, repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()));
return repositoryUtils.getResponseForAskQuery(query, repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()));
}

public RepositoryResult<Statement> getStatements(RepositoryConnection con, Resource subject)
throws RmesException {
RepositoryResult<Statement> statements = null;
if (con == null) {
con = repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()).getConnection();
con = repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()).getConnection();
}

try {
Expand Down Expand Up @@ -146,8 +144,8 @@ public RepositoryResult<Statement> getStatementsPredicateObject(RepositoryConnec
throws RmesException {

if (con == null) {
con = repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()).getConnection();
con = repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()).getConnection();
}

RepositoryResult<Statement> statements = null;
Expand All @@ -160,15 +158,15 @@ public RepositoryResult<Statement> getStatementsPredicateObject(RepositoryConnec
}

public File getGraphAsFile(String context) throws RmesException {
if (context != null) return repositoryUtils.getCompleteGraphInTrig(repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()), context);
return repositoryUtils.getAllGraphsInZip(repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()));
if (context != null) return repositoryUtils.getCompleteGraphInTrig(repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()), context);
return repositoryUtils.getAllGraphsInZip(repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()));
}

public String[] getAllGraphs() throws RmesException {
return repositoryUtils.getAllGraphs(repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()));
return repositoryUtils.getAllGraphs(repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()));
}

public void closeStatements(RepositoryResult<Statement> statements) throws RmesException {
Expand All @@ -181,8 +179,8 @@ public void closeStatements(RepositoryResult<Statement> statements) throws RmesE

public void loadConcept(IRI concept, Model model, List<List<IRI>> notesToDeleteAndUpdate)
throws RmesException {
try (RepositoryConnection conn = repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()).getConnection() ){
try (RepositoryConnection conn = repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()).getConnection() ){
// notes to delete
for (IRI note : notesToDeleteAndUpdate.get(0)) {
conn.remove(note, null, null);
Expand All @@ -207,8 +205,8 @@ public void deleteTripletByPredicateAndValue(Resource object, IRI predicate, Res

public void deleteTripletByPredicateAndValue(Resource object, IRI predicate, Resource graph, Value value) throws RmesException {
try {
RepositoryConnection conn = repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()).getConnection();
RepositoryConnection conn = repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()).getConnection();

conn.remove(object, predicate, value, graph);
conn.close();
Expand Down Expand Up @@ -276,8 +274,8 @@ public void replaceGraph(Resource graph, Model model, RepositoryConnection conn)
private void processConnection(Consumer<RepositoryConnection> processor, RepositoryConnection initialConnection, String message)throws RmesException{
try {
if (initialConnection == null) {
initialConnection = repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()).getConnection();
initialConnection = repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()).getConnection();
}
processor.accept(initialConnection);
initialConnection.close();
Expand All @@ -288,15 +286,15 @@ private void processConnection(Consumer<RepositoryConnection> processor, Reposit


public HttpStatus persistFile(InputStream input, RDFFormat format, String graph) throws RmesException {
return repositoryUtils.persistFile(input, format, graph, repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()), null);
return repositoryUtils.persistFile(input, format, graph, repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()), null);
}

public void loadObjectWithReplaceLinks(IRI object, Model model) throws RmesException {


try (RepositoryConnection conn=repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()).getConnection();)
try (RepositoryConnection conn=repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()).getConnection();)
{
clearReplaceLinks(object);
loadSimpleObject(object, model, conn);
Expand All @@ -308,8 +306,8 @@ public void loadObjectWithReplaceLinks(IRI object, Model model) throws RmesExcep
public void objectsValidation(List<IRI> collectionsToValidateList, Model model) throws RmesException {

try {
RepositoryConnection conn=repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()).getConnection();
RepositoryConnection conn=repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()).getConnection();
for (IRI item : collectionsToValidateList) {
conn.remove(item, INSEE.VALIDATION_STATE, null);
conn.remove(item, INSEE.IS_VALIDATED, null);
Expand All @@ -324,8 +322,8 @@ public void objectsValidation(List<IRI> collectionsToValidateList, Model model)
public void objectValidation(IRI ressourceURI, Model model) throws RmesException {

try {
RepositoryConnection conn=repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()).getConnection();
RepositoryConnection conn=repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()).getConnection();
conn.remove(ressourceURI, INSEE.VALIDATION_STATE, null);
conn.remove(ressourceURI, INSEE.IS_VALIDATED, null);
conn.add(model);
Expand All @@ -348,8 +346,8 @@ private void clearReplaceLinks(Resource object) throws RmesException {
private void getStatementsAndRemove(Resource object, List<IRI> typeOfLink)
throws RmesException {
String exceptionCirucmstances = "";
try(RepositoryConnection conn = repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()).getConnection()){
try(RepositoryConnection conn = repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()).getConnection()){
for (IRI predicat : typeOfLink) {
RepositoryResult<Statement> statements = null;
exceptionCirucmstances="get " + predicat + " links from " + object;
Expand All @@ -365,17 +363,17 @@ private void getStatementsAndRemove(Resource object, List<IRI> typeOfLink)
}

public void clearStructureNodeAndComponents(Resource structure) throws RmesException {
repositoryUtils.clearStructureAndComponents(structure, repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()));
repositoryUtils.clearStructureAndComponents(structure, repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()));
}

public void keepHierarchicalOperationLinks(Resource object, Model model) throws RmesException {
getHierarchicalOperationLinksModel(object, model, List.of(DCTERMS.HAS_PART, DCTERMS.IS_PART_OF));
}

private void getHierarchicalOperationLinksModel(Resource object, Model model, List<IRI> typeOfLink) throws RmesException {
RepositoryConnection conn = repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()).getConnection();
RepositoryConnection conn = repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()).getConnection();
for (IRI predicat : typeOfLink) {
RepositoryResult<Statement> statements;
try {
Expand Down Expand Up @@ -411,17 +409,17 @@ public void loadSimpleObject(IRI geoIRI, Model model) throws RmesException {
}

public RepositoryConnection getConnection() throws RmesException {
return repositoryUtils.getConnection(repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()));
return repositoryUtils.getConnection(repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()));
}


public void overrideTriplets(IRI simsUri, Model model, Resource graph) throws RmesException {


try {
RepositoryConnection connection =repositoryUtils.initRepository(config.getRdfServerGestion(),
config.getRepositoryIdGestion()).getConnection();
RepositoryConnection connection =repositoryUtils.initRepository(rdfGestion.getUrlServer(),
rdfGestion.repositoryId()).getConnection();
model.predicates().forEach(predicate -> connection.remove(simsUri, predicate, null, graph));
connection.add(model);
connection.close();
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/fr/insee/rmes/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ public class Config {
/******************************************************/
/** DATABASES ***********************************/
/******************************************************/
@Value("${fr.insee.rmes.bauhaus.sesame.gestion.sesameServer}")
private String rdfServerGestion;
@Value("${fr.insee.rmes.bauhaus.sesame.gestion.repository}")
private String idRepositoryGestion;
@Value("${fr.insee.rmes.bauhaus.sesame.gestion.baseURI}")
private String baseUriGestion;

Expand Down Expand Up @@ -151,14 +147,6 @@ public String getBaseGraph() {
return baseGraph;
}

public String getRdfServerGestion() {
return rdfServerGestion;
}

public String getRepositoryIdGestion() {
return idRepositoryGestion;
}

public String getBaseUriGestion() {
return baseUriGestion;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.insee.rmes.external_services.authentication.stamps;
package fr.insee.rmes.external.services.authentication.stamps;

import fr.insee.rmes.config.auth.security.UserDecoder;
import fr.insee.rmes.config.auth.user.Stamp;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.insee.rmes.external_services.authentication.stamps;
package fr.insee.rmes.external.services.authentication.stamps;

import fr.insee.rmes.config.auth.user.Stamp;
import fr.insee.rmes.exceptions.RmesException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.insee.rmes.external_services.export;
package fr.insee.rmes.external.services.export;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.insee.rmes.external_services.export;
package fr.insee.rmes.external.services.export;

import fr.insee.rmes.bauhaus_services.operations.operations.VarBookExportBuilder;
import fr.insee.rmes.exceptions.RmesException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.insee.rmes.external_services.rbac;
package fr.insee.rmes.external.services.rbac;

import fr.insee.rmes.model.rbac.RBAC;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.insee.rmes.external_services.rbac;
package fr.insee.rmes.external.services.rbac;

import fr.insee.rmes.config.auth.RBACConfiguration;
import fr.insee.rmes.model.rbac.RBAC;
Expand Down
Loading

0 comments on commit 27d28cb

Please sign in to comment.