|
|
@@ -0,0 +1,113 @@
|
|
|
+package cn.iselab.mooctest.site.web.ctrl;
|
|
|
+
|
|
|
+import cn.iselab.mooctest.site.Application;
|
|
|
+import cn.iselab.mooctest.site.models.Task;
|
|
|
+import cn.iselab.mooctest.site.service.TaskService;
|
|
|
+import cn.iselab.mooctest.site.web.data.ExamVO;
|
|
|
+import cn.iselab.mooctest.site.web.data.wrapper.ExamVOWrapper;
|
|
|
+import cn.iselab.mooctest.site.web.logic.ExamLogic;
|
|
|
+import cn.iselab.mooctest.site.web.logic.impl.ExamLogicImpl;
|
|
|
+import org.junit.Assert;
|
|
|
+import org.junit.Before;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.mockito.InjectMocks;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.runners.MockitoJUnitRunner;
|
|
|
+import org.springframework.boot.test.SpringApplicationConfiguration;
|
|
|
+import org.springframework.test.context.web.WebAppConfiguration;
|
|
|
+import org.springframework.test.web.servlet.MockMvc;
|
|
|
+import org.springframework.test.web.servlet.MvcResult;
|
|
|
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+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.result.MockMvcResultHandlers.print;
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by major on 2017/6/21.
|
|
|
+ */
|
|
|
+
|
|
|
+@RunWith(MockitoJUnitRunner.class)
|
|
|
+@WebAppConfiguration
|
|
|
+@SpringApplicationConfiguration(classes = Application.class)
|
|
|
+public class ExamControllerTest {
|
|
|
+ private MockMvc mockMvc;
|
|
|
+
|
|
|
+ @InjectMocks
|
|
|
+ private ExamController examController = new ExamController();
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private ExamLogic examLogic;
|
|
|
+ List<ExamVO> expect = new ArrayList<>();
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void setupMockMvc() {
|
|
|
+ initMocks(this);
|
|
|
+ mockMvc = MockMvcBuilders.standaloneSetup(examController).build();
|
|
|
+
|
|
|
+ ExamVO examVO = new ExamVO();
|
|
|
+ examVO.setId(497L);
|
|
|
+ examVO.setBeginTime(1495443120000L);
|
|
|
+ examVO.setEndTime(1495999990000L);
|
|
|
+ examVO.setDuration(1);
|
|
|
+ examVO.setInfo("lalaland");
|
|
|
+ examVO.setManagerId(17L);
|
|
|
+ examVO.setName("shi juan 1");
|
|
|
+ examVO.setSubsiteId(2L);
|
|
|
+ List<Long> groupIds = new ArrayList<>();
|
|
|
+ groupIds.add(111L);
|
|
|
+ examVO.setGroupIds(groupIds);
|
|
|
+ examVO.setManagerId(17L);
|
|
|
+ expect.add(examVO);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void should_returnMergedList_when_givenOrganizerId() throws Exception {
|
|
|
+ //arrange
|
|
|
+ long organizerId = 17;
|
|
|
+
|
|
|
+
|
|
|
+ when(examLogic.getOrganizerExamList(organizerId)).thenReturn(expect);
|
|
|
+ //action
|
|
|
+ MvcResult result = mockMvc.perform(
|
|
|
+ get("/api/exams").param("organizer_id", "17")
|
|
|
+ ).andDo(print()).andExpect(status().isOk()).andReturn();
|
|
|
+
|
|
|
+ //assert
|
|
|
+ Assert.assertEquals("[{\"id\":497,\"name\":\"shi juan 1\",\"beginTime\":1495443120000," +
|
|
|
+ "\"endTime\":1495999990000,\"info\":\"lalaland\",\"subsiteId\":2,\"managerId\":17," +
|
|
|
+ "\"managerName\":null,\"duration\":1,\"groupIds\":[111],\"workerCount\":null," +
|
|
|
+ "\"groupNames\":null,\"status\":null}]"
|
|
|
+ ,result.getResponse().getContentAsString());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void should_returnMergedList_when_givenParticipant() throws Exception {
|
|
|
+ //arrange
|
|
|
+ long participantId = 17;
|
|
|
+
|
|
|
+
|
|
|
+ when(examLogic.getParticipantExamList(participantId)).thenReturn(expect);
|
|
|
+ //action
|
|
|
+ MvcResult result = mockMvc.perform(
|
|
|
+ get("/api/exams").param("participant_id", "17")
|
|
|
+ ).andDo(print()).andExpect(status().isOk()).andReturn();
|
|
|
+
|
|
|
+ //assert
|
|
|
+ Assert.assertEquals("[{\"id\":497,\"name\":\"shi juan 1\",\"beginTime\":1495443120000," +
|
|
|
+ "\"endTime\":1495999990000,\"info\":\"lalaland\",\"subsiteId\":2,\"managerId\":17," +
|
|
|
+ "\"managerName\":null,\"duration\":1,\"groupIds\":[111],\"workerCount\":null," +
|
|
|
+ "\"groupNames\":null,\"status\":null}]"
|
|
|
+ ,result.getResponse().getContentAsString());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|