From 918e806c83f812721f0e9527fbc3e3e67d071580 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Thu, 7 Apr 2016 21:25:12 -0700 Subject: Adding CustomFieldCache for CCB User Defined Fields. --- .../p4square/grow/ccb/CustomFieldCacheTest.java | 221 +++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 tst/com/p4square/grow/ccb/CustomFieldCacheTest.java (limited to 'tst/com/p4square/grow/ccb/CustomFieldCacheTest.java') diff --git a/tst/com/p4square/grow/ccb/CustomFieldCacheTest.java b/tst/com/p4square/grow/ccb/CustomFieldCacheTest.java new file mode 100644 index 0000000..e374496 --- /dev/null +++ b/tst/com/p4square/grow/ccb/CustomFieldCacheTest.java @@ -0,0 +1,221 @@ +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 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); + } +} \ No newline at end of file -- cgit v1.2.3 From 10c5fd17b603f125ae2c0ef14b1a65341dbdf961 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sat, 9 Apr 2016 09:48:56 -0700 Subject: CustomFieldCache is now case-insensitive. --- src/com/p4square/grow/ccb/CustomFieldCache.java | 6 +++--- tst/com/p4square/grow/ccb/CustomFieldCacheTest.java | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) (limited to 'tst/com/p4square/grow/ccb/CustomFieldCacheTest.java') diff --git a/src/com/p4square/grow/ccb/CustomFieldCache.java b/src/com/p4square/grow/ccb/CustomFieldCache.java index ba473fb..d93e6d9 100644 --- a/src/com/p4square/grow/ccb/CustomFieldCache.java +++ b/src/com/p4square/grow/ccb/CustomFieldCache.java @@ -72,7 +72,7 @@ public class CustomFieldCache { items = mItemByNameTable.get(type); } - return items.get(name); + return items.get(name.toLowerCase()); } private synchronized void refresh() { @@ -113,8 +113,8 @@ public class CustomFieldCache { private synchronized boolean cacheLookupTable(final LookupTableType type) { try { final GetLookupTableResponse resp = mAPI.getLookupTable(new GetLookupTableRequest().withType(type)); - mItemByNameTable.put(type, - resp.getItems().stream().collect(Collectors.toMap(LookupTableItem::getName, Function.identity()))); + mItemByNameTable.put(type, resp.getItems().stream().collect( + Collectors.toMap(item -> item.getName().toLowerCase(), Function.identity()))); return true; } catch (IOException e) { diff --git a/tst/com/p4square/grow/ccb/CustomFieldCacheTest.java b/tst/com/p4square/grow/ccb/CustomFieldCacheTest.java index e374496..bcfd260 100644 --- a/tst/com/p4square/grow/ccb/CustomFieldCacheTest.java +++ b/tst/com/p4square/grow/ccb/CustomFieldCacheTest.java @@ -174,6 +174,26 @@ public class CustomFieldCacheTest { assertEquals("Believer", item.getName()); } + @Test + public void testGetPullDownOptionsMixedCase() throws Exception { + // Setup mocks + Capture 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 -- cgit v1.2.3