Search in sources :

Example 11 with OrgDisambiguatedExternalIdentifierEntity

use of org.orcid.persistence.jpa.entities.OrgDisambiguatedExternalIdentifierEntity in project ORCID-Source by ORCID.

the class LoadGridDataTest method execute_AddMissingWikipediaExtId_Test.

@Test
public void execute_AddMissingWikipediaExtId_Test() throws URISyntaxException {
    when(orgDisambiguatedDao.findBySourceIdAndSourceType("grid.1", "GRID")).thenAnswer(new Answer<OrgDisambiguatedEntity>() {

        @Override
        public OrgDisambiguatedEntity answer(InvocationOnMock invocation) throws Throwable {
            OrgDisambiguatedEntity entity = new OrgDisambiguatedEntity();
            entity.setId(1L);
            entity.setName("org_1");
            entity.setSourceId("grid.1");
            entity.setCity("City One");
            entity.setCountry(Iso3166Country.US);
            entity.setOrgType("type_1");
            entity.setRegion("Alabama");
            entity.setSourceType("GRID");
            entity.setStatus("active");
            entity.setUrl("http://link1.com");
            return entity;
        }
    });
    OrgDisambiguatedExternalIdentifierEntity extId = new OrgDisambiguatedExternalIdentifierEntity();
    extId.setPreferred(false);
    OrgDisambiguatedExternalIdentifierEntity extIdPreferred = new OrgDisambiguatedExternalIdentifierEntity();
    extIdPreferred.setPreferred(true);
    when(orgDisambiguatedExternalIdentifierDao.findByDetails(1L, "ISNI1", "ISNI")).thenReturn(extId);
    when(orgDisambiguatedExternalIdentifierDao.findByDetails(1L, "FUNDREF1", "FUNDREF")).thenReturn(extIdPreferred);
    when(orgDisambiguatedExternalIdentifierDao.findByDetails(1L, "ORGREF1", "ORGREF")).thenReturn(extId);
    when(orgDisambiguatedExternalIdentifierDao.findByDetails(1L, "WIKIDATA1", "WIKIDATA")).thenReturn(extId);
    Path path = Paths.get(getClass().getClassLoader().getResource("grid/grid_1_org_5_external_identifiers.json").toURI());
    File testFile = path.toFile();
    loadGridData.setFileToLoad(testFile);
    loadGridData.execute();
    assertEquals(0L, loadGridData.getAddedDisambiguatedOrgs());
    assertEquals(1L, loadGridData.getAddedExternalIdentifiers());
    assertEquals(0L, loadGridData.getUpdatedOrgs());
    assertEquals(0L, loadGridData.getDeprecatedOrgs());
    assertEquals(0L, loadGridData.getObsoletedOrgs());
    assertEquals(0L, loadGridData.getUpdatedExternalIdentifiers());
    ArgumentCaptor<OrgDisambiguatedExternalIdentifierEntity> captor = ArgumentCaptor.forClass(OrgDisambiguatedExternalIdentifierEntity.class);
    verify(orgDisambiguatedExternalIdentifierDao).persist(captor.capture());
    OrgDisambiguatedExternalIdentifierEntity orgToBeUpdated = captor.getValue();
    assertEquals("http://en.wikipedia.org/wiki/org_1", orgToBeUpdated.getIdentifier());
    assertEquals("WIKIPEDIA_URL", orgToBeUpdated.getIdentifierType());
    assertEquals(Boolean.TRUE, orgToBeUpdated.getPreferred());
}
Also used : Path(java.nio.file.Path) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OrgDisambiguatedEntity(org.orcid.persistence.jpa.entities.OrgDisambiguatedEntity) OrgDisambiguatedExternalIdentifierEntity(org.orcid.persistence.jpa.entities.OrgDisambiguatedExternalIdentifierEntity) File(java.io.File) Test(org.junit.Test)

Example 12 with OrgDisambiguatedExternalIdentifierEntity

use of org.orcid.persistence.jpa.entities.OrgDisambiguatedExternalIdentifierEntity in project ORCID-Source by ORCID.

the class LoadGridDataTest method execute_UpdatePreferredIndicator_Test.

@SuppressWarnings("deprecation")
@Test
public void execute_UpdatePreferredIndicator_Test() throws URISyntaxException {
    when(orgDisambiguatedDao.findBySourceIdAndSourceType("grid.1", "GRID")).thenAnswer(new Answer<OrgDisambiguatedEntity>() {

        @Override
        public OrgDisambiguatedEntity answer(InvocationOnMock invocation) throws Throwable {
            OrgDisambiguatedEntity entity = new OrgDisambiguatedEntity();
            entity.setId(1L);
            entity.setName("org_1");
            entity.setSourceId("grid.1");
            entity.setCity("City One");
            entity.setCountry(Iso3166Country.US);
            entity.setOrgType("type_1");
            entity.setRegion("Alabama");
            entity.setSourceType("GRID");
            entity.setUrl("http://link1.com");
            return entity;
        }
    });
    // On DB WIKIDATA1 is preferred, but in the file WIKIDATA2 is the preferred one
    OrgDisambiguatedExternalIdentifierEntity wikidata1 = new OrgDisambiguatedExternalIdentifierEntity();
    wikidata1.setIdentifier("WIKIDATA1");
    wikidata1.setIdentifierType("WIKIDATA");
    wikidata1.setPreferred(Boolean.TRUE);
    OrgDisambiguatedExternalIdentifierEntity wikidata2 = new OrgDisambiguatedExternalIdentifierEntity();
    wikidata2.setIdentifier("WIKIDATA2");
    wikidata2.setIdentifierType("WIKIDATA");
    wikidata2.setPreferred(Boolean.FALSE);
    when(orgDisambiguatedExternalIdentifierDao.findByDetails(1L, "WIKIDATA1", "WIKIDATA")).thenReturn(wikidata1);
    when(orgDisambiguatedExternalIdentifierDao.findByDetails(1L, "WIKIDATA2", "WIKIDATA")).thenReturn(wikidata2);
    Path path = Paths.get(getClass().getClassLoader().getResource("grid/grid_1_org_2_ext_ids_#2_preferred.json").toURI());
    File testFile = path.toFile();
    loadGridData.setFileToLoad(testFile);
    loadGridData.execute();
    verify(orgDisambiguatedDao, times(0)).persist(Matchers.any(OrgDisambiguatedEntity.class));
    verify(orgDisambiguatedDao, times(0)).merge(Matchers.any(OrgDisambiguatedEntity.class));
    verify(orgDisambiguatedExternalIdentifierDao, times(0)).persist(Matchers.any(OrgDisambiguatedExternalIdentifierEntity.class));
    assertEquals(0L, loadGridData.getAddedDisambiguatedOrgs());
    assertEquals(0L, loadGridData.getAddedExternalIdentifiers());
    assertEquals(0L, loadGridData.getUpdatedOrgs());
    assertEquals(0L, loadGridData.getDeprecatedOrgs());
    assertEquals(0L, loadGridData.getObsoletedOrgs());
    assertEquals(2L, loadGridData.getUpdatedExternalIdentifiers());
    ArgumentCaptor<OrgDisambiguatedExternalIdentifierEntity> captor = ArgumentCaptor.forClass(OrgDisambiguatedExternalIdentifierEntity.class);
    verify(orgDisambiguatedExternalIdentifierDao, times(2)).merge(captor.capture());
    List<OrgDisambiguatedExternalIdentifierEntity> extIdsToBeUpdated = captor.getAllValues();
    OrgDisambiguatedExternalIdentifierEntity wikidata1ExtId = extIdsToBeUpdated.get(0);
    assertEquals("WIKIDATA1", wikidata1ExtId.getIdentifier());
    assertEquals("WIKIDATA", wikidata1ExtId.getIdentifierType());
    assertEquals(Boolean.FALSE, wikidata1ExtId.getPreferred());
    OrgDisambiguatedExternalIdentifierEntity wikidata2ExtId = extIdsToBeUpdated.get(1);
    assertEquals("WIKIDATA2", wikidata2ExtId.getIdentifier());
    assertEquals("WIKIDATA", wikidata2ExtId.getIdentifierType());
    assertEquals(Boolean.TRUE, wikidata2ExtId.getPreferred());
}
Also used : Path(java.nio.file.Path) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OrgDisambiguatedEntity(org.orcid.persistence.jpa.entities.OrgDisambiguatedEntity) OrgDisambiguatedExternalIdentifierEntity(org.orcid.persistence.jpa.entities.OrgDisambiguatedExternalIdentifierEntity) File(java.io.File) Test(org.junit.Test)

Example 13 with OrgDisambiguatedExternalIdentifierEntity

use of org.orcid.persistence.jpa.entities.OrgDisambiguatedExternalIdentifierEntity in project ORCID-Source by ORCID.

the class LoadGridDataTest method execute_JustAddOneExeternalIdentifier_Test.

@Test
public void execute_JustAddOneExeternalIdentifier_Test() throws URISyntaxException {
    when(orgDisambiguatedDao.findBySourceIdAndSourceType("grid.1", "GRID")).thenAnswer(new Answer<OrgDisambiguatedEntity>() {

        @Override
        public OrgDisambiguatedEntity answer(InvocationOnMock invocation) throws Throwable {
            OrgDisambiguatedEntity entity = new OrgDisambiguatedEntity();
            entity.setId(1L);
            entity.setName("org_1");
            entity.setSourceId("grid.1");
            entity.setCity("City One");
            entity.setCountry(Iso3166Country.US);
            entity.setOrgType("type_1");
            entity.setRegion("Alabama");
            entity.setSourceType("GRID");
            entity.setUrl("http://link1.com");
            return entity;
        }
    });
    OrgDisambiguatedExternalIdentifierEntity extId = new OrgDisambiguatedExternalIdentifierEntity();
    extId.setPreferred(Boolean.FALSE);
    OrgDisambiguatedExternalIdentifierEntity extIdPreferred = new OrgDisambiguatedExternalIdentifierEntity();
    extIdPreferred.setPreferred(Boolean.TRUE);
    when(orgDisambiguatedExternalIdentifierDao.findByDetails(1L, "ISNI1", "ISNI")).thenReturn(extId);
    when(orgDisambiguatedExternalIdentifierDao.findByDetails(1L, "FUNDREF1", "FUNDREF")).thenReturn(extIdPreferred);
    when(orgDisambiguatedExternalIdentifierDao.findByDetails(1L, "ORGREF1", "ORGREF")).thenReturn(extId);
    when(orgDisambiguatedExternalIdentifierDao.findByDetails(1L, "WIKIDATA1", "WIKIDATA")).thenReturn(extId);
    when(orgDisambiguatedExternalIdentifierDao.findByDetails(1L, "http://en.wikipedia.org/wiki/org_1", "WIKIPEDIA_URL")).thenReturn(extId);
    Path path = Paths.get(getClass().getClassLoader().getResource("grid/grid_1_org_6_external_identifiers.json").toURI());
    File testFile = path.toFile();
    loadGridData.setFileToLoad(testFile);
    loadGridData.execute();
    verify(orgDisambiguatedDao, times(0)).persist(Matchers.any(OrgDisambiguatedEntity.class));
    verify(orgDisambiguatedDao, times(0)).merge(Matchers.any(OrgDisambiguatedEntity.class));
    verify(orgDisambiguatedExternalIdentifierDao, times(1)).persist(Matchers.any(OrgDisambiguatedExternalIdentifierEntity.class));
    assertEquals(0L, loadGridData.getAddedDisambiguatedOrgs());
    assertEquals(1L, loadGridData.getAddedExternalIdentifiers());
    assertEquals(0L, loadGridData.getUpdatedOrgs());
    assertEquals(0L, loadGridData.getDeprecatedOrgs());
    assertEquals(0L, loadGridData.getObsoletedOrgs());
    assertEquals(0L, loadGridData.getUpdatedExternalIdentifiers());
}
Also used : Path(java.nio.file.Path) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OrgDisambiguatedEntity(org.orcid.persistence.jpa.entities.OrgDisambiguatedEntity) OrgDisambiguatedExternalIdentifierEntity(org.orcid.persistence.jpa.entities.OrgDisambiguatedExternalIdentifierEntity) File(java.io.File) Test(org.junit.Test)

Example 14 with OrgDisambiguatedExternalIdentifierEntity

use of org.orcid.persistence.jpa.entities.OrgDisambiguatedExternalIdentifierEntity in project ORCID-Source by ORCID.

the class LoadRinggoldDataTest method setupInitialMocks.

private void setupInitialMocks() {
    when(mockOrgDisambiguatedDao.findBySourceIdAndSourceType(anyString(), eq("RINGGOLD"))).thenAnswer(new Answer<OrgDisambiguatedEntity>() {

        @Override
        public OrgDisambiguatedEntity answer(InvocationOnMock invocation) throws Throwable {
            Long ringgoldId = Long.valueOf((String) invocation.getArgument(0));
            if (ringgoldId < 1L || ringgoldId > 4L) {
                return null;
            }
            OrgDisambiguatedEntity entity = new OrgDisambiguatedEntity();
            // 1000, 2000, 3000, 4000
            entity.setId(ringgoldId * 1000);
            entity.setSourceId(String.valueOf(ringgoldId));
            entity.setSourceType("RINGGOLD");
            OrgDisambiguatedExternalIdentifierEntity extId1, extId2;
            Set<OrgDisambiguatedExternalIdentifierEntity> extIds;
            switch(String.valueOf(ringgoldId)) {
                case "1":
                    entity.setName("1. Name");
                    entity.setCountry(Iso3166Country.US);
                    entity.setCity("City#1");
                    entity.setRegion("State#1");
                    entity.setOrgType("type/1");
                    entity.setSourceParentId(null);
                    entity.setStatus(null);
                    extId1 = new OrgDisambiguatedExternalIdentifierEntity();
                    extId1.setId(1L);
                    extId1.setOrgDisambiguated(entity);
                    extId1.setIdentifier("1");
                    extId1.setIdentifierType("ISNI");
                    extId2 = new OrgDisambiguatedExternalIdentifierEntity();
                    extId2.setId(2L);
                    extId2.setOrgDisambiguated(entity);
                    extId2.setIdentifier("2");
                    extId2.setIdentifierType("IPED");
                    extIds = new HashSet<>();
                    extIds.add(extId1);
                    extIds.add(extId2);
                    entity.setExternalIdentifiers(extIds);
                    break;
                case "2":
                    entity.setName("2. Name");
                    entity.setCountry(Iso3166Country.CR);
                    entity.setCity("City#2");
                    entity.setRegion(null);
                    entity.setOrgType("type/2");
                    entity.setSourceParentId(null);
                    entity.setStatus(null);
                    extId1 = new OrgDisambiguatedExternalIdentifierEntity();
                    extId1.setId(3L);
                    extId1.setOrgDisambiguated(entity);
                    extId1.setIdentifier("3");
                    extId1.setIdentifierType("NCES");
                    extId2 = new OrgDisambiguatedExternalIdentifierEntity();
                    extId2.setId(4L);
                    extId2.setOrgDisambiguated(entity);
                    extId2.setIdentifier("4");
                    extId2.setIdentifierType("OFR");
                    extIds = new HashSet<>();
                    extIds.add(extId1);
                    extIds.add(extId2);
                    entity.setExternalIdentifiers(extIds);
                    break;
                case "3":
                    entity.setName("3. Name");
                    entity.setCountry(Iso3166Country.US);
                    entity.setCity("City#3");
                    entity.setRegion(null);
                    entity.setOrgType("type/3");
                    entity.setSourceParentId(null);
                    entity.setStatus(null);
                    extId1 = new OrgDisambiguatedExternalIdentifierEntity();
                    extId1.setId(5L);
                    extId1.setOrgDisambiguated(entity);
                    extId1.setIdentifier("5");
                    extId1.setIdentifierType("ISNI");
                    extId2 = new OrgDisambiguatedExternalIdentifierEntity();
                    extId2.setId(6L);
                    extId2.setOrgDisambiguated(entity);
                    extId2.setIdentifier("6");
                    extId2.setIdentifierType("IPED");
                    extIds = new HashSet<>();
                    extIds.add(extId1);
                    extIds.add(extId2);
                    entity.setExternalIdentifiers(extIds);
                    break;
                case "4":
                    entity.setName("4. Name");
                    entity.setCountry(Iso3166Country.CR);
                    entity.setCity("City#4");
                    entity.setRegion("State#4");
                    entity.setOrgType("type/4");
                    entity.setSourceParentId("1");
                    entity.setStatus(null);
                    extId1 = new OrgDisambiguatedExternalIdentifierEntity();
                    extId1.setId(7L);
                    extId1.setOrgDisambiguated(entity);
                    extId1.setIdentifier("7");
                    extId1.setIdentifierType("NCES");
                    extId2 = new OrgDisambiguatedExternalIdentifierEntity();
                    extId1.setId(8L);
                    extId2.setOrgDisambiguated(entity);
                    extId2.setIdentifier("8");
                    extId2.setIdentifierType("OFR");
                    extIds = new HashSet<>();
                    extIds.add(extId1);
                    extIds.add(extId2);
                    entity.setExternalIdentifiers(extIds);
                    break;
            }
            return entity;
        }
    });
    when(mockOrgDao.findByNameCityRegionAndCountry(anyString(), anyString(), nullable(String.class), any())).thenAnswer(new Answer<OrgEntity>() {

        @Override
        public OrgEntity answer(InvocationOnMock invocation) throws Throwable {
            String name = invocation.getArgument(0);
            String city = invocation.getArgument(1);
            String region = invocation.getArgument(2);
            Iso3166Country country = (Iso3166Country) invocation.getArgument(3);
            OrgEntity entity = new OrgEntity();
            entity.setName(name);
            entity.setCity(city);
            entity.setRegion(region);
            entity.setCountry(country);
            if ("1. Alt Name".equals(name) && "AltCity#1".equals(city) && region == null && Iso3166Country.MX.equals(country)) {
                OrgDisambiguatedEntity od = new OrgDisambiguatedEntity();
                // ringgold_id * 1000
                od.setId(Long.valueOf(1000));
                entity.setOrgDisambiguated(od);
            } else if ("2. Alt Name".equals(name) && "AltCity#2".equals(city) && region == null && Iso3166Country.BR.equals(country)) {
                OrgDisambiguatedEntity od = new OrgDisambiguatedEntity();
                // ringgold_id * 1000
                od.setId(Long.valueOf(2000));
                entity.setOrgDisambiguated(od);
            } else if ("Testing Org".equals(name)) {
            // This is an org not linked to any disambiguated org yet
            } else {
                return null;
            }
            return entity;
        }
    });
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) OrgDisambiguatedExternalIdentifierEntity(org.orcid.persistence.jpa.entities.OrgDisambiguatedExternalIdentifierEntity) Iso3166Country(org.orcid.jaxb.model.message.Iso3166Country) OrgEntity(org.orcid.persistence.jpa.entities.OrgEntity) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) OrgDisambiguatedEntity(org.orcid.persistence.jpa.entities.OrgDisambiguatedEntity) HashSet(java.util.HashSet)

Example 15 with OrgDisambiguatedExternalIdentifierEntity

use of org.orcid.persistence.jpa.entities.OrgDisambiguatedExternalIdentifierEntity in project ORCID-Source by ORCID.

the class LoadRinggoldDataTest method test_removeAndAddExternalIdentifier.

@Test
public void test_removeAndAddExternalIdentifier() throws URISyntaxException {
    setupInitialMocks();
    Path path = Paths.get(getClass().getClassLoader().getResource("ringgold/test_RemoveAndAddExternalIdentifier/").toURI());
    File testDirectory = path.toFile();
    assertTrue(testDirectory.exists());
    loader.setDirectory(testDirectory);
    loader.execute();
    verify(mockOrgDisambiguatedDao, times(0)).persist(any());
    verify(mockOrgDisambiguatedDao, times(0)).merge(any());
    ArgumentCaptor<OrgDisambiguatedExternalIdentifierEntity> addCaptor = ArgumentCaptor.forClass(OrgDisambiguatedExternalIdentifierEntity.class);
    ArgumentCaptor<Long> removeCaptor = ArgumentCaptor.forClass(Long.class);
    verify(mockOrgDisambiguatedExternalIdentifierDao, times(1)).persist(addCaptor.capture());
    verify(mockOrgDisambiguatedExternalIdentifierDao, times(0)).merge(any());
    verify(mockOrgDisambiguatedExternalIdentifierDao, times(1)).remove(removeCaptor.capture());
    List<OrgDisambiguatedExternalIdentifierEntity> addedList = addCaptor.getAllValues();
    assertEquals(1, addedList.size());
    OrgDisambiguatedExternalIdentifierEntity entity = addedList.get(0);
    assertEquals("9", entity.getIdentifier());
    assertEquals("ISNI", entity.getIdentifierType());
    assertEquals(false, entity.getPreferred());
    assertEquals(Long.valueOf(1000), entity.getOrgDisambiguated().getId());
    List<Long> deletedIds = removeCaptor.getAllValues();
    assertEquals(1, deletedIds.size());
    Long deletedId = deletedIds.get(0);
    assertEquals(Long.valueOf(5), deletedId);
    verify(mockOrgDao, times(0)).persist(any());
    verify(mockOrgDao, times(0)).merge(any());
}
Also used : Path(java.nio.file.Path) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) OrgDisambiguatedExternalIdentifierEntity(org.orcid.persistence.jpa.entities.OrgDisambiguatedExternalIdentifierEntity) File(java.io.File) Test(org.junit.Test)

Aggregations

OrgDisambiguatedExternalIdentifierEntity (org.orcid.persistence.jpa.entities.OrgDisambiguatedExternalIdentifierEntity)16 OrgDisambiguatedEntity (org.orcid.persistence.jpa.entities.OrgDisambiguatedEntity)9 File (java.io.File)8 Path (java.nio.file.Path)8 Test (org.junit.Test)8 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 Date (java.util.Date)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)2 OrgEntity (org.orcid.persistence.jpa.entities.OrgEntity)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 Transactional (javax.transaction.Transactional)1 Pair (org.apache.commons.lang3.tuple.Pair)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1