Skip to content

Commit

Permalink
Adding more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amontenegro committed Apr 4, 2024
1 parent a49d476 commit 791d080
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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 = ",";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ public void setValidated(boolean validated) {
public static List<ExternalIdentifiersSummary> valueOf(PersonExternalIdentifiers personExternalIdentifiers, String orcid) {
List<ExternalIdentifiersSummary> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AffiliationType, List<AffiliationGroup<AffiliationSummary>>> affiliations = new HashMap<AffiliationType, List<AffiliationGroup<AffiliationSummary>>>();

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
Expand Down Expand Up @@ -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<AffiliationType, List<AffiliationGroup<AffiliationSummary>>> 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<AffiliationType, List<AffiliationGroup<AffiliationSummary>>> 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<AffiliationType, List<AffiliationGroup<AffiliationSummary>>> 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<AffiliationType, List<AffiliationGroup<AffiliationSummary>>> 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<AffiliationType, List<AffiliationGroup<AffiliationSummary>>> 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
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -405,4 +560,17 @@ private List<PeerReviewMinimizedSummary> getPeerReviewSummaryList() {
}
return peerReviews;
}

private Map<AffiliationType, List<AffiliationGroup<AffiliationSummary>>> generateAffiliations() {
Map<AffiliationType, List<AffiliationGroup<AffiliationSummary>>> affiliations = new HashMap<AffiliationType, List<AffiliationGroup<AffiliationSummary>>>();

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;
}
}

0 comments on commit 791d080

Please sign in to comment.