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

Tests for REST controller

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