1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
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<UpdateIndividualProfileRequest> 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();
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<UpdateIndividualProfileRequest> 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<UpdateIndividualProfileRequest> 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<UpdateIndividualProfileRequest> 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<CustomPulldownFieldValue> 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);
}
}
|