This commit is contained in:
Michele Artini 2023-06-29 10:39:53 +02:00
parent 5b4597a4ae
commit 18e553f3e7
8 changed files with 3455 additions and 2866 deletions

View File

@ -11,6 +11,8 @@ import java.util.stream.Collectors;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
@ -30,21 +32,26 @@ public class ContextMappingUtils {
private static final List<String> DATE_PATTERN = Lists.newArrayList("yyyy-MM-dd'T'hh:mm:ss", "yyyy-MM-dd'T'hh:mm:ssXXX", "yyyy-MM-dd'T'hh:mm:ss+00:00");
private static final Log log = LogFactory.getLog(ContextMappingUtils.class);
public static Context parseContext(final String s, final Queue<Throwable> errors) {
try {
final Document doc = DocumentHelper.parseText(s);
final Element eContext = (Element) doc.selectSingleNode("/RESOURCE_PROFILE/BODY/CONFIGURATION/context");
final String creationDate = eContext.valueOf("./param[./@name='creationdate']/text()");
final String otherDate = doc.valueOf("/RESOURCE_PROFILE/HEADER/DATE_OF_CREATION/@value");
final Context c = new Context()
.setId(eContext.attributeValue("id"))
.setLabel(eContext.attributeValue("label"))
.setType(eContext.attributeValue("type"))
.setLastUpdateDate(asDate(doc.valueOf("/RESOURCE_PROFILE/HEADER/DATE_OF_CREATION/@value")))
.setLastUpdateDate(asDate(otherDate))
.setParams(parseParams(eContext))
.setCategories(parseCategories(eContext));
// the creation date will be added in the param elements of the community profile. Funders may not have it, hence the check.
if (StringUtils.isNotBlank(creationDate)) {
c.setCreationDate(asDate(creationDate));
} else {
c.setCreationDate(asDate(otherDate));
}
return c;
} catch (final DocumentException e) {
@ -56,9 +63,11 @@ public class ContextMappingUtils {
private static Date asDate(final String s) {
for (final String pattern : DATE_PATTERN) {
try {
return DateUtils.parseDate(s, pattern);
final Date res = DateUtils.parseDate(s, pattern);
if (res != null) { return res; }
} catch (final ParseException e) {}
}
log.error("Invalid Date: " + s);
return null;
}

View File

@ -1,21 +1,32 @@
package eu.dnetlib.openaire.community.importer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import eu.dnetlib.openaire.community.CommunityService;
import eu.dnetlib.openaire.community.model.DbOrganization;
import eu.dnetlib.openaire.community.repository.DbOrganizationRepository;
import eu.dnetlib.openaire.context.ContextMappingUtils;
import eu.dnetlib.openaire.exporter.model.community.CommunityContentprovider;
import eu.dnetlib.openaire.exporter.model.community.CommunityDetails;
import eu.dnetlib.openaire.exporter.model.community.CommunityOrganization;
import eu.dnetlib.openaire.exporter.model.community.CommunityProject;
import eu.dnetlib.openaire.exporter.model.community.SubCommunity;
import eu.dnetlib.openaire.exporter.model.context.Context;
@ExtendWith(MockitoExtension.class)
class CommunityImporterServiceTest {
@ -48,9 +59,49 @@ class CommunityImporterServiceTest {
assertEquals(14, list.stream().filter(o -> o.getCommunity().equals("beopen")).count());
}
@SuppressWarnings("unchecked")
@Test
public void testImportCommunity() {
fail("Not yet implemented");
public void testImportCommunity() throws Exception {
final String profile = IOUtils.toString(getClass().getResourceAsStream("old_community_profile.xml"), StandardCharsets.UTF_8.toString());
final Queue<Throwable> errors = new LinkedList<>();
final Context context = ContextMappingUtils.parseContext(profile, errors);
assertTrue(errors.isEmpty());
importer.importCommunity(context);
final ArgumentCaptor<CommunityDetails> detailsCapture = ArgumentCaptor.forClass(CommunityDetails.class);
final ArgumentCaptor<List<CommunityProject>> projectsCapture = ArgumentCaptor.forClass(List.class);
final ArgumentCaptor<List<CommunityContentprovider>> datasourcesCapture = ArgumentCaptor.forClass(List.class);
final ArgumentCaptor<List<CommunityOrganization>> orgsCapture = ArgumentCaptor.forClass(List.class);
final ArgumentCaptor<List<SubCommunity>> subCommunitiesCapture = ArgumentCaptor.forClass(List.class);
Mockito.verify(service, Mockito.times(1)).saveCommunity(detailsCapture.capture());
Mockito.verify(service, Mockito.times(1)).addCommunityProjectList(Mockito.anyString(), projectsCapture.capture());
Mockito.verify(service, Mockito.times(1)).addCommunityContentProvidersList(Mockito.anyString(), datasourcesCapture.capture());
Mockito.verify(service, Mockito.times(1)).addCommunityOrganizationList(Mockito.anyString(), orgsCapture.capture());
Mockito.verify(service, Mockito.times(1)).addSubCommunityList(subCommunitiesCapture.capture());
final CommunityDetails details = detailsCapture.getValue();
assertEquals("egi", details.getId());
// System.out.println(details);
final List<CommunityProject> projects = projectsCapture.getValue();
assertEquals(83, projects.size());
// projects.forEach(System.out::println);
final List<CommunityContentprovider> datasources = datasourcesCapture.getValue();
assertEquals(1, datasources.size());
// datasources.forEach(System.out::println);
final List<CommunityOrganization> orgs = orgsCapture.getValue();
assertEquals(1, orgs.size());
// orgs.forEach(System.out::println);
final List<SubCommunity> subs = subCommunitiesCapture.getValue();
assertEquals(688, subs.size());
// subs.forEach(System.out::println);
}
}

View File

@ -71,11 +71,6 @@ public class CommunityContentprovider {
}
@Override
public String toString() {
return String.format("id %s, name %s, selection criteria %s", this.openaireId, this.name, toJson());
}
public String toJson() {
if (selectioncriteria == null) { return ""; }
try {
@ -89,4 +84,21 @@ public class CommunityContentprovider {
if (selectioncriteria == null) { return ""; }
return "<![CDATA[" + toJson() + "]]>";
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("CommunityContentprovider [\n\topenaireId = ")
.append(openaireId)
.append(",\n\tcommunityId = ")
.append(communityId)
.append(",\n\tname = ")
.append(name)
.append(",\n\tofficialname = ")
.append(officialname)
.append(",\n\tselectioncriteria = ")
.append(selectioncriteria)
.append("\n]");
return builder.toString();
}
}

View File

@ -131,4 +131,49 @@ public class CommunityDetails extends CommunitySummary {
this.otherZenodoCommunities = otherZenodoCommunities;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("CommunityDetails [\n\tcreationDate = ")
.append(creationDate)
.append(",\n\tlastUpdateDate = ")
.append(lastUpdateDate)
.append(",\n\tsubjects = ")
.append(subjects)
.append(",\n\tfos = ")
.append(fos)
.append(",\n\tsdg = ")
.append(sdg)
.append(",\n\tadvancedConstraints = ")
.append(advancedConstraints)
.append(",\n\tremoveConstraints = ")
.append(removeConstraints)
.append(",\n\tclaim = ")
.append(claim)
.append(",\n\tmembership = ")
.append(membership)
.append(",\n\totherZenodoCommunities = ")
.append(otherZenodoCommunities)
.append(",\n\tid = ")
.append(id)
.append(",\n\tqueryId = ")
.append(queryId)
.append(",\n\ttype = ")
.append(type)
.append(",\n\tname = ")
.append(name)
.append(",\n\tshortName = ")
.append(shortName)
.append(",\n\tdescription = ")
.append(description)
.append(",\n\tlogoUrl = ")
.append(logoUrl)
.append(",\n\tstatus = ")
.append(status)
.append(",\n\tzenodoCommunity = ")
.append(zenodoCommunity)
.append("\n]");
return builder.toString();
}
}

View File

@ -60,4 +60,19 @@ public class CommunityOrganization {
this.website_url = website_url;
return this;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("CommunityOrganization [\n\tcommunityId = ")
.append(communityId)
.append(",\n\tname = ")
.append(name)
.append(",\n\tlogo_url = ")
.append(logo_url)
.append(",\n\twebsite_url = ")
.append(website_url)
.append("\n]");
return builder.toString();
}
}

View File

@ -73,4 +73,23 @@ public class CommunityProject {
this.grantId = grantId;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("CommunityProject [\n\topenaireId = ")
.append(openaireId)
.append(",\n\tcommunityId = ")
.append(communityId)
.append(",\n\tname = ")
.append(name)
.append(",\n\tacronym = ")
.append(acronym)
.append(",\n\tfunder = ")
.append(funder)
.append(",\n\tgrantId = ")
.append(grantId)
.append("\n]");
return builder.toString();
}
}

View File

@ -89,4 +89,25 @@ public class SubCommunity {
this.claim = claim;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("SubCommunity [\n\tsubCommunityId = ")
.append(subCommunityId)
.append(",\n\tcommunityId = ")
.append(communityId)
.append(",\n\tparent = ")
.append(parent)
.append(",\n\tlabel = ")
.append(label)
.append(",\n\tcategory = ")
.append(category)
.append(",\n\tparams = ")
.append(params)
.append(",\n\tclaim = ")
.append(claim)
.append("\n]");
return builder.toString();
}
}