summaryrefslogtreecommitdiff
path: root/tst/com/p4square/grow/ccb/CustomFieldCacheTest.java
blob: bcfd2601928a281d87996108c754b7b61ad6b526 (plain)
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
232
233
234
235
236
237
238
239
240
241
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.io.IOException;
import java.util.Arrays;

import static org.junit.Assert.*;

/**
 * Tests for the CustomFieldCache.
 */
public class CustomFieldCacheTest {

    private CustomFieldCache cache;

    private CCBAPI api;
    private GetCustomFieldLabelsResponse customFieldsResponse;
    private GetLookupTableResponse lookupTableResponse;

    @Before
    public void setUp() {
        api = EasyMock.mock(CCBAPI.class);
        cache = new CustomFieldCache(api);

        // Prepare some custom fields for the test.
        CustomField textField = new CustomField();
        textField.setName("udf_ind_text_6");
        textField.setLabel("Grow Level");

        CustomField dateField = new CustomField();
        dateField.setName("udf_ind_date_6");
        dateField.setLabel("Grow Level");

        CustomField pullDown = new CustomField();
        pullDown.setName("udf_ind_pulldown_6");
        pullDown.setLabel("Grow Level");

        customFieldsResponse = new GetCustomFieldLabelsResponse();
        customFieldsResponse.setCustomFields(Arrays.asList(textField, dateField, pullDown));

        // Prepare some pulldown items for the tests.
        LookupTableItem seeker = new LookupTableItem();
        seeker.setId(1);
        seeker.setOrder(1);
        seeker.setName("Seeker");

        LookupTableItem believer = new LookupTableItem();
        believer.setId(2);
        believer.setOrder(2);
        believer.setName("Believer");

        lookupTableResponse = new GetLookupTableResponse();
        lookupTableResponse.setItems(Arrays.asList(seeker, believer));
    }

    @Test
    public void testGetTextFieldByLabel() throws Exception {
        // Setup mocks
        EasyMock.expect(api.getCustomFieldLabels()).andReturn(customFieldsResponse);
        EasyMock.replay(api);

        // Test the cache
        CustomField field = cache.getTextFieldByLabel("Grow Level");

        // Verify result.
        EasyMock.verify(api);
        assertEquals("udf_ind_text_6", field.getName());
        assertEquals("Grow Level", field.getLabel());
    }

    @Test
    public void testGetDateFieldByLabel() throws Exception {
        // Setup mocks
        EasyMock.expect(api.getCustomFieldLabels()).andReturn(customFieldsResponse);
        EasyMock.replay(api);

        // Test the cache
        CustomField field = cache.getDateFieldByLabel("Grow Level");

        // Verify result.
        EasyMock.verify(api);
        assertEquals("udf_ind_date_6", field.getName());
        assertEquals("Grow Level", field.getLabel());
    }

    @Test
    public void testGetPullDownFieldByLabel() throws Exception {
        // Setup mocks
        EasyMock.expect(api.getCustomFieldLabels()).andReturn(customFieldsResponse);
        EasyMock.replay(api);

        // Test the cache
        CustomField field = cache.getIndividualPulldownByLabel("Grow Level");

        // Verify result.
        EasyMock.verify(api);
        assertEquals("udf_ind_pulldown_6", field.getName());
        assertEquals("Grow Level", field.getLabel());
    }

    @Test
    public void testGetPullDownFieldByLabelMissing() throws Exception {
        // Setup mocks
        EasyMock.expect(api.getCustomFieldLabels()).andReturn(customFieldsResponse);
        EasyMock.replay(api);

        // Test the cache
        CustomField field = cache.getIndividualPulldownByLabel("Missing Label");

        // Verify result.
        EasyMock.verify(api);
        assertNull(field);
    }

    @Test
    public void testGetPullDownFieldByLabelException() throws Exception {
        // Setup mocks
        EasyMock.expect(api.getCustomFieldLabels()).andThrow(new IOException());
        EasyMock.expect(api.getCustomFieldLabels()).andReturn(customFieldsResponse);
        EasyMock.replay(api);

        // Test the cache
        CustomField field1 = cache.getIndividualPulldownByLabel("Grow Level");
        CustomField field2 = cache.getIndividualPulldownByLabel("Grow Level");

        // Verify result.
        EasyMock.verify(api);
        assertNull(field1);
        assertNotNull(field2);
    }

    @Test
    public void testGetMultipleFields() throws Exception {
        // Setup mocks
        // Note: only one API call.
        EasyMock.expect(api.getCustomFieldLabels()).andReturn(customFieldsResponse);
        EasyMock.replay(api);

        // Test the cache
        CustomField field1 = cache.getTextFieldByLabel("Grow Level");
        CustomField field2 = cache.getIndividualPulldownByLabel("Grow Level");

        // Verify result.
        EasyMock.verify(api);
        assertEquals("udf_ind_text_6", field1.getName());
        assertEquals("Grow Level", field1.getLabel());
        assertEquals("udf_ind_pulldown_6", field2.getName());
        assertEquals("Grow Level", field2.getLabel());
    }

    @Test
    public void testGetPullDownOptions() throws Exception {
        // Setup mocks
        Capture<GetLookupTableRequest> requestCapture = EasyMock.newCapture();
        EasyMock.expect(api.getLookupTable(EasyMock.capture(requestCapture))).andReturn(lookupTableResponse);
        EasyMock.replay(api);

        // Test the cache
        LookupTableItem item = cache.getPulldownItemByName(
                LookupTableType.valueOf("udf_ind_pulldown_6".toUpperCase()),
                "Believer");

        // Verify result.
        EasyMock.verify(api);
        assertEquals(LookupTableType.UDF_IND_PULLDOWN_6, requestCapture.getValue().getType());
        assertEquals(2, item.getId());
        assertEquals(2, item.getOrder());
        assertEquals("Believer", item.getName());
    }

    @Test
    public void testGetPullDownOptionsMixedCase() throws Exception {
        // Setup mocks
        Capture<GetLookupTableRequest> requestCapture = EasyMock.newCapture();
        EasyMock.expect(api.getLookupTable(EasyMock.capture(requestCapture))).andReturn(lookupTableResponse);
        EasyMock.replay(api);

        // Test the cache
        LookupTableItem item = cache.getPulldownItemByName(
                LookupTableType.valueOf("udf_ind_pulldown_6".toUpperCase()),
                "BeLiEvEr");

        // Verify result.
        EasyMock.verify(api);
        assertEquals(LookupTableType.UDF_IND_PULLDOWN_6, requestCapture.getValue().getType());
        assertEquals(2, item.getId());
        assertEquals(2, item.getOrder());
        assertEquals("Believer", item.getName());
    }

    @Test
    public void testGetPullDownOptionMissing() throws Exception {
        // Setup mocks
        EasyMock.expect(api.getLookupTable(EasyMock.anyObject())).andReturn(lookupTableResponse);
        EasyMock.replay(api);

        // Test the cache
        LookupTableItem item = cache.getPulldownItemByName(LookupTableType.UDF_IND_PULLDOWN_6, "Something else");

        // Verify result.
        EasyMock.verify(api);
        assertNull(item);
    }

    @Test
    public void testGetPullDownMissing() throws Exception {
        // Setup mocks
        EasyMock.expect(api.getLookupTable(EasyMock.anyObject())).andReturn(new GetLookupTableResponse());
        EasyMock.replay(api);

        // Test the cache
        LookupTableItem item = cache.getPulldownItemByName(LookupTableType.UDF_IND_PULLDOWN_6, "Believer");

        // Verify result.
        EasyMock.verify(api);
        assertNull(item);
    }

    @Test
    public void testGetPullDownException() throws Exception {
        // Setup mocks
        EasyMock.expect(api.getLookupTable(EasyMock.anyObject())).andThrow(new IOException());
        EasyMock.expect(api.getLookupTable(EasyMock.anyObject())).andReturn(lookupTableResponse);
        EasyMock.replay(api);

        // Test the cache
        LookupTableItem item1 = cache.getPulldownItemByName(LookupTableType.UDF_IND_PULLDOWN_6, "Believer");
        LookupTableItem item2 = cache.getPulldownItemByName(LookupTableType.UDF_IND_PULLDOWN_6, "Believer");

        // Verify result.
        EasyMock.verify(api);
        assertNull(item1);
        assertNotNull(item2);
    }
}