|
|
@@ -1,5 +1,6 @@
|
|
|
package cn.iselab.mooctest.site.web.ctrl;
|
|
|
|
|
|
+import cn.iselab.mooctest.site.common.constant.UrlConstants;
|
|
|
import cn.iselab.mooctest.site.web.data.CaseExtendsVO;
|
|
|
import cn.iselab.mooctest.site.web.logic.CaseLogic;
|
|
|
import org.json.JSONArray;
|
|
|
@@ -13,6 +14,7 @@ import org.mockito.Mockito;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageImpl;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
|
import org.springframework.test.web.servlet.MvcResult;
|
|
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
@@ -21,10 +23,11 @@ import org.springframework.web.util.NestedServletException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
+import static org.junit.Assert.assertEquals;
|
|
|
import static org.mockito.Matchers.*;
|
|
|
import static org.mockito.Mockito.when;
|
|
|
import static org.mockito.MockitoAnnotations.initMocks;
|
|
|
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
|
|
|
@@ -113,4 +116,141 @@ public class CaseControllerTest {
|
|
|
JSONObject jsonObject = new JSONObject(result.getResponse().getContentAsString());
|
|
|
Assert.assertEquals(123L,jsonObject.getLong("id"));
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void should_useCaseLogicCreate_when_givenCaseExtendsVO() throws Exception{
|
|
|
+ //arrange
|
|
|
+ CaseExtendsVO caseExtendsVO=new CaseExtendsVO();
|
|
|
+ short platform=5;
|
|
|
+ caseExtendsVO.setPlatform(platform);
|
|
|
+ caseExtendsVO.setProperties("");
|
|
|
+ caseExtendsVO.setSubsiteId(46L);
|
|
|
+ caseExtendsVO.setTargetId(123L);
|
|
|
+ caseExtendsVO.setName("Test");
|
|
|
+ caseExtendsVO.setDescription("This is just a test.");
|
|
|
+ caseExtendsVO.setManagerId(17L);
|
|
|
+
|
|
|
+ JSONObject input=new JSONObject();
|
|
|
+ input.put("name","Test");
|
|
|
+ input.put("description","This is just a test.");
|
|
|
+ input.put("properties","");
|
|
|
+ input.put("platform",platform);
|
|
|
+ input.put("managerId",17L);
|
|
|
+ input.put("subsiteId",46L);
|
|
|
+ input.put("targetId",123L);
|
|
|
+
|
|
|
+ //action
|
|
|
+ MvcResult result = mockMvc.perform(
|
|
|
+ post("/api/case").content(input.toString())
|
|
|
+ ).andDo(print()).andExpect(status().isOk()).andReturn();
|
|
|
+
|
|
|
+ //assert
|
|
|
+ Mockito.verify(caseLogic).create(caseExtendsVO);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void should_useCaseLogicUpdate_when_givenCaseExtendVOAndId() throws Exception{
|
|
|
+ //arrange
|
|
|
+ CaseExtendsVO caseExtendsVO=new CaseExtendsVO();
|
|
|
+ short platform=5;
|
|
|
+ caseExtendsVO.setPlatform(platform);
|
|
|
+ caseExtendsVO.setProperties("");
|
|
|
+ caseExtendsVO.setSubsiteId(46L);
|
|
|
+ caseExtendsVO.setTargetId(123L);
|
|
|
+ caseExtendsVO.setName("Test");
|
|
|
+ caseExtendsVO.setDescription("This is just a test.");
|
|
|
+ caseExtendsVO.setManagerId(123L);
|
|
|
+ caseExtendsVO.setId(123L);
|
|
|
+
|
|
|
+ JSONObject input=new JSONObject();
|
|
|
+ input.put("id",123L);
|
|
|
+ input.put("name","Test");
|
|
|
+ input.put("description","This is just a test.");
|
|
|
+ input.put("properties","");
|
|
|
+ input.put("platform",platform);
|
|
|
+ input.put("managerId",123L);
|
|
|
+ input.put("subsiteId",46L);
|
|
|
+ input.put("targetId",123L);
|
|
|
+
|
|
|
+ //action
|
|
|
+ MvcResult result = mockMvc.perform(
|
|
|
+ put("/api/case/123").contentType(MediaType.APPLICATION_JSON).content(input.toString())
|
|
|
+ ).andDo(print()).andExpect(status().isOk()).andReturn();
|
|
|
+
|
|
|
+ //assert
|
|
|
+ Mockito.verify(caseLogic).update(caseExtendsVO,123L);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void should_throwException_whenCaseExtendIdNotSame(){
|
|
|
+ //arrange
|
|
|
+ CaseExtendsVO caseExtendsVO=new CaseExtendsVO();
|
|
|
+ short platform=5;
|
|
|
+ caseExtendsVO.setPlatform(platform);
|
|
|
+ caseExtendsVO.setProperties("");
|
|
|
+ caseExtendsVO.setSubsiteId(46L);
|
|
|
+ caseExtendsVO.setTargetId(123L);
|
|
|
+ caseExtendsVO.setName("Test");
|
|
|
+ caseExtendsVO.setDescription("This is just a test.");
|
|
|
+ caseExtendsVO.setManagerId(123L);
|
|
|
+ caseExtendsVO.setId(123456L);
|
|
|
+
|
|
|
+ JSONObject input=new JSONObject();
|
|
|
+ input.put("id",12345L);
|
|
|
+ input.put("name","Test");
|
|
|
+ input.put("description","This is just a test.");
|
|
|
+ input.put("properties","");
|
|
|
+ input.put("platform",platform);
|
|
|
+ input.put("managerId",123L);
|
|
|
+ input.put("subsiteId",46L);
|
|
|
+ input.put("targetId",123L);
|
|
|
+
|
|
|
+ //action
|
|
|
+ try{
|
|
|
+ MvcResult result = mockMvc.perform(
|
|
|
+ put("/api/case/123").contentType(MediaType.APPLICATION_JSON).content(input.toString())
|
|
|
+ ).andDo(print()).andExpect(status().isOk()).andReturn();
|
|
|
+ }catch(Exception e){
|
|
|
+ Assert.assertEquals(((NestedServletException)e).getRootCause().getClass(),IllegalArgumentException.class);
|
|
|
+ Assert.assertEquals(((NestedServletException)e).getRootCause().getMessage(),"Id are not same");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void should_returnCaseExtendsVO_when_givenDeleteId() throws Exception{
|
|
|
+ //arrange
|
|
|
+ CaseExtendsVO caseExtendsVO=new CaseExtendsVO();
|
|
|
+ short platform=5;
|
|
|
+ caseExtendsVO.setId(123L);
|
|
|
+ caseExtendsVO.setPlatform(platform);
|
|
|
+ caseExtendsVO.setProperties("");
|
|
|
+ caseExtendsVO.setSubsiteId(46L);
|
|
|
+ caseExtendsVO.setTargetId(123L);
|
|
|
+ caseExtendsVO.setName("Test");
|
|
|
+ caseExtendsVO.setDescription("This is just a test.");
|
|
|
+ caseExtendsVO.setManagerId(17L);
|
|
|
+
|
|
|
+ JSONObject input=new JSONObject();
|
|
|
+ input.put("name","Test");
|
|
|
+ input.put("description","This is just a test.");
|
|
|
+ input.put("properties","");
|
|
|
+ input.put("platform",platform);
|
|
|
+ input.put("managerId",17L);
|
|
|
+ input.put("subsiteId",46L);
|
|
|
+ input.put("targetId",123L);
|
|
|
+ input.put("id",123L);
|
|
|
+
|
|
|
+ when(caseLogic.delete(123L)).thenReturn(caseExtendsVO);
|
|
|
+
|
|
|
+ //action
|
|
|
+ MvcResult result = mockMvc.perform(
|
|
|
+ delete("/api/case/123").contentType(MediaType.APPLICATION_JSON).content(input.toString())
|
|
|
+ ).andDo(print()).andExpect(status().isOk()).andReturn();
|
|
|
+
|
|
|
+ //assert
|
|
|
+ JSONObject re=new JSONObject(result.getResponse().getContentAsString());
|
|
|
+ assertEquals(123,re.get("id"));
|
|
|
+ assertEquals("Test",re.get("name"));
|
|
|
+ assertEquals("This is just a test.",re.get("description"));
|
|
|
+ }
|
|
|
}
|