From 9d7cd517e8a00049357ce6ec4b65cb5a12dc2f24 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sat, 9 Apr 2016 09:51:01 -0700 Subject: Implementing CCBProgressReporter This reporter updates two pairs of user-defined fields on an Individual Profile. The first pair are pulldown & date fields used to track the assessment results. The second pair are updated after each chapter is completed. --- .../p4square/grow/ccb/CCBProgressReporterTest.java | 235 +++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 tst/com/p4square/grow/ccb/CCBProgressReporterTest.java (limited to 'tst/com/p4square/grow/ccb/CCBProgressReporterTest.java') diff --git a/tst/com/p4square/grow/ccb/CCBProgressReporterTest.java b/tst/com/p4square/grow/ccb/CCBProgressReporterTest.java new file mode 100644 index 0000000..6854f22 --- /dev/null +++ b/tst/com/p4square/grow/ccb/CCBProgressReporterTest.java @@ -0,0 +1,235 @@ +package com.p4square.grow.ccb; + +import com.p4square.ccbapi.CCBAPI; +import com.p4square.ccbapi.model.*; +import org.easymock.Capture; +import org.easymock.EasyMock; +import org.junit.Before; +import org.junit.Test; + +import java.time.LocalDate; +import java.util.Date; + +import static org.junit.Assert.*; + +/** + * Tests for the CCBProgressReporter. + */ +public class CCBProgressReporterTest { + + private static final String GROW_LEVEL = "GrowLevelTrain"; + private static final String ASSESSMENT_LEVEL = "GrowLevelAsmnt"; + + private CCBProgressReporter reporter; + + private CCBAPI api; + private CustomFieldCache cache; + + private CCBUser user; + private Date date; + + @Before + public void setUp() { + // Setup some data for testing. + IndividualProfile profile = new IndividualProfile(); + profile.setId(123); + profile.setFirstName("Larry"); + profile.setLastName("Cucumber"); + profile.setEmail("larry.cucumber@example.com"); + + user = new CCBUser(profile); + date = new Date(1427889600000L); // 2015-04-01 + + // Setup the mocks. + api = EasyMock.mock(CCBAPI.class); + cache = EasyMock.mock(CustomFieldCache.class); + reporter = new CCBProgressReporter(api, cache); + } + + private void setupCacheMocks() { + // Setup the Grow Level field. + CustomField growLevelDate = new CustomField(); + growLevelDate.setName("udf_ind_date_1"); + growLevelDate.setLabel(GROW_LEVEL); + + CustomField growLevelPulldown = new CustomField(); + growLevelPulldown.setName("udf_ind_pulldown_1"); + growLevelPulldown.setLabel(GROW_LEVEL); + + LookupTableItem believer = new LookupTableItem(); + believer.setId(1); + believer.setOrder(2); + believer.setName("Believer"); + + EasyMock.expect(cache.getDateFieldByLabel(GROW_LEVEL)) + .andReturn(growLevelDate).anyTimes(); + EasyMock.expect(cache.getIndividualPulldownByLabel(GROW_LEVEL)) + .andReturn(growLevelPulldown).anyTimes(); + EasyMock.expect(cache.getPulldownItemByName(LookupTableType.UDF_IND_PULLDOWN_1, "Believer")) + .andReturn(believer).anyTimes(); + + // Setup the Grow Assessment field. + CustomField growAssessmentDate = new CustomField(); + growAssessmentDate.setName("udf_ind_date_2"); + growAssessmentDate.setLabel(ASSESSMENT_LEVEL); + + CustomField growAssessmentPulldown = new CustomField(); + growAssessmentPulldown.setName("udf_ind_pulldown_2"); + growAssessmentPulldown.setLabel(ASSESSMENT_LEVEL); + + EasyMock.expect(cache.getDateFieldByLabel(ASSESSMENT_LEVEL)) + .andReturn(growAssessmentDate).anyTimes(); + EasyMock.expect(cache.getIndividualPulldownByLabel(ASSESSMENT_LEVEL)) + .andReturn(growAssessmentPulldown).anyTimes(); + EasyMock.expect(cache.getPulldownItemByName(LookupTableType.UDF_IND_PULLDOWN_2, "Believer")) + .andReturn(believer).anyTimes(); + } + + @Test + public void reportAssessmentComplete() throws Exception { + // Setup mocks + setupCacheMocks(); + Capture reqCapture = EasyMock.newCapture(); + EasyMock.expect(api.updateIndividualProfile(EasyMock.capture(reqCapture))) + .andReturn(EasyMock.mock(UpdateIndividualProfileResponse.class)); + replay(); + + // Test reporter + reporter.reportAssessmentComplete(user, "Believer", date, "Data"); + + // Assert that the profile was updated. + verify(); + assertTrue(reqCapture.hasCaptured()); + UpdateIndividualProfileRequest req = reqCapture.getValue(); + + // Both the Grow Level and Grow Assessment fields should be updated. + assertEquals(1, req.getCustomPulldownFields().get("udf_pulldown_1").intValue()); + assertEquals("2015-04-01", req.getCustomDateFields().get("udf_date_1").toString()); + assertEquals(1, req.getCustomPulldownFields().get("udf_pulldown_2").intValue()); + assertEquals("2015-04-01", req.getCustomDateFields().get("udf_date_2").toString()); + } + + @Test + public void testReportChapterCompleteNoPreviousChapter() throws Exception { + // Setup mocks + setupCacheMocks(); + Capture reqCapture = EasyMock.newCapture(); + EasyMock.expect(api.updateIndividualProfile(EasyMock.capture(reqCapture))) + .andReturn(EasyMock.mock(UpdateIndividualProfileResponse.class)); + replay(); + + // Test reporter + reporter.reportChapterComplete(user, "Believer", date); + + // Assert that the profile was updated. + verify(); + assertTrue(reqCapture.hasCaptured()); + UpdateIndividualProfileRequest req = reqCapture.getValue(); + assertEquals(1, req.getCustomPulldownFields().get("udf_pulldown_1").intValue()); + assertEquals("2015-04-01", req.getCustomDateFields().get("udf_date_1").toString()); + } + + @Test + public void testReportChapterCompleteLowerPreviousChapter() throws Exception { + // Setup mocks + setupCacheMocks(); + Capture reqCapture = EasyMock.newCapture(); + EasyMock.expect(api.updateIndividualProfile(EasyMock.capture(reqCapture))) + .andReturn(EasyMock.mock(UpdateIndividualProfileResponse.class)); + + setUserPulldownSelection(GROW_LEVEL, "Seeker"); + + replay(); + + // Test reporter + reporter.reportChapterComplete(user, "Believer", date); + + // Assert that the profile was updated. + verify(); + assertTrue(reqCapture.hasCaptured()); + UpdateIndividualProfileRequest req = reqCapture.getValue(); + assertEquals(1, req.getCustomPulldownFields().get("udf_pulldown_1").intValue()); + assertEquals("2015-04-01", req.getCustomDateFields().get("udf_date_1").toString()); + } + + @Test + public void testReportChapterCompleteHigherPreviousChapter() throws Exception { + // Setup mocks + setupCacheMocks(); + setUserPulldownSelection(GROW_LEVEL, "Disciple"); + + replay(); + + // Test reporter + reporter.reportChapterComplete(user, "Believer", date); + + // Assert that the profile was updated. + verify(); + } + + @Test + public void testReportChapterCompleteNoCustomField() throws Exception { + // Setup mocks + EasyMock.expect(cache.getDateFieldByLabel(EasyMock.anyString())).andReturn(null).anyTimes(); + EasyMock.expect(cache.getIndividualPulldownByLabel(EasyMock.anyString())).andReturn(null).anyTimes(); + EasyMock.expect(cache.getPulldownItemByName(EasyMock.anyObject(), EasyMock.anyString())) + .andReturn(null).anyTimes(); + replay(); + + // Test reporter + reporter.reportChapterComplete(user, "Believer", date); + + // Assert that the profile was updated. + verify(); + } + + @Test + public void testReportChapterCompleteNoSuchValue() throws Exception { + // Setup mocks + setupCacheMocks(); + EasyMock.expect(cache.getPulldownItemByName(LookupTableType.UDF_IND_PULLDOWN_1, "Foo")) + .andReturn(null).anyTimes(); + Capture reqCapture = EasyMock.newCapture(); + EasyMock.expect(api.updateIndividualProfile(EasyMock.capture(reqCapture))) + .andReturn(EasyMock.mock(UpdateIndividualProfileResponse.class)); + replay(); + + // Test reporter + reporter.reportChapterComplete(user, "Foo", date); + + // Assert that the profile was updated. + verify(); + assertTrue(reqCapture.hasCaptured()); + UpdateIndividualProfileRequest req = reqCapture.getValue(); + assertNull(req.getCustomPulldownFields().get("udf_pulldown_1")); + assertEquals("2015-04-01", req.getCustomDateFields().get("udf_date_1").toString()); + } + + private void setUserPulldownSelection(final String field, final String value) { + // Get the pulldown field collection for the user. + CustomFieldCollection pulldowns = user.getProfile().getCustomPulldownFields(); + if (pulldowns == null) { + pulldowns = new CustomFieldCollection<>(); + user.getProfile().setCustomPulldownFields(pulldowns); + } + + // Create the selection for the value. + PulldownSelection selection = new PulldownSelection(); + selection.setLabel(value); + + // Create the field/value pair and add it to the collection. + CustomPulldownFieldValue fieldValue = new CustomPulldownFieldValue(); + fieldValue.setName(field); // This is unused by the test, but it should be a udf_ identifier. + fieldValue.setLabel(field); + fieldValue.setSelection(selection); + pulldowns.add(fieldValue); + } + + private void replay() { + EasyMock.replay(api, cache); + } + + private void verify() { + EasyMock.verify(api, cache); + } +} \ No newline at end of file -- cgit v1.2.3 From 1a175649a383e11671e4c8c6754c204ed80fe3cd Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sat, 9 Apr 2016 10:04:46 -0700 Subject: Updating CCBProgressReporterTest --- tst/com/p4square/grow/ccb/CCBProgressReporterTest.java | 4 ---- 1 file changed, 4 deletions(-) (limited to 'tst/com/p4square/grow/ccb/CCBProgressReporterTest.java') diff --git a/tst/com/p4square/grow/ccb/CCBProgressReporterTest.java b/tst/com/p4square/grow/ccb/CCBProgressReporterTest.java index 6854f22..63a973a 100644 --- a/tst/com/p4square/grow/ccb/CCBProgressReporterTest.java +++ b/tst/com/p4square/grow/ccb/CCBProgressReporterTest.java @@ -101,10 +101,6 @@ public class CCBProgressReporterTest { verify(); assertTrue(reqCapture.hasCaptured()); UpdateIndividualProfileRequest req = reqCapture.getValue(); - - // Both the Grow Level and Grow Assessment fields should be updated. - assertEquals(1, req.getCustomPulldownFields().get("udf_pulldown_1").intValue()); - assertEquals("2015-04-01", req.getCustomDateFields().get("udf_date_1").toString()); assertEquals(1, req.getCustomPulldownFields().get("udf_pulldown_2").intValue()); assertEquals("2015-04-01", req.getCustomDateFields().get("udf_date_2").toString()); } -- cgit v1.2.3