Skip to content

Commit

Permalink
feat: do not publish the lastCodeUriSegment triplet (#793)
Browse files Browse the repository at this point in the history
* feat: do not publish the lastCodeUriSegment triplet

* feat: add unit test

* feat: add unit test

* feat: add unit test
  • Loading branch information
EmmanuelDemey authored Nov 28, 2024
1 parent b331bb4 commit 58d5ab7
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ private boolean shouldExcludeTriplet(Statement statement){
String pred = RdfUtils.toString(statement.getPredicate());
return pred.endsWith("validationState")
|| pred.endsWith(Constants.CREATOR)
|| pred.endsWith(Constants.CONTRIBUTOR);
|| pred.endsWith(Constants.CONTRIBUTOR)
|| pred.endsWith("lastCodeUriSegment");
}


private void publishCodeListAndCodesWithConnection(Resource codeListOrCode, RepositoryConnection connection) throws RmesException {
RepositoryResult<Statement> statements = repoGestion.getStatements(connection, codeListOrCode);

try {

checkIfResourceExists(statements, codeListOrCode);

Model model = new LinkedHashModel();
Expand Down Expand Up @@ -80,7 +80,6 @@ private void publishCodeListAndCodesWithConnection(Resource codeListOrCode, Repo
throw new RuntimeException(e);
} finally {
repoGestion.closeStatements(statements);

}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package fr.insee.rmes.bauhaus_services.code_list;

import fr.insee.rmes.bauhaus_services.Constants;
import fr.insee.rmes.bauhaus_services.rdf_utils.PublicationUtils;
import fr.insee.rmes.bauhaus_services.rdf_utils.RepositoryGestion;
import fr.insee.rmes.bauhaus_services.rdf_utils.RepositoryPublication;
import fr.insee.rmes.exceptions.RmesException;
import org.eclipse.rdf4j.common.iteration.CloseableIteratorIteration;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.model.vocabulary.SKOS;
import org.eclipse.rdf4j.repository.RepositoryResult;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class CodeListPublicationTest {

@InjectMocks
CodeListPublication codeListPublication;

@Mock
RepositoryGestion repositoryGestion;

@Mock
RepositoryPublication repositoryPublication;

@Mock
PublicationUtils publicationUtils;

@Test
void shouldThrowExceptionIfNoStatements() throws RmesException {
List<Statement> fakeStatements = Collections.emptyList();

IRI resource = SimpleValueFactory.getInstance().createIRI("http://codes-list/1");
RepositoryResult<Statement> fakeRepositoryResult =
new RepositoryResult<>(new CloseableIteratorIteration<>(fakeStatements.iterator()));


when(repositoryGestion.getConnection()).thenReturn(null);
when(repositoryGestion.getStatements(any(), eq(resource))).thenReturn(new RepositoryResult<>(fakeRepositoryResult));

Assertions.assertThrows(RuntimeException.class, () -> codeListPublication.publishCodeListAndCodes(resource));
}
@Test
void shouldNotPublishExcludedTriplets() throws RmesException {
SimpleValueFactory valueFactory = SimpleValueFactory.getInstance();
IRI resource = valueFactory.createIRI("http://codes-list/1");

IRI subject = valueFactory.createIRI("http://codes-list/1");

Statement stmt1 = valueFactory.createStatement(
subject,
valueFactory.createIRI("http://example.org/predicate1"),
valueFactory.createLiteral("Object 1"),
valueFactory.createIRI("http://example.org/context")
);

Statement creator = valueFactory.createStatement(
subject,
valueFactory.createIRI("http://purl.org/dc/elements/1.1/creator"),
valueFactory.createLiteral("Object 2"),
valueFactory.createIRI("http://example.org/context")
);

Statement contributor = valueFactory.createStatement(
subject,
valueFactory.createIRI("http://purl.org/dc/elements/1.1/contributor"),
valueFactory.createLiteral("Object 2"),
valueFactory.createIRI("http://example.org/context")
);

Statement validationState = valueFactory.createStatement(
subject,
valueFactory.createIRI("http://rdf.insee.fr/def/base#validationState"),
valueFactory.createLiteral("Object 3"),
valueFactory.createIRI("http://example.org/context")
);

Statement lastCodeUriSegment = valueFactory.createStatement(
subject,
valueFactory.createIRI("http://rdf.insee.fr/def/base#lastCodeUriSegment"),
valueFactory.createLiteral("Object 3"),
valueFactory.createIRI("http://example.org/context")
);

List<Statement> fakeStatements = Arrays.asList(stmt1, creator, contributor, validationState, lastCodeUriSegment);
List<Statement> codeStatement = Collections.emptyList();


RepositoryResult<Statement> fakeRepositoryResult =
new RepositoryResult<>(new CloseableIteratorIteration<>(fakeStatements.iterator()));


when(publicationUtils.tranformBaseURIToPublish(eq(subject))).thenReturn(subject);
when(repositoryGestion.getConnection()).thenReturn(null);
when(repositoryGestion.getStatementsPredicateObject(any(), eq(SKOS.IN_SCHEME), any())).thenReturn(new RepositoryResult<>(new CloseableIteratorIteration<>(codeStatement.iterator())));
when(repositoryGestion.getStatements(any(), eq(resource))).thenReturn(new RepositoryResult<>(fakeRepositoryResult));

codeListPublication.publishCodeListAndCodes(resource);

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

verify(repositoryPublication).publishResource(any(), model.capture(), eq(Constants.CODELIST));
Assertions.assertEquals("[(http://codes-list/1, http://example.org/predicate1, \"Object 1\", http://example.org/context) [http://example.org/context]]", model.getValue().toString());
verify(repositoryGestion).closeStatements(any());
}

}

0 comments on commit 58d5ab7

Please sign in to comment.