new_model_for_communities #15
|
@ -11,6 +11,8 @@ import java.util.stream.Collectors;
|
||||||
import org.apache.commons.lang3.BooleanUtils;
|
import org.apache.commons.lang3.BooleanUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.commons.lang3.time.DateUtils;
|
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.Document;
|
||||||
import org.dom4j.DocumentException;
|
import org.dom4j.DocumentException;
|
||||||
import org.dom4j.DocumentHelper;
|
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 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) {
|
public static Context parseContext(final String s, final Queue<Throwable> errors) {
|
||||||
try {
|
try {
|
||||||
final Document doc = DocumentHelper.parseText(s);
|
final Document doc = DocumentHelper.parseText(s);
|
||||||
final Element eContext = (Element) doc.selectSingleNode("/RESOURCE_PROFILE/BODY/CONFIGURATION/context");
|
final Element eContext = (Element) doc.selectSingleNode("/RESOURCE_PROFILE/BODY/CONFIGURATION/context");
|
||||||
final String creationDate = eContext.valueOf("./param[./@name='creationdate']/text()");
|
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()
|
final Context c = new Context()
|
||||||
.setId(eContext.attributeValue("id"))
|
.setId(eContext.attributeValue("id"))
|
||||||
.setLabel(eContext.attributeValue("label"))
|
.setLabel(eContext.attributeValue("label"))
|
||||||
.setType(eContext.attributeValue("type"))
|
.setType(eContext.attributeValue("type"))
|
||||||
.setLastUpdateDate(asDate(doc.valueOf("/RESOURCE_PROFILE/HEADER/DATE_OF_CREATION/@value")))
|
.setLastUpdateDate(asDate(otherDate))
|
||||||
.setParams(parseParams(eContext))
|
.setParams(parseParams(eContext))
|
||||||
.setCategories(parseCategories(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.
|
// 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)) {
|
if (StringUtils.isNotBlank(creationDate)) {
|
||||||
c.setCreationDate(asDate(creationDate));
|
c.setCreationDate(asDate(creationDate));
|
||||||
|
} else {
|
||||||
|
c.setCreationDate(asDate(otherDate));
|
||||||
}
|
}
|
||||||
return c;
|
return c;
|
||||||
} catch (final DocumentException e) {
|
} catch (final DocumentException e) {
|
||||||
|
@ -56,9 +63,11 @@ public class ContextMappingUtils {
|
||||||
private static Date asDate(final String s) {
|
private static Date asDate(final String s) {
|
||||||
for (final String pattern : DATE_PATTERN) {
|
for (final String pattern : DATE_PATTERN) {
|
||||||
try {
|
try {
|
||||||
return DateUtils.parseDate(s, pattern);
|
final Date res = DateUtils.parseDate(s, pattern);
|
||||||
|
if (res != null) { return res; }
|
||||||
} catch (final ParseException e) {}
|
} catch (final ParseException e) {}
|
||||||
}
|
}
|
||||||
|
log.error("Invalid Date: " + s);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,21 +1,32 @@
|
||||||
package eu.dnetlib.openaire.community.importer;
|
package eu.dnetlib.openaire.community.importer;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
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.nio.charset.StandardCharsets;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Mockito;
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
import eu.dnetlib.openaire.community.CommunityService;
|
import eu.dnetlib.openaire.community.CommunityService;
|
||||||
import eu.dnetlib.openaire.community.model.DbOrganization;
|
import eu.dnetlib.openaire.community.model.DbOrganization;
|
||||||
import eu.dnetlib.openaire.community.repository.DbOrganizationRepository;
|
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)
|
@ExtendWith(MockitoExtension.class)
|
||||||
class CommunityImporterServiceTest {
|
class CommunityImporterServiceTest {
|
||||||
|
@ -48,9 +59,49 @@ class CommunityImporterServiceTest {
|
||||||
assertEquals(14, list.stream().filter(o -> o.getCommunity().equals("beopen")).count());
|
assertEquals(14, list.stream().filter(o -> o.getCommunity().equals("beopen")).count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
@Test
|
@Test
|
||||||
public void testImportCommunity() {
|
public void testImportCommunity() throws Exception {
|
||||||
fail("Not yet implemented");
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -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() {
|
public String toJson() {
|
||||||
if (selectioncriteria == null) { return ""; }
|
if (selectioncriteria == null) { return ""; }
|
||||||
try {
|
try {
|
||||||
|
@ -89,4 +84,21 @@ public class CommunityContentprovider {
|
||||||
if (selectioncriteria == null) { return ""; }
|
if (selectioncriteria == null) { return ""; }
|
||||||
return "<![CDATA[" + toJson() + "]]>";
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -131,4 +131,49 @@ public class CommunityDetails extends CommunitySummary {
|
||||||
this.otherZenodoCommunities = otherZenodoCommunities;
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,4 +60,19 @@ public class CommunityOrganization {
|
||||||
this.website_url = website_url;
|
this.website_url = website_url;
|
||||||
return this;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,4 +73,23 @@ public class CommunityProject {
|
||||||
this.grantId = grantId;
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,4 +89,25 @@ public class SubCommunity {
|
||||||
this.claim = claim;
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue