DefaultIndexHandler implements IndexHandler<K, V> { public void add(final K key, final V value) { for (final String attributeName : indexes.keySet()) { final Object indexedKey = getFieldValue(attributeName, value); if (indexedKey == null) { throw new NullPointerException(); } indexes.get(attributeName).put(indexedKey, key); } } DefaultIndexHandler(); void addIndex(final String attributeName, final IndexType type); void add(final K key, final V value); void remove(final K key, final V value); void clear(); @SuppressWarnings("unchecked") List<K> execute(final Query query);  }
@Test public void add() { String attributeName = "name"; MyObject object = new MyObject(); object.name = attributeName; Set<String> keySet = new HashSet<>(); keySet.add(attributeName); doReturn(keySet).when(indexes).keySet(); doReturn(cacheIndex).when(indexes).get(attributeName); doNothing().when(cacheIndex).put(object, object); handler.add(object, object); verify(cacheIndex).put(attributeName, object); verify(indexes).get(attributeName); }
@Test(expected = AttributeException.class) public void addThrowsAttributeException() { String attributeName = "name"; MyObject object = new MyObject(); Set<String> keySet = new HashSet<String>(); keySet.add("ada"); doReturn(keySet).when(indexes).keySet(); doReturn(cacheIndex).when(indexes).get(attributeName); doNothing().when(cacheIndex).put(object, object); handler.add(object, object); }
@Test(expected = NullPointerException.class) public void addIndexedKeyNull() { String attributeName = "name"; MyObject object = new MyObject(); Set<String> keySet = new HashSet<String>(); keySet.add(attributeName); doReturn(keySet).when(indexes).keySet(); handler.add(object, object); }