diff --git a/dspace-api/src/main/java/org/dspace/content/authority/RorOrgUnitAuthority.java b/dspace-api/src/main/java/org/dspace/content/authority/RorOrgUnitAuthority.java index 1b46b82acc46..c68caa8ddaf8 100644 --- a/dspace-api/src/main/java/org/dspace/content/authority/RorOrgUnitAuthority.java +++ b/dspace-api/src/main/java/org/dspace/content/authority/RorOrgUnitAuthority.java @@ -21,15 +21,15 @@ import org.dspace.importer.external.datamodel.ImportRecord; import org.dspace.importer.external.exception.MetadataSourceException; import org.dspace.importer.external.metadatamapping.MetadatumDTO; -import org.dspace.importer.external.ror.service.RorImportMetadataSourceServiceImpl; +import org.dspace.importer.external.ror.service.RorImportMetadataSourceService; +import org.dspace.importer.external.ror.service.RorServicesFactory; import org.dspace.services.ConfigurationService; import org.dspace.services.factory.DSpaceServicesFactory; -import org.dspace.utils.DSpace; public class RorOrgUnitAuthority extends ItemAuthority { - private final RorImportMetadataSourceServiceImpl rorImportMetadataSource = new DSpace().getServiceManager() - .getServicesByType(RorImportMetadataSourceServiceImpl.class).get(0); + private final RorImportMetadataSourceService rorImportMetadataSource = + RorServicesFactory.getInstance().getRorImportMetadataSourceService(); private final ItemAuthorityServiceFactory itemAuthorityServiceFactory = dspace.getServiceManager().getServiceByName("itemAuthorityServiceFactory", ItemAuthorityServiceFactory.class); diff --git a/dspace-api/src/main/java/org/dspace/importer/external/ror/service/RorImportMetadataSourceService.java b/dspace-api/src/main/java/org/dspace/importer/external/ror/service/RorImportMetadataSourceService.java new file mode 100644 index 000000000000..a6ab0ef0b8fd --- /dev/null +++ b/dspace-api/src/main/java/org/dspace/importer/external/ror/service/RorImportMetadataSourceService.java @@ -0,0 +1,16 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.importer.external.ror.service; + +import org.dspace.importer.external.service.components.QuerySource; + +/** + * @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.com) + **/ +public interface RorImportMetadataSourceService extends QuerySource { +} diff --git a/dspace-api/src/main/java/org/dspace/importer/external/ror/service/RorImportMetadataSourceServiceImpl.java b/dspace-api/src/main/java/org/dspace/importer/external/ror/service/RorImportMetadataSourceServiceImpl.java index 735281f57bc7..4de8656e5ab6 100644 --- a/dspace-api/src/main/java/org/dspace/importer/external/ror/service/RorImportMetadataSourceServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/importer/external/ror/service/RorImportMetadataSourceServiceImpl.java @@ -31,7 +31,6 @@ import org.dspace.importer.external.exception.MetadataSourceException; import org.dspace.importer.external.liveimportclient.service.LiveImportClient; import org.dspace.importer.external.service.AbstractImportMetadataSourceService; -import org.dspace.importer.external.service.components.QuerySource; import org.springframework.beans.factory.annotation.Autowired; /** @@ -40,7 +39,7 @@ * @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.com) */ public class RorImportMetadataSourceServiceImpl extends AbstractImportMetadataSourceService - implements QuerySource { + implements RorImportMetadataSourceService { private final static Logger log = LogManager.getLogger(); protected static final String ROR_IDENTIFIER_PREFIX = "https://ror.org/"; diff --git a/dspace-api/src/main/java/org/dspace/importer/external/ror/service/RorServicesFactory.java b/dspace-api/src/main/java/org/dspace/importer/external/ror/service/RorServicesFactory.java new file mode 100644 index 000000000000..bca14a7a4521 --- /dev/null +++ b/dspace-api/src/main/java/org/dspace/importer/external/ror/service/RorServicesFactory.java @@ -0,0 +1,26 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.importer.external.ror.service; + +import org.dspace.services.factory.DSpaceServicesFactory; + +/** + * Factory that handles {@code RorImportMetadataSourceService} instance + * + * @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.com) + **/ +public abstract class RorServicesFactory { + + public abstract RorImportMetadataSourceService getRorImportMetadataSourceService(); + + public static RorServicesFactory getInstance() { + return DSpaceServicesFactory.getInstance().getServiceManager() + .getServiceByName("rorServiceFactory", RorServicesFactory.class); + } + +} diff --git a/dspace-api/src/main/java/org/dspace/importer/external/ror/service/RorServicesFactoryImpl.java b/dspace-api/src/main/java/org/dspace/importer/external/ror/service/RorServicesFactoryImpl.java new file mode 100644 index 000000000000..6781809c4e4d --- /dev/null +++ b/dspace-api/src/main/java/org/dspace/importer/external/ror/service/RorServicesFactoryImpl.java @@ -0,0 +1,27 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.importer.external.ror.service; + +import org.springframework.beans.factory.annotation.Autowired; + +/** + * @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.com) + **/ +public class RorServicesFactoryImpl extends RorServicesFactory { + + protected RorImportMetadataSourceService metadataSourceService; + + public RorServicesFactoryImpl(@Autowired RorImportMetadataSourceService metadataSourceService) { + this.metadataSourceService = metadataSourceService; + } + + @Override + public RorImportMetadataSourceService getRorImportMetadataSourceService() { + return metadataSourceService; + } +} diff --git a/dspace-api/src/main/resources/spring/spring-dspace-addon-import-services.xml b/dspace-api/src/main/resources/spring/spring-dspace-addon-import-services.xml index ac22e1205677..33526e01914e 100644 --- a/dspace-api/src/main/resources/spring/spring-dspace-addon-import-services.xml +++ b/dspace-api/src/main/resources/spring/spring-dspace-addon-import-services.xml @@ -187,6 +187,7 @@ + diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/RorOrgUnitAuthorityIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/RorOrgUnitAuthorityIT.java index f517e84d56f3..fcfe4215740f 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/RorOrgUnitAuthorityIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/RorOrgUnitAuthorityIT.java @@ -15,49 +15,160 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.dspace.app.rest.test.AbstractControllerIntegrationTest; +import org.dspace.content.authority.service.ChoiceAuthorityService; +import org.dspace.content.authority.service.MetadataAuthorityService; +import org.dspace.core.service.PluginService; +import org.dspace.importer.external.datamodel.ImportRecord; +import org.dspace.importer.external.metadatamapping.MetadatumDTO; +import org.dspace.importer.external.ror.service.RorImportMetadataSourceService; +import org.dspace.importer.external.ror.service.RorServicesFactory; +import org.dspace.importer.external.ror.service.RorServicesFactoryImpl; +import org.dspace.services.ConfigurationService; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; public class RorOrgUnitAuthorityIT extends AbstractControllerIntegrationTest { + public static final String ROR_ORGUNIT_AUTHORITY = + "org.dspace.content.authority.RorOrgUnitAuthority = OrgUnitAuthority"; + + public static final String ROR_FILTER = "02z02cv32 OR 03vb2cr34"; + + private static MockedStatic mockRorServiceFactory; + + @Autowired + protected ChoiceAuthorityService choiceAuthorityService; + @Autowired + protected PluginService pluginService; + @Autowired + protected MetadataAuthorityService metadataAuthorityService; + @Autowired + protected ConfigurationService configurationService; + @Mock + private RorImportMetadataSourceService metadataSourceService; + + @BeforeClass + public static void init() { + mockRorServiceFactory = Mockito.mockStatic(RorServicesFactory.class); + } + + @AfterClass + public static void close() { + mockRorServiceFactory.close(); + } + + @Before + public void setup() throws Exception { + + RorServicesFactoryImpl rorServiceFactory = + new RorServicesFactoryImpl(this.metadataSourceService); + + Mockito.when( + metadataSourceService.getRecords( + ROR_FILTER, 0, 0 + ) + ).thenReturn(List.of(getImportRecord1(), getImportRecord2())); + + mockRorServiceFactory.when(RorServicesFactory::getInstance).thenReturn(rorServiceFactory); + } + @Test public void testAuthority() throws Exception { - Map expectedExtras = new HashMap<>(); - expectedExtras.put("data-ror_orgunit_id", "https://ror.org/02z02cv32"); - expectedExtras.put("ror_orgunit_id", "https://ror.org/02z02cv32"); - expectedExtras.put("data-ror_orgunit_type", "Nonprofit"); - expectedExtras.put("ror_orgunit_type", "Nonprofit"); - expectedExtras.put("data-ror_orgunit_acronym", "WEICan, IEEC"); - expectedExtras.put("ror_orgunit_acronym", "WEICan, IEEC"); - expectedExtras.put("data-ror_orgunit_country", "CA"); - expectedExtras.put("data-ror_orgunit_countryName", "CA"); - expectedExtras.put("ror_orgunit_countryName", "CA"); - - Map expectedExtras2 = new HashMap<>(); - expectedExtras2.put("data-ror_orgunit_id", "https://ror.org/03vb2cr34"); - expectedExtras2.put("ror_orgunit_id", "https://ror.org/03vb2cr34"); - expectedExtras2.put("data-ror_orgunit_type", "Company"); - expectedExtras2.put("ror_orgunit_type", "Company"); - expectedExtras2.put("data-ror_orgunit_countryName", "Italia"); - expectedExtras2.put("ror_orgunit_countryName", "Italia"); - expectedExtras2.put("data-ror_orgunit_country", "IT"); - String token = getAuthToken(eperson.getEmail(), password); getClient(token).perform(get("/api/submission/vocabularies/OrgUnitAuthority/entries") - .param("filter", "02z02cv32 OR 03vb2cr34")) + .param("filter", ROR_FILTER)) .andExpect(status().isOk()) .andExpect(jsonPath("$._embedded.entries", hasSize(2))) .andExpect(jsonPath("$._embedded.entries", hasItems( matchItemAuthorityWithOtherInformations("will be referenced::ROR-ID::https://ror.org/02z02cv32", "Wind Energy Institute of Canada", "Wind Energy Institute of Canada", "vocabularyEntry", - expectedExtras), + getExtrasRecord1() + ), matchItemAuthorityWithOtherInformations("will be referenced::ROR-ID::https://ror.org/03vb2cr34", "4Science", "4Science", "vocabularyEntry", - expectedExtras2)))); + getExtrasRecord2() + ) + ) + )); + } + + private ImportRecord getImportRecord1() { + return new ImportRecord( + List.of( + new RorMetadatum("dc.title", "Wind Energy Institute of Canada"), + new RorMetadatum("organization.identifier.ror", "https://ror.org/02z02cv32"), + new RorMetadatum("oairecerif.identifier.url", "http://www.weican.ca/"), + new RorMetadatum("dc.type", "Nonprofit"), + new RorMetadatum("oairecerif.acronym", "IEEC"), + new RorMetadatum("oairecerif.acronym", "WEICan"), + new RorMetadatum("organization.address.addressCountry", "CA"), + new RorMetadatum("organization.foundingDate", "1981") + + ) + ); + } + + private Map getExtrasRecord1() { + Map extras = new HashMap<>(); + extras.put("data-ror_orgunit_id", "https://ror.org/02z02cv32"); + extras.put("ror_orgunit_id", "https://ror.org/02z02cv32"); + extras.put("data-ror_orgunit_type", "Nonprofit"); + extras.put("ror_orgunit_type", "Nonprofit"); + extras.put("data-ror_orgunit_acronym", "IEEC, WEICan"); + extras.put("ror_orgunit_acronym", "IEEC, WEICan"); + extras.put("data-ror_orgunit_country", "CA"); + extras.put("data-ror_orgunit_countryName", "CA"); + extras.put("ror_orgunit_countryName", "CA"); + return extras; + } + + private ImportRecord getImportRecord2() { + return new ImportRecord( + List.of( + new RorMetadatum("dc.title", "4Science"), + new RorMetadatum("organization.identifier.ror", "https://ror.org/03vb2cr34"), + new RorMetadatum("oairecerif.identifier.url", "https://www.4science.it/"), + new RorMetadatum("dc.type", "Company"), + new RorMetadatum("organization.address.addressCountry", "IT"), + new RorMetadatum("organization.foundingDate", "2015") + ) + ); + } + + private Map getExtrasRecord2() { + Map extras = new HashMap<>(); + extras.put("data-ror_orgunit_id", "https://ror.org/03vb2cr34"); + extras.put("ror_orgunit_id", "https://ror.org/03vb2cr34"); + extras.put("data-ror_orgunit_type", "Company"); + extras.put("ror_orgunit_type", "Company"); + extras.put("data-ror_orgunit_countryName", "Italia"); + extras.put("ror_orgunit_countryName", "Italia"); + extras.put("data-ror_orgunit_country", "IT"); + return extras; + } + + private final class RorMetadatum extends MetadatumDTO { + public RorMetadatum(String field, String value) { + String[] split = field.split("\\."); + setSchema(split[0]); + setElement(split[1]); + if (split.length > 2) { + setQualifier(split[2]); + } + setValue(value); + } } }