Commit dadfbf70 authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch '353-unpublishing-referenced-descriptors' into 'master'

Resolve "Unpublishing referenced descriptors"

Closes #353

See merge request genesys-pgr/genesys-server!254
parents e2a2f553 188c41b3
......@@ -23,6 +23,7 @@ import java.util.UUID;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.api.FilteredPage;
import org.genesys.catalog.exceptions.InvalidApiUsageException;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.filters.DescriptorFilter;
import org.genesys.catalog.model.traits.Descriptor;
......@@ -33,6 +34,7 @@ import org.genesys2.server.api.ApiBaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
......@@ -259,7 +261,11 @@ public class DescriptorController {
*/
@DeleteMapping(value = "/{UUID},{version}")
public Descriptor deleteDescriptor(@PathVariable("UUID") final UUID uuid, @PathVariable("version") final int version) {
try {
return descriptorService.removeDescriptor(descriptorService.getDescriptor(uuid, version));
} catch (DataIntegrityViolationException e) {
throw new InvalidApiUsageException("Cannot delete a referenced descriptor");
}
}
/**
......
......@@ -420,6 +420,17 @@ public class DescriptorListServiceImpl implements DescriptorListService {
}
loaded.setState(PublishState.DRAFT);
descriptorListRepository.save(loaded);
for (final Descriptor referencedDescriptor : loaded.getDescriptors()) {
LOG.info("Rejecting descriptor {}", referencedDescriptor);
try {
descriptorService.rejectDescriptor(referencedDescriptor);
} catch(final InvalidApiUsageException e) {
LOG.info("Not unpublishing a descriptor referenced in a published dataset or descriptor list: {}", e.getMessage());
}
}
return lazyLoad(descriptorListRepository.save(loaded));
}
......
......@@ -58,6 +58,7 @@ import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.acls.domain.BasePermission;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import com.querydsl.core.BooleanBuilder;
......@@ -385,7 +386,7 @@ public class DescriptorServiceImpl implements DescriptorService {
* .catalog.model.traits.Descriptor)
*/
@Override
@Transactional
@Transactional(isolation = Isolation.READ_UNCOMMITTED, noRollbackFor = InvalidApiUsageException.class)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#descriptor, 'administration')")
public Descriptor rejectDescriptor(final Descriptor descriptor) {
final Descriptor loaded = descriptorRepository.findByUuidAndVersion(descriptor.getUuid(), descriptor.getVersion());
......@@ -400,6 +401,19 @@ public class DescriptorServiceImpl implements DescriptorService {
}
}
{
for (final Dataset referencedDataset : loaded.getDatasets()) {
if (referencedDataset.getState() == PublishState.PUBLISHED) {
throw new InvalidApiUsageException("Cannot be un-published. The descriptor is referenced by a published dataset.");
}
}
for (final DescriptorList referencedDescriptorList : loaded.getDescriptorLists()) {
if (referencedDescriptorList.getState() == PublishState.PUBLISHED) {
throw new InvalidApiUsageException("Cannot be un-published. The descriptor is referenced by a published descriptor list.");
}
}
}
loaded.setState(PublishState.DRAFT);
return lazyLoad(descriptorRepository.save(loaded));
}
......
......@@ -53,10 +53,13 @@ public class DescriptorServiceTest extends CatalogServiceTest {
// private static final String VERSION_1_1 = "1.1";
private static final String VERSION_2_0 = "2.0";
private static final String DESCRIPTION_1 = "Some description";
private static final String DESCRIPTION_2 = "Some description 2";
@Override
public void cleanup() throws Exception {
descriptorListRepository.deleteAll();
descriptorRepository.deleteAll();
partnerRepository.deleteAll();
super.cleanup();
}
......@@ -120,6 +123,136 @@ public class DescriptorServiceTest extends CatalogServiceTest {
descriptorService.getDescriptor(result.getUuid(), result.getVersion());
}
@Test
public void testUnpublishDescriptorWithDescriptorList() {
// create descriptor
Descriptor descriptor = descriptorService.createDescriptor(setupDescriptor(null, DESCRIPTOR_TITLE_1, Category.PASSPORT, DataType.NUMERIC, VERSION_1_0, PublishState.DRAFT));
assertThat(descriptor, not(nullValue()));
// create descriptor list
DescriptorList descriptorList = descriptorListService.createDescriptorList(setupDescriptorList("TestTitle", VERSION_1_0, DESCRIPTION_1));
assertThat(descriptorList, not(nullValue()));
// add descriptor to the descriptor list
descriptorList = descriptorListService.addDescriptors(descriptorList, descriptor);
assertThat(descriptorList.getDescriptors(), hasSize(1));
// approve descriptor list
descriptorList = descriptorListService.reviewDescriptorList(descriptorList);
descriptorList = descriptorListService.approveDescriptorList(descriptorList);
descriptor = descriptorService.getDescriptor(descriptor.getUuid());
// check if descriptorList and referenced descriptor are PUBLISHED
assertThat(descriptorList.getState(), is(PublishState.PUBLISHED));
assertThat(descriptor.getState(), is(PublishState.PUBLISHED));
// reject descriptorList
descriptorList = descriptorListService.rejectDescriptorList(descriptorList);
descriptor = descriptorService.getDescriptor(descriptor.getUuid());
assertThat(descriptorList.getState(), is(PublishState.DRAFT));
//referenced descriptor also should be rejected by the code
assertThat(descriptor.getState(), is(PublishState.DRAFT));
}
@Test(expected = InvalidApiUsageException.class)
public void testUnpublishReferencedByDescriptorListFailed() {
// create descriptor
Descriptor descriptor = descriptorService.createDescriptor(setupDescriptor(null, DESCRIPTOR_TITLE_1, Category.PASSPORT, DataType.NUMERIC, VERSION_1_0, PublishState.DRAFT));
assertThat(descriptor, not(nullValue()));
// create descriptor list
DescriptorList descriptorList = descriptorListService.createDescriptorList(setupDescriptorList("TestTitle", VERSION_1_0, DESCRIPTION_1));
assertThat(descriptorList, not(nullValue()));
// add descriptor to the descriptor list
descriptorList = descriptorListService.addDescriptors(descriptorList, descriptor);
assertThat(descriptorList.getDescriptors(), hasSize(1));
// change status of descriptor list and descriptor to the REVIEWING
descriptorList = descriptorListService.reviewDescriptorList(descriptorList);
descriptor = descriptorService.getDescriptor(descriptor.getUuid());
assertThat(descriptorList.getState(), is(PublishState.REVIEWING));
assertThat(descriptor.getState(), is(PublishState.REVIEWING));
// change status of descriptor list and descriptor to the PUBLISHED
descriptorList = descriptorListService.approveDescriptorList(descriptorList);
descriptor = descriptorService.getDescriptor(descriptor.getUuid());
assertThat(descriptorList.getState(), is(PublishState.PUBLISHED));
assertThat(descriptor.getState(), is(PublishState.PUBLISHED));
// here we expect an exception because the descriptor is already referenced by published descriptor list
descriptorService.rejectDescriptor(descriptor);
}
@Test
public void testUnpublishReferencedByDescriptorList() {
// create descriptor
Descriptor descriptor = descriptorService.createDescriptor(setupDescriptor(null, DESCRIPTOR_TITLE_1, Category.PASSPORT, DataType.NUMERIC, VERSION_1_0, PublishState.DRAFT));
assertThat(descriptor, not(nullValue()));
// create descriptor list
DescriptorList descriptorList1 = descriptorListService.createDescriptorList(setupDescriptorList("TestTitle1", VERSION_1_0, DESCRIPTION_1));
assertThat(descriptorList1, not(nullValue()));
DescriptorList descriptorList2 = descriptorListService.createDescriptorList(setupDescriptorList("TestTitle2", VERSION_1_0, DESCRIPTION_2));
assertThat(descriptorList1, not(nullValue()));
// add descriptor to the descriptor list
descriptorList1 = descriptorListService.addDescriptors(descriptorList1, descriptor);
assertThat(descriptorList1.getDescriptors(), hasSize(1));
// add descriptor to the descriptor list 2
descriptorList2 = descriptorListService.addDescriptors(descriptorList2, descriptor);
assertThat(descriptorList2.getDescriptors(), hasSize(1));
// change status of descriptor lists the PUBLISHED
descriptorList1 = descriptorListService.reviewDescriptorList(descriptorList1);
descriptorList2 = descriptorListService.reviewDescriptorList(descriptorList2);
descriptorList1 = descriptorListService.approveDescriptorList(descriptorList1);
descriptorList2 = descriptorListService.approveDescriptorList(descriptorList2);
assertThat(descriptorList1.getState(), is(PublishState.PUBLISHED));
assertThat(descriptorList2.getState(), is(PublishState.PUBLISHED));
descriptor = descriptorService.getDescriptor(descriptor.getUuid());
assertThat(descriptor.getState(), is(PublishState.PUBLISHED));
// reject one descriptor list
descriptorList1 = descriptorListService.rejectDescriptorList(descriptorList1);
assertThat(descriptorList1.getState(), is(PublishState.DRAFT));
// descriptor should stay in PUBLISHED state
descriptor = descriptorService.getDescriptor(descriptor.getUuid());
assertThat(descriptor.getState(), is(PublishState.PUBLISHED));
}
@Test
public void testUnpublishReferencedByDescriptorList2() {
// create descriptor
Descriptor descriptor = descriptorService.createDescriptor(setupDescriptor(null, DESCRIPTOR_TITLE_1, Category.PASSPORT, DataType.NUMERIC, VERSION_1_0, PublishState.DRAFT));
assertThat(descriptor, not(nullValue()));
// create descriptor list
DescriptorList descriptorList = descriptorListService.createDescriptorList(setupDescriptorList("TestTitle", VERSION_1_0, DESCRIPTION_1));
assertThat(descriptorList, not(nullValue()));
// add descriptor to the descriptor list
descriptorList = descriptorListService.addDescriptors(descriptorList, descriptor);
assertThat(descriptorList.getDescriptors(), hasSize(1));
// change status of descriptor list and descriptor to the REVIEWING
descriptorList = descriptorListService.reviewDescriptorList(descriptorList);
descriptor = descriptorService.getDescriptor(descriptor.getUuid());
assertThat(descriptorList.getState(), is(PublishState.REVIEWING));
assertThat(descriptor.getState(), is(PublishState.REVIEWING));
// change status of descriptor list and descriptor to the DRAFT
descriptorList = descriptorListService.rejectDescriptorList(descriptorList);
descriptor = descriptorService.getDescriptor(descriptor.getUuid());
assertThat(descriptorList.getState(), is(PublishState.DRAFT));
assertThat(descriptor.getState(), is(PublishState.DRAFT));
}
@Test
public void createDescriptorWithVocabularyForDataTypeTEXT() {
final UUID uuid = UUID.randomUUID();
......
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