PageRenderTime 65ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/org/genesys2/tests/resttests/RequestsControllerTest.java

https://bitbucket.org/genesys2/genesys2-server
Java | 237 lines | 184 code | 53 blank | 0 comment | 0 complexity | 034934acb5e355614b88fd3a611af173 MD5 | raw file
  1. package org.genesys2.tests.resttests;
  2. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  3. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  4. import java.util.ArrayList;
  5. import java.util.HashSet;
  6. import java.util.List;
  7. import java.util.Set;
  8. import com.fasterxml.jackson.core.JsonProcessingException;
  9. import com.fasterxml.jackson.databind.ObjectMapper;
  10. import org.apache.commons.logging.Log;
  11. import org.apache.commons.logging.LogFactory;
  12. import org.genesys2.server.exception.UserException;
  13. import org.genesys2.server.model.genesys.MaterialRequest;
  14. import org.genesys2.server.model.genesys.MaterialSubRequest;
  15. import org.genesys2.server.model.impl.Article;
  16. import org.genesys2.server.model.impl.ClassPK;
  17. import org.genesys2.server.model.impl.FaoInstitute;
  18. import org.genesys2.server.service.RequestService;
  19. import org.genesys2.server.service.impl.EasySMTAException;
  20. import org.genesys2.server.service.impl.RequestServiceImpl;
  21. import org.junit.After;
  22. import org.junit.Before;
  23. import org.junit.Ignore;
  24. import org.junit.Test;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.data.domain.PageRequest;
  27. import org.springframework.data.domain.Sort;
  28. import org.springframework.http.MediaType;
  29. import org.springframework.test.web.servlet.MockMvc;
  30. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  31. import org.springframework.transaction.annotation.Transactional;
  32. import org.springframework.web.context.WebApplicationContext;
  33. @Transactional
  34. @Ignore
  35. public class RequestsControllerTest extends AbstractRestTest {
  36. private static final Log LOG = LogFactory.getLog(RequestsControllerTest.class);
  37. @Autowired
  38. private WebApplicationContext webApplicationContext;
  39. MockMvc mockMvc;
  40. private MaterialRequest materialRequest;
  41. private MaterialSubRequest materialSubRequest;
  42. private Article article;
  43. Article secondArticle = new Article();
  44. private FaoInstitute faoInstitute;
  45. private List<FaoInstitute> institutes = new ArrayList<>();
  46. @Before
  47. public void setUp() throws UserException, JsonProcessingException, EasySMTAException {
  48. mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
  49. article = new Article();
  50. article.setBody("BodyArticle");
  51. article.setSlug("smtp.material-confirm-no-pid");
  52. article.setLang("en");
  53. secondArticle.setBody("BodyArticle");
  54. secondArticle.setSlug("smtp.material-request");
  55. secondArticle.setLang("en");
  56. ClassPK classPK = contentService.ensureClassPK(Article.class);
  57. article.setClassPk(classPK);
  58. secondArticle.setClassPk(classPK);
  59. List<Article> listArticles = new ArrayList<>();
  60. listArticles.add(article);
  61. listArticles.add(secondArticle);
  62. contentService.save(listArticles);
  63. ObjectMapper objectMapper = new ObjectMapper();
  64. Set<Long> accessionsId = new HashSet<>();
  65. accessionsId.add(1L);
  66. RequestService.RequestInfo requestInfo = new RequestService.RequestInfo();
  67. requestInfo.setEmail("salexandrbasov@gmail.com");
  68. requestInfo.setNotes("notes");
  69. requestInfo.setPreacceptSMTA(false);
  70. requestInfo.setPurposeType(1);
  71. RequestServiceImpl.RequestBody requestBody = new RequestServiceImpl.RequestBody();
  72. requestBody.pid = easySMTAConnector.getUserData("salexandrbasov@gmail.com");
  73. requestBody.requestInfo = requestInfo;
  74. requestBody.accessionIds = accessionsId;
  75. materialRequest = new MaterialRequest();
  76. materialRequest.setEmail("salexandrbasov@gmail.com");
  77. materialRequest.setVersion(1);
  78. materialRequest.setBody(objectMapper.writeValueAsString(requestBody));
  79. materialRequestRepository.save(materialRequest);
  80. materialSubRequest = new MaterialSubRequest();
  81. materialSubRequest.setBody(objectMapper.writeValueAsString(requestBody));
  82. materialSubRequest.setInstCode("Code");
  83. materialSubRequest.setVersion(2);
  84. materialSubRequest.setSourceRequest(materialRequest);
  85. materialSubRequestRepository.save(materialSubRequest);
  86. faoInstitute = new FaoInstitute();
  87. faoInstitute.setFullName("This is name of institute");
  88. faoInstitute.setCurrent(true);
  89. faoInstitute.setPgrActivity(true);
  90. faoInstitute.setMaintainsCollection(true);
  91. faoInstitute.setPgrActivity(true);
  92. faoInstitute.setAccessionCount(1);
  93. faoInstitute.setUniqueAcceNumbs(true);
  94. faoInstitute.setCode("Code");
  95. institutes.add(faoInstitute);
  96. instituteService.update(institutes);
  97. }
  98. @After
  99. public void teerDown() {
  100. materialSubRequestRepository.deleteAll();
  101. materialRequestRepository.deleteAll();
  102. articleRepository.deleteAll();
  103. }
  104. @Test
  105. public void listRequestsTest() throws Exception {
  106. LOG.info("Start test-method listRequestsTest");
  107. ObjectMapper objectMapper = new ObjectMapper();
  108. mockMvc.perform(get("/api/v0/requests")
  109. .contentType(MediaType.APPLICATION_JSON)
  110. .param("page", "0"))
  111. .andExpect(status().isOk())
  112. .andExpect(content().contentType(MediaType.APPLICATION_JSON))
  113. .andExpect(content().string(objectMapper.writeValueAsString(requestService.list(new PageRequest(0, 10, new Sort(Sort.Direction.DESC, "createdDate"))))));
  114. LOG.info("Test listRequestsTest passed");
  115. }
  116. @Test
  117. public void getRequestTest() throws Exception {
  118. LOG.info("Start test-method getRequestTest");
  119. ObjectMapper objectMapper = new ObjectMapper();
  120. MaterialRequest materialRequestForTest = requestService.list(new PageRequest(0, 10, new Sort(Sort.Direction.DESC, "createdDate"))).getContent().get(0);
  121. mockMvc.perform(get("/api/v0/requests/r/{uuid}", materialRequestForTest.getUuid())
  122. .contentType(MediaType.APPLICATION_JSON))
  123. .andExpect(status().isOk())
  124. .andExpect(content().contentType(MediaType.APPLICATION_JSON))
  125. .andExpect(content().string(objectMapper.writeValueAsString(materialRequestForTest)));
  126. LOG.info("Test getRequestTest passed");
  127. }
  128. @Test
  129. public void reconfirmRequestTest() throws Exception {
  130. LOG.info("Start test-method reconfirmRequestTest");
  131. MaterialRequest materialRequestForTest = requestService.list(new PageRequest(0, 10, new Sort(Sort.Direction.DESC, "createdDate"))).getContent().get(0);
  132. mockMvc.perform(post("/api/v0/requests/r/{uuid}/reconfirm", materialRequestForTest.getUuid())
  133. .contentType(MediaType.APPLICATION_JSON))
  134. .andExpect(status().isOk())
  135. .andExpect(content().contentType(MediaType.APPLICATION_JSON));
  136. LOG.info("Test reconfirmRequestTest passed");
  137. }
  138. @Test
  139. public void validateRequestTest() throws Exception {
  140. LOG.info("Start test-method validateRequestTest");
  141. MaterialRequest materialRequestForTest = requestService.list(new PageRequest(0, 10, new Sort(Sort.Direction.DESC, "createdDate"))).getContent().get(0);
  142. materialRequestForTest.setSubRequests(materialSubRequestRepository.findAll());
  143. mockMvc.perform(post("/api/v0/requests/r/{uuid}/validate", materialRequestForTest.getUuid())
  144. .contentType(MediaType.APPLICATION_JSON))
  145. .andExpect(status().isOk())
  146. .andExpect(content().contentType(MediaType.APPLICATION_JSON));
  147. LOG.info("Test validateRequestTest passed");
  148. }
  149. @Test
  150. public void updatePidTest() throws Exception {
  151. LOG.info("Start test-method updatePidTest");
  152. ObjectMapper objectMapper = new ObjectMapper();
  153. MaterialRequest materialRequestForTest = requestService.list(new PageRequest(0, 10, new Sort(Sort.Direction.DESC, "createdDate"))).getContent().get(0);
  154. materialRequestForTest.setSubRequests(materialSubRequestRepository.findAll());
  155. mockMvc.perform(post("/api/v0/requests/r/{uuid}/update-pid", materialRequestForTest.getUuid())
  156. .contentType(MediaType.APPLICATION_JSON))
  157. .andExpect(status().isOk())
  158. .andExpect(content().contentType(MediaType.APPLICATION_JSON))
  159. .andExpect(content().string(objectMapper.writeValueAsString(requestService.recheckPid(materialRequestForTest))));
  160. LOG.info("Test updatePidTest passed");
  161. }
  162. @Test
  163. public void listInstituteRequestsTest() throws Exception {
  164. LOG.info("Start test-method listInstituteRequestsTest");
  165. ObjectMapper objectMapper = new ObjectMapper();
  166. mockMvc.perform(get("/api/v0/requests/{instCode}", materialSubRequest.getInstCode())
  167. .contentType(MediaType.APPLICATION_JSON))
  168. .andExpect(status().isOk())
  169. .andExpect(content().contentType(MediaType.APPLICATION_JSON))
  170. .andExpect(content().string(objectMapper.writeValueAsString(requestService.list(faoInstitute, new PageRequest(0, 10, new Sort(Sort.Direction.DESC, "createdDate"))))));
  171. LOG.info("Test listInstituteRequestsTest passed");
  172. }
  173. @Test
  174. public void getInstituteRequestTest() throws Exception {
  175. LOG.info("Start test-method getInstituteRequestTest");
  176. ObjectMapper objectMapper = new ObjectMapper();
  177. mockMvc.perform(get("/api/v0/requests/{instCode}/r/{uuid}", materialSubRequest.getInstCode(), materialSubRequest.getUuid())
  178. .contentType(MediaType.APPLICATION_JSON))
  179. .andExpect(status().isOk())
  180. .andExpect(content().contentType(MediaType.APPLICATION_JSON))
  181. .andExpect(content().string(objectMapper.writeValueAsString(requestService.get(faoInstitute, materialSubRequest.getUuid()))));
  182. LOG.info("Test getInstituteRequestTest passed");
  183. }
  184. }