Skip to content

Commit

Permalink
feat: migrate query to Freemarker (#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey authored Dec 12, 2024
1 parent 053a023 commit d04513a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,13 @@ public static String familyQuery(String id, boolean familiesRichTextNextStructur
return buildRequest("getFamily.ftlh", params);
}

public static String getSeries(String idFamily) {
return "SELECT ?id ?labelLg1 ?labelLg2 \n"
+ " FROM <"+config.getOperationsGraph()+"> \n"
+ "WHERE { \n"

+ "?family dcterms:hasPart ?uri . \n"
+ "?uri skos:prefLabel ?labelLg1 . \n"
+ "FILTER (lang(?labelLg1) = '" + config.getLg1() + "') . \n"
+ "?uri skos:prefLabel ?labelLg2 . \n"
+ "FILTER (lang(?labelLg2) = '" + config.getLg2() + "') . \n"
+ "BIND(STRAFTER(STR(?uri),'/operations/serie/') AS ?id) . \n"


+ "FILTER(STRENDS(STR(?family),'/operations/famille/" + idFamily + "')) . \n"
+ "}"
+ " ORDER BY ?id"
;
public static String getSeries(String idFamily) throws RmesException {
HashMap<String, Object> params = new HashMap<>();
params.put(OPERATIONS_GRAPH, config.getOperationsGraph());
params.put("LG1", config.getLg1());
params.put("LG2", config.getLg2());
params.put("ID", idFamily);
return buildRequest("getSeries.ftlh", params);
}

public static String getSubjects(String idFamily) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/request/operations/famOpeSer/getSeries.ftlh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SELECT ?id ?labelLg1 ?labelLg2
FROM <${OPERATIONS_GRAPH}>
WHERE {
?family dcterms:hasPart ?uri .
?uri skos:prefLabel ?labelLg1 .
FILTER (lang(?labelLg1) = '${LG1}') .
?uri skos:prefLabel ?labelLg2 .
FILTER (lang(?labelLg2) = '${LG2}') .
BIND(STRAFTER(STR(?uri),'/operations/serie/') AS ?id) .
FILTER(STRENDS(STR(?family),'/operations/famille/${ID}')) .
}
ORDER BY ?id
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package fr.insee.rmes.testcontainers.queries.sparql_queries.operations.families;

import fr.insee.rmes.bauhaus_services.rdf_utils.RepositoryGestion;
import fr.insee.rmes.bauhaus_services.rdf_utils.RepositoryInitiator;
import fr.insee.rmes.bauhaus_services.rdf_utils.RepositoryUtils;
import fr.insee.rmes.config.ConfigStub;
import fr.insee.rmes.persistance.sparql_queries.operations.families.OpFamiliesQueries;
import fr.insee.rmes.persistance.sparql_queries.operations.operations.OperationsQueries;
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;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class OpFamiliesQueriesTest extends WithGraphDBContainer {
RepositoryGestion repositoryGestion = new RepositoryGestion(getRdfGestionConnectionDetails(), new RepositoryUtils(null, RepositoryInitiator.Type.DISABLED));

@BeforeAll
static void initData(){
container.withTrigFiles("all-operations-and-indicators.trig");
}

@Test
void should_return_all_families() throws Exception {
OpSeriesQueries.setConfig(new ConfigStub());
JSONArray result = repositoryGestion.getResponseAsArray(OpFamiliesQueries.familiesQuery());
assertEquals(56, result.length());

for (var i = 0; i < result.length(); i++){
assertNotNull(result.getJSONObject(i).getString("id"));
assertNotNull(result.getJSONObject(i).getString("label"));
}
}

@Test
void should_return_series() throws Exception {
OpSeriesQueries.setConfig(new ConfigStub());
JSONArray result = repositoryGestion.getResponseAsArray(OpFamiliesQueries.getSeries("s79"));
assertEquals(3, result.length());

assertEquals("s1178", result.getJSONObject(0).getString("id"));
assertEquals("s1266", result.getJSONObject(1).getString("id"));
assertEquals("s1279", result.getJSONObject(2).getString("id"));
}
}

0 comments on commit d04513a

Please sign in to comment.