package com.gmail.matthewclarke47;

import com.gmail.matthewclarke47.metadata.*;
import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.List;
import java.util.Optional;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.core.Is.is;

public class ResourceParserTest {

    @Test
    public void shouldDocumentGetResource(){

        GetResource simpleGet = new GetResource();

        ResourceMetaData resourceMetaData = assertAndReturnResourceData(new ResourceParser(simpleGet).getMetaData());

        assertThat(resourceMetaData.getPath(), is(simpleGet.classPathValue));

        MethodMetaData methodData = resourceMetaData.getMethodMetaDataList().get(0);

        assertMethod(methodData, "GET", simpleGet.methodPathValue);

        assertThat(methodData.getParameterMetaData().isEmpty(), is(true));
    }

    @Test
    public void shouldDocumentPutResource(){

        PutResource simplePut = new PutResource();

        ResourceMetaData resourceMetaData = assertAndReturnResourceData(new ResourceParser(simplePut).getMetaData());

        assertThat(resourceMetaData.getPath(), is(simplePut.classPathValue));

        MethodMetaData methodData = resourceMetaData.getMethodMetaDataList().get(0);

        assertMethod(methodData, "PUT", simplePut.methodPathValue);

        assertThat(methodData.getParameterMetaData(), containsInAnyOrder(new PathPropertyMetaData("id", String.class)));
    }

    @Test
    public void shouldDocumentPostResource(){

        PostResource simplePost = new PostResource();

        ResourceMetaData resourceMetaData = assertAndReturnResourceData(new ResourceParser(simplePost).getMetaData());

        assertThat(resourceMetaData.getPath(), is(simplePost.classPathValue));

        List<MethodMetaData> methodData = resourceMetaData.getMethodMetaDataList();

        ParameterMetaData queryParamData = new QueryPropertyMetaData("id", String.class);

        ParameterMetaData thisThing = new PropertyParameterMetaData("thisThing", String.class);
        ParameterMetaData otherParam = new PropertyParameterMetaData("otherParam", String.class);
        ParameterMetaData count = new PropertyParameterMetaData("count", int.class);
        ParameterMetaData isBoolean = new PropertyParameterMetaData("isBoolean", boolean.class);

        List<ParameterMetaData> paramList1 = Lists.newArrayList(queryParamData);
        List<ParameterMetaData> paramList2 = Lists.newArrayList(thisThing, otherParam, count, isBoolean);


        MethodMetaData methodMetaData1 = new MethodMetaData("POST", "some/endpoint", paramList1);
        MethodMetaData methodMetaData2 = new MethodMetaData("POST", "some/endpoint/postObject", paramList2);

        assertThat(methodData, containsInAnyOrder(methodMetaData2, methodMetaData1));
    }


    private ResourceMetaData assertAndReturnResourceData(Optional<ResourceMetaData> resourceMetaDataOptional){
        assertThat(resourceMetaDataOptional.isPresent(), is(true));

        return resourceMetaDataOptional.get();
    }

    private void assertMethod(MethodMetaData methodData, String httpMethodName, String methodPath){
        assertThat(methodData.getHttpMethod(), is(httpMethodName));
        assertThat(methodData.getPathSuffix(), is(methodPath));
    }
}
@@ -1821,93 +1821,8 @@
 );%0A%0A
-        List%3CMethodMetaData%3E methodData = resourceMetaData.getMethodMetaDataList();%0A%0A
     
@@ -1908,32 +1908,37 @@
 lass);%0A%0A        
+List%3C
 ParameterMetaDat
@@ -1942,87 +1942,68 @@
 Data
- thisThing = new Prop
+%3E paramList1 = Lists.newArrayList(qu
 er
-t
 yParam
-eterMetaData(%22thisThing%22, String.class
+Data
 );%0A
+%0A
         
 Para
@@ -1990,32 +1990,37 @@
 Data);%0A%0A        
+List%3C
 ParameterMetaDat
@@ -2024,63 +2024,75 @@
 Data
- otherParam = new PropertyParameterMetaData
+%3E paramList2 = Lists.newArrayList(%0A        propertyParam
 (%22
-o
 th
-erParam
+isThing
 %22, S
@@ -2099,25 +2099,25 @@
 tring.class)
-;
+,
 %0A        Par
@@ -2117,63 +2117,72 @@
     
-ParameterMetaData count = new PropertyParameterMetaData
+propertyParam(%22otherParam%22, String.class),%0A        propertyParam
 (%22co
@@ -2197,17 +2197,17 @@
 t.class)
-;
+,
 %0A       
@@ -2211,67 +2211,21 @@
     
-ParameterMetaData isBoolean = new PropertyParameterMetaData
+propertyParam
 (%22is
@@ -2244,24 +2244,25 @@
 olean.class)
+)
 ;%0A%0A        L
@@ -2264,197 +2264,113 @@
     
-List%3CParameterMetaData%3E paramList1 = Lists.newArrayList(queryParamData);%0A        List%3CParameterMetaData%3E paramList2 = Lists.newArrayList(thisThing, otherParam, count, isBoolean);%0A%0A%0A
+assertThat(resourceMetaData.getMethodMetaDataList(),%0A                containsInAnyOrder(%0A        
         
 Meth
@@ -2369,55 +2369,90 @@
     
-M
+m
 ethod
-MetaData methodMetaData1 = new M
+Data(%22POST%22, %22some/endpoint/postObject%22, paramList2),%0A                m
 ethod
-Meta
 Data
@@ -2488,25 +2488,38 @@
 amList1)
+))
 ;%0A    
-   
+%7D%0A%0A    private
  MethodM
@@ -2536,172 +2536,268 @@
 thod
-MetaData2 = new MethodMetaData(%22POST%22, %22some/endpoint/postObject%22, paramL
+Data(String method, String path, List%3CParameterMetaData%3E list)%7B%0A        return new MethodMetaData(method, path, l
 ist
-2
 );%0A
-%0A
     
+%7D%0A%0A
     
-assertThat(methodData, containsInAnyOrder(methodMetaData2, methodMetaData1)
+private PropertyParameterMetaData propertyParam(String key, Class%3C?%3E type)%7B%0A        return new PropertyParameterMetaData(key, type
 );%0A 
@@ -2802,17 +2802,16 @@
 %0A    %7D%0A%0A
-%0A
     priv

test refactor