package wang.yanjiong.metamate.core.api;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.BootstrapWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import wang.yanjiong.metamate.core.dai.ExtensionDai;
import wang.yanjiong.metamate.core.fi.AnStructureValidator;
import wang.yanjiong.metamate.core.fi.AnVisibilityValidator;

/**
 * Created by WangYanJiong on 3/24/17.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@ComponentScan("com.sinewang.metamate.core")
@SpringBootTest(classes = {TestDeclareExtensionApi.class})
public class TestDeclareExtensionApi {

    @Autowired
    private DeclareExtensionApi declareExtensionApi;

    @Autowired
    private ExtensionDai extensionDai;

    private String ownerId = "testOwnerId";

    private String operatorId = "testOperatorId";

    @Test
    public void testGroupOnly() {
        DeclareExtensionApi.ExtensionForm extensionForm = new DeclareExtensionApi.ExtensionForm();
        extensionForm.setGroup("testGroup");
        ResponseEntity<DeclareExtensionApi.ExtensionReceipt> response = declareExtensionApi.declareByFormUrlEncoded(extensionForm, ownerId, operatorId);
        DeclareExtensionApi.ExtensionReceipt extensionReceipt = response.getBody();
        HttpStatus httpStatus = response.getStatusCode();
        Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), httpStatus.value());
        Assert.assertNull(extensionReceipt);
    }

    @Test
    public void testNameOnly() {
        DeclareExtensionApi.ExtensionForm extensionForm = new DeclareExtensionApi.ExtensionForm();
        extensionForm.setName("testName");
        ResponseEntity<DeclareExtensionApi.ExtensionReceipt> response = declareExtensionApi.declareByFormUrlEncoded(extensionForm, ownerId, operatorId);
        DeclareExtensionApi.ExtensionReceipt extensionReceipt = response.getBody();
        HttpStatus httpStatus = response.getStatusCode();
        Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), httpStatus.value());
        Assert.assertNull(extensionReceipt);
    }

    @Test
    public void testVersionOnly() {
        DeclareExtensionApi.ExtensionForm extensionForm = new DeclareExtensionApi.ExtensionForm();
        extensionForm.setTree("testTree");
        ResponseEntity<DeclareExtensionApi.ExtensionReceipt> response = declareExtensionApi.declareByFormUrlEncoded(extensionForm, ownerId, operatorId);
        DeclareExtensionApi.ExtensionReceipt extensionReceipt = response.getBody();
        HttpStatus httpStatus = response.getStatusCode();
        Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), httpStatus.value());
        Assert.assertNull(extensionReceipt);
    }

    @Test
    public void testVisibilityOnly() {
        DeclareExtensionApi.ExtensionForm extensionForm = new DeclareExtensionApi.ExtensionForm();
        extensionForm.setVisibility("testVisibility");
        ResponseEntity<DeclareExtensionApi.ExtensionReceipt> response = declareExtensionApi.declareByFormUrlEncoded(extensionForm, ownerId, operatorId);
        DeclareExtensionApi.ExtensionReceipt extensionReceipt = response.getBody();
        HttpStatus httpStatus = response.getStatusCode();
        Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), httpStatus.value());
        Assert.assertNull(extensionReceipt);
    }


    @Test
    public void testInvalidStructure() {
        DeclareExtensionApi.ExtensionForm extensionForm = new DeclareExtensionApi.ExtensionForm();
        extensionForm.setGroup("testGroup");
        extensionForm.setName("testName");
        extensionForm.setTree("testTree");
        extensionForm.setVisibility("protected");
        ResponseEntity<DeclareExtensionApi.ExtensionReceipt> response = declareExtensionApi.declareByFormUrlEncoded(extensionForm, ownerId, operatorId);
        DeclareExtensionApi.ExtensionReceipt extensionReceipt = response.getBody();
        HttpStatus httpStatus = response.getStatusCode();
        Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), httpStatus.value());
        Assert.assertNull(extensionReceipt);
    }

    @Test
    public void testInvalidVisibility() {
        DeclareExtensionApi.ExtensionForm extensionForm = new DeclareExtensionApi.ExtensionForm();
        extensionForm.setGroup("testGroup");
        extensionForm.setName("testName");
        extensionForm.setTree("testTree");
        extensionForm.setVisibility("testVisibility");
        ResponseEntity<DeclareExtensionApi.ExtensionReceipt> response = declareExtensionApi.declareByFormUrlEncoded(extensionForm, ownerId, operatorId);
        DeclareExtensionApi.ExtensionReceipt extensionReceipt = response.getBody();
        HttpStatus httpStatus = response.getStatusCode();
        Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), httpStatus.value());
        Assert.assertNull(extensionReceipt);
    }

    @Test
    public void testSave() {
        String group = "testGroup";
        String name = "testName";
        String tree = "testTree";

        DeclareExtensionApi.ExtensionForm extensionForm = new DeclareExtensionApi.ExtensionForm();
        extensionForm.setGroup(group);
        extensionForm.setName(name);
        extensionForm.setTree(tree);
        extensionForm.setVisibility(AnVisibilityValidator.Visibility.PROTECTED.name());
        ResponseEntity<DeclareExtensionApi.ExtensionReceipt> response = declareExtensionApi.declareByFormUrlEncoded(extensionForm, ownerId, operatorId);
        DeclareExtensionApi.ExtensionReceipt extensionReceipt = response.getBody();
        HttpStatus httpStatus = response.getStatusCode();
        Assert.assertEquals(HttpStatus.ACCEPTED.value(), httpStatus.value());
        Assert.assertNotNull(extensionReceipt);
        String id = extensionReceipt.getId();
        Assert.assertNotNull(id);


        ExtensionDai.Extension extension = extensionDai.selectExtensionById(id);
        Assert.assertEquals(group, extension.getGroup());
        Assert.assertEquals(name, extension.getName());
        Assert.assertEquals(tree, extension.getTree());

        extensionDai.deleteExtensionById(id);
    }
}

@@ -1278,24 +1278,69 @@
 eratorId%22;%0A%0A
+    private String requestId = %22requestId%22;%0A%0A
     @Test%0A  
@@ -1329,32 +1329,32 @@
 Id%22;%0A%0A    @Test%0A
-
     public void 
@@ -1623,32 +1623,43 @@
 yFormUrlEncoded(
+requestId, 
 extensionForm, o
@@ -2247,32 +2247,43 @@
 yFormUrlEncoded(
+requestId, 
 extensionForm, o
@@ -2874,32 +2874,43 @@
 yFormUrlEncoded(
+requestId, 
 extensionForm, o
@@ -3516,32 +3516,43 @@
 yFormUrlEncoded(
+requestId, 
 extensionForm, o
@@ -4287,32 +4287,43 @@
 yFormUrlEncoded(
+requestId, 
 extensionForm, o
@@ -5063,32 +5063,43 @@
 yFormUrlEncoded(
+requestId, 
 extensionForm, o
@@ -5830,32 +5830,32 @@
 TECTED.name());%0A
-
         Response
@@ -5954,16 +5954,27 @@
 Encoded(
+requestId, 
 extensio

add request id to test declare extension api