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