package com.shagaba.jacksync.service;

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

import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.shagaba.jacksync.PatchOperation;
import com.shagaba.jacksync.post.dto.Author;
import com.shagaba.jacksync.post.dto.Post;
import com.shagaba.jacksync.post.dto.Section;

public class LocaleDiffServiceTest {
	
	private ObjectMapper mapper = null;

    public ObjectMapper newObjectMapper() {
        ObjectMapper jacksonObjectMapper = new ObjectMapper();

        // - SerializationFeature for changing how JSON is written

        // to allow serialization of "empty" POJOs (no properties to serialize)
        // (without this setting, an exception is thrown in those cases)
        jacksonObjectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        // Feature that determines whether Map entries with null values are to be serialized (true) or not (false)
        jacksonObjectMapper.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
        // only properties with non-null
        jacksonObjectMapper.setSerializationInclusion(Include.NON_NULL);

        // - DeserializationFeature for changing how JSON is read as POJOs:

        // to prevent exception when encountering unknown property:
        jacksonObjectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        // default: java.util.Date serialized as textual (ISO-8601) values
        return jacksonObjectMapper;
    }
    
    @Before
    public void beforeEach() {
    	mapper = newObjectMapper();
    	
    }

    @Test
    public void addTitle() throws Exception {
    	String title = "my test title";
    	Post postV1 = new Post();
//    	postV1.setTitle(title);
    	postV1.setAuthor(new Author("1", "2", "3"));
    	postV1.setSections(new ArrayList<Section>());
    	postV1.getSections().add(new Section("section-1", null));
    	postV1.getSections().add(new Section("section-2", null));
    	postV1.getSections().add(new Section("section-3", null));
    	postV1.getSections().add(new Section("section-4", null));

    	Post postV1_1 = new Post();
    	postV1_1.setTitle(title);
    	postV1_1.setAuthor(new Author("james", "2", "3"));
    	postV1_1.setSections(new ArrayList<Section>());
    	postV1_1.getSections().add(new Section("section-1", null));
    	postV1_1.getSections().add(new Section("section-2", null));
    	postV1_1.getSections().add(new Section("section-X", null));
    	postV1_1.getSections().add(new Section("section-4", null));

        // read action
    	LocaleDiffService<Post> diffService = new LocaleDiffService<>();
        diffService.setObjectMapper(mapper);
        List<PatchOperation> operations = diffService.diff(postV1, postV1_1);
        System.out.println(operations);
        
    }


}

@@ -30,24 +30,71 @@
 service;%0D%0A%0D%0A
+import static org.hamcrest.CoreMatchers.is;%0D%0A%0D%0A
 import java.
@@ -132,24 +132,50 @@
 il.List;%0D%0A%0D%0A
+import org.junit.Assert;%0D%0A
 import org.j
@@ -498,16 +498,58 @@
 ation;%0D%0A
+import com.shagaba.jacksync.SyncCapsule;%0D%0A
 import c
@@ -2080,16 +2080,45 @@
 ost();%0D%0A
+    %09postV1.setVersion(0L);%0D%0A
 //    %09p
@@ -2528,32 +2528,63 @@
  = new Post();%0D%0A
+    %09postV1_1.setVersion(1L);%0D%0A
     %09postV1_1.se
@@ -3129,39 +3129,31 @@
     
-List%3CPatchOperation%3E operations
+SyncCapsule syncCapsule
  = d
@@ -3200,49 +3200,265 @@
     
-System.out.println(operations);%0D%0A        
+%0D%0A        LocalSyncService localSyncService = new LocalSyncService();%0D%0A        localSyncService.setObjectMapper(mapper);%0D%0A        Post postV1_2 = localSyncService.clientSync(postV1, syncCapsule);%0D%0A        %0D%0A        Assert.assertThat(postV1_2, is(postV1_1));
 %0D%0A  

test diff service using localSyncService.clientSync