daily pastebin goal
30%
SHARE
TWEET

Untitled

a guest Jul 11th, 2018 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ua.slavik.carwash.Controller;
  2.  
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.mockito.Mockito;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  9. import org.springframework.boot.test.context.SpringBootTest;
  10. import org.springframework.boot.test.mock.mockito.SpyBean;
  11. import org.springframework.http.MediaType;
  12. import org.springframework.test.annotation.DirtiesContext;
  13. import org.springframework.test.context.junit4.SpringRunner;
  14. import org.springframework.test.web.servlet.MockMvc;
  15. import org.springframework.test.web.servlet.RequestBuilder;
  16. import ua.slavik.carwash.DTO.JobItemDTO.CreateJobItemDTO;
  17. import ua.slavik.carwash.DTO.JobItemDTO.UpdateJobItemDTO;
  18. import ua.slavik.carwash.model.Job;
  19. import ua.slavik.carwash.model.JobItem;
  20. import ua.slavik.carwash.model.JobStatus;
  21. import ua.slavik.carwash.repository.JobItemRepository;
  22. import ua.slavik.carwash.repository.JobRepository;
  23. import ua.slavik.carwash.service.Impl.JobItemServiceImpl;
  24. import java.util.Date;
  25. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  26. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  27.  
  28. @RunWith(SpringRunner.class)
  29. @SpringBootTest
  30. @AutoConfigureMockMvc
  31. @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
  32. public class JobItemControllerTest
  33. {
  34.     @Autowired
  35.     private MockMvc mockMvc;
  36.  
  37.     @Autowired
  38.     private JobRepository jobRepository;
  39.  
  40.     @Autowired
  41.     private JobItemRepository jobItemRepository;
  42.  
  43.     @Autowired
  44.     private ObjectMapper objectMapper;
  45.  
  46.     @SpyBean
  47.     private JobItemServiceImpl JobItemServiceMock;
  48.  
  49.     @Test
  50.     public void getJobItem() throws Exception
  51.     {
  52.         JobItem mockJobItem = JobItem.builder().name("window cleaning").description("cleaning of window").price(10).duration(20).priority(3).status(JobStatus.IN_PROGRESS).repeatable(false)
  53.                 .id(1L).build();
  54.  
  55.         Mockito.when(
  56.                 JobItemServiceMock.getJobItemById(1L)
  57.         ).thenReturn(mockJobItem);
  58.  
  59.         RequestBuilder requestBuilder = get("/jobitem/{id}", 1L);
  60.  
  61.         mockMvc.perform(requestBuilder)
  62.                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
  63.                 .andExpect(status().isOk())
  64.                 .andExpect(jsonPath("$.id").value(mockJobItem.getId()))
  65.                 .andExpect(jsonPath("$.name").value(mockJobItem.getName()))
  66.                 .andExpect(jsonPath("$.description").value(mockJobItem.getDescription()))
  67.                 .andExpect(jsonPath("$.price").value(mockJobItem.getPrice()))
  68.                 .andExpect(jsonPath("$.duration").value(mockJobItem.getDuration()))
  69.                 .andExpect(jsonPath("$.priority").value(mockJobItem.getPriority()))
  70.                 .andExpect(jsonPath("$.status").value(mockJobItem.getStatus().toString()));
  71.     }
  72.  
  73.     @Test
  74.     public void postJobItem() throws Exception
  75.     {
  76.         Job mockJob = Job.builder().startDate(new Date(1531282957L)).endDate(new Date(1531282992L))
  77.                 .status(JobStatus.NOT_STARTED).id(1L).build();
  78.         jobRepository.save(mockJob);
  79.  
  80.         CreateJobItemDTO mockJobItemDTO = CreateJobItemDTO.builder().name("window cleaning").description("cleaning of window").price(10).duration(20).priority(3).status(JobStatus.IN_PROGRESS).repeatable(false)
  81.                 .jobId(1L).build();
  82.  
  83.         String mockJobItemDTOJSON = objectMapper.writeValueAsString(mockJobItemDTO);
  84.  
  85.         RequestBuilder requestBuilder = post("/jobitem/")
  86.                 .contentType(MediaType.APPLICATION_JSON_UTF8)
  87.                 .content(mockJobItemDTOJSON);
  88.  
  89.         mockMvc.perform(requestBuilder)
  90.                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
  91.                 .andExpect(status().isCreated())
  92.                 .andExpect(jsonPath("$.id").value(1L))
  93.                 .andExpect(jsonPath("$.name").value(mockJobItemDTO.getName()))
  94.                 .andExpect(jsonPath("$.description").value(mockJobItemDTO.getDescription()))
  95.                 .andExpect(jsonPath("$.price").value(mockJobItemDTO.getPrice()))
  96.                 .andExpect(jsonPath("$.duration").value(mockJobItemDTO.getDuration()))
  97.                 .andExpect(jsonPath("$.priority").value(mockJobItemDTO.getPriority()))
  98.                 .andExpect(jsonPath("$.status").value(mockJobItemDTO.getStatus().toString()));
  99.     }
  100.  
  101.     @Test
  102.     public void updateJobItem() throws Exception
  103.     {
  104.         JobItem mockJobItem = JobItem.builder().name("window cleaning").description("cleaning of window").price(10).duration(20).priority(3).status(JobStatus.IN_PROGRESS).repeatable(false)
  105.                 .build();
  106.         jobItemRepository.save(mockJobItem);
  107.  
  108.         UpdateJobItemDTO jobItemUpdate = UpdateJobItemDTO.builder().name("car cleaning").description("cleaning of car").price(50).duration(40).priority(2).status(JobStatus.COMPLETED).repeatable(true)
  109.                 .id(1L).build();
  110.  
  111.         RequestBuilder requestBuilder = put("/jobitem/")
  112.                 .contentType(MediaType.APPLICATION_JSON_UTF8)
  113.                 .content(objectMapper.writeValueAsString(jobItemUpdate));
  114.  
  115.         mockMvc.perform(requestBuilder)
  116.                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
  117.                 .andExpect(status().isOk())
  118.                 .andExpect(jsonPath("$.id").value(1L))
  119.                 .andExpect(jsonPath("$.name").value(jobItemUpdate.getName()))
  120.                 .andExpect(jsonPath("$.description").value(jobItemUpdate.getDescription()))
  121.                 .andExpect(jsonPath("$.price").value(jobItemUpdate.getPrice()))
  122.                 .andExpect(jsonPath("$.duration").value(jobItemUpdate.getDuration()))
  123.                 .andExpect(jsonPath("$.priority").value(jobItemUpdate.getPriority()))
  124.                 .andExpect(jsonPath("$.status").value(jobItemUpdate.getStatus().toString()));
  125.     }
  126.  
  127.     @Test
  128.     public void deleteJobItem() throws Exception
  129.     {
  130.         JobItem mockJobItem = JobItem.builder().name("window cleaning").description("cleaning of window").price(10).duration(20).priority(3).status(JobStatus.COMPLETED).repeatable(false)
  131.                 .id(1L).build();
  132.         jobItemRepository.save(mockJobItem);
  133.  
  134.         RequestBuilder requestBuilder = delete("/jobitem/{id}", 1L);
  135.  
  136.         mockMvc.perform(requestBuilder)
  137.                 .andExpect(content().string("deleted"))
  138.                 .andExpect(status().isOk());
  139.     }
  140. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top