FreqMapPredicator extends KorypheFunction<FreqMap, FreqMap> implements Serializable { @Override public FreqMap apply(final FreqMap freqMap) { return filterPredicate(freqMap); } FreqMapPredicator();  FreqMapPredicator(final KoryphePredicate<String> predicate); @Override FreqMap apply(final FreqMap freqMap); KoryphePredicate<String> getPredicate(); void setPredicate(final KoryphePredicate<String> predicate);  }
@Test public void shouldFilterMapWithMultipleResults() { final Regex predicate = new Regex("^\\wo\\w$"); final FreqMapPredicator fRegexPredicator = new FreqMapPredicator(predicate); final FreqMap fRegex = fRegexPredicator.apply(freqMap); assertEquals(fRegex.size(), 2); assertTrue(fRegex.containsKey("cow")); assertTrue(fRegex.containsKey("dog")); }
@Test public void shouldFilterMapWithSingleResult() { final Regex predicate = new Regex("^c.*o.*g$"); final FreqMapPredicator fRegexPredicator = new FreqMapPredicator(predicate); final FreqMap fRegex = fRegexPredicator.apply(freqMap); assertEquals(fRegex.size(), 1); assertTrue(fRegex.containsKey("catdog")); }
@Test public void shouldHandleNulls() { final FreqMapPredicator nullRegPredicator = new FreqMapPredicator(null); final FreqMap map = nullRegPredicator.apply(freqMap); assertThat(map, is(freqMap)); }
@Test public void shouldNotMutateOriginalValue() { final Regex predicate = new Regex("^\\wo\\w$"); final FreqMapPredicator fRegexPredicator = new FreqMapPredicator(predicate); final FreqMap fRegex = fRegexPredicator.apply(freqMap); assertEquals(fRegex.size(), 2); assertTrue(fRegex.containsKey("cow")); assertTrue(fRegex.containsKey("dog")); assertEquals(freqMap.size(), 4); assertTrue(freqMap.containsKey("cat")); assertTrue(freqMap.containsKey("dog")); assertTrue(freqMap.containsKey("catdog")); assertTrue(freqMap.containsKey("cow")); }
@Test public void shouldJsonSerialiseAndDeserialise() throws SerialisationException { final FreqMapPredicator nullPredicator = new FreqMapPredicator(); final FreqMapPredicator regexPredicator = new FreqMapPredicator(new Regex("^\\wo\\w$")); final String json = new String(JSONSerialiser.serialise(nullPredicator, true)); final String json2 = new String(JSONSerialiser.serialise(regexPredicator, false)); JsonAssert.assertEquals(String.format("{%n" + " \"class\" : \"uk.gov.gchq.gaffer.types.function.FreqMapPredicator\"%n" + "}"), json); JsonAssert.assertEquals("{\"class\":\"uk.gov.gchq.gaffer.types.function.FreqMapPredicator\"," + "\"predicate\":{\"class\":\"uk.gov.gchq.koryphe.impl.predicate.Regex\",\"value\":" + "{\"java.util.regex.Pattern\":\"^\\\\wo\\\\w$\"}}}", json2); final FreqMapPredicator deserializedNull = JSONSerialiser.deserialise(json, FreqMapPredicator.class); final FreqMapPredicator deserializedRegex = JSONSerialiser.deserialise(json2, FreqMapPredicator.class); assertEquals(deserializedNull.apply(freqMap).size(), freqMap.size()); assertEquals(deserializedRegex.apply(freqMap).size(), 2); }