Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : change id generator for distribution and dataset #659

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public class DatasetServiceImpl extends RdfService implements DatasetService {
public static final String CATALOG_RECORD_UPDATED = "catalogRecordUpdated";
public static final String CREATOR = "creator";


@Autowired
SeriesUtils seriesUtils;

Expand Down Expand Up @@ -214,7 +213,7 @@ public String update(String datasetId, String body) throws RmesException {
@Override
public String create(String body) throws RmesException {
Dataset dataset = Deserializer.deserializeBody(body, Dataset.class);
dataset.setId(IdGenerator.generateNextId(repoGestion.getResponseAsObject(DatasetQueries.lastDatasetId(getDatasetsGraph())), "jd"));
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
dataset.setId(idGenerator.generateNextId());
dataset.setValidationState(ValidationStatus.UNPUBLISHED.toString());

if(dataset.getIdSerie() != null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
@Service
public class DistributionServiceImpl extends RdfService implements DistributionService {


@Value("${fr.insee.rmes.bauhaus.datasets.graph}")
private String datasetsGraphSuffix;

Expand Down Expand Up @@ -77,10 +78,8 @@ public String getDistributionByID(String id) throws RmesException {
@Override
public String create(String body) throws RmesException {
Distribution distribution = Deserializer.deserializeBody(body, Distribution.class);
String idnewt = IdGenerator.generateNextId(repoGestion.getResponseAsObject(DistributionQueries.lastDatasetId(getDistributionGraph())), "d");
distribution.setId(idnewt);
distribution.setId(idGenerator.generateNextId());
distribution.setValidationState(ValidationStatus.UNPUBLISHED.toString());
FBibonne marked this conversation as resolved.
Show resolved Hide resolved

this.validate(distribution);

distribution.setCreated(DateUtils.getCurrentDate());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fr.insee.rmes.config.Config;
import fr.insee.rmes.config.auth.security.restrictions.StampsRestrictionsService;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.utils.IdGenerator;
import org.eclipse.rdf4j.model.*;
import org.json.JSONArray;
import org.json.JSONObject;
Expand All @@ -17,6 +18,9 @@ public abstract class RdfService {
@Autowired
protected RepositoryGestion repoGestion;

@Autowired
protected IdGenerator idGenerator;

@Autowired
protected RepositoryPublication repositoryPublication;

Expand Down
21 changes: 9 additions & 12 deletions src/main/java/fr/insee/rmes/utils/IdGenerator.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package fr.insee.rmes.utils;

import fr.insee.rmes.bauhaus_services.Constants;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

public class IdGenerator {
public static String generateNextId(JSONObject json, String prefix) {
if (json.isEmpty()) {
return prefix + "1000";
}
String id = json.getString(Constants.ID);
if (Constants.UNDEFINED.equals(id)) {
return prefix + "1000";
}
return prefix + (Integer.parseInt(id) + 1);
import java.util.UUID;

FBibonne marked this conversation as resolved.
Show resolved Hide resolved
@Component
public record IdGenerator() {
public String generateNextId() {
UUID uuid = UUID.randomUUID();
return uuid.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import fr.insee.rmes.exceptions.RmesNotFoundException;
import fr.insee.rmes.model.dataset.PatchDataset;
import fr.insee.rmes.utils.DateUtils;
import fr.insee.rmes.utils.IdGenerator;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
Expand Down Expand Up @@ -52,6 +53,8 @@ class DatasetServiceImplTest {

@MockBean
SeriesUtils seriesUtils;
@MockBean
IdGenerator idGenerator;

@MockBean
PublicationUtils publicationUtils;
Expand Down Expand Up @@ -307,33 +310,9 @@ void shouldReturnAnErrorIfUnknownSeriesNotDefinedWhenCreatingEvenIfAltIdentifier

@Test
void shouldPersistNewDatasetWithAndIncrementedId() throws RmesException {
when(repositoryGestion.getResponseAsObject(anyString())).then(invocationOnMock -> {
JSONObject lastId = new JSONObject();
lastId.put("id", "1000");
return lastId;
});
createANewDataset("jd1001");
}

@Test
void shouldPersistNewDatasetWithTheDefaultId() throws RmesException {
when(repositoryGestion.getResponseAsObject(anyString())).then(invocationOnMock -> {
JSONObject lastId = new JSONObject();
return lastId;
});
createANewDataset("jd1000");
}

@Test
void shouldPersistNewDatasetWithTheDefaultIdIfUndefined() throws RmesException {
when(repositoryGestion.getResponseAsObject(anyString())).then(invocationOnMock -> {
JSONObject lastId = new JSONObject();
lastId.put("id", "undefined");
return lastId;
});
createANewDataset("jd1000");
}

@Test
void shouldPatchDataset() throws RmesException {
IRI iri = SimpleValueFactory.getInstance().createIRI("http://datasetIRI/jd1001");
Expand Down Expand Up @@ -419,13 +398,14 @@ private JSONObject generateCatalogRecord() {
}

private void createANewDataset(String nextId) throws RmesException {
IRI iri = SimpleValueFactory.getInstance().createIRI("http://datasetIRI/" + nextId);
IRI catalogRecordIri = SimpleValueFactory.getInstance().createIRI("http://recordIRI/" + nextId);
try (
MockedStatic<DatasetQueries> datasetQueriesMock = Mockito.mockStatic(DatasetQueries.class);
MockedStatic<RdfUtils> rdfUtilsMock = Mockito.mockStatic(RdfUtils.class);
MockedStatic<DateUtils> dateUtilsMock = Mockito.mockStatic(DateUtils.class)
MockedStatic<DateUtils> dateUtilsMock = Mockito.mockStatic(DateUtils.class);
) {
when(idGenerator.generateNextId()).thenReturn(nextId);
IRI iri = SimpleValueFactory.getInstance().createIRI("http://datasetIRI/" + nextId);
IRI catalogRecordIri = SimpleValueFactory.getInstance().createIRI("http://recordIRI/" + nextId);

rdfUtilsMock.when(() -> RdfUtils.createIRI(any())).thenCallRealMethod();
rdfUtilsMock.when(() -> RdfUtils.toURI(anyString())).thenCallRealMethod();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import fr.insee.rmes.exceptions.RmesNotFoundException;
import fr.insee.rmes.model.dataset.PatchDistribution;
import fr.insee.rmes.utils.DateUtils;
import fr.insee.rmes.utils.IdGenerator;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
Expand All @@ -17,6 +18,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -48,13 +50,16 @@ class DistributionServiceImplTest {

@MockBean
PublicationUtils publicationUtils;
@MockBean
IdGenerator idGenerator;
public static final String EMPTY_JSON_OBJECT = "{}";
@Autowired
DistributionServiceImpl distributionService;
public static final String DISTRIB = "{\"id\":\"d1000\"}";
public static final String DISTRIB_A_PATCHER = "{\"byteSize\":\"3\",\"labelLg2\":\"test_patch\",\"labelLg1\":\"test_patch\",\"created\":\"2024-04-10T16:34:09.651166561\",\"idDataset\":\"jd1004\",\"id\":\"d1004\",\"updated\":\"2024-04-07T16:34:09.651166561\",\"url\":\"http://test\"}";
public static final String DISTRIB_PATCHEE = "[(http://distributionIRI/jd1004, http://www.w3.org/ns/dcat#distribution, http://distributionIRI/d1004, http://datasetGraph/) [http://datasetGraph/], (http://distributionIRI/d1004, http://purl.org/dc/terms/identifier, \"d1004\", http://datasetGraph/) [http://datasetGraph/], (http://distributionIRI/d1004, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/ns/dcat#Distribution, http://datasetGraph/) [http://datasetGraph/], (http://distributionIRI/d1004, http://purl.org/dc/terms/title, \"test_patch\"@fr, http://datasetGraph/) [http://datasetGraph/], (http://distributionIRI/d1004, http://purl.org/dc/terms/title, \"test_patch\"@en, http://datasetGraph/) [http://datasetGraph/], (http://distributionIRI/d1004, http://purl.org/dc/terms/created, \"2024-04-10T16:34:09.651166561\"^^<http://www.w3.org/2001/XMLSchema#dateTime>, http://datasetGraph/) [http://datasetGraph/], (http://distributionIRI/d1004, http://purl.org/dc/terms/modified, \"2024-04-05T16:34:09.651166561\"^^<http://www.w3.org/2001/XMLSchema#dateTime>, http://datasetGraph/) [http://datasetGraph/], (http://distributionIRI/d1004, http://www.w3.org/ns/dcat#byteSize, \"5\", http://datasetGraph/) [http://datasetGraph/], (http://distributionIRI/d1004, http://www.w3.org/ns/dcat#downloadURL, \"http://test2\", http://datasetGraph/) [http://datasetGraph/]]";


@Test
void shouldReturnDistributions() throws RmesException {
JSONArray array = new JSONArray();
Expand Down Expand Up @@ -133,32 +138,8 @@ void shouldReturnAnErrorIfLabelLg2NotDefinedWhenCreating() throws RmesException,
}
}

@Test
void shouldPersistNewDistributionWithAndIncrementedId() throws RmesException {
when(repositoryGestion.getResponseAsObject(anyString())).then(invocationOnMock -> {
JSONObject lastId = new JSONObject();
lastId.put("id", "1000");
return lastId;
});
createANewDistribution("d1001");
}

@Test
void shouldPersistNewDistributionWithTheDefaultId() throws RmesException {
when(repositoryGestion.getResponseAsObject(anyString())).then(invocationOnMock -> {
JSONObject lastId = new JSONObject();
return lastId;
});
createANewDistribution("d1000");
}

@Test
void shouldPersistNewDistributionWithTheDefaultIdIfUndefined() throws RmesException {
when(repositoryGestion.getResponseAsObject(anyString())).then(invocationOnMock -> {
JSONObject lastId = new JSONObject();
lastId.put("id", "undefined");
return lastId;
});
createANewDistribution("d1000");
}

Expand All @@ -167,8 +148,9 @@ private void createANewDistribution(String nextId) throws RmesException {
try (
MockedStatic<DistributionQueries> datasetQueriesMock = Mockito.mockStatic(DistributionQueries.class);
MockedStatic<RdfUtils> rdfUtilsMock = Mockito.mockStatic(RdfUtils.class);
MockedStatic<DateUtils> dateUtilsMock = Mockito.mockStatic(DateUtils.class)
MockedStatic<DateUtils> dateUtilsMock = Mockito.mockStatic(DateUtils.class);
) {
when(idGenerator.generateNextId()).thenReturn(nextId);
IRI iri = SimpleValueFactory.getInstance().createIRI("http://distributionIRI/" + nextId);

rdfUtilsMock.when(() -> RdfUtils.createIRI(any())).thenCallRealMethod();
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/fr/insee/rmes/utils/IdGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package fr.insee.rmes.utils;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class IdGeneratorTest {

@Test
void shouldGenerateIdOfVersion4() {
IdGenerator idGenerator = new IdGenerator();
String uuid = idGenerator.generateNextId();
// la version d'un UUID est donnée par le 13eme caractere de l'uuid (sans compter les "-")
String cleanUUID = uuid.replace("-", "");
Character version = cleanUUID.charAt(12);
assertEquals('4',version);
}
}
Loading