package com.yammer.stresstime.resources;

import com.google.common.reflect.TypeToken;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import com.yammer.stresstime.entities.BaseEntity;
import com.yammer.stresstime.entities.Group;
import org.junit.Before;
import org.junit.Test;

import javax.ws.rs.core.MultivaluedMap;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public abstract class GetCreateDeleteResource<E extends BaseEntity> extends BaseResourceTest {

    MultivaluedMap postForm;
    String resourcePath;

    @Before
    public void setup() throws Exception {
        super.setUp();
        postForm = getSamplePostForm();
        resourcePath = getResourcePath();
        if (resourcePath.endsWith("/")) {
            resourcePath = resourcePath.substring(0, resourcePath.length()-1);
        }
    }

    protected abstract MultivaluedMap getSamplePostForm();
    protected abstract String getResourcePath();
    protected abstract boolean checkCreatedEntity(E entity);
    protected abstract Class<E> getEntityClass();
    protected abstract Class<E[]> getEntityArrayClass();

    @Test
    public void testCreateGetGroups() throws Exception{
        E entity = (E) resource(resourcePath).entity(postForm).post(getEntityClass());
        assertNotNull(entity);
        assertTrue(checkCreatedEntity(entity));
        @SuppressWarnings("unchecked")
        List<E> entities = (List<E>)(List<?>)Arrays.asList(resource(resourcePath).get(getEntityArrayClass()));
        assertNotNull(entities);
        assertThat(entities.size(), equalTo(1));
        assertThat(entities.get(0).getId(), equalTo(entity.getId()));
        assertTrue(checkCreatedEntity(entities.get(0)));
    }

    @Test
    public void testDeleteGroup() {
        E entity = (E) resource(resourcePath).entity(postForm).post(getEntityClass());
        assertNotNull(entity);
        assertTrue(checkCreatedEntity(entity));
        @SuppressWarnings("unchecked")
        List<E> entities = (List<E>)(List<?>)Arrays.asList((Object[])resource(resourcePath).get(getEntityArrayClass()));
        assertNotNull(entities);
        assertThat(entities.size(), equalTo(1));
        assertThat(entities.get(0).getId(), equalTo(entity.getId()));
        resource(resourcePath + "/" + entity.getId()).delete();
        List<Group> empty = (List<Group>)(List<?>)Arrays.asList((Object[])resource(resourcePath).get(getEntityArrayClass()));
        assertNotNull(empty);
        assertThat(empty.size(), equalTo(0));
    }

}

@@ -1518,47 +1518,8 @@
 ));%0A
-        @SuppressWarnings(%22unchecked%22)%0A
     
@@ -1541,34 +1541,16 @@
 ities = 
-(List%3CE%3E)(List%3C?%3E)
 Arrays.a
@@ -2039,47 +2039,8 @@
 ));%0A
-        @SuppressWarnings(%22unchecked%22)%0A
     
@@ -2066,26 +2066,8 @@
 s = 
-(List%3CE%3E)(List%3C?%3E)
 Arra
@@ -2072,34 +2072,24 @@
 rays.asList(
-(Object%5B%5D)
 resource(res
@@ -2357,21 +2357,17 @@
    List%3C
-Group
+E
 %3E empty 
@@ -2372,30 +2372,8 @@
 y = 
-(List%3CGroup%3E)(List%3C?%3E)
 Arra
@@ -2386,18 +2386,8 @@
 ist(
-(Object%5B%5D)
 reso

Refactor GetCreateDeleteResource