Commit 53fb86ef authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

Tests for REST controller

parent 14bd7737
......@@ -229,6 +229,8 @@ public class SubsetServiceImpl implements SubsetService {
// Keep accessions that are not in the list
subset.setAccessionIds(subset.getAccessionIds().stream().filter(accessionId -> !uuidsToRemove.contains(accessionId.getUuid())).collect(Collectors.toList()));
subset.setState(PublishState.DRAFT);
return deepLoad(subsetRepository.save(subset));
}
......@@ -253,6 +255,7 @@ public class SubsetServiceImpl implements SubsetService {
currentAccessions.add(accession.getUuid());
}
}
subset.setState(PublishState.DRAFT);
return deepLoad(subsetRepository.save(subset));
}
......@@ -313,12 +316,13 @@ public class SubsetServiceImpl implements SubsetService {
throw new NotFoundElement("No subset with specified uuid and version");
}
if (!securityUtils.hasRole(UserRole.ADMINISTRATOR)) {
if (!securityUtils.hasRole(UserRole.ADMINISTRATOR) && loaded.getState().equals(PublishState.PUBLISHED)) {
long oneDay = 24 * 60 * 60 * 1000;
if (loaded.getLastModifiedDate() != null && loaded.getLastModifiedDate().getTime() <= (System.currentTimeMillis() - oneDay)) {
throw new InvalidApiUsageException("Cannot be un-published. More than 24 hours have passed since the publication.");
}
}
loaded.setState(PublishState.DRAFT);
return deepLoad(subsetRepository.save(loaded));
}
......
......@@ -30,8 +30,10 @@ import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.HashSet;
import org.genesys.test.base.AbstractApiTest;
import org.genesys.test.base.WithMockOAuth2Authentication;
import org.genesys2.server.api.v1.SubsetController;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.genesys.Accession;
......@@ -54,6 +56,7 @@ import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
......@@ -226,24 +229,30 @@ public class SubsetRestControllerTest extends AbstractApiTest {
assertNull(subsetRepository.getByUuid(subset.getUuid()));
}
//FIXME uncomment and fix
// @Test
// public void listSubsetsTest() throws Exception {
// final Subset subset = subsetService.create(setUpSubset());
// final SubsetFilter subsetFilter = new SubsetFilter();
// subsetFilter.published = true;
//
// /*@formatter:off*/
// mockMvc.perform(post(SubsetController.API_BASE.concat("/list"))
// .contentType(MediaType.APPLICATION_JSON)
// .content(objectMapper.writeValueAsString(subsetFilter)))
// // .andDo(MockMvcResultHandlers.print())
// .andExpect(status().isOk())
// .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
// .andExpect(jsonPath("$.content[0]", not(nullValue())))
// .andExpect(jsonPath("$.content[0].id", is(subset.getId().intValue())));
// /*@formatter:on*/
// }
@Test
public void listSubsetsTest() throws Exception {
Subset subset = subsetService.create(setUpSubset());
assertEquals(subset.getState(), PublishState.DRAFT);
subset = subsetService.reviewSubset(subset);
assertEquals(subset.getState(), PublishState.REVIEWING);
subset = subsetService.approveSubset(subset);
assertEquals(subset.getState(), PublishState.PUBLISHED);
final SubsetFilter subsetFilter = new SubsetFilter();
subsetFilter.state = new HashSet<>();
subsetFilter.state.add(PublishState.PUBLISHED);
/*@formatter:off*/
mockMvc.perform(post(SubsetController.API_BASE.concat("/list"))
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(subsetFilter)))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.content[0]", not(nullValue())))
.andExpect(jsonPath("$.content[0].id", is(subset.getId().intValue())));
/*@formatter:on*/
}
@Test
public void removeAccessionsFromSubsetTest() throws Exception {
......@@ -286,6 +295,65 @@ public class SubsetRestControllerTest extends AbstractApiTest {
assertThat(updated.getAccessionIds().size(), is(4));
}
@Test
@WithMockOAuth2Authentication(roles = { "ADMINISTRATOR" }, scopes = { "write" })
public void approveSubsetTest() throws Exception {
Subset subset = subsetService.create(setUpSubset());
assertEquals(subset.getState(), PublishState.DRAFT);
subset = subsetService.reviewSubset(subset);
assertEquals(subset.getState(), PublishState.REVIEWING);
/*@formatter:off*/
mockMvc
.perform(post(SubsetController.API_BASE.concat("/approve"))
.param("uuid", subset.getUuid().toString())
.param("version", subset.getVersion().toString()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.uuid", is(subset.getUuid().toString())))
.andExpect(jsonPath("$.state", is(PublishState.PUBLISHED.name())));
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void reviewSubsetTest() throws Exception {
final Subset subset = subsetService.create(setUpSubset());
assertEquals(subset.getState(), PublishState.DRAFT);
/*@formatter:off*/
mockMvc
.perform(post(SubsetController.API_BASE.concat("/for-review"))
.param("uuid", subset.getUuid().toString())
.param("version", subset.getVersion().toString()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.uuid", is(subset.getUuid().toString())))
.andExpect(jsonPath("$.state", is(PublishState.REVIEWING.name())));
/*@formatter:on*/
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void rejectSubsetTest() throws Exception {
final Subset subset = subsetService.create(setUpSubset());
assertEquals(subset.getState(), PublishState.DRAFT);
/*@formatter:off*/
mockMvc
.perform(post(SubsetController.API_BASE.concat("/reject"))
.param("uuid", subset.getUuid().toString())
.param("version", subset.getVersion().toString()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.uuid", is(subset.getUuid().toString())))
.andExpect(jsonPath("$.state", is(PublishState.DRAFT.name())));
/*@formatter:on*/
}
private Subset setUpSubset() {
final Subset subset = new Subset();
subset.setWiewsCode(institute.getCode());
......
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