From 791d080696ac757342c281af44e1e46ba3c55259 Mon Sep 17 00:00:00 2001 From: amontenegro Date: Wed, 3 Apr 2024 21:09:29 -0600 Subject: [PATCH] Adding more unit tests --- .../impl/WorkManagerReadOnlyImpl.java | 28 +-- .../summary/ExternalIdentifiersSummary.java | 8 +- .../common/manager/SummaryManagerTest.java | 200 ++++++++++++++++-- 3 files changed, 199 insertions(+), 37 deletions(-) diff --git a/orcid-core/src/main/java/org/orcid/core/manager/v3/read_only/impl/WorkManagerReadOnlyImpl.java b/orcid-core/src/main/java/org/orcid/core/manager/v3/read_only/impl/WorkManagerReadOnlyImpl.java index ca0ebf5c75c..e5a611f3f2b 100644 --- a/orcid-core/src/main/java/org/orcid/core/manager/v3/read_only/impl/WorkManagerReadOnlyImpl.java +++ b/orcid-core/src/main/java/org/orcid/core/manager/v3/read_only/impl/WorkManagerReadOnlyImpl.java @@ -1,5 +1,15 @@ package org.orcid.core.manager.v3.read_only.impl; +import java.math.BigInteger; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import javax.annotation.Resource; + import org.orcid.core.adapter.jsonidentifier.converter.JSONWorkExternalIdentifiersConverterV3; import org.orcid.core.adapter.v3.JpaJaxbWorkAdapter; import org.orcid.core.adapter.v3.converter.ContributorsRolesAndSequencesConverter; @@ -15,19 +25,12 @@ import org.orcid.core.manager.v3.read_only.ClientDetailsManagerReadOnly; import org.orcid.core.manager.v3.read_only.WorkManagerReadOnly; import org.orcid.core.togglz.Features; -import org.orcid.core.utils.comparators.DateComparator; -import org.orcid.core.utils.comparators.DateComparatorWorkGroupExtended; -import org.orcid.core.utils.comparators.TitleComparator; -import org.orcid.core.utils.comparators.TitleComparatorWorkGroupExtended; -import org.orcid.core.utils.comparators.TypeComparator; -import org.orcid.core.utils.comparators.TypeComparatorWorkGroupExtended; import org.orcid.core.utils.v3.ContributorUtils; import org.orcid.core.utils.v3.activities.ActivitiesGroup; import org.orcid.core.utils.v3.activities.ActivitiesGroupGenerator; import org.orcid.core.utils.v3.activities.WorkComparators; import org.orcid.core.utils.v3.activities.WorkGroupAndGroupingSuggestionGenerator; import org.orcid.jaxb.model.record.bulk.BulkElement; -import org.orcid.jaxb.model.v3.release.common.Visibility; import org.orcid.jaxb.model.v3.release.record.ExternalID; import org.orcid.jaxb.model.v3.release.record.ExternalIDs; import org.orcid.jaxb.model.v3.release.record.GroupAble; @@ -51,17 +54,6 @@ import org.orcid.pojo.grouping.WorkGroupingSuggestion; import org.springframework.beans.factory.annotation.Value; -import javax.annotation.Resource; -import java.math.BigInteger; -import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - public class WorkManagerReadOnlyImpl extends ManagerReadOnlyBaseImpl implements WorkManagerReadOnly { public static final String BULK_PUT_CODES_DELIMITER = ","; diff --git a/orcid-core/src/main/java/org/orcid/pojo/summary/ExternalIdentifiersSummary.java b/orcid-core/src/main/java/org/orcid/pojo/summary/ExternalIdentifiersSummary.java index 61fab224d86..12013555ba8 100644 --- a/orcid-core/src/main/java/org/orcid/pojo/summary/ExternalIdentifiersSummary.java +++ b/orcid-core/src/main/java/org/orcid/pojo/summary/ExternalIdentifiersSummary.java @@ -58,9 +58,11 @@ public void setValidated(boolean validated) { public static List valueOf(PersonExternalIdentifiers personExternalIdentifiers, String orcid) { List externalIdentifiersSummaryList = new ArrayList<>(); - personExternalIdentifiers.getExternalIdentifiers().forEach(personExternalIdentifier -> { - externalIdentifiersSummaryList.add(ExternalIdentifiersSummary.valueOf(personExternalIdentifier, orcid)); - }); + if(personExternalIdentifiers != null) { + personExternalIdentifiers.getExternalIdentifiers().forEach(personExternalIdentifier -> { + externalIdentifiersSummaryList.add(ExternalIdentifiersSummary.valueOf(personExternalIdentifier, orcid)); + }); + } return externalIdentifiersSummaryList; } diff --git a/orcid-core/src/test/java/org/orcid/core/common/manager/SummaryManagerTest.java b/orcid-core/src/test/java/org/orcid/core/common/manager/SummaryManagerTest.java index 6d1751dfe9f..5c68c2df8c9 100644 --- a/orcid-core/src/test/java/org/orcid/core/common/manager/SummaryManagerTest.java +++ b/orcid-core/src/test/java/org/orcid/core/common/manager/SummaryManagerTest.java @@ -118,27 +118,15 @@ public void setUp() { ReflectionTestUtils.setField(manager, "recordNameManagerReadOnly", recordNameManagerReadOnlyMock); // Set external identifiers - PersonExternalIdentifiers personExternalIdentifiers = getPersonExternalIdentifiers(); - Mockito.when(externalIdentifierManagerReadOnlyMock.getPublicExternalIdentifiers(Mockito.eq(ORCID))).thenReturn(personExternalIdentifiers); + Mockito.when(externalIdentifierManagerReadOnlyMock.getPublicExternalIdentifiers(Mockito.eq(ORCID))).thenReturn(getPersonExternalIdentifiers()); ReflectionTestUtils.setField(manager, "externalIdentifierManagerReadOnly", externalIdentifierManagerReadOnlyMock); // Set affiliations - Map>> affiliations = new HashMap>>(); - - affiliations.put(AffiliationType.DISTINCTION, getAffiliations(AffiliationType.DISTINCTION)); - affiliations.put(AffiliationType.EDUCATION, getAffiliations(AffiliationType.EDUCATION)); - affiliations.put(AffiliationType.EMPLOYMENT, getAffiliations(AffiliationType.EMPLOYMENT)); - affiliations.put(AffiliationType.INVITED_POSITION, getAffiliations(AffiliationType.INVITED_POSITION)); - affiliations.put(AffiliationType.MEMBERSHIP, getAffiliations(AffiliationType.MEMBERSHIP)); - affiliations.put(AffiliationType.QUALIFICATION, getAffiliations(AffiliationType.QUALIFICATION)); - affiliations.put(AffiliationType.SERVICE, getAffiliations(AffiliationType.SERVICE)); - - Mockito.when(affiliationsManagerReadOnlyMock.getGroupedAffiliations(Mockito.eq(ORCID), Mockito.eq(true))).thenReturn(affiliations); + Mockito.when(affiliationsManagerReadOnlyMock.getGroupedAffiliations(Mockito.eq(ORCID), Mockito.eq(true))).thenReturn(generateAffiliations()); ReflectionTestUtils.setField(manager, "affiliationsManagerReadOnly", affiliationsManagerReadOnlyMock); - // Set works - Works works = getWorkGroups(); - Mockito.when(worksCacheManagerMock.getGroupedWorks(Mockito.eq(ORCID))).thenReturn(works); + // Set works + Mockito.when(worksCacheManagerMock.getGroupedWorks(Mockito.eq(ORCID))).thenReturn(getWorkGroups()); ReflectionTestUtils.setField(manager, "worksCacheManager", worksCacheManagerMock); // Set fundings @@ -199,12 +187,122 @@ public void generateAffiliationsSummaryTest() { // 3 of every professional activity type assertEquals(12, rs.getProfessionalActivitiesCount()); } + + @Test + public void generateAffiliationsSummary_EmptyTest() { + RecordSummary rs = new RecordSummary(); + Map>> affiliations = generateAffiliations(); + affiliations.remove(AffiliationType.DISTINCTION); + affiliations.remove(AffiliationType.EDUCATION); + affiliations.remove(AffiliationType.EMPLOYMENT); + affiliations.remove(AffiliationType.INVITED_POSITION); + affiliations.remove(AffiliationType.MEMBERSHIP); + affiliations.remove(AffiliationType.QUALIFICATION); + affiliations.remove(AffiliationType.SERVICE); + + Mockito.when(affiliationsManagerReadOnlyMock.getGroupedAffiliations(Mockito.eq(ORCID), Mockito.eq(true))).thenReturn(affiliations); + manager.generateAffiliationsSummary(rs, ORCID); + assertEquals(0, rs.getEmploymentAffiliations().size()); + assertEquals(0, rs.getEmploymentAffiliationsCount()); + assertEquals(0, rs.getProfessionalActivities().size()); + assertEquals(0, rs.getProfessionalActivitiesCount()); + } + + @Test + public void generateAffiliationsSummary_EmploymentOnlyTest() { + RecordSummary rs = new RecordSummary(); + Map>> affiliations = generateAffiliations(); + affiliations.remove(AffiliationType.DISTINCTION); + affiliations.remove(AffiliationType.EDUCATION); + affiliations.remove(AffiliationType.INVITED_POSITION); + affiliations.remove(AffiliationType.MEMBERSHIP); + affiliations.remove(AffiliationType.QUALIFICATION); + affiliations.remove(AffiliationType.SERVICE); + + Mockito.when(affiliationsManagerReadOnlyMock.getGroupedAffiliations(Mockito.eq(ORCID), Mockito.eq(true))).thenReturn(affiliations); + manager.generateAffiliationsSummary(rs, ORCID); + assertEquals(3, rs.getEmploymentAffiliations().size()); + assertEquals(3, rs.getEmploymentAffiliationsCount()); + assertEquals(0, rs.getProfessionalActivities().size()); + assertEquals(0, rs.getProfessionalActivitiesCount()); + } + + @Test + public void generateAffiliationsSummary_EducationOnlyTest() { + RecordSummary rs = new RecordSummary(); + Map>> affiliations = generateAffiliations(); + affiliations.remove(AffiliationType.DISTINCTION); + affiliations.remove(AffiliationType.EMPLOYMENT); + affiliations.remove(AffiliationType.INVITED_POSITION); + affiliations.remove(AffiliationType.MEMBERSHIP); + affiliations.remove(AffiliationType.QUALIFICATION); + affiliations.remove(AffiliationType.SERVICE); + + Mockito.when(affiliationsManagerReadOnlyMock.getGroupedAffiliations(Mockito.eq(ORCID), Mockito.eq(true))).thenReturn(affiliations); + manager.generateAffiliationsSummary(rs, ORCID); + assertEquals(0, rs.getEmploymentAffiliations().size()); + assertEquals(0, rs.getEmploymentAffiliationsCount()); + assertEquals(0, rs.getProfessionalActivities().size()); + assertEquals(0, rs.getProfessionalActivitiesCount()); + } + + @Test + public void generateAffiliationsSummary_InvitedPositionOnlyTest() { + RecordSummary rs = new RecordSummary(); + Map>> affiliations = generateAffiliations(); + affiliations.remove(AffiliationType.DISTINCTION); + affiliations.remove(AffiliationType.EMPLOYMENT); + affiliations.remove(AffiliationType.MEMBERSHIP); + affiliations.remove(AffiliationType.QUALIFICATION); + affiliations.remove(AffiliationType.SERVICE); + + Mockito.when(affiliationsManagerReadOnlyMock.getGroupedAffiliations(Mockito.eq(ORCID), Mockito.eq(true))).thenReturn(affiliations); + manager.generateAffiliationsSummary(rs, ORCID); + assertEquals(0, rs.getEmploymentAffiliations().size()); + assertEquals(0, rs.getEmploymentAffiliationsCount()); + assertEquals(3, rs.getProfessionalActivities().size()); + assertEquals(3, rs.getProfessionalActivitiesCount()); + } + + @Test + public void generateAffiliationsSummary_ProfessionalActivitiesOnlyTest() { + RecordSummary rs = new RecordSummary(); + Map>> affiliations = generateAffiliations(); + affiliations.remove(AffiliationType.EMPLOYMENT); + + Mockito.when(affiliationsManagerReadOnlyMock.getGroupedAffiliations(Mockito.eq(ORCID), Mockito.eq(true))).thenReturn(affiliations); + manager.generateAffiliationsSummary(rs, ORCID); + assertEquals(0, rs.getEmploymentAffiliations().size()); + assertEquals(0, rs.getEmploymentAffiliationsCount()); + assertEquals(3, rs.getProfessionalActivities().size()); + assertEquals(12, rs.getProfessionalActivitiesCount()); + } @Test public void generateExternalIdentifiersSummaryTest() { RecordSummary rs = new RecordSummary(); manager.generateExternalIdentifiersSummary(rs, ORCID); assertEquals(1, rs.getExternalIdentifiers().size()); + assertEquals("0", rs.getExternalIdentifiers().get(0).getId()); + assertEquals("0000", rs.getExternalIdentifiers().get(0).getReference()); + } + + @Test + public void generateExternalIdentifiersSummary_NullTest() { + RecordSummary rs = new RecordSummary(); + Mockito.when(externalIdentifierManagerReadOnlyMock.getPublicExternalIdentifiers(Mockito.eq(ORCID))).thenReturn(null); + + manager.generateExternalIdentifiersSummary(rs, ORCID); + assertEquals(0, rs.getExternalIdentifiers().size()); + } + + @Test + public void generateExternalIdentifiersSummary_EmptyTest() { + RecordSummary rs = new RecordSummary(); + Mockito.when(externalIdentifierManagerReadOnlyMock.getPublicExternalIdentifiers(Mockito.eq(ORCID))).thenReturn(new PersonExternalIdentifiers()); + + manager.generateExternalIdentifiersSummary(rs, ORCID); + assertEquals(0, rs.getExternalIdentifiers().size()); } @Test @@ -215,6 +313,62 @@ public void generateWorksSummaryTest() { assertEquals(3, rs.getValidatedWorks()); } + @Test + public void generateWorksSummary_OboValidatedTest() { + RecordSummary rs = new RecordSummary(); + Works works = getWorkGroups(); + Source s = new Source(); + s.setSourceClientId(new SourceClientId(CLIENT1)); + s.setAssertionOriginClientId(new SourceClientId(CLIENT1)); + for(WorkGroup wg : works.getWorkGroup()) { + for(WorkSummary ws : wg.getWorkSummary()) { + ws.setSource(s); + } + } + Mockito.when(worksCacheManagerMock.getGroupedWorks(Mockito.eq(ORCID))).thenReturn(works); + + manager.generateWorksSummary(rs, ORCID); + assertEquals(0, rs.getSelfAssertedWorks()); + assertEquals(3, rs.getValidatedWorks()); + } + + @Test + public void generateWorksSummary_SelfAssertedOnlyTest() { + RecordSummary rs = new RecordSummary(); + Works works = getWorkGroups(); + Source s = new Source(); + s.setSourceOrcid(new SourceOrcid(ORCID)); + for(WorkGroup wg : works.getWorkGroup()) { + for(WorkSummary ws : wg.getWorkSummary()) { + ws.setSource(s); + } + } + Mockito.when(worksCacheManagerMock.getGroupedWorks(Mockito.eq(ORCID))).thenReturn(works); + + manager.generateWorksSummary(rs, ORCID); + assertEquals(3, rs.getSelfAssertedWorks()); + assertEquals(0, rs.getValidatedWorks()); + } + + @Test + public void generateWorksSummary_OboSelfAssertedOnlyTest() { + RecordSummary rs = new RecordSummary(); + Works works = getWorkGroups(); + Source s = new Source(); + s.setSourceClientId(new SourceClientId(CLIENT1)); + s.setAssertionOriginOrcid(new SourceOrcid(ORCID)); + for(WorkGroup wg : works.getWorkGroup()) { + for(WorkSummary ws : wg.getWorkSummary()) { + ws.setSource(s); + } + } + Mockito.when(worksCacheManagerMock.getGroupedWorks(Mockito.eq(ORCID))).thenReturn(works); + + manager.generateWorksSummary(rs, ORCID); + assertEquals(3, rs.getSelfAssertedWorks()); + assertEquals(0, rs.getValidatedWorks()); + } + @Test public void generateFundingSummaryTest() { RecordSummary rs = new RecordSummary(); @@ -306,6 +460,7 @@ private Works getWorkGroups() { // Fourth source is the user through OBO for (int j = 0; j < 4; j++) { WorkSummary ws = new WorkSummary(); + ws.setVisibility(Visibility.PUBLIC); WorkTitle wt = new WorkTitle(); wt.setTitle(new Title("Work-" + j)); ws.setTitle(wt); @@ -405,4 +560,17 @@ private List getPeerReviewSummaryList() { } return peerReviews; } + + private Map>> generateAffiliations() { + Map>> affiliations = new HashMap>>(); + + affiliations.put(AffiliationType.DISTINCTION, getAffiliations(AffiliationType.DISTINCTION)); + affiliations.put(AffiliationType.EDUCATION, getAffiliations(AffiliationType.EDUCATION)); + affiliations.put(AffiliationType.EMPLOYMENT, getAffiliations(AffiliationType.EMPLOYMENT)); + affiliations.put(AffiliationType.INVITED_POSITION, getAffiliations(AffiliationType.INVITED_POSITION)); + affiliations.put(AffiliationType.MEMBERSHIP, getAffiliations(AffiliationType.MEMBERSHIP)); + affiliations.put(AffiliationType.QUALIFICATION, getAffiliations(AffiliationType.QUALIFICATION)); + affiliations.put(AffiliationType.SERVICE, getAffiliations(AffiliationType.SERVICE)); + return affiliations; + } }