package com.proofpoint.sample;

import com.proofpoint.stats.Duration;
import com.proofpoint.testing.Assertions;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

import static java.util.Arrays.asList;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

public class TestPersonStore
{
    @Test
    public void testStartsEmpty()
    {
        PersonStore store = new PersonStore(new StoreConfig());
        assertTrue(store.getAll().isEmpty());
    }

    @Test
    public void testTtl()
            throws InterruptedException
    {
        StoreConfig config = new StoreConfig();
        config.setTtl(new Duration(1, TimeUnit.MILLISECONDS));

        PersonStore store = new PersonStore(config);
        store.put("foo", new Person("foo@example.com", "Mr Foo"));
        Thread.sleep(2);
        Assert.assertNull(store.get("foo"));
    }

    @Test
    public void testPut()
    {
        PersonStore store = new PersonStore(new StoreConfig());
        store.put("foo", new Person("foo@example.com", "Mr Foo"));

        assertEquals(new Person("foo@example.com", "Mr Foo"), store.get("foo"));
        assertEquals(store.getAll().size(), 1);
    }

    @Test
    public void testDelete()
    {
        PersonStore store = new PersonStore(new StoreConfig());
        store.put("foo", new Person("foo@example.com", "Mr Foo"));
        store.delete("foo");

        assertNull(store.get("foo"));
        assertTrue(store.getAll().isEmpty());
    }

    @Test
    public void testGetAll()
    {
        PersonStore store = new PersonStore(new StoreConfig());

        store.put("foo", new Person("foo@example.com", "Mr Foo"));
        store.put("bar", new Person("bar@example.com", "Mr Bar"));

        assertEquals(store.getAll().size(), 2);
        assertEquals(store.getAll(), asList(new Person("foo@example.com", "Mr Foo"), new Person("bar@example.com", "Mr Bar")));
    }

}

@@ -1314,264 +1314,1072 @@
 test
-Delete()%0A    %7B%0A        PersonStore store = new PersonStore(new StoreConfig());%0A        store.put(%22foo%22, new Person(%22foo@example.com%22, %22Mr Foo%22));%0A        store.delete(%22foo%22);%0A%0A        assertNull(store.get(%22foo%22));%0A        assertTrue(store.getAll().isEmpty(
+IdempotentPut()%0A    %7B%0A        PersonStore store = new PersonStore(new StoreConfig());%0A        store.put(%22foo%22, new Person(%22foo@example.com%22, %22Mr Foo%22));%0A        store.put(%22foo%22, new Person(%22foo@example.com%22, %22Mr Bar%22));%0A%0A        assertEquals(new Person(%22foo@example.com%22, %22Mr Bar%22), store.get(%22foo%22));%0A        assertEquals(store.getAll().size(), 1);%0A    %7D%0A%0A    @Test%0A    public void testDelete()%0A    %7B%0A        PersonStore store = new PersonStore(new StoreConfig());%0A        store.put(%22foo%22, new Person(%22foo@example.com%22, %22Mr Foo%22));%0A        store.delete(%22foo%22);%0A%0A        assertNull(store.get(%22foo%22));%0A        assertTrue(store.getAll().isEmpty());%0A    %7D%0A%0A    @Test%0A    public void testIdempotentDelete()%0A    %7B%0A        PersonStore store = new PersonStore(new StoreConfig());%0A        store.put(%22foo%22, new Person(%22foo@example.com%22, %22Mr Foo%22));%0A%0A        store.delete(%22foo%22);%0A        assertTrue(store.getAll().isEmpty());%0A        assertNull(store.get(%22foo%22));%0A%0A        store.delete(%22foo%22);%0A        assertTrue(store.getAll().isEmpty());%0A        assertNull(store.get(%22foo%22
 ));%0A

test for idempotence of put and delete