Commit 681a231f authored by Matija Obreza's avatar Matija Obreza
Browse files

Only list published Subsets for Accession

parent b898a6d1
......@@ -552,7 +552,7 @@ public class SubsetServiceImpl implements SubsetService {
@Override
public List<Subset> listByAccession(Accession accession) {
return (List<Subset>) subsetRepository.findAll(QSubset.subset.accessionRefs.any().accession.eq(accession));
return (List<Subset>) subsetRepository.findAll(QSubset.subset.state.in(PublishState.PUBLISHED).and(QSubset.subset.accessionRefs.any().accession.eq(accession)));
}
@Transactional
......
package org.genesys.test.catalog.services;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.HashSet;
import java.util.List;
......@@ -19,6 +19,7 @@ import org.genesys2.server.model.impl.SubsetCreator;
import org.genesys2.server.persistence.SubsetAccessionRefRepository;
import org.genesys2.server.persistence.SubsetCreatorRepository;
import org.genesys2.server.persistence.SubsetRepository;
import org.genesys2.server.persistence.SubsetVersionsRepository;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.SubsetService;
import org.junit.After;
......@@ -33,222 +34,258 @@ import com.google.common.collect.Sets;
public class SubsetServiceTest extends CatalogServiceTest {
@Autowired
private SubsetRepository subsetRepository;
@Autowired
private SubsetRepository subsetRepository;
@Autowired
private SubsetVersionsRepository subsetVersionsRepository;
@Autowired
private SubsetCreatorRepository subsetCreatorRepository;
@Autowired
private SubsetCreatorRepository subsetCreatorRepository;
@Autowired
private SubsetService subsetService;
@Autowired
private SubsetService subsetService;
@Autowired
private InstituteService instituteService;
@Autowired
private SubsetAccessionRefRepository accessionRefRepository;
@Autowired
private InstituteService instituteService;
@Autowired
private SubsetAccessionRefRepository accessionRefRepository;
private Subset subset;
private SubsetCreator subsetCreator;
private FaoInstitute faoInstitute;
private Subset subset;
private SubsetCreator subsetCreator;
private FaoInstitute faoInstitute;
private UUID SUBSET_CREATOR_UUID = UUID.randomUUID();
private UUID SUBSET_UUID = UUID.randomUUID();
private UUID SUBSET_CREATOR_UUID = UUID.randomUUID();
private UUID SUBSET_UUID = UUID.randomUUID();
private static final String SUBSET_CREATOR_FULL_NAME = "SubsetCreator";
private static final String SUBSET_CREATOR_EMAIL = "test@example.com";
private static final String SUBSET_CREATOR_FULL_NAME = "SubsetCreator";
private static final String SUBSET_CREATOR_EMAIL = "test@example.com";
@Before
@Override
public void beforeTest() throws Exception {
super.beforeTest();
@Before
@Override
public void beforeTest() throws Exception {
super.beforeTest();
faoInstitute = createInstitute();
subset = createSubset(SUBSET_UUID);
assertThat(accessionRefRepository.count(), is(0l));
assertThat(subsetVersionsRepository.count(), is(0l));
assertThat(subsetRepository.count(), is(0l));
subsetCreator = subsetService.createSubsetCreator(subset, buildSubsetCreator(SUBSET_CREATOR_UUID));
faoInstitute = createInstitute();
subset = createSubset(SUBSET_UUID);
assertThat(subsetCreator, not(nullValue()));
assertThat(subsetCreator.getId(), not(nullValue()));
assertThat(subsetCreator.getFullName(), is(SUBSET_CREATOR_FULL_NAME));
assertThat(subsetCreator.getEmail(), is(SUBSET_CREATOR_EMAIL));
}
subsetCreator = subsetService.createSubsetCreator(subset, buildSubsetCreator(SUBSET_CREATOR_UUID));
@After
@Override
public void cleanup() throws Exception {
subsetCreatorRepository.deleteAll();
if (accessionRefRepository.count() > 0) {
Thread.sleep(600);
}
accessionRefRepository.deleteAll();
subsetRepository.deleteAll();
assertThat(subsetCreator, not(nullValue()));
assertThat(subsetCreator.getId(), not(nullValue()));
assertThat(subsetCreator.getFullName(), is(SUBSET_CREATOR_FULL_NAME));
assertThat(subsetCreator.getEmail(), is(SUBSET_CREATOR_EMAIL));
}
super.cleanup();
}
@After
@Override
public void cleanup() throws Exception {
subsetCreatorRepository.deleteAll();
if (accessionRefRepository.count() > 0) {
// If we made some accessions, let's wait for background processes to finish
Thread.sleep(100);
}
accessionRefRepository.deleteAll();
subsetVersionsRepository.deleteAll();
subsetRepository.deleteAll();
@Test
public void testUpdateSubsetCreator() {
// change some values
String newFullName = "New Full Name";
String newEmail = "new-test@example.com";
super.cleanup();
}
@Test
public void testUpdateSubsetCreator() {
// change some values
String newFullName = "New Full Name";
String newEmail = "new-test@example.com";
subsetCreator.setFullName(newFullName);
subsetCreator.setEmail(newEmail);
subsetCreator.setFullName(newFullName);
subsetCreator.setEmail(newEmail);
subsetService.updateSubsetCreator(subset, subsetCreator);
subsetService.updateSubsetCreator(subset, subsetCreator);
final SubsetCreator result = subsetCreatorRepository.findOne(subsetCreator.getId());
assertThat(result, not(nullValue()));
assertThat(result.getFullName(), equalTo(newFullName));
assertThat(result.getEmail(), equalTo(newEmail));
}
/**
* Testing javax validation.
* Fail to update subset when title = null
*/
@Test(expected = ConstraintViolationException.class)
public void testUpdateSubsetWithFailedValidation() {
assertThat(subset.getTitle(), notNullValue());
subset.setTitle(null);
// title cannot be null
subsetService.update(subset);
}
@Test
public void testLoadMoreAccessions() {
final Accession accession1 = upsertAccession("XXX0001", "acceNumb1", "genus1");
final Accession accession2 = upsertAccession("XXX0002", "acceNumb2", "genus2");
final Accession accession3 = upsertAccession("XXX0003", "acceNumb3", "genus3");
final Accession accession4 = upsertAccession("XXX0004", "acceNumb4", "genus4");
final Accession accession5 = upsertAccession("XXX0005", "acceNumb5", "genus5");
final int PAGE_SIZE = 2;
Set<SubsetAccessionRef> accessionRefs = Sets.newHashSet(new SubsetAccessionRef(accession1), new SubsetAccessionRef(accession2), new SubsetAccessionRef(accession3),
new SubsetAccessionRef(accession4), new SubsetAccessionRef(accession5));
// add accessions to the Subset
subset = subsetService.addAccessionRefs(subset, accessionRefs);
assertThat(result, not(nullValue()));
assertThat(result.getFullName(), equalTo(newFullName));
assertThat(result.getEmail(), equalTo(newEmail));
}
/**
* Testing javax validation.
* Fail to update subset when title = null
*/
@Test(expected = ConstraintViolationException.class)
public void testUpdateSubsetWithFailedValidation() {
assertThat(subset.getTitle(), notNullValue());
subset.setTitle(null);
// title cannot be null
subsetService.update(subset);
}
@Test
public void testLoadMoreAccessions() {
final Accession accession1 = upsertAccession("XXX0001", "acceNumb1", "genus1");
final Accession accession2 = upsertAccession("XXX0002", "acceNumb2", "genus2");
final Accession accession3 = upsertAccession("XXX0003", "acceNumb3", "genus3");
final Accession accession4 = upsertAccession("XXX0004", "acceNumb4", "genus4");
final Accession accession5 = upsertAccession("XXX0005", "acceNumb5", "genus5");
final int PAGE_SIZE = 2;
Set<SubsetAccessionRef> accessionRefs = Sets.newHashSet(new SubsetAccessionRef(accession1), new SubsetAccessionRef(accession2), new SubsetAccessionRef(accession3),
new SubsetAccessionRef(accession4), new SubsetAccessionRef(accession5));
// add accessions to the Subset
subset = subsetService.addAccessionRefs(subset, accessionRefs);
subset = subsetService.rematchSubsetAccessions(subset);
assertThat(subset.getAccessionCount(), is(accessionRefs.size()));
assertThat(subset.getAccessionCount(), is(accessionRefs.size()));
final Set<SubsetAccessionRef> refs = new HashSet<>(5);
final Set<SubsetAccessionRef> refs = new HashSet<>(5);
// fetch first page
// fetch first page
Page<SubsetAccessionRef> page1 = subsetService.listAccessions(subset, new PageRequest(0, PAGE_SIZE));
assertThat(page1.getTotalElements(), is((long) subset.getAccessionCount()));
assertThat(page1.getContent().size(), is(PAGE_SIZE));
// add page content to the HashSet to be sure that we avoid duplicates
refs.addAll(page1.getContent());
assertThat(page1.getTotalElements(), is((long) subset.getAccessionCount()));
assertThat(page1.getContent().size(), is(PAGE_SIZE));
// add page content to the HashSet to be sure that we avoid duplicates
refs.addAll(page1.getContent());
// fetch second page
// fetch second page
Page<SubsetAccessionRef> page2 = subsetService.listAccessions(subset, new PageRequest(1, PAGE_SIZE));
assertThat(page2.getTotalElements(), is((long) subset.getAccessionCount()));
assertThat(page2.getContent().size(), is(PAGE_SIZE));
refs.addAll(page2.getContent());
assertThat(page2.getTotalElements(), is((long) subset.getAccessionCount()));
assertThat(page2.getContent().size(), is(PAGE_SIZE));
refs.addAll(page2.getContent());
// fetch third page
// fetch third page
Page<SubsetAccessionRef> page3 = subsetService.listAccessions(subset, new PageRequest(2, PAGE_SIZE));
assertThat(page3.getTotalElements(), is((long) subset.getAccessionCount()));
assertThat(page3.getTotalElements(), is((long) subset.getAccessionCount()));
// it's a last page, so content size should be 1
assertThat(page3.getContent().size(), is(subset.getAccessionCount() - (PAGE_SIZE * 2)));
refs.addAll(page3.getContent());
// check that we didn't fetch duplicates
assertThat(refs.size(), is((int) subset.getAccessionCount()));
}
@Test
public void testListByAccession() {
final Accession accession1 = upsertAccession("XXX0001", "acceNumb1", "genus1");
final Accession accession2 = upsertAccession("XXX0002", "acceNumb2", "genus2");
Set<SubsetAccessionRef> accessionRefs = Sets.newHashSet(new SubsetAccessionRef(accession1));
// add accessions to the Subset
subset = subsetService.addAccessionRefs(subset, accessionRefs);
subset = subsetService.rematchSubsetAccessions(subset);
assertThat(subset.getAccessionCount(), is(accessionRefs.size()));
// it's a last page, so content size should be 1
assertThat(page3.getContent().size(), is(subset.getAccessionCount() - (PAGE_SIZE * 2)));
refs.addAll(page3.getContent());
// check unpublished
assertThat(subsetService.listByAccession(accession1), hasSize(0));
assertThat(subsetService.listByAccession(accession2), hasSize(0));
subset = subsetService.reviewSubset(subset);
assertThat(subsetService.listByAccession(accession1), hasSize(0));
assertThat(subsetService.listByAccession(accession2), hasSize(0));
// check that we didn't fetch duplicates
assertThat(refs.size(), is((int) subset.getAccessionCount()));
}
// approved
subset = subsetService.approveSubset(subset);
assertThat(subsetService.listByAccession(accession1), hasSize(1));
assertThat(subsetService.listByAccession(accession2), hasSize(0));
}
@Test
public void testRemoveSubsetCreator() {
assertThat(subsetCreatorRepository.findAll().size(), is(1));
@Test
public void testRemoveSubsetCreator() {
assertThat(subsetCreatorRepository.findAll().size(), is(1));
subsetService.removeSubsetCreator(subset, subsetCreator);
subsetService.removeSubsetCreator(subset, subsetCreator);
assertThat(subsetCreatorRepository.findAll().size(), is(0));
}
assertThat(subsetCreatorRepository.findAll().size(), is(0));
}
@Test
public void testRemoveSubsetCreatorByUUID() {
assertThat(subsetCreatorRepository.findAll().size(), is(1));
@Test
public void testRemoveSubsetCreatorByUUID() {
assertThat(subsetCreatorRepository.findAll().size(), is(1));
subsetService.removeSubsetCreator(subset, SUBSET_CREATOR_UUID);
subsetService.removeSubsetCreator(subset, SUBSET_CREATOR_UUID);
assertThat(subsetCreatorRepository.findAll().size(), is(0));
}
assertThat(subsetCreatorRepository.findAll().size(), is(0));
}
@Test
public void testLoadSubsetCreatorByUUID() {
final SubsetCreator result = subsetService.loadSubsetCreator(SUBSET_CREATOR_UUID);
@Test
public void testLoadSubsetCreatorByUUID() {
final SubsetCreator result = subsetService.loadSubsetCreator(SUBSET_CREATOR_UUID);
assertThat(result, not(nullValue()));
assertThat(result.getId(), is(subsetCreator.getId()));
assertThat(result.getUuid(), is(subsetCreator.getUuid()));
}
assertThat(result, not(nullValue()));
assertThat(result.getId(), is(subsetCreator.getId()));
assertThat(result.getUuid(), is(subsetCreator.getUuid()));
}
@Test
public void testLoadSubsetCreators() {
// Check default data - should return 1 result
List<SubsetCreator> results = subsetService.loadSubsetCreators(subset);
@Test
public void testLoadSubsetCreators() {
// Check default data - should return 1 result
List<SubsetCreator> results = subsetService.loadSubsetCreators(subset);
assertThat(results.size(), is(1));
assertTrue(results.contains(subsetCreator));
assertThat(results.size(), is(1));
assertTrue(results.contains(subsetCreator));
// Add new subsetCreators with the same subset
Subset newSubset = createSubset(UUID.randomUUID());
int count = 3;
for (int i = 0; i < count; i++) {
subsetService.createSubsetCreator(newSubset, buildSubsetCreator(UUID.randomUUID()));
}
// Add new subsetCreators with the same subset
Subset newSubset = createSubset(UUID.randomUUID());
int count = 3;
for (int i = 0; i < count; i++) {
subsetService.createSubsetCreator(newSubset, buildSubsetCreator(UUID.randomUUID()));
}
List<SubsetCreator> newResults = subsetService.loadSubsetCreators(newSubset);
List<SubsetCreator> newResults = subsetService.loadSubsetCreators(newSubset);
assertThat(newResults.size(), is(count));
}
assertThat(newResults.size(), is(count));
}
@Test
public void testListSubsetCreators() {
@Test
public void testListSubsetCreators() {
final Page<SubsetCreator> page = subsetService.listSubsetCreators(SUBSET_UUID, new PageRequest(0, 10));
assertThat(page.getTotalElements(), is(1l));
assertThat(page.getTotalElements(), is(1l));
SubsetCreator result = page.getContent().get(0);
SubsetCreator result = page.getContent().get(0);
assertThat(result, is(subsetCreator));
assertThat(result.getSubset().getUuid(), is(SUBSET_UUID));
}
assertThat(result, is(subsetCreator));
assertThat(result.getSubset().getUuid(), is(SUBSET_UUID));
}
private SubsetCreator buildSubsetCreator(final UUID uuid) {
SubsetCreator subsetCreator = new SubsetCreator();
subsetCreator.setFullName(SUBSET_CREATOR_FULL_NAME);
subsetCreator.setEmail(SUBSET_CREATOR_EMAIL);
subsetCreator.setUuid(uuid);
private SubsetCreator buildSubsetCreator(final UUID uuid) {
SubsetCreator subsetCreator = new SubsetCreator();
subsetCreator.setFullName(SUBSET_CREATOR_FULL_NAME);
subsetCreator.setEmail(SUBSET_CREATOR_EMAIL);
subsetCreator.setUuid(uuid);
return subsetCreator;
}
return subsetCreator;
}
private Subset createSubset(final UUID uuid) {
subset = new Subset();
subset.setTitle("Subset");
subset.setWiewsCode("0000");
subset.setInstitute(faoInstitute);
subset.setOwner(partner);
subset.setUuid(uuid);
private Subset createSubset(final UUID uuid) {
subset = new Subset();
subset.setTitle("Subset");
subset.setWiewsCode("0000");
subset.setInstitute(faoInstitute);
subset.setOwner(partner);
subset.setUuid(uuid);
return subsetService.create(faoInstitute, subset);
}
return subsetService.create(faoInstitute, subset);
}
private FaoInstitute createInstitute() {
faoInstitute = new FaoInstitute();
faoInstitute.setCode("0000");
faoInstitute.setFullName("Institute");
instituteService.update(Lists.newArrayList(faoInstitute));
private FaoInstitute createInstitute() {
faoInstitute = new FaoInstitute();
faoInstitute.setCode("0000");
faoInstitute.setFullName("Institute");
instituteService.update(Lists.newArrayList(faoInstitute));
return instituteService.getInstitute("0000");
}
return instituteService.getInstitute("0000");
}
}
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