Commit 054263c5 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov Committed by Matija Obreza

Request API: remove request

- Removes subRequests
Signed-off-by: Matija Obreza's avatarMatija Obreza <matija.obreza@croptrust.org>
parent ae74deec
......@@ -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")));
}
/**
* 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
*
......
......@@ -157,6 +157,8 @@ public interface RequestService {
MaterialSubRequest get(FaoInstitute institute, String uuid);
MaterialRequest remove(String uuid);
MaterialRequest sendValidationEmail(MaterialRequest materialRequest);
/**
......
......@@ -290,8 +290,8 @@ public class RequestServiceImpl implements RequestService {
*
* @param materialRequest
* @return
* @throws EasySMTAException
* @throws NoPidException
* @throws EasySMTAException
* @throws NoPidException
*/
@Override
@Transactional
......@@ -315,7 +315,7 @@ public class RequestServiceImpl implements RequestService {
*
* @param materialRequest
* @return
* @throws NoPidException
* @throws NoPidException
*/
private List<MaterialSubRequest> breakup(MaterialRequest materialRequest) throws NoPidException {
if (StringUtils.isBlank(materialRequest.getPid())) {
......@@ -482,4 +482,17 @@ public class RequestServiceImpl implements RequestService {
public MaterialSubRequest get(FaoInstitute institute, String 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;
import static org.hamcrest.Matchers.*;
......@@ -14,6 +30,8 @@ import org.genesys.test.base.AbstractApiTest;
import org.genesys2.server.api.v0.AccessionController;
import org.genesys2.server.api.v1.RequestsController;
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.VerificationToken;
import org.genesys2.server.persistence.AccessionRepository;
......@@ -25,6 +43,7 @@ import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.RequestService;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
......@@ -157,6 +176,46 @@ public class RequestControllerTest extends AbstractApiTest {
/*@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
public void validateRequestTest() throws Exception {
......@@ -196,6 +255,14 @@ public class RequestControllerTest extends AbstractApiTest {
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 {
LOG.debug("Upsering {}", verboseMapper.writerWithDefaultPrettyPrinter().writeValueAsString(accessionJson));
/*@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