Commit 77bcc713 authored by Maxym Borodenko's avatar Maxym Borodenko
Browse files

FIX: Current version can be only for published Dataset/Subset

parent 0febb8e9
......@@ -920,9 +920,11 @@ public class DatasetServiceImpl implements DatasetService {
if (loaded.isPublished() && Objects.equals(loaded.getCurrent(), true)) {
final DatasetVersions datasetVersions = loaded.getVersions();
if (datasetVersions.getAllVersions().size() > 1) {
UUID youngestDatasetUUID = datasetVersions.getAllVersions().stream()
.filter(s -> s.getCurrent() == null)
List<Dataset> notCurrentPublishedVersions = datasetVersions.getAllVersions().stream()
.filter(d -> d.getCurrent() == null && d.isPublished()).collect(Collectors.toList());
if (!notCurrentPublishedVersions.isEmpty()) {
UUID youngestDatasetUUID = notCurrentPublishedVersions.stream()
.max(Comparator.comparing(Dataset::getCreatedDate)).get().getUuid();
loaded.setCurrent(null);
......
......@@ -29,6 +29,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
......@@ -515,9 +516,11 @@ public class SubsetServiceImpl implements SubsetService {
if (loadedSubset.isPublished() && Objects.equals(loadedSubset.getCurrent(), true)) {
final SubsetVersions subsetVersions = loadedSubset.getVersions();
if (subsetVersions.getAllVersions().size() > 1) {
UUID youngestSubsetUUID = subsetVersions.getAllVersions().stream()
.filter(s -> s.getCurrent() == null)
List<Subset> notCurrentPublishedVersions = subsetVersions.getAllVersions().stream()
.filter(s -> s.getCurrent() == null && s.isPublished()).collect(Collectors.toList());
if (!notCurrentPublishedVersions.isEmpty()) {
UUID youngestSubsetUUID = notCurrentPublishedVersions.stream()
.max(Comparator.comparing(Subset::getCreatedDate)).get().getUuid();
loadedSubset.setCurrent(null);
......
......@@ -282,7 +282,7 @@ public class DatasetControllerTest extends AbstractApiTest {
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void makeCurrentAYoungestDatasetTest() throws Exception {
public void makeCurrentAYoungestPublishedDatasetTest() throws Exception {
Dataset publishedDataset_1 = toPublished(setUpDataset());
String response1 = createNewVersion(publishedDataset_1).andReturn().getResponse().getContentAsString();
......@@ -346,6 +346,112 @@ public class DatasetControllerTest extends AbstractApiTest {
assertEquals(true, datasetRepository.findByUuid(youngestSubset.getUuid()).getCurrent());
}
/**
*
* The system shouldn't be able to assign a dataset as current if it isn't published
*/
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void rejectCurrentWithoutOtherPublishedVersionsTest() throws Exception {
Dataset publishedDataset_1 = toPublished(setUpDataset());
assertEquals(publishedDataset_1.getCurrent(), true);
assertEquals(publishedDataset_1.getState(), PublishState.PUBLISHED);
String response1 = createNewVersion(publishedDataset_1).andReturn().getResponse().getContentAsString();
// should be 2 datasets in DB
assertEquals(datasetRepository.count(), 2);
// should be only one dataset version in DB
assertEquals(datasetVersionsRepository.count(), 1);
Dataset respDataset1 = objectMapper.readValue(response1, Dataset.class);
assertEquals(respDataset1.getState(), PublishState.DRAFT);
assertNull(respDataset1.getCurrent());
String response2 = createNewVersion(publishedDataset_1).andReturn().getResponse().getContentAsString();
// should be 3 datasets in DB
assertEquals(datasetRepository.count(), 3);
// should be only one dataset version in DB
assertEquals(datasetVersionsRepository.count(), 1);
Dataset respDataset2 = objectMapper.readValue(response2, Dataset.class);
assertEquals(respDataset2.getState(), PublishState.DRAFT);
assertNull(respDataset1.getCurrent());
Dataset youngestDataset = null;
if (respDataset1.getCreatedDate().after(respDataset2.getCreatedDate())) {
youngestDataset = respDataset1;
} else {
youngestDataset = respDataset2;
}
/*@formatter:off*/
mockMvc.perform(post(DatasetController.CONTROLLER_URL.concat("/reject"))
.param("uuid", publishedDataset_1.getUuid().toString())
.param("version", publishedDataset_1.getVersion().toString()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
/*@formatter:on*/
assertNull(datasetRepository.findByUuid(youngestDataset.getUuid()).getCurrent());
}
@Test
@WithMockUser(username = "user", password = "user", roles = "ADMINISTRATOR")
public void makeCurrentAPublishedSubsetWhenRejectTest() throws Exception {
Dataset publishedDataset_1 = toPublished(setUpDataset());
assertEquals(publishedDataset_1.getCurrent(), true);
assertEquals(publishedDataset_1.getState(), PublishState.PUBLISHED);
String response1 = createNewVersion(publishedDataset_1).andReturn().getResponse().getContentAsString();
// should be 2 datasets in DB
assertEquals(datasetRepository.count(), 2);
// should be only one dataset version in DB
assertEquals(datasetVersionsRepository.count(), 1);
Dataset respDataset1 = objectMapper.readValue(response1, Dataset.class);
assertEquals(respDataset1.getState(), PublishState.DRAFT);
assertNull(respDataset1.getCurrent());
String response2 = createNewVersion(publishedDataset_1).andReturn().getResponse().getContentAsString();
// should be 3 datasets in DB
assertEquals(datasetRepository.count(), 3);
// should be only one dataset version in DB
assertEquals(datasetVersionsRepository.count(), 1);
Dataset respDataset2 = objectMapper.readValue(response2, Dataset.class);
assertEquals(respDataset2.getState(), PublishState.DRAFT);
assertNull(respDataset1.getCurrent());
String response3 = createNewVersion(publishedDataset_1).andReturn().getResponse().getContentAsString();
// should be 4 datasets in DB
assertEquals(datasetRepository.count(), 4);
// should be only one dataset version in DB
assertEquals(datasetVersionsRepository.count(), 1);
Dataset respDataset3 = objectMapper.readValue(response3, Dataset.class);
assertEquals(respDataset2.getState(), PublishState.DRAFT);
Dataset publishedDataset_2 = toPublished(respDataset3);
assertEquals(publishedDataset_2.getState(), PublishState.PUBLISHED);
assertEquals(publishedDataset_2.getCurrent(), true);
assertEquals(publishedDataset_2.getVersions().getAllVersions().size(), 4);
/*@formatter:off*/
mockMvc.perform(post(DatasetController.CONTROLLER_URL.concat("/reject"))
.param("uuid", publishedDataset_2.getUuid().toString())
.param("version", publishedDataset_2.getVersion().toString()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
/*@formatter:on*/
assertEquals(true, datasetRepository.findByUuid(publishedDataset_1.getUuid()).getCurrent());
}
private ResultActions createNewVersion(final Dataset dataset) throws Exception {
/*@formatter:off*/
return mockMvc.perform(post(DatasetController.CONTROLLER_URL.concat("/create-new-version"))
......
......@@ -31,7 +31,6 @@ import java.util.stream.Collectors;
import com.google.common.collect.Sets;
import org.genesys.catalog.model.Partner;
import org.genesys.catalog.model.dataset.DatasetCreator;
import org.genesys.test.base.AbstractApiTest;
import org.genesys.test.base.WithMockOAuth2Authentication;
import org.genesys2.server.api.v1.SubsetController;
......@@ -602,7 +601,7 @@ public class SubsetRestControllerTest extends AbstractApiTest {
}
@Test
public void makeCurrentAYoungestSubsetTest() throws Exception {
public void makeCurrentAYoungestPublishedSubsetTest() throws Exception {
Subset publishedSubset_1 = toPublished(setUpSubset());
String response1 = createNewVersion(publishedSubset_1).andReturn().getResponse().getContentAsString();
......@@ -666,6 +665,110 @@ public class SubsetRestControllerTest extends AbstractApiTest {
assertEquals(true, subsetRepository.getByUuid(youngestSubset.getUuid()).getCurrent());
}
/**
*
* The system shouldn't be able to assign a subset as current if it isn't published
*/
@Test
public void rejectCurrentWithoutOtherPublishedVersionsTest() throws Exception {
Subset publishedSubset_1 = toPublished(setUpSubset());
assertEquals(publishedSubset_1.getCurrent(), true);
assertEquals(publishedSubset_1.getState(), PublishState.PUBLISHED);
String response1 = createNewVersion(publishedSubset_1).andReturn().getResponse().getContentAsString();
// should be 2 subsets in DB
assertEquals(subsetRepository.count(), 2);
// should be only one subset version in DB
assertEquals(subsetVersionsRepository.count(), 1);
Subset respSubset1 = objectMapper.readValue(response1, Subset.class);
assertEquals(respSubset1.getState(), PublishState.DRAFT);
assertNull(respSubset1.getCurrent());
String response2 = createNewVersion(publishedSubset_1).andReturn().getResponse().getContentAsString();
// should be 3 subsets in DB
assertEquals(subsetRepository.count(), 3);
// should be only one subset version in DB
assertEquals(subsetVersionsRepository.count(), 1);
Subset respSubset2 = objectMapper.readValue(response2, Subset.class);
assertEquals(respSubset2.getState(), PublishState.DRAFT);
assertNull(respSubset1.getCurrent());
Subset youngestSubset = null;
if (respSubset1.getCreatedDate().after(respSubset2.getCreatedDate())) {
youngestSubset = respSubset1;
} else {
youngestSubset = respSubset2;
}
/*@formatter:off*/
mockMvc.perform(post(SubsetController.API_BASE.concat("/reject"))
.param("uuid", publishedSubset_1.getUuid().toString())
.param("version", publishedSubset_1.getVersion().toString()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
/*@formatter:on*/
assertNull(subsetRepository.getByUuid(youngestSubset.getUuid()).getCurrent());
}
@Test
public void makeCurrentAPublishedSubsetWhenRejectTest() throws Exception {
Subset publishedSubset_1 = toPublished(setUpSubset());
assertEquals(publishedSubset_1.getCurrent(), true);
assertEquals(publishedSubset_1.getState(), PublishState.PUBLISHED);
String response1 = createNewVersion(publishedSubset_1).andReturn().getResponse().getContentAsString();
// should be 2 subsets in DB
assertEquals(subsetRepository.count(), 2);
// should be only one subset version in DB
assertEquals(subsetVersionsRepository.count(), 1);
Subset respSubset1 = objectMapper.readValue(response1, Subset.class);
assertEquals(respSubset1.getState(), PublishState.DRAFT);
assertNull(respSubset1.getCurrent());
String response2 = createNewVersion(publishedSubset_1).andReturn().getResponse().getContentAsString();
// should be 3 subsets in DB
assertEquals(subsetRepository.count(), 3);
// should be only one subset version in DB
assertEquals(subsetVersionsRepository.count(), 1);
Subset respSubset2 = objectMapper.readValue(response2, Subset.class);
assertEquals(respSubset2.getState(), PublishState.DRAFT);
assertNull(respSubset1.getCurrent());
String response3 = createNewVersion(publishedSubset_1).andReturn().getResponse().getContentAsString();
// should be 4 subsets in DB
assertEquals(subsetRepository.count(), 4);
// should be only one subset version in DB
assertEquals(subsetVersionsRepository.count(), 1);
Subset respSubset3 = objectMapper.readValue(response3, Subset.class);
assertEquals(respSubset2.getState(), PublishState.DRAFT);
Subset publishedSubset_2 = toPublished(respSubset3);
assertEquals(publishedSubset_2.getState(), PublishState.PUBLISHED);
assertEquals(publishedSubset_2.getCurrent(), true);
assertEquals(publishedSubset_2.getVersions().getAllVersions().size(), 4);
/*@formatter:off*/
mockMvc.perform(post(SubsetController.API_BASE.concat("/reject"))
.param("uuid", publishedSubset_2.getUuid().toString())
.param("version", publishedSubset_2.getVersion().toString()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
/*@formatter:on*/
assertEquals(true, subsetRepository.getByUuid(publishedSubset_1.getUuid()).getCurrent());
}
private ResultActions createNewVersion(final Subset subset) throws Exception {
/*@formatter:off*/
return mockMvc.perform(post(SubsetController.API_BASE.concat("/create-new-version"))
......
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