package jp.ac.nii.prl.mape.autoscaling.controller;

import jp.ac.nii.prl.mape.autoscaling.MapeAutoscalingServiceApplication;
import org.junit.Assert;
import org.junit.Before;
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.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.mock.http.MockHttpOutputMessage;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;

import static org.junit.Assert.fail;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MapeAutoscalingServiceApplication.class)
@WebAppConfiguration
public class AutoscalingControllerTest {
	
	private MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
            MediaType.APPLICATION_JSON.getSubtype(),
            Charset.forName("utf8"));
	
	private HttpMessageConverter mappingJackson2HttpMessageConverter;
	
	@Autowired
    void setConverters(HttpMessageConverter<?>[] converters) {

        this.mappingJackson2HttpMessageConverter = Arrays.asList(converters).stream().filter(
                hmc -> hmc instanceof MappingJackson2HttpMessageConverter).findAny().get();

        Assert.assertNotNull("the JSON message converter must not be null",
                this.mappingJackson2HttpMessageConverter);
    }
	
	private MockMvc mockMvc;
	
	@Autowired
	private WebApplicationContext webApplicationContext;

	@Before
	public void setUp() throws Exception {
		this.mockMvc = webAppContextSetup(webApplicationContext).build();
	}

	@Test
	public void testCreateDeployment() {
		fail("Not yet implemented");
	}

	@Test
	public void testGetDeployment() {
		fail("Not yet implemented");
	}
	
	protected String json(final Object o) throws IOException {
        MockHttpOutputMessage mockHttpOutputMessage = new MockHttpOutputMessage();
        this.mappingJackson2HttpMessageConverter.write(
                o, MediaType.APPLICATION_JSON, mockHttpOutputMessage);
        return mockHttpOutputMessage.getBodyAsString();
    }

}

@@ -45,16 +45,72 @@
 oller;%0A%0A
+import edu.emory.mathcs.backport.java.util.Collections;%0A
 import j
@@ -170,24 +170,262 @@
 pplication;%0A
+import jp.ac.nii.prl.mape.autoscaling.model.dto.DeploymentDTO;%0Aimport jp.ac.nii.prl.mape.autoscaling.model.dto.InstanceDTO;%0Aimport jp.ac.nii.prl.mape.autoscaling.model.dto.InstanceTypeDTO;%0Aimport org.apache.commons.httpclient.HttpStatus;%0A
 import org.j
@@ -1233,16 +1233,111 @@
 rrays;%0A%0A
+import static io.restassured.RestAssured.given;%0Aimport static io.restassured.RestAssured.post;%0A
 import s
@@ -2549,35 +2549,166 @@
  %7B%0A%09
-%09fail(%22Not yet implemented%22
+    DeploymentDTO deployment = buildEmptyDeploymentDTO();%0A%09%09given().body(deployment).post(%22/autoscaling%22).then().assertThat().statusCode(HttpStatus.SC_CREATED
 );%0A%09
@@ -2709,33 +2709,36 @@
 ED);%0A%09%7D%0A%0A%09@Test%0A
-%09
+    
 public void test
@@ -2741,11 +2741,21 @@
 test
-Get
+CreateGarbage
 Depl
@@ -2770,39 +2770,129 @@
  %7B%0A%09
-%09fail(%22Not yet implemented%22);%0A%09
+    given().body(%22%7Bhello%7D%22).post(%22/autoscaling%22).then().assertThat().statusCode(HttpStatus.SC_UNPROCESSABLE_ENTITY);%0A    
 %7D%0A%09%0A
@@ -3224,10 +3224,669 @@
 %0A    %7D%0A%0A
+    private static DeploymentDTO buildEmptyDeploymentDTO() %7B%0A%09    InstanceDTO instance = new InstanceDTO();%0A%09    instance.setInstID(%221%22);%0A%09    instance.setInstLoad(0.5);%0A%09    instance.setInstType(%22t2.micro%22);%0A%0A        InstanceTypeDTO instanceType = new InstanceTypeDTO();%0A        instanceType.setTypeID(%22t2.micro%22);%0A        instanceType.setTypeCost(0.01);%0A        instanceType.setTypeCPUs(1);%0A        instanceType.setTypeRAM(0.5);%0A%0A%09    DeploymentDTO deployment = new DeploymentDTO();%0A%09    deployment.setInstances(Collections.singletonList(instance));%0A%09    deployment.setInstanceTypes(Collections.singletonList(instanceType));%0A%0A%09    return deployment;%0A    %7D%0A%0A
 %7D%0A

Add tests