/*
 * Copyright 2016-2018 Crown Copyright
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.gov.gchq.gaffer.types;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class FreqMapTests {

    private FreqMap freqMap;

    @Before
    public void initialiseFreqMap() {
        freqMap = new FreqMap();
    }

    @Test
    public void testUpsertCreatesNewKeyValue() {

        //given
        final String key = "test";
        final Long value = 6L;

        //when
        freqMap.upsert(key, value);

        //then
        assertTrue(freqMap.containsKey(key));
        assertEquals(value, freqMap.get(key));

    }

    @Test
    public void testUpsertUpdatesExistingKeyValue() {

        //given
        final String key = "test";
        final Long initialValue = 3L;
        final Long increment = 11L;
        final Long expected = 14L;
        freqMap.put(key, initialValue);

        //when
        freqMap.upsert(key, increment);

        //then
        assertEquals(freqMap.get(key), expected);

    }

    @Test
    public void testUpsertOverloadedCreateDefaultValue() {

        //given
        final String key = "test";
        final Long expected = 1L;

        //when
        freqMap.upsert(key);

        //then
        assertTrue(freqMap.containsKey(key));
        assertEquals(freqMap.get(key), expected);

    }

    @Test
    public void testUpsertOverloadedIncrementsDefaultValue() {

        //given
        final String key = "test";
        final Long initialValue = 57L;
        final Long expected = 58L;
        freqMap.upsert(key, initialValue);

        //when
        freqMap.upsert(key);

        //then
        assertEquals(freqMap.get(key), expected);
    }

    @Test
    public void testKeyExistsButValueNullIsHandled() {

        //given
        final String key = "test";
        freqMap.put(key, null);
        final Long initialValue = 7L;
        final Long expectedValue = 8L;

        //when
        freqMap.upsert(key, 7L);
        freqMap.upsert(key);

        //then
        assertEquals(freqMap.get(key), expectedValue);
    }

}

@@ -690,44 +690,150 @@
 ort 
-static org.junit.Assert.assertEquals
+java.util.Map;%0Aimport java.util.function.BiPredicate;%0A%0Aimport static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertFalse
 ;%0Aim
@@ -2791,10 +2791,1716 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testRegexAndPredicates() %7B%0A        freqMap.upsert(%22cat%22);%0A        freqMap.upsert(%22cat%22);%0A        freqMap.upsert(%22dog%22);%0A        freqMap.upsert(%22cow%22);%0A        freqMap.upsert(%22cow%22);%0A        freqMap.upsert(%22catdog%22);%0A        freqMap.upsert(%22catdog%22);%0A        freqMap.upsert(%22catdog%22);%0A        freqMap.upsert(%22cat%22);%0A        freqMap.upsert(%22cat%22);%0A%0A        //Check regex works%0A        FreqMap fRegex = freqMap.filterRegex(%22%5E%5C%5Cwo%5C%5Cw$%22);%0A%0A        assertEquals(fRegex.size(), 2);%0A        assertFalse(fRegex.containsKey(%22cat%22));%0A        assertTrue(fRegex.containsKey(%22cow%22));%0A%0A        //Again regex but in predicate form of key%0A        FreqMap fPredicateString = freqMap.filterPredicate((s, aLong) -%3E s.matches(%22%5E%5C%5Cwo%5C%5Cw$%22));%0A%0A        assertEquals(fRegex.size(), 2);%0A        assertFalse(fRegex.containsKey(%22cat%22));%0A        assertTrue(fRegex.containsKey(%22cow%22));%0A%0A        //Value predicate%0A        FreqMap fPredicateLong = freqMap.filterPredicate((s, aLong) -%3E aLong %3E 1);%0A%0A        assertEquals(fPredicateLong.size(), 3);%0A        assertFalse(fPredicateLong.containsKey(%22dog%22));%0A%0A        //Both value and key%0A        FreqMap fPredicateBoth = freqMap.filterPredicate((s, aLong) -%3E (s.matches(%22%5E%5C%5Cwo%5C%5Cw$%22) && aLong %3E 1));%0A%0A        assertEquals(fPredicateBoth.size(), 1);%0A        assertFalse(fPredicateBoth.containsKey(%22dog%22));%0A        assertTrue(fPredicateBoth.containsKey(%22cow%22));%0A%0A        //Tests to to see the freqMap was not manipulated itself%0A        assertEquals(freqMap.size(), 4);%0A        assertTrue(freqMap.containsKey(%22cat%22));%0A        assertTrue(freqMap.containsKey(%22dog%22));%0A        assertTrue(freqMap.containsKey(%22catdog%22));%0A        assertTrue(freqMap.containsKey(%22cow%22));%0A    %7D%0A
 %7D%0A

Add tests for FreqMap regex and predicate testing