/*
 *  Copyright (c) 2017 Otávio Santana and others
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   and Apache License v2.0 which accompanies this distribution.
 *   The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *   and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
 *
 *   You may elect to redistribute this code under either of these licenses.
 *
 *   Contributors:
 *
 *   Otavio Santana
 */

package org.jnosql.diana.redis.key;

import org.jnosql.diana.api.key.BucketManagerFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.*;

public class RedisMapTest {

    private BucketManagerFactory entityManagerFactory;

    private Species mammals = new Species("lion", "cow", "dog");
    private Species fishes = new Species("redfish", "glassfish");
    private Species amphibians = new Species("crododile", "frog");

    private Map<String, Species> vertebrates;

    @BeforeEach
    public void init() {
        entityManagerFactory = RedisTestUtils.get();
        vertebrates = entityManagerFactory.getMap("vertebrates", String.class, Species.class);
    }

    @Test
    public void shouldPutAndGetMap() {
        assertTrue(vertebrates.isEmpty());

        assertNotNull(vertebrates.put("mammals", mammals));
        Species species = vertebrates.get("mammals");
        assertNotNull(species);
        assertEquals(species.getAnimals().get(0), mammals.getAnimals().get(0));
        assertTrue(vertebrates.size() == 1);
    }

    @Test
    public void shouldVerifyExist() {
        vertebrates.put("mammals", mammals);
        assertTrue(vertebrates.containsKey("mammals"));
        assertFalse(vertebrates.containsKey("redfish"));

        assertTrue(vertebrates.containsValue(mammals));
        assertFalse(vertebrates.containsValue(fishes));
    }

    @Test
    public void shouldShowKeyAndValues() {
        Map<String, Species> vertebratesMap = new HashMap<>();
        vertebratesMap.put("mammals", mammals);
        vertebratesMap.put("fishes", fishes);
        vertebratesMap.put("amphibians", amphibians);
        vertebrates.putAll(vertebratesMap);

        Set<String> keys = vertebrates.keySet();
        Collection<Species> collectionSpecies = vertebrates.values();

        assertTrue(keys.size() == 3);
        assertTrue(collectionSpecies.size() == 3);
        assertNotNull(vertebrates.remove("mammals"));
        assertNull(vertebrates.remove("mammals"));
        assertNull(vertebrates.get("mammals"));
        assertTrue(vertebrates.size() == 2);
    }

    @Test
    public void shouldRemove() {
        vertebrates.put("mammals", mammals);
        vertebrates.put("fishes", fishes);
        vertebrates.put("amphibians", amphibians);

        vertebrates.remove("fishes");
        assertTrue(vertebrates.size() == 2);
        assertThat(vertebrates, not(hasKey(fishes)));
    }

    @Test
    public void shouldClear() {
        vertebrates.put("mammals", mammals);
        vertebrates.put("fishes", fishes);

        vertebrates.clear();
        assertTrue(vertebrates.isEmpty());
    }

    @AfterEach
    public void dispose() {
        vertebrates.clear();
    }

}

@@ -1652,52 +1652,8 @@
 ) %7B%0A
-        assertTrue(vertebrates.isEmpty());%0A%0A
     
@@ -2317,85 +2317,19 @@
     
-Map%3CString, Species%3E vertebratesMap = new HashMap%3C%3E();%0A        vertebratesMap
+vertebrates
 .put
@@ -2365,27 +2365,24 @@
  vertebrates
-Map
 .put(%22fishes
@@ -2412,19 +2412,16 @@
 tebrates
-Map
 .put(%22am
@@ -2447,52 +2447,8 @@
 ns);
-%0A        vertebrates.putAll(vertebratesMap);
 %0A%0A  

remove unecessary code