package one.kii.kiimate.model.core.dai;

import one.kii.summer.io.exception.BadRequest;
import one.kii.summer.io.exception.Conflict;
import one.kii.summer.io.exception.NotFound;
import one.kii.summer.io.exception.Panic;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.BootstrapWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Date;

/**
 * Created by WangYanJiong on 12/06/2017.
 */

@RunWith(SpringJUnit4ClassRunner.class)
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@ComponentScan("com.sinewang.kiimate")
@MapperScan("com.sinewang.kiimate")
@SpringBootTest(classes = {TestLoadExtensionDai.class})
@EnableAutoConfiguration
public class TestLoadExtensionDai {

    @Autowired
    private ExtensionDai dai;


    private ExtensionDai.Record normalRecord;


    private Long ID = 100000L;


    @Before
    public void before() {
        normalRecord = new ExtensionDai.Record();
        normalRecord.setBeginTime(new Date());
        normalRecord.setEndTime(null);
        normalRecord.setGroup("testGroup");
        normalRecord.setName("testName");
        normalRecord.setId(ID);
        normalRecord.setOperatorId("testOperator");
        normalRecord.setVisibility("testVisibility");
        normalRecord.setOwnerId("testOwner");
        normalRecord.setTree("testTree");
        normalRecord.setCommit("0x0");
    }


    @Test
    public void testLoadById() throws BadRequest, Panic, Conflict, NotFound {
        dai.remember(normalRecord);
        ExtensionDai.ChannelId id = new ExtensionDai.ChannelId();
        id.setId(ID);
        ExtensionDai.Record record = null;
        record = dai.loadLast(id);
        Assert.assertNotNull(record);

        dai.forget(ID);
        try {
            dai.loadLast(id);
        } catch (NotFound notFound) {
            Assert.assertEquals(String.valueOf(ID), notFound.getReasons().getFirst("id"));
        }
    }

    @Test
    public void testLoadByName() throws BadRequest, Panic, Conflict, NotFound {
        dai.remember(normalRecord);
        ExtensionDai.ChannelName name = new ExtensionDai.ChannelName();
        name.setOwnerId("testOwnerId");
        name.setGroup("testGroup");
        name.setName("testName");
        name.setTree("testTree");

        ExtensionDai.Record record = null;
        record = dai.loadLast(name);
        Assert.assertNotNull(record);

        dai.forget(ID);
        try {
            dai.loadLast(name);
        } catch (NotFound notFound) {
            Assert.assertEquals("testName", notFound.getReasons().getFirst("name"));
            Assert.assertEquals("testGroup", notFound.getReasons().getFirst("group"));
            Assert.assertEquals("testOwnerId", notFound.getReasons().getFirst("ownerId"));
        }
    }

    @After
    public void after() {
        dai.forget(normalRecord.getId());
    }
}

@@ -1220,16 +1220,220 @@
 nDai %7B%0A%0A
+    private static String TEST_GROUP = %22testGroup%22;%0A    private static String TEST_NAME = %22testName%22;%0A    private static String TEST_TREE = %22testTree%22;%0A    private static String TEST_OWNER = %22testOwner%22;%0A
     @Aut
@@ -1469,18 +1469,16 @@
 ai dai;%0A
-%0A%0A
     priv
@@ -1515,18 +1515,16 @@
 Record;%0A
-%0A%0A
     priv
@@ -1547,17 +1547,16 @@
 0000L;%0A%0A
-%0A
     @Bef
@@ -1748,35 +1748,34 @@
 rd.setGroup(
-%22testGroup%22
+TEST_GROUP
 );%0A        n
@@ -1790,34 +1790,33 @@
 ord.setName(
-%22testName%22
+TEST_NAME
 );%0A        n
@@ -1976,27 +1976,26 @@
 OwnerId(
-%22testOwner%22
+TEST_OWNER
 );%0A     
@@ -2018,26 +2018,25 @@
 setTree(
-%22testTree%22
+TEST_TREE
 );%0A     
@@ -2315,39 +2315,32 @@
 ai.Record record
- = null
 ;%0A        record
@@ -2835,29 +2835,26 @@
 OwnerId(
-%22testOwnerId%22
+TEST_OWNER
 );%0A     
@@ -2870,27 +2870,26 @@
 etGroup(
-%22testGroup%22
+TEST_GROUP
 );%0A     
@@ -2904,26 +2904,25 @@
 setName(
-%22testName%22
+TEST_NAME
 );%0A     
@@ -2937,26 +2937,25 @@
 setTree(
-%22testTree%22
+TEST_TREE
 );%0A%0A    
@@ -2988,15 +2988,8 @@
 cord
- = null
 ;%0A  
@@ -3202,26 +3202,25 @@
 tEquals(
-%22testName%22
+TEST_NAME
 , notFou
@@ -3286,27 +3286,26 @@
 tEquals(
-%22testGroup%22
+TEST_GROUP
 , notFou
@@ -3376,21 +3376,18 @@
 als(
-%22testOwnerId%22
+TEST_OWNER
 , no

improve test case