package org.hildan.livedoc.springmvc.scanner.properties;

import java.util.ArrayList;
import java.util.List;

import org.hildan.livedoc.core.scanners.properties.Property;
import org.hildan.livedoc.core.scanners.properties.PropertyScanner;
import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import static org.junit.Assert.assertEquals;

public class JacksonPropertyScannerTest {

    private PropertyScanner scanner;

    private interface MyInterface {}
    private static class MyImpl implements MyInterface {}

    @Before
    public void setUp() throws Exception {
        scanner = new JacksonPropertyScanner(new ObjectMapper());
    }

    @JsonIgnoreProperties({"ignoredOnClass", "ignoredOnClass2"})
    private static class MyDto {

        @JsonProperty("publicFieldRenamed")
        public String publicField;

        private int fieldWithGetter;

        @JsonSerialize(as=MyImpl.class)
        private MyInterface interfaceField;

        private String writeOnly;

        @JsonIgnore
        private long ignoredOnField;

        private long ignoredOnGetter;

        private long ignoredOnClass;

        public long ignoredOnClass2;

        public int getFieldWithGetter() {
            return fieldWithGetter;
        }

        public String getComputedValue() {
            return publicField + fieldWithGetter;
        }

        public void setWriteOnly(String value) {
            this.writeOnly = value;
        }

        public long getIgnoredOnField() {
            return ignoredOnField;
        }

        @JsonIgnore
        public long getIgnoredOnGetter() {
            return ignoredOnGetter;
        }

        public long getIgnoredOnClass() {
            return ignoredOnClass;
        }
    }

    @Test
    public void getProperties() throws Exception {
        List<Property> properties = scanner.getProperties(MyDto.class);
        List<Property> expectedProps = new ArrayList<>();
        expectedProps.add(new Property("fieldWithGetter", int.class, MyDto.class.getDeclaredMethod("getFieldWithGetter")));
        expectedProps.add(new Property("interfaceField", MyImpl.class, MyDto.class.getDeclaredField("interfaceField")));
        expectedProps.add(new Property("computedValue", String.class, MyDto.class.getDeclaredMethod("getComputedValue")));
        expectedProps.add(new Property("publicFieldRenamed", String.class, MyDto.class.getDeclaredField("publicField")));
        assertEquals(expectedProps, properties);
    }
}

@@ -51,16 +51,47 @@
 rties;%0A%0A
+import java.lang.reflect.Type;%0A
 import j
@@ -104,20 +104,17 @@
 il.Array
-List
+s
 ;%0Aimport
@@ -592,16 +592,60 @@
 rialize;
+%0Aimport com.google.common.reflect.TypeToken;
 %0A%0Aimport
@@ -802,16 +802,17 @@
 face %7B%7D%0A
+%0A
     priv
@@ -1199,32 +1199,71 @@
 eldWithGetter;%0A%0A
+        private List%3CDouble%3E generic;%0A%0A
         @JsonSer
@@ -1271,17 +1271,19 @@
 alize(as
-=
+ = 
 MyImpl.c
@@ -1622,32 +1622,114 @@
 ter;%0A        %7D%0A%0A
+        public List%3CDouble%3E getGeneric() %7B%0A            return generic;%0A        %7D%0A%0A
         public S
@@ -2336,16 +2336,87 @@
 .class);
+%0A%0A        Type listDouble = new TypeToken%3CList%3CDouble%3E%3E() %7B%7D.getType();
 %0A       
@@ -2450,54 +2450,40 @@
 ps =
- new
  Array
-List%3C%3E();%0A        expectedProps.add(
+s.asList(%0A                
 new 
@@ -2573,27 +2573,31 @@
 etter%22))
-);%0A
+,%0A     
         
 expected
@@ -2588,34 +2588,129 @@
         
-expectedProps.add(
+   new Property(%22generic%22, List.class, listDouble, MyDto.class.getDeclaredMethod(%22getGeneric%22)),%0A                
 new Prop
@@ -2785,34 +2785,33 @@
 nterfaceField%22))
-);
+,
 %0A        expecte
@@ -2803,34 +2803,24 @@
         
-expectedProps.add(
+        
 new Prop
@@ -2905,18 +2905,17 @@
 Value%22))
-);
+,
 %0A       
@@ -2919,26 +2919,16 @@
     
-expectedProps.add(
+        
 new 

Add test for generics in JacksonPropertyScanner