2020-04-27 14:52:31 +02:00
|
|
|
|
2020-04-10 17:53:07 +02:00
|
|
|
package eu.dnetlib.dhp.oa.graph.raw;
|
2020-03-25 09:43:58 +01:00
|
|
|
|
2022-07-28 11:27:08 +02:00
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
2020-05-29 12:03:51 +02:00
|
|
|
import static org.mockito.ArgumentMatchers.anyString;
|
|
|
|
import static org.mockito.Mockito.lenient;
|
2020-04-18 12:42:58 +02:00
|
|
|
|
2020-03-25 09:43:58 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.sql.Array;
|
|
|
|
import java.sql.Date;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
2022-04-29 12:25:24 +02:00
|
|
|
import java.util.HashSet;
|
2020-03-25 09:43:58 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
2022-04-29 12:25:24 +02:00
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.stream.Collectors;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
2020-04-18 12:42:58 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2020-04-20 18:53:06 +02:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2020-04-18 12:42:58 +02:00
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
|
import org.mockito.Mock;
|
|
|
|
import org.mockito.Mockito;
|
|
|
|
import org.mockito.junit.jupiter.MockitoExtension;
|
2020-03-25 09:43:58 +01:00
|
|
|
|
2020-04-28 11:23:29 +02:00
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
2021-01-25 15:43:04 +01:00
|
|
|
import eu.dnetlib.dhp.common.vocabulary.VocabularyGroup;
|
2022-07-28 11:27:08 +02:00
|
|
|
import eu.dnetlib.dhp.schema.common.ModelConstants;
|
2022-04-29 12:25:24 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.*;
|
2021-04-27 15:44:01 +02:00
|
|
|
import eu.dnetlib.dhp.schema.oaf.utils.OafMapperUtils;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
2020-03-25 17:59:45 +01:00
|
|
|
@ExtendWith(MockitoExtension.class)
|
2022-07-28 11:27:08 +02:00
|
|
|
class MigrateDbEntitiesApplicationTest {
|
2020-03-25 09:43:58 +01:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
private MigrateDbEntitiesApplication app;
|
|
|
|
|
2020-12-18 15:38:59 +01:00
|
|
|
@Mock(lenient = true)
|
2020-04-27 14:52:31 +02:00
|
|
|
private ResultSet rs;
|
|
|
|
|
2020-05-29 12:03:51 +02:00
|
|
|
@Mock
|
|
|
|
private VocabularyGroup vocs;
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
@BeforeEach
|
|
|
|
public void setUp() {
|
2020-05-29 12:03:51 +02:00
|
|
|
lenient()
|
|
|
|
.when(vocs.getTermAsQualifier(anyString(), anyString()))
|
2021-08-23 14:33:39 +02:00
|
|
|
.thenAnswer(
|
|
|
|
invocation -> OafMapperUtils
|
|
|
|
.qualifier(
|
|
|
|
invocation.getArgument(1), invocation.getArgument(1), invocation.getArgument(0),
|
|
|
|
invocation.getArgument(0)));
|
2020-05-29 12:03:51 +02:00
|
|
|
|
|
|
|
lenient().when(vocs.termExists(anyString(), anyString())).thenReturn(true);
|
|
|
|
|
|
|
|
this.app = new MigrateDbEntitiesApplication(vocs);
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-07-28 11:27:08 +02:00
|
|
|
void testProcessService() throws Exception {
|
2022-04-29 12:25:24 +02:00
|
|
|
final List<TypedField> fields = prepareMocks("services_resultset_entry.json");
|
2020-04-27 14:52:31 +02:00
|
|
|
|
2022-04-26 16:18:41 +02:00
|
|
|
final List<Oaf> list = app.processService(rs);
|
2020-04-27 14:52:31 +02:00
|
|
|
assertEquals(1, list.size());
|
|
|
|
verifyMocks(fields);
|
|
|
|
|
|
|
|
final Datasource ds = (Datasource) list.get(0);
|
|
|
|
assertValidId(ds.getId());
|
2022-04-29 12:25:24 +02:00
|
|
|
ds
|
|
|
|
.getCollectedfrom()
|
|
|
|
.stream()
|
|
|
|
.map(KeyValue::getKey)
|
2022-07-28 11:27:08 +02:00
|
|
|
.forEach(this::assertValidId);
|
2022-04-29 15:56:55 +02:00
|
|
|
|
|
|
|
assertEquals(1, ds.getPid().size());
|
|
|
|
assertEquals("r3d100010218", ds.getPid().get(0).getValue());
|
|
|
|
assertEquals("re3data", ds.getPid().get(0).getQualifier().getClassid());
|
|
|
|
assertEquals("dnet:pid_types", ds.getPid().get(0).getQualifier().getSchemeid());
|
|
|
|
|
2020-09-24 12:10:59 +02:00
|
|
|
assertEquals(getValueAsString("officialname", fields), ds.getOfficialname().getValue());
|
|
|
|
assertEquals(getValueAsString("englishname", fields), ds.getEnglishname().getValue());
|
|
|
|
assertEquals(getValueAsString("websiteurl", fields), ds.getWebsiteurl().getValue());
|
2022-04-29 12:25:24 +02:00
|
|
|
assertEquals(getValueAsString("logourl", fields), ds.getLogourl());
|
|
|
|
assertEquals(getValueAsString("contactemail", fields), ds.getContactemail().getValue());
|
2020-09-24 12:10:59 +02:00
|
|
|
assertEquals(getValueAsString("namespaceprefix", fields), ds.getNamespaceprefix().getValue());
|
|
|
|
assertEquals(getValueAsString("officialname", fields), ds.getJournal().getName());
|
2020-10-02 09:37:08 +02:00
|
|
|
assertEquals(getValueAsString("issnPrinted", fields), ds.getJournal().getIssnPrinted());
|
|
|
|
assertEquals(getValueAsString("issnOnline", fields), ds.getJournal().getIssnOnline());
|
|
|
|
assertEquals(getValueAsString("issnLinking", fields), ds.getJournal().getIssnLinking());
|
2021-07-28 11:32:34 +02:00
|
|
|
|
|
|
|
assertEquals("pubsrepository::journal", ds.getDatasourcetype().getClassid());
|
|
|
|
assertEquals("dnet:datasource_typologies", ds.getDatasourcetype().getSchemeid());
|
|
|
|
|
|
|
|
assertEquals("pubsrepository::journal", ds.getDatasourcetypeui().getClassid());
|
|
|
|
assertEquals("dnet:datasource_typologies_ui", ds.getDatasourcetypeui().getSchemeid());
|
|
|
|
|
2022-04-29 12:25:24 +02:00
|
|
|
assertEquals("Data Source", ds.getEosctype().getClassid());
|
|
|
|
assertEquals("Data Source", ds.getEosctype().getClassname());
|
|
|
|
assertEquals("dnet:eosc_types", ds.getEosctype().getSchemeid());
|
|
|
|
assertEquals("dnet:eosc_types", ds.getEosctype().getSchemename());
|
|
|
|
|
|
|
|
assertEquals("Journal archive", ds.getEoscdatasourcetype().getClassid());
|
|
|
|
assertEquals("Journal archive", ds.getEoscdatasourcetype().getClassname());
|
|
|
|
assertEquals("dnet:eosc_datasource_types", ds.getEoscdatasourcetype().getSchemeid());
|
|
|
|
assertEquals("dnet:eosc_datasource_types", ds.getEoscdatasourcetype().getSchemename());
|
|
|
|
|
|
|
|
assertEquals("openaire4.0", ds.getOpenairecompatibility().getClassid());
|
|
|
|
assertEquals("openaire4.0", ds.getOpenairecompatibility().getClassname());
|
|
|
|
assertEquals("dnet:datasourceCompatibilityLevel", ds.getOpenairecompatibility().getSchemeid());
|
|
|
|
assertEquals("dnet:datasourceCompatibilityLevel", ds.getOpenairecompatibility().getSchemename());
|
|
|
|
|
|
|
|
assertEquals(getValueAsDouble("latitude", fields).toString(), ds.getLatitude().getValue());
|
|
|
|
assertEquals(getValueAsDouble("longitude", fields).toString(), ds.getLongitude().getValue());
|
|
|
|
assertEquals(getValueAsString("dateofvalidation", fields), ds.getDateofvalidation());
|
|
|
|
|
|
|
|
assertEquals(getValueAsString("description", fields), ds.getDescription().getValue());
|
|
|
|
|
|
|
|
// TODO assertEquals(getValueAsString("subjects", fields), ds.getSubjects());
|
|
|
|
|
|
|
|
assertEquals("0.0", ds.getOdnumberofitems().getValue());
|
|
|
|
assertEquals(getValueAsString("odnumberofitemsdate", fields), ds.getOdnumberofitemsdate());
|
|
|
|
assertEquals(getValueAsString("odpolicies", fields), ds.getOdpolicies());
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
getValueAsList("odlanguages", fields),
|
|
|
|
ds.getOdlanguages().stream().map(Field::getValue).collect(Collectors.toList()));
|
|
|
|
assertEquals(getValueAsList("languages", fields), ds.getLanguages());
|
|
|
|
assertEquals(
|
|
|
|
getValueAsList("accessinfopackage", fields),
|
|
|
|
ds.getAccessinfopackage().stream().map(Field::getValue).collect(Collectors.toList()));
|
|
|
|
assertEquals(getValueAsString("releasestartdate", fields), ds.getReleasestartdate());
|
|
|
|
assertEquals(getValueAsString("releaseenddate", fields), ds.getReleasestartdate());
|
|
|
|
assertEquals(getValueAsString("missionstatementurl", fields), ds.getMissionstatementurl());
|
|
|
|
|
2022-05-09 16:05:06 +02:00
|
|
|
assertEquals(null, ds.getDataprovider());
|
|
|
|
assertEquals(null, ds.getServiceprovider());
|
2022-04-29 12:25:24 +02:00
|
|
|
|
|
|
|
assertEquals(getValueAsString("databaseaccesstype", fields), ds.getDatabaseaccesstype());
|
|
|
|
assertEquals(getValueAsString("datauploadtype", fields), ds.getDatauploadtype());
|
|
|
|
assertEquals(getValueAsString("databaseaccessrestriction", fields), ds.getDatabaseaccessrestriction());
|
|
|
|
assertEquals(getValueAsString("datauploadrestriction", fields), ds.getDatauploadrestriction());
|
|
|
|
|
|
|
|
assertEquals(false, ds.getVersioning().getValue());
|
|
|
|
assertEquals(false, ds.getVersioncontrol());
|
|
|
|
|
|
|
|
assertEquals(getValueAsString("citationguidelineurl", fields), ds.getCitationguidelineurl());
|
|
|
|
assertEquals(getValueAsString("pidsystems", fields), ds.getPidsystems());
|
|
|
|
assertEquals(getValueAsString("certificates", fields), ds.getCertificates());
|
|
|
|
|
|
|
|
assertEquals(getValueAsList("researchentitytypes", fields), ds.getResearchentitytypes());
|
|
|
|
|
2021-07-28 11:32:34 +02:00
|
|
|
assertEquals("National", ds.getJurisdiction().getClassid());
|
|
|
|
assertEquals("eosc:jurisdictions", ds.getJurisdiction().getSchemeid());
|
|
|
|
|
|
|
|
assertTrue(ds.getThematic());
|
|
|
|
|
2022-04-29 12:25:24 +02:00
|
|
|
HashSet<String> cpSchemeId = ds
|
|
|
|
.getContentpolicies()
|
|
|
|
.stream()
|
|
|
|
.map(Qualifier::getSchemeid)
|
|
|
|
.collect(Collectors.toCollection(HashSet::new));
|
2022-07-29 11:56:01 +02:00
|
|
|
assertEquals(1, cpSchemeId.size());
|
2022-04-29 12:25:24 +02:00
|
|
|
assertTrue(cpSchemeId.contains("eosc:contentpolicies"));
|
|
|
|
HashSet<String> cpSchemeName = ds
|
|
|
|
.getContentpolicies()
|
|
|
|
.stream()
|
|
|
|
.map(Qualifier::getSchemename)
|
|
|
|
.collect(Collectors.toCollection(HashSet::new));
|
2022-07-28 11:27:08 +02:00
|
|
|
assertEquals(1, cpSchemeName.size());
|
2022-04-29 12:25:24 +02:00
|
|
|
assertTrue(cpSchemeName.contains("eosc:contentpolicies"));
|
|
|
|
assertEquals(2, ds.getContentpolicies().size());
|
|
|
|
assertEquals("Taxonomic classification", ds.getContentpolicies().get(0).getClassid());
|
|
|
|
assertEquals("Resource collection", ds.getContentpolicies().get(1).getClassid());
|
|
|
|
|
|
|
|
assertEquals(getValueAsString("submissionpolicyurl", fields), ds.getSubmissionpolicyurl());
|
|
|
|
assertEquals(getValueAsString("preservationpolicyurl", fields), ds.getPreservationpolicyurl());
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
getValueAsList("researchproductaccesspolicies", fields),
|
|
|
|
ds.getResearchproductaccesspolicies());
|
|
|
|
assertEquals(
|
|
|
|
getValueAsList("researchproductmetadataaccesspolicies", fields),
|
|
|
|
ds.getResearchproductmetadataaccesspolicies());
|
2021-07-28 11:32:34 +02:00
|
|
|
|
2022-03-11 09:32:42 +01:00
|
|
|
assertEquals(true, ds.getConsenttermsofuse());
|
|
|
|
assertEquals(true, ds.getFulltextdownload());
|
|
|
|
assertEquals("2022-03-11", ds.getConsenttermsofusedate());
|
2022-04-29 12:25:24 +02:00
|
|
|
assertEquals("2022-03-11", ds.getLastconsenttermsofusedate());
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-07-28 11:27:08 +02:00
|
|
|
void testProcessProject() throws Exception {
|
2020-04-27 14:52:31 +02:00
|
|
|
final List<TypedField> fields = prepareMocks("projects_resultset_entry.json");
|
|
|
|
|
|
|
|
final List<Oaf> list = app.processProject(rs);
|
|
|
|
assertEquals(1, list.size());
|
|
|
|
verifyMocks(fields);
|
|
|
|
|
|
|
|
final Project p = (Project) list.get(0);
|
|
|
|
assertValidId(p.getId());
|
|
|
|
assertValidId(p.getCollectedfrom().get(0).getKey());
|
2020-09-24 12:10:59 +02:00
|
|
|
assertEquals(getValueAsString("acronym", fields), p.getAcronym().getValue());
|
|
|
|
assertEquals(getValueAsString("title", fields), p.getTitle().getValue());
|
|
|
|
assertEquals(getValueAsString("collectedfromname", fields), p.getCollectedfrom().get(0).getValue());
|
|
|
|
assertEquals(getValueAsFloat("fundedamount", fields), p.getFundedamount());
|
|
|
|
assertEquals(getValueAsFloat("totalcost", fields), p.getTotalcost());
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-07-28 11:27:08 +02:00
|
|
|
void testProcessOrganization() throws Exception {
|
2020-04-27 14:52:31 +02:00
|
|
|
final List<TypedField> fields = prepareMocks("organizations_resultset_entry.json");
|
|
|
|
|
|
|
|
final List<Oaf> list = app.processOrganization(rs);
|
|
|
|
|
|
|
|
assertEquals(1, list.size());
|
|
|
|
|
|
|
|
verifyMocks(fields);
|
|
|
|
|
|
|
|
final Organization o = (Organization) list.get(0);
|
|
|
|
assertValidId(o.getId());
|
|
|
|
assertValidId(o.getCollectedfrom().get(0).getKey());
|
2020-09-24 12:10:59 +02:00
|
|
|
assertEquals(getValueAsString("legalshortname", fields), o.getLegalshortname().getValue());
|
|
|
|
assertEquals(getValueAsString("legalname", fields), o.getLegalname().getValue());
|
|
|
|
assertEquals(getValueAsString("websiteurl", fields), o.getWebsiteurl().getValue());
|
|
|
|
assertEquals(getValueAsString("country", fields).split("@@@")[0], o.getCountry().getClassid());
|
|
|
|
assertEquals(getValueAsString("country", fields).split("@@@")[0], o.getCountry().getClassname());
|
|
|
|
assertEquals(getValueAsString("country", fields).split("@@@")[1], o.getCountry().getSchemeid());
|
|
|
|
assertEquals(getValueAsString("country", fields).split("@@@")[1], o.getCountry().getSchemename());
|
|
|
|
assertEquals(getValueAsString("collectedfromname", fields), o.getCollectedfrom().get(0).getValue());
|
2021-07-28 11:32:34 +02:00
|
|
|
final List<String> alternativenames = getValueAsList("alternativenames", fields);
|
2021-04-20 12:45:21 +02:00
|
|
|
assertEquals(2, alternativenames.size());
|
|
|
|
assertTrue(alternativenames.contains("Pippo"));
|
|
|
|
assertTrue(alternativenames.contains("Foo"));
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-07-28 11:27:08 +02:00
|
|
|
void testProcessDatasourceOrganization() throws Exception {
|
2020-04-27 14:52:31 +02:00
|
|
|
final List<TypedField> fields = prepareMocks("datasourceorganization_resultset_entry.json");
|
|
|
|
|
2022-05-02 15:52:33 +02:00
|
|
|
final List<Oaf> list = app.processServiceOrganization(rs);
|
2020-04-27 14:52:31 +02:00
|
|
|
|
|
|
|
assertEquals(2, list.size());
|
|
|
|
verifyMocks(fields);
|
|
|
|
|
|
|
|
final Relation r1 = (Relation) list.get(0);
|
|
|
|
final Relation r2 = (Relation) list.get(1);
|
|
|
|
assertValidId(r1.getSource());
|
|
|
|
assertValidId(r2.getSource());
|
|
|
|
assertEquals(r1.getSource(), r2.getTarget());
|
|
|
|
assertEquals(r2.getSource(), r1.getTarget());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-07-28 11:27:08 +02:00
|
|
|
void testProcessProjectOrganization() throws Exception {
|
2020-04-27 14:52:31 +02:00
|
|
|
final List<TypedField> fields = prepareMocks("projectorganization_resultset_entry.json");
|
|
|
|
|
|
|
|
final List<Oaf> list = app.processProjectOrganization(rs);
|
|
|
|
|
|
|
|
assertEquals(2, list.size());
|
|
|
|
verifyMocks(fields);
|
|
|
|
|
|
|
|
final Relation r1 = (Relation) list.get(0);
|
|
|
|
final Relation r2 = (Relation) list.get(1);
|
|
|
|
assertValidId(r1.getSource());
|
|
|
|
assertValidId(r2.getSource());
|
|
|
|
assertEquals(r1.getSource(), r2.getTarget());
|
|
|
|
assertEquals(r2.getSource(), r1.getTarget());
|
|
|
|
assertValidId(r1.getCollectedfrom().get(0).getKey());
|
|
|
|
assertValidId(r2.getCollectedfrom().get(0).getKey());
|
2022-07-28 11:27:08 +02:00
|
|
|
|
|
|
|
assertEquals(ModelConstants.PROJECT_ORGANIZATION, r1.getRelType());
|
|
|
|
assertEquals(ModelConstants.PROJECT_ORGANIZATION, r2.getRelType());
|
|
|
|
|
|
|
|
assertEquals(ModelConstants.PARTICIPATION, r1.getSubRelType());
|
|
|
|
assertEquals(ModelConstants.PARTICIPATION, r2.getSubRelType());
|
|
|
|
|
|
|
|
if (r1.getSource().startsWith("40")) {
|
|
|
|
assertEquals(ModelConstants.HAS_PARTICIPANT, r1.getRelClass());
|
|
|
|
assertEquals(ModelConstants.IS_PARTICIPANT, r2.getRelClass());
|
|
|
|
} else if (r1.getSource().startsWith("20")) {
|
|
|
|
assertEquals(ModelConstants.IS_PARTICIPANT, r1.getRelClass());
|
|
|
|
assertEquals(ModelConstants.HAS_PARTICIPANT, r2.getRelClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNotNull(r1.getProperties());
|
|
|
|
checkProperty(r1, "contribution", "436754.0");
|
|
|
|
checkProperty(r2, "contribution", "436754.0");
|
|
|
|
|
2022-07-29 11:56:01 +02:00
|
|
|
checkProperty(r1, "currency", "EUR");
|
2022-07-28 11:27:08 +02:00
|
|
|
checkProperty(r2, "currency", "EUR");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkProperty(Relation r, String property, String value) {
|
|
|
|
final List<KeyValue> p = r
|
2022-07-29 11:56:01 +02:00
|
|
|
.getProperties()
|
|
|
|
.stream()
|
|
|
|
.filter(kv -> kv.getKey().equals(property))
|
|
|
|
.collect(Collectors.toList());
|
2022-07-28 11:27:08 +02:00
|
|
|
assertFalse(p.isEmpty());
|
|
|
|
assertEquals(1, p.size());
|
|
|
|
assertEquals(value, p.get(0).getValue());
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testProcessClaims_context() throws Exception {
|
|
|
|
final List<TypedField> fields = prepareMocks("claimscontext_resultset_entry.json");
|
|
|
|
|
|
|
|
final List<Oaf> list = app.processClaims(rs);
|
|
|
|
|
|
|
|
assertEquals(1, list.size());
|
|
|
|
assertTrue(list.get(0) instanceof Result);
|
|
|
|
final Result r = (Result) list.get(0);
|
|
|
|
|
|
|
|
verifyMocks(fields);
|
|
|
|
|
|
|
|
assertValidId(r.getCollectedfrom().get(0).getKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-07-28 11:27:08 +02:00
|
|
|
void testProcessClaims_rels() throws Exception {
|
2020-04-27 14:52:31 +02:00
|
|
|
final List<TypedField> fields = prepareMocks("claimsrel_resultset_entry.json");
|
|
|
|
|
|
|
|
final List<Oaf> list = app.processClaims(rs);
|
|
|
|
|
|
|
|
assertEquals(2, list.size());
|
|
|
|
verifyMocks(fields);
|
|
|
|
|
|
|
|
assertTrue(list.get(0) instanceof Relation);
|
|
|
|
assertTrue(list.get(1) instanceof Relation);
|
|
|
|
|
|
|
|
final Relation r1 = (Relation) list.get(0);
|
|
|
|
final Relation r2 = (Relation) list.get(1);
|
|
|
|
|
|
|
|
assertValidId(r1.getSource());
|
|
|
|
assertValidId(r1.getTarget());
|
|
|
|
assertValidId(r2.getSource());
|
|
|
|
assertValidId(r2.getTarget());
|
|
|
|
assertNotNull(r1.getDataInfo());
|
|
|
|
assertNotNull(r2.getDataInfo());
|
|
|
|
assertNotNull(r1.getDataInfo().getTrust());
|
|
|
|
assertNotNull(r2.getDataInfo().getTrust());
|
|
|
|
assertEquals(r1.getSource(), r2.getTarget());
|
|
|
|
assertEquals(r2.getSource(), r1.getTarget());
|
|
|
|
assertTrue(StringUtils.isNotBlank(r1.getRelClass()));
|
|
|
|
assertTrue(StringUtils.isNotBlank(r2.getRelClass()));
|
|
|
|
assertTrue(StringUtils.isNotBlank(r1.getRelType()));
|
|
|
|
assertTrue(StringUtils.isNotBlank(r2.getRelType()));
|
|
|
|
|
|
|
|
assertValidId(r1.getCollectedfrom().get(0).getKey());
|
|
|
|
assertValidId(r2.getCollectedfrom().get(0).getKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<TypedField> prepareMocks(final String jsonFile) throws IOException, SQLException {
|
|
|
|
final String json = IOUtils.toString(getClass().getResourceAsStream(jsonFile));
|
|
|
|
final ObjectMapper mapper = new ObjectMapper();
|
2021-08-23 14:33:39 +02:00
|
|
|
final List<TypedField> list = mapper.readValue(json, new TypeReference<List<TypedField>>() {
|
|
|
|
});
|
2020-04-27 14:52:31 +02:00
|
|
|
|
|
|
|
for (final TypedField tf : list) {
|
|
|
|
if (tf.getValue() == null) {
|
|
|
|
switch (tf.getType()) {
|
2021-08-23 14:33:39 +02:00
|
|
|
case "not_used":
|
|
|
|
break;
|
|
|
|
case "boolean":
|
|
|
|
Mockito.when(rs.getBoolean(tf.getField())).thenReturn(false);
|
|
|
|
break;
|
|
|
|
case "date":
|
|
|
|
Mockito.when(rs.getDate(tf.getField())).thenReturn(null);
|
|
|
|
break;
|
|
|
|
case "int":
|
|
|
|
Mockito.when(rs.getInt(tf.getField())).thenReturn(0);
|
|
|
|
break;
|
|
|
|
case "double":
|
|
|
|
Mockito.when(rs.getDouble(tf.getField())).thenReturn(0.0);
|
|
|
|
break;
|
|
|
|
case "array":
|
|
|
|
Mockito.when(rs.getArray(tf.getField())).thenReturn(null);
|
|
|
|
break;
|
|
|
|
case "string":
|
|
|
|
default:
|
|
|
|
Mockito.when(rs.getString(tf.getField())).thenReturn(null);
|
|
|
|
break;
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (tf.getType()) {
|
2021-08-23 14:33:39 +02:00
|
|
|
case "not_used":
|
|
|
|
break;
|
|
|
|
case "boolean":
|
|
|
|
Mockito
|
|
|
|
.when(rs.getBoolean(tf.getField()))
|
|
|
|
.thenReturn(Boolean.parseBoolean(tf.getValue().toString()));
|
|
|
|
break;
|
|
|
|
case "date":
|
|
|
|
Mockito
|
|
|
|
.when(rs.getDate(tf.getField()))
|
|
|
|
.thenReturn(Date.valueOf(tf.getValue().toString()));
|
|
|
|
break;
|
|
|
|
case "int":
|
|
|
|
Mockito
|
|
|
|
.when(rs.getInt(tf.getField()))
|
|
|
|
.thenReturn(new Integer(tf.getValue().toString()));
|
|
|
|
break;
|
|
|
|
case "double":
|
|
|
|
Mockito
|
|
|
|
.when(rs.getDouble(tf.getField()))
|
|
|
|
.thenReturn(new Double(tf.getValue().toString()));
|
|
|
|
break;
|
|
|
|
case "array":
|
|
|
|
final Array arr = Mockito.mock(Array.class);
|
|
|
|
final String[] values = ((List<?>) tf.getValue())
|
|
|
|
.stream()
|
|
|
|
.filter(Objects::nonNull)
|
2022-07-28 11:27:08 +02:00
|
|
|
.map(Object::toString)
|
2021-08-23 14:33:39 +02:00
|
|
|
.toArray(String[]::new);
|
|
|
|
|
|
|
|
Mockito.when(arr.getArray()).thenReturn(values);
|
|
|
|
Mockito.when(rs.getArray(tf.getField())).thenReturn(arr);
|
|
|
|
break;
|
|
|
|
case "string":
|
|
|
|
default:
|
|
|
|
Mockito.when(rs.getString(tf.getField())).thenReturn(tf.getValue().toString());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void verifyMocks(final List<TypedField> list) throws SQLException {
|
|
|
|
for (final TypedField tf : list) {
|
|
|
|
|
|
|
|
switch (tf.getType()) {
|
2020-04-27 14:52:31 +02:00
|
|
|
case "not_used":
|
|
|
|
break;
|
|
|
|
case "boolean":
|
2021-08-23 14:33:39 +02:00
|
|
|
Mockito.verify(rs, Mockito.atLeastOnce()).getBoolean(tf.getField());
|
2020-04-27 14:52:31 +02:00
|
|
|
break;
|
|
|
|
case "date":
|
2021-08-23 14:33:39 +02:00
|
|
|
Mockito.verify(rs, Mockito.atLeastOnce()).getDate(tf.getField());
|
2020-04-27 14:52:31 +02:00
|
|
|
break;
|
|
|
|
case "int":
|
2021-08-23 14:33:39 +02:00
|
|
|
Mockito.verify(rs, Mockito.atLeastOnce()).getInt(tf.getField());
|
2020-04-27 14:52:31 +02:00
|
|
|
break;
|
|
|
|
case "double":
|
2021-08-23 14:33:39 +02:00
|
|
|
Mockito.verify(rs, Mockito.atLeastOnce()).getDouble(tf.getField());
|
2020-04-27 14:52:31 +02:00
|
|
|
break;
|
|
|
|
case "array":
|
2021-08-23 14:33:39 +02:00
|
|
|
Mockito.verify(rs, Mockito.atLeastOnce()).getArray(tf.getField());
|
2020-04-27 14:52:31 +02:00
|
|
|
break;
|
|
|
|
case "string":
|
|
|
|
default:
|
2021-08-23 14:33:39 +02:00
|
|
|
Mockito.verify(rs, Mockito.atLeastOnce()).getString(tf.getField());
|
2020-04-27 14:52:31 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertValidId(final String id) {
|
|
|
|
assertEquals(49, id.length());
|
|
|
|
assertEquals('|', id.charAt(2));
|
|
|
|
assertEquals(':', id.charAt(15));
|
|
|
|
assertEquals(':', id.charAt(16));
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getValueAsString(final String name, final List<TypedField> fields) {
|
2020-09-24 12:10:59 +02:00
|
|
|
return getValueAs(name, fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Float getValueAsFloat(final String name, final List<TypedField> fields) {
|
2022-04-29 12:25:24 +02:00
|
|
|
final Object value = getValueAs(name, fields);
|
|
|
|
return value != null ? new Float(value.toString()) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Double getValueAsDouble(final String name, final List<TypedField> fields) {
|
|
|
|
final Object value = getValueAs(name, fields);
|
|
|
|
return value != null ? new Double(value.toString()) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Integer getValueAsInt(final String name, final List<TypedField> fields) {
|
|
|
|
final Object value = getValueAs(name, fields);
|
|
|
|
return value != null ? new Integer(value.toString()) : null;
|
2020-09-24 12:10:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private <T> T getValueAs(final String name, final List<TypedField> fields) {
|
2022-04-29 12:25:24 +02:00
|
|
|
final Optional<T> field = fields
|
2020-09-24 12:16:00 +02:00
|
|
|
.stream()
|
|
|
|
.filter(f -> f.getField().equals(name))
|
|
|
|
.findFirst()
|
2022-04-29 12:25:24 +02:00
|
|
|
.map(TypedField::getValue)
|
|
|
|
.map(o -> (T) o);
|
|
|
|
if (!field.isPresent()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return field.get();
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
2021-04-20 12:45:21 +02:00
|
|
|
|
|
|
|
private List<String> getValueAsList(final String name, final List<TypedField> fields) {
|
2021-05-14 10:58:12 +02:00
|
|
|
return getValueAs(name, fields);
|
2021-04-20 12:45:21 +02:00
|
|
|
}
|
2020-03-25 09:43:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class TypedField {
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
private String field;
|
|
|
|
private String type;
|
|
|
|
private Object value;
|
2020-03-25 09:43:58 +01:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
public String getField() {
|
|
|
|
return field;
|
|
|
|
}
|
2020-03-25 09:43:58 +01:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
public void setField(final String field) {
|
|
|
|
this.field = field;
|
|
|
|
}
|
2020-03-25 09:43:58 +01:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
public String getType() {
|
|
|
|
return type;
|
|
|
|
}
|
2020-03-25 09:43:58 +01:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
public void setType(final String type) {
|
|
|
|
this.type = type;
|
|
|
|
}
|
2020-03-25 09:43:58 +01:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
public Object getValue() {
|
|
|
|
return value;
|
|
|
|
}
|
2020-03-25 09:43:58 +01:00
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
public void setValue(final Object value) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
2020-03-25 09:43:58 +01:00
|
|
|
}
|