From 66db098b4759a4b55985fc72730bd30aa31661d5 Mon Sep 17 00:00:00 2001 From: Russ Poetker Date: Sat, 7 Oct 2023 14:16:41 -0400 Subject: [PATCH] Add tests --- .../grant/data/JhuPassInitUpdaterIT.java | 120 ++++++++++++---- .../support/grant/data/JhuPassUpdaterIT.java | 135 +++++++++++++++--- 2 files changed, 208 insertions(+), 47 deletions(-) diff --git a/pass-grant-loader/pass-grant-data/src/test/java/org/eclipse/pass/support/grant/data/JhuPassInitUpdaterIT.java b/pass-grant-loader/pass-grant-data/src/test/java/org/eclipse/pass/support/grant/data/JhuPassInitUpdaterIT.java index 6ecdbe66..132db707 100644 --- a/pass-grant-loader/pass-grant-data/src/test/java/org/eclipse/pass/support/grant/data/JhuPassInitUpdaterIT.java +++ b/pass-grant-loader/pass-grant-data/src/test/java/org/eclipse/pass/support/grant/data/JhuPassInitUpdaterIT.java @@ -41,6 +41,10 @@ import static org.eclipse.pass.support.grant.data.JhuPassUpdater.JHED_LOCATOR_ID; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import java.io.IOException; import java.util.ArrayList; @@ -48,6 +52,7 @@ import java.util.List; import java.util.Map; +import org.apache.commons.lang3.reflect.FieldUtils; import org.eclipse.pass.support.client.PassClient; import org.eclipse.pass.support.client.PassClientResult; import org.eclipse.pass.support.client.PassClientSelector; @@ -56,9 +61,12 @@ import org.eclipse.pass.support.client.model.Grant; import org.eclipse.pass.support.client.model.Policy; import org.eclipse.pass.support.client.model.User; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class JhuPassInitUpdaterIT { private final String[] grantAwardNumber = {"A10000000", "A10000001", "A10000002"}; @@ -77,29 +85,11 @@ public class JhuPassInitUpdaterIT { private final String[] userLastName = {"Melon", "Einstein", "Jones"}; private final String[] userEmail = {"amelon1@jhu.edu", "aeinst1@jhu.edu", "jjones1@jhu.edu"}; - private String primaryFunderPolicyUriString; - private String directFunderPolicyUriString; - private final String grantIdPrefix = "johnshopkins.edu:grant:"; //private final String funderIdPrefix = "johnshopkins.edu:funder:"; private final PassClient passClient = PassClient.newInstance(); - @BeforeEach - public void setup() throws IOException { - Policy policy1 = new Policy(); - policy1.setTitle("Primary Policy"); - policy1.setDescription("MOO"); - passClient.createObject(policy1); - primaryFunderPolicyUriString = policy1.getId(); - - Policy policy2 = new Policy(); - policy2.setTitle("Direct Policy"); - policy2.setDescription("MOO"); - passClient.createObject(policy2); - directFunderPolicyUriString = policy2.getId(); - } - /** * we put an initial award for a grant into PASS, then simulate a pull of all records related * to this grant from the Beginning of Time (including records which created the initial object) @@ -110,6 +100,7 @@ public void setup() throws IOException { * */ @Test + @Order(1) public void processInitGrantIT() throws IOException { // GIVEN List> resultSet = new ArrayList<>(); @@ -187,6 +178,59 @@ public void processInitGrantIT() throws IOException { assertTrue(updatePassGrant.getCoPis().contains(user2));//Jones } + @Test + @Order(2) + public void processInitGrantIT_DoesNotUpdateWithNoChange() throws IOException, IllegalAccessException { + // GIVEN + Map piRecord0 = makeRowMap(0, 0, "P"); + Map coPiRecord0 = makeRowMap(0, 1, "C"); + Map piRecord1 = makeRowMap(1, 0, "P"); + Map coPiRecord1 = makeRowMap(1, 1, "C"); + Map newCoPiRecord1 = makeRowMap(1, 2, "C"); + Map piRecord2 = makeRowMap(2, 1, "P"); + + //in the initial pull, we will find all of the records (check?) + List> resultSet = new ArrayList<>(); + resultSet.add(piRecord0); + resultSet.add(coPiRecord0); + resultSet.add(piRecord1); + resultSet.add(coPiRecord1); + resultSet.add(newCoPiRecord1); + resultSet.add(piRecord2); + + PassClient spyPassClient = spy(passClient); + JhuPassInitUpdater passUpdater = new JhuPassInitUpdater(); + FieldUtils.writeField(passUpdater, "passClient", spyPassClient, true); + + // WHEN + passUpdater.updatePass(resultSet, "grant"); + + // THEN + PassClientSelector grantSelector = new PassClientSelector<>(Grant.class); + grantSelector.setFilter(RSQL.equals("localKey", grantIdPrefix + grantLocalKey[2])); + grantSelector.setInclude("primaryFunder", "directFunder", "pi", "coPis"); + PassClientResult resultGrant = passClient.selectObjects(grantSelector); + assertEquals(1, resultGrant.getTotal()); + Grant passGrant = resultGrant.getObjects().get(0); + + User user0 = getVerifiedUser(0); + User user1 = getVerifiedUser(1); + User user2 = getVerifiedUser(2); + + verify(spyPassClient, times(0)).updateObject(any()); + assertEquals(grantAwardNumber[0], passGrant.getAwardNumber());//initial + assertEquals(AwardStatus.ACTIVE, passGrant.getAwardStatus()); + assertEquals(grantIdPrefix + grantLocalKey[0], passGrant.getLocalKey()); + assertEquals(grantProjectName[0], passGrant.getProjectName());//initial + assertEquals(createZonedDateTime(grantAwardDate[0]), passGrant.getAwardDate());//initial + assertEquals(createZonedDateTime(grantStartDate[0]), passGrant.getStartDate());//initial + assertEquals(createZonedDateTime(grantEndDate[2]), passGrant.getEndDate());//latest + assertEquals(user1, passGrant.getPi());//Einstein + assertEquals(2, passGrant.getCoPis().size()); + assertTrue(passGrant.getCoPis().contains(user0));//Melon + assertTrue(passGrant.getCoPis().contains(user2));//Jones + } + private User getVerifiedUser(int userIndex) throws IOException { PassClientSelector userSelector = new PassClientSelector<>(User.class); userSelector.setFilter(RSQL.hasMember("locatorIds", EMPLOYEE_LOCATOR_ID + userEmployeeId[userIndex])); @@ -207,7 +251,7 @@ private User getVerifiedUser(int userIndex) throws IOException { * @param abbrRole the role: Pi ("P") or co-pi (C" or "K") * @return the row map for the record */ - private Map makeRowMap(int iteration, int user, String abbrRole) { + private Map makeRowMap(int iteration, int user, String abbrRole) throws IOException { Map rowMap = new HashMap<>(); rowMap.put(C_GRANT_AWARD_NUMBER, grantAwardNumber[iteration]); rowMap.put(C_GRANT_AWARD_STATUS, "Active"); @@ -217,9 +261,9 @@ private Map makeRowMap(int iteration, int user, String abbrRole) rowMap.put(C_GRANT_START_DATE, grantStartDate[iteration]); rowMap.put(C_GRANT_END_DATE, grantEndDate[iteration]); - rowMap.put(C_DIRECT_FUNDER_LOCAL_KEY, "20000000"); + rowMap.put(C_DIRECT_FUNDER_LOCAL_KEY, "30000000"); rowMap.put(C_DIRECT_FUNDER_NAME, "Enormous State University"); - rowMap.put(C_PRIMARY_FUNDER_LOCAL_KEY, "20000001"); + rowMap.put(C_PRIMARY_FUNDER_LOCAL_KEY, "30000001"); rowMap.put(C_PRIMARY_FUNDER_NAME, "J L Gotrocks Foundation"); rowMap.put(C_USER_FIRST_NAME, userFirstName[user]); @@ -232,10 +276,38 @@ private Map makeRowMap(int iteration, int user, String abbrRole) rowMap.put(C_UPDATE_TIMESTAMP, grantUpdateTimestamp[iteration]); rowMap.put(C_ABBREVIATED_ROLE, abbrRole); - rowMap.put(C_DIRECT_FUNDER_POLICY, directFunderPolicyUriString); - rowMap.put(C_PRIMARY_FUNDER_POLICY, primaryFunderPolicyUriString); + rowMap.put(C_DIRECT_FUNDER_POLICY, getDirectFunderPolicyId()); + rowMap.put(C_PRIMARY_FUNDER_POLICY, getPrimaryFunderPolicyId()); return rowMap; } + private String getPrimaryFunderPolicyId() throws IOException { + PassClientSelector policySelector = new PassClientSelector<>(Policy.class); + policySelector.setFilter(RSQL.equals("title", "Init Primary Funder Policy")); + PassClientResult resultPolicy = passClient.selectObjects(policySelector); + if (resultPolicy.getObjects().isEmpty()) { + Policy policy1 = new Policy(); + policy1.setTitle("Init Primary Funder Policy"); + policy1.setDescription("BAA"); + passClient.createObject(policy1); + return policy1.getId(); + } + return resultPolicy.getObjects().get(0).getId(); + } + + private String getDirectFunderPolicyId() throws IOException { + PassClientSelector policySelector = new PassClientSelector<>(Policy.class); + policySelector.setFilter(RSQL.equals("title", "Init Direct Funder Policy")); + PassClientResult resultPolicy = passClient.selectObjects(policySelector); + if (resultPolicy.getObjects().isEmpty()) { + Policy policy1 = new Policy(); + policy1.setTitle("Init Direct Funder Policy"); + policy1.setDescription("BAA"); + passClient.createObject(policy1); + return policy1.getId(); + } + return resultPolicy.getObjects().get(0).getId(); + } + } diff --git a/pass-grant-loader/pass-grant-data/src/test/java/org/eclipse/pass/support/grant/data/JhuPassUpdaterIT.java b/pass-grant-loader/pass-grant-data/src/test/java/org/eclipse/pass/support/grant/data/JhuPassUpdaterIT.java index ba824370..b402cb12 100644 --- a/pass-grant-loader/pass-grant-data/src/test/java/org/eclipse/pass/support/grant/data/JhuPassUpdaterIT.java +++ b/pass-grant-loader/pass-grant-data/src/test/java/org/eclipse/pass/support/grant/data/JhuPassUpdaterIT.java @@ -41,6 +41,10 @@ import static org.eclipse.pass.support.grant.data.JhuPassUpdater.JHED_LOCATOR_ID; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import java.io.IOException; import java.util.ArrayList; @@ -48,6 +52,7 @@ import java.util.List; import java.util.Map; +import org.apache.commons.lang3.reflect.FieldUtils; import org.eclipse.pass.support.client.PassClient; import org.eclipse.pass.support.client.PassClientResult; import org.eclipse.pass.support.client.PassClientSelector; @@ -56,7 +61,6 @@ import org.eclipse.pass.support.client.model.Grant; import org.eclipse.pass.support.client.model.Policy; import org.eclipse.pass.support.client.model.User; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class JhuPassUpdaterIT { @@ -85,29 +89,11 @@ public class JhuPassUpdaterIT { private final String[] userEmail = {"arecko1@jhu.edu", "sclass1@jhu.edu", "jgunn1@jhu.edu", "jdoe1@jhu.edu", "jdoe2@jhu.edu"}; - private String primaryFunderPolicyUriString; - private String directFunderPolicyUriString; - private final String grantIdPrefix = "johnshopkins.edu:grant:"; //private final String funderIdPrefix = "johnshopkins.edu:funder:"; private final PassClient passClient = PassClient.newInstance(); - @BeforeEach - public void setup() throws IOException { - Policy policy1 = new Policy(); - policy1.setTitle("Primary Policy 2"); - policy1.setDescription("BAA"); - passClient.createObject(policy1); - primaryFunderPolicyUriString = policy1.getId(); - - Policy policy2 = new Policy(); - policy2.setTitle("Direct Policy 2"); - policy2.setDescription("BAA"); - passClient.createObject(policy2); - directFunderPolicyUriString = policy2.getId(); - } - /** * we put an initial award for a grant into PASS, then simulate a pull of all subsequent records *

@@ -134,7 +120,7 @@ public void processGrantIT() throws IOException { // THEN PassClientSelector grantSelector = new PassClientSelector<>(Grant.class); - grantSelector.setFilter(RSQL.equals("localKey", grantIdPrefix + grantLocalKey[2])); + grantSelector.setFilter(RSQL.equals("localKey", grantIdPrefix + grantLocalKey[0])); grantSelector.setInclude("primaryFunder", "directFunder", "pi", "coPis"); PassClientResult resultGrant = passClient.selectObjects(grantSelector); assertEquals(1, resultGrant.getTotal()); @@ -200,6 +186,81 @@ public void processGrantIT() throws IOException { assertTrue(updatePassGrant.getCoPis().contains(user2));//Gunn } + @Test + public void processGrantIT_DoesNotUpdateWithNoChange() throws IOException, IllegalAccessException { + // GIVEN + Map piRecord0 = makeRowMap(3, 3, "P"); + Map coPiRecord0 = makeRowMap(3, 3, "C"); + + List> resultSet = new ArrayList<>(); + resultSet.add(piRecord0); + resultSet.add(coPiRecord0); + + PassClient spyPassClient = spy(passClient); + JhuPassUpdater passUpdater = new JhuPassUpdater(); + FieldUtils.writeField(passUpdater, "passClient", spyPassClient, true); + + // WHEN + passUpdater.updatePass(resultSet, "grant"); + + // THEN + verify(spyPassClient, times(1)).createObject(any(Grant.class)); + PassClientSelector grantSelector = new PassClientSelector<>(Grant.class); + grantSelector.setFilter(RSQL.equals("localKey", grantIdPrefix + grantLocalKey[3])); + grantSelector.setInclude("primaryFunder", "directFunder", "pi", "coPis"); + PassClientResult resultGrant = passClient.selectObjects(grantSelector); + assertEquals(1, resultGrant.getTotal()); + Grant passGrant = resultGrant.getObjects().get(0); + + User user3 = getVerifiedUser(3); + + assertEquals(grantAwardNumber[3], passGrant.getAwardNumber()); + assertEquals(AwardStatus.ACTIVE, passGrant.getAwardStatus()); + assertEquals(grantIdPrefix + grantLocalKey[3], passGrant.getLocalKey()); + assertEquals(grantProjectName[3], passGrant.getProjectName()); + assertEquals(createZonedDateTime(grantAwardDate[3]), passGrant.getAwardDate()); + assertEquals(createZonedDateTime(grantStartDate[3]), passGrant.getStartDate()); + assertEquals(createZonedDateTime(grantEndDate[3]), passGrant.getEndDate()); + assertEquals(grantUpdateTimestamp[3], passUpdater.getLatestUpdate());//latest + assertEquals(user3, passGrant.getPi()); //Reckondwith + assertEquals(1, passGrant.getCoPis().size()); + assertEquals(user3, passGrant.getCoPis().get(0)); + + //check statistics + assertEquals(1, passUpdater.getStatistics().getGrantsCreated()); + assertEquals(1, passUpdater.getStatistics().getUsersCreated()); + assertEquals(1, passUpdater.getStatistics().getPisAdded()); + assertEquals(1, passUpdater.getStatistics().getCoPisAdded()); + + // WHEN + passUpdater.updatePass(resultSet, "grant"); + + // THEN + verify(spyPassClient, times(0)).updateObject(any()); + PassClientResult resultGrant2 = passClient.selectObjects(grantSelector); + assertEquals(1, resultGrant2.getTotal()); + Grant passGrant2 = resultGrant2.getObjects().get(0); + + User user3_2 = getVerifiedUser(3); + + assertEquals(grantAwardNumber[3], passGrant2.getAwardNumber()); + assertEquals(AwardStatus.ACTIVE, passGrant2.getAwardStatus()); + assertEquals(grantIdPrefix + grantLocalKey[3], passGrant2.getLocalKey()); + assertEquals(grantProjectName[3], passGrant2.getProjectName()); + assertEquals(createZonedDateTime(grantAwardDate[3]), passGrant2.getAwardDate()); + assertEquals(createZonedDateTime(grantStartDate[3]), passGrant2.getStartDate()); + assertEquals(createZonedDateTime(grantEndDate[3]), passGrant2.getEndDate()); + assertEquals(user3_2, passGrant2.getPi()); //Reckondwith + assertEquals(1, passGrant2.getCoPis().size()); + assertEquals(user3_2, passGrant2.getCoPis().get(0)); + assertEquals(grantUpdateTimestamp[3], passUpdater.getLatestUpdate());//latest + + //check statistics + assertEquals(0, passUpdater.getStatistics().getGrantsCreated()); + assertEquals(0, passUpdater.getStatistics().getGrantsUpdated()); + assertEquals(0, passUpdater.getStatistics().getUsersCreated()); + } + @Test public void processGrantIT_UpdateUserLocatorsJhed() throws IOException { // GIVEN @@ -304,7 +365,7 @@ private User getVerifiedUser(int userIndex) throws IOException { * @param abbrRole the role: Pi ("P") or co-pi (C" or "K") * @return row map for pull record */ - private Map makeRowMap(int iteration, int user, String abbrRole) { + private Map makeRowMap(int iteration, int user, String abbrRole) throws IOException { Map rowMap = new HashMap<>(); rowMap.put(C_GRANT_AWARD_NUMBER, grantAwardNumber[iteration]); rowMap.put(C_GRANT_AWARD_STATUS, "Active"); @@ -329,10 +390,38 @@ private Map makeRowMap(int iteration, int user, String abbrRole) rowMap.put(C_UPDATE_TIMESTAMP, grantUpdateTimestamp[iteration]); rowMap.put(C_ABBREVIATED_ROLE, abbrRole); - rowMap.put(C_DIRECT_FUNDER_POLICY, directFunderPolicyUriString); - rowMap.put(C_PRIMARY_FUNDER_POLICY, primaryFunderPolicyUriString); + rowMap.put(C_DIRECT_FUNDER_POLICY, getDirectFunderPolicyId()); + rowMap.put(C_PRIMARY_FUNDER_POLICY, getPrimaryFunderPolicyId()); return rowMap; } + private String getPrimaryFunderPolicyId() throws IOException { + PassClientSelector policySelector = new PassClientSelector<>(Policy.class); + policySelector.setFilter(RSQL.equals("title", "Primary Funder Policy")); + PassClientResult resultPolicy = passClient.selectObjects(policySelector); + if (resultPolicy.getObjects().isEmpty()) { + Policy policy1 = new Policy(); + policy1.setTitle("Primary Funder Policy"); + policy1.setDescription("BAA"); + passClient.createObject(policy1); + return policy1.getId(); + } + return resultPolicy.getObjects().get(0).getId(); + } + + private String getDirectFunderPolicyId() throws IOException { + PassClientSelector policySelector = new PassClientSelector<>(Policy.class); + policySelector.setFilter(RSQL.equals("title", "Direct Funder Policy")); + PassClientResult resultPolicy = passClient.selectObjects(policySelector); + if (resultPolicy.getObjects().isEmpty()) { + Policy policy1 = new Policy(); + policy1.setTitle("Direct Funder Policy"); + policy1.setDescription("BAA"); + passClient.createObject(policy1); + return policy1.getId(); + } + return resultPolicy.getObjects().get(0).getId(); + } + }