Commit a8b6c76a authored by Matija Obreza's avatar Matija Obreza

Merge branch '445-request-api-remove-request' into 'master'

Resolve "Request API: remove request"

Closes #445

See merge request genesys-pgr/genesys-server!396
parents ae74deec 054263c5
...@@ -103,6 +103,19 @@ public class RequestsController extends ApiBaseController { ...@@ -103,6 +103,19 @@ public class RequestsController extends ApiBaseController {
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, requestService.list(filterInfo.filter, page.toPageRequest(maxPageSize, Sort.Direction.ASC, "id"))); return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, requestService.list(filterInfo.filter, page.toPageRequest(maxPageSize, Sort.Direction.ASC, "id")));
} }
/**
* Remove request
*
* @return
*/
@PreAuthorize("hasRole('ADMINISTRATOR')")
@RequestMapping(value = "/r/{uuid:.{36}}/remove", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
public MaterialRequest removeRequest(@PathVariable("uuid") String uuid) {
LOG.info("Removing request uuid={}", uuid);
return requestService.remove(uuid);
}
/** /**
* Get request * Get request
* *
......
...@@ -157,6 +157,8 @@ public interface RequestService { ...@@ -157,6 +157,8 @@ public interface RequestService {
MaterialSubRequest get(FaoInstitute institute, String uuid); MaterialSubRequest get(FaoInstitute institute, String uuid);
MaterialRequest remove(String uuid);
MaterialRequest sendValidationEmail(MaterialRequest materialRequest); MaterialRequest sendValidationEmail(MaterialRequest materialRequest);
/** /**
......
...@@ -290,8 +290,8 @@ public class RequestServiceImpl implements RequestService { ...@@ -290,8 +290,8 @@ public class RequestServiceImpl implements RequestService {
* *
* @param materialRequest * @param materialRequest
* @return * @return
* @throws EasySMTAException * @throws EasySMTAException
* @throws NoPidException * @throws NoPidException
*/ */
@Override @Override
@Transactional @Transactional
...@@ -315,7 +315,7 @@ public class RequestServiceImpl implements RequestService { ...@@ -315,7 +315,7 @@ public class RequestServiceImpl implements RequestService {
* *
* @param materialRequest * @param materialRequest
* @return * @return
* @throws NoPidException * @throws NoPidException
*/ */
private List<MaterialSubRequest> breakup(MaterialRequest materialRequest) throws NoPidException { private List<MaterialSubRequest> breakup(MaterialRequest materialRequest) throws NoPidException {
if (StringUtils.isBlank(materialRequest.getPid())) { if (StringUtils.isBlank(materialRequest.getPid())) {
...@@ -482,4 +482,17 @@ public class RequestServiceImpl implements RequestService { ...@@ -482,4 +482,17 @@ public class RequestServiceImpl implements RequestService {
public MaterialSubRequest get(FaoInstitute institute, String uuid) { public MaterialSubRequest get(FaoInstitute institute, String uuid) {
return subRequestRepository.findByInstCodeAndUuid(institute.getCode(), uuid); return subRequestRepository.findByInstCodeAndUuid(institute.getCode(), uuid);
} }
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR')")
public MaterialRequest remove(String uuid) {
MaterialRequest request = requestRepository.findByUuid(uuid);
subRequestRepository.delete(request.getSubRequests());
requestRepository.delete(request);
request.setId(0L);
return request;
}
} }
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.test.server.api.v1; package org.genesys.test.server.api.v1;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
...@@ -14,6 +30,8 @@ import org.genesys.test.base.AbstractApiTest; ...@@ -14,6 +30,8 @@ import org.genesys.test.base.AbstractApiTest;
import org.genesys2.server.api.v0.AccessionController; import org.genesys2.server.api.v0.AccessionController;
import org.genesys2.server.api.v1.RequestsController; import org.genesys2.server.api.v1.RequestsController;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.MaterialRequest;
import org.genesys2.server.model.genesys.MaterialSubRequest;
import org.genesys2.server.model.impl.FaoInstitute; import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.VerificationToken; import org.genesys2.server.model.impl.VerificationToken;
import org.genesys2.server.persistence.AccessionRepository; import org.genesys2.server.persistence.AccessionRepository;
...@@ -25,6 +43,7 @@ import org.genesys2.server.service.ContentService; ...@@ -25,6 +43,7 @@ import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.InstituteService; import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.RequestService; import org.genesys2.server.service.RequestService;
import org.junit.After; import org.junit.After;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
...@@ -157,6 +176,46 @@ public class RequestControllerTest extends AbstractApiTest { ...@@ -157,6 +176,46 @@ public class RequestControllerTest extends AbstractApiTest {
/*@formatter:on*/ /*@formatter:on*/
} }
@Test
public void removeRequestTest() throws Exception {
initRequest();
initRequest(); // make 2
MaterialRequest request = materialRequestRepository.findAll().get(0);
Assert.assertEquals(2, materialRequestRepository.findAll().size());
/*@formatter:off*/
mockMvc.perform(post(RequestsController.CONTROLLER_URL + "/r/" + request.getUuid() + "/remove")
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.uuid", is(request.getUuid())));
/*@formatter:on*/
Assert.assertEquals(1, materialRequestRepository.findAll().size());
}
@Test
public void removeRequestWithSubRequestTest() throws Exception {
initRequest();
initRequest(); // make 2
MaterialRequest request = materialRequestRepository.findAll().get(0);
initSubrequest(request);
Assert.assertEquals(2, materialRequestRepository.findAll().size());
Assert.assertEquals(1, materialSubRequestRepository.findAll().size());
/*@formatter:off*/
mockMvc.perform(post(RequestsController.CONTROLLER_URL + "/r/" + request.getUuid() + "/remove")
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.uuid", is(request.getUuid())));
/*@formatter:on*/
Assert.assertEquals(1, materialRequestRepository.findAll().size());
Assert.assertEquals(0, materialSubRequestRepository.findAll().size());
}
@Test @Test
public void validateRequestTest() throws Exception { public void validateRequestTest() throws Exception {
...@@ -196,6 +255,14 @@ public class RequestControllerTest extends AbstractApiTest { ...@@ -196,6 +255,14 @@ public class RequestControllerTest extends AbstractApiTest {
requestService.initiateRequestByUuids(requestInfo, Collections.singleton(uuid)); requestService.initiateRequestByUuids(requestInfo, Collections.singleton(uuid));
} }
private MaterialSubRequest initSubrequest(MaterialRequest request) {
final MaterialSubRequest subRequest = new MaterialSubRequest();
subRequest.setInstCode(institute.getCode());
subRequest.setSourceRequest(request);
subRequest.setBody(request.getBody());
return materialSubRequestRepository.save(subRequest);
}
private ResultActions upsert(ObjectNode accessionJson) throws Exception, JsonProcessingException { private ResultActions upsert(ObjectNode accessionJson) throws Exception, JsonProcessingException {
LOG.debug("Upsering {}", verboseMapper.writerWithDefaultPrettyPrinter().writeValueAsString(accessionJson)); LOG.debug("Upsering {}", verboseMapper.writerWithDefaultPrettyPrinter().writeValueAsString(accessionJson));
/*@formatter:off*/ /*@formatter:off*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment