Commit 8c2a5c9a authored by Viacheslav Pavlov's avatar Viacheslav Pavlov

AccessionUploader: delete operation

Now accessions with doi cannot be deleted
Added test for deleting two accessions (one with doi and one without doi)
parent 167dfd29
......@@ -44,13 +44,17 @@ import org.genesys2.server.model.genesys.AccessionAlias;
import org.genesys2.server.model.genesys.AccessionAlias.AliasType;
import org.genesys2.server.model.genesys.AccessionCollect;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.genesys.AccessionHistoric;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.AccessionRemark;
import org.genesys2.server.model.genesys.PDCI;
import org.genesys2.server.model.genesys.SelfCopy;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.json.Api1Constants;
import org.genesys2.server.persistence.AccessionHistoricRepository;
import org.genesys2.server.persistence.AccessionIdRepository;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.service.CropService;
......@@ -91,6 +95,12 @@ public class AccessionUploader implements InitializingBean {
@Autowired
private AccessionRepository accessionRepository;
@Autowired
private AccessionIdRepository accessionIdRepository;
@Autowired
private AccessionHistoricRepository accessionHistoricRepository;
@Autowired
private TaxonomyService taxonomyService;
......@@ -677,6 +687,7 @@ public class AccessionUploader implements InitializingBean {
// Require matching genus for deletion
List<Accession> existingAccessions = accessionRepository.find(true, identifiers);
List<Accession> toRemove = new ArrayList<>(identifiers.size());
List<AccessionHistoric> deleted = new ArrayList<>();
for (int i = 0; i < identifiers.size(); i++) {
final AccessionHeaderJson deletion = identifiers.get(i);
......@@ -685,24 +696,48 @@ public class AccessionUploader implements InitializingBean {
AccessionOpResponse response;
responses.add(response = new AccessionOpResponse(deletion.getHoldingInstitute(), deletion.getAccessionNumber(), deletion.getGenus()));
Accession accession = existingAccessions.stream().filter(existing -> {
return (existing.getDoi() != null && existing.getDoi().equals(deletion.getDoi())) || (existing.getInstituteCode().equals(deletion.getHoldingInstitute()) && existing
.getAccessionNumber().equals(deletion.getAccessionNumber()) && existing.getTaxonomy().getGenus().equals(deletion.getGenus()));
}).findFirst().orElse(null);
Accession accession = existingAccessions.stream()
.filter(existing -> (
existing.getInstituteCode().equals(deletion.getHoldingInstitute()) &&
existing.getAccessionNumber().equals(deletion.getAccessionNumber()) &&
existing.getTaxonomy().getGenus().equals(deletion.getGenus())
)).findFirst().orElse(null);
if (accession == null) {
response.setResult(new UpsertResult(UpsertResult.Type.NOOP));
} else {
LOG.trace("Deleting accession {}", accession);
toRemove.add(accession);
response.setResult(new UpsertResult(UpsertResult.Type.DELETE).setUUID(accession.getUuid()));
try {
if (deletion.doi != null || accession.getDoi() != null) {
throw new InvalidApiUsageException("Accessions with doi cannot be deleted");
}
LOG.trace("Deleting accession {}", accession);
toRemove.add(accession);
response.setResult(new UpsertResult(UpsertResult.Type.DELETE).setUUID(accession.getUuid()));
AccessionHistoric hist = new AccessionHistoric();
SelfCopy.copy(accession, hist);
hist.setAccessionId(accessionIdRepository.findOne(accession.getId()));
deleted.add(hist);
} catch (InvalidApiUsageException e) {
if (LOG.isDebugEnabled()) {
LOG.error(e.getMessage(), e);
} else {
LOG.error(e.getMessage());
}
response.setError(e.getMessage());
}
}
}
if (!toRemove.isEmpty()) {
accessionRepository.delete(toRemove);
LOG.trace("Deleted: {}", toRemove);
LOG.trace("Done deleting: {}, now adding", toRemove);
accessionHistoricRepository.save(deleted);
}
accessionCounter.recountInstitute(institute);
return responses;
......
......@@ -41,6 +41,7 @@ import org.genesys.test.base.AbstractServiceTest;
import org.genesys2.server.api.model.AccessionHeaderJson;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionHistoricRepository;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.service.InstituteService;
......@@ -124,6 +125,9 @@ public abstract class CatalogServiceTest extends AbstractServiceTest {
@Autowired
protected AccessionRepository accessionRepository;
@Autowired
protected AccessionHistoricRepository accessionHistoricRepository;
@Autowired
private RepositoryFolderRepository repositoryFolderRepository;
......@@ -144,6 +148,7 @@ public abstract class CatalogServiceTest extends AbstractServiceTest {
deleteFolders(repositoryFolderRepository.findAll());
partnerRepository.deleteAll();
accessionRepository.deleteAll();
accessionHistoricRepository.deleteAll();
instituteRepository.deleteAll();
super.cleanup();
......
......@@ -17,12 +17,21 @@ package org.genesys.test.server.services;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.genesys2.server.api.model.AccessionHeaderJson;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionHistoric;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.json.Api1Constants;
import org.genesys2.server.persistence.AccessionHistoricRepository;
import org.genesys2.server.persistence.AccessionIdRepository;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
......@@ -66,6 +75,8 @@ public class AccessionUploaderTest extends AbstractServicesTest {
@Autowired
private AccessionRepository accessionRepository;
@Autowired
private AccessionHistoricRepository accessionHistoricRepository;
@Autowired
private Taxonomy2Repository taxonomyRepository;
@Autowired
private AccessionIdRepository accessionIdRepository;
......@@ -99,6 +110,7 @@ public class AccessionUploaderTest extends AbstractServicesTest {
@Override
public void cleanup() throws Exception {
accessionRepository.deleteAll();
accessionHistoricRepository.deleteAll();
accessionIdRepository.deleteAll();
taxonomyRepository.deleteAll();
instituteRepository.deleteAllInBatch();
......@@ -307,6 +319,139 @@ public class AccessionUploaderTest extends AbstractServicesTest {
assertThat(a1a.getTaxonomy().getSpAuthor(), equalTo("M."));
}
@Test
public void testDeleteAccessions() {
accessionUploader.upsertAccessions(institute, upsertAccession(institute.getCode(), ACCENUMB_1, GENUS_1, null));
Accession a1 = accessionRepository.findByInstituteCodeAndAccessionNumber(institute.getCode(), ACCENUMB_1);
assertThat(a1.getAccessionNumber(), is(ACCENUMB_1));
assertNull(a1.getDoi());
AccessionHeaderJson aHS = new AccessionHeaderJson();
aHS.instCode = institute.getCode();
aHS.acceNumb = ACCENUMB_1;
aHS.genus = GENUS_1;
accessionUploader.deleteAccessions(institute, Collections.singletonList(aHS));
AccessionHistoric accessionHistoric = accessionHistoricRepository.findOneByUuid(a1.getUuid());
assertNotNull(accessionHistoric);
assertThat(accessionHistoric.getAccessionNumber(), is(a1.getAccessionNumber()));
Accession a2 = accessionRepository.findByInstituteCodeAndAccessionNumber(institute.getCode(), ACCENUMB_1);
assertNull(a2);
}
@Test
public void testDeleteAccessionsWithDoiShouldFail() {
accessionUploader.upsertAccessions(institute, upsertAccession(institute.getCode(), ACCENUMB_1, GENUS_1, DOI_1));
Accession a1 = accessionRepository.findByInstituteCodeAndAccessionNumber(institute.getCode(), ACCENUMB_1);
assertThat(a1.getAccessionNumber(), is(ACCENUMB_1));
assertThat(a1.getDoi(), is(DOI_1));
AccessionHeaderJson aHS = new AccessionHeaderJson();
aHS.instCode = institute.getCode();
aHS.acceNumb = ACCENUMB_1;
aHS.genus = GENUS_1;
List<AccessionOpResponse> responses = accessionUploader.deleteAccessions(institute, Collections.singletonList(aHS));
assertEquals("Accessions with doi cannot be deleted", responses.get(0).getError());
}
@Test
public void testDeleteAccessionsWithDoiAsInputShouldFail() {
accessionUploader.upsertAccessions(institute, upsertAccession(institute.getCode(), ACCENUMB_1, GENUS_1, DOI_1));
Accession a1 = accessionRepository.findByInstituteCodeAndAccessionNumber(institute.getCode(), ACCENUMB_1);
assertThat(a1.getAccessionNumber(), is(ACCENUMB_1));
assertThat(a1.getDoi(), is(DOI_1));
AccessionHeaderJson aHS = new AccessionHeaderJson();
aHS.doi = DOI_1;
aHS.instCode = institute.getCode();
aHS.acceNumb = ACCENUMB_1;
aHS.genus = GENUS_1;
List<AccessionOpResponse> responses = accessionUploader.deleteAccessions(institute, Collections.singletonList(aHS));
assertEquals("Accessions with doi cannot be deleted", responses.get(0).getError());
}
@Test(expected = org.genesys2.server.exception.InvalidApiUsageException.class)
public void testDeleteAccessionsWithIncorrectInstituteShouldFail() {
accessionUploader.upsertAccessions(institute, upsertAccession(institute.getCode(), ACCENUMB_1, GENUS_1, null));
Accession a1 = accessionRepository.findByInstituteCodeAndAccessionNumber(institute.getCode(), ACCENUMB_1);
assertThat(a1.getAccessionNumber(), is(ACCENUMB_1));
assertNull(a1.getDoi());
AccessionHeaderJson aHS = new AccessionHeaderJson();
aHS.instCode = institute.getCode();
aHS.acceNumb = ACCENUMB_1;
aHS.genus = GENUS_1;
accessionUploader.deleteAccessions(institute2, Collections.singletonList(aHS));
fail();
}
@Test
public void testDeleteAccessionsShouldNotDeleteAccessionsFromAnotherInstitute() {
accessionUploader.upsertAccessions(institute, upsertAccession(institute.getCode(), ACCENUMB_1, GENUS_1, null));
Accession a1 = accessionRepository.findByInstituteCodeAndAccessionNumber(institute.getCode(), ACCENUMB_1);
assertThat(a1.getAccessionNumber(), is(ACCENUMB_1));
assertNull(a1.getDoi());
AccessionHeaderJson aHS = new AccessionHeaderJson();
aHS.instCode = institute2.getCode();
aHS.acceNumb = ACCENUMB_1;
aHS.genus = GENUS_1;
accessionUploader.deleteAccessions(institute2, Collections.singletonList(aHS));
AccessionHistoric accessionHistoric = accessionHistoricRepository.findOneByUuid(a1.getUuid());
assertNull(accessionHistoric);
Accession a2 = accessionRepository.findByInstituteCodeAndAccessionNumber(institute.getCode(), ACCENUMB_1);
assertEquals(a2.getUuid(), a1.getUuid());
}
@Test
public void testDeleteAccessionsShouldOneWithoutDOI() {
accessionUploader.upsertAccessions(institute, upsertAccession(institute.getCode(), ACCENUMB_1, GENUS_1, null));
accessionUploader.upsertAccessions(institute, upsertAccession(institute.getCode(), ACCENUMB_2, GENUS_2, DOI_1));
Accession accWithoutDoi = accessionRepository.findByInstituteCodeAndAccessionNumber(institute.getCode(), ACCENUMB_1);
assertThat(accWithoutDoi.getAccessionNumber(), is(ACCENUMB_1));
assertNull(accWithoutDoi.getDoi());
Accession accWithDoi = accessionRepository.findByInstituteCodeAndAccessionNumber(institute.getCode(), ACCENUMB_2);
assertThat(accWithDoi.getAccessionNumber(), is(ACCENUMB_2));
assertThat(accWithDoi.getDoi(), is(DOI_1));
AccessionHeaderJson aHS1 = new AccessionHeaderJson();
aHS1.instCode = institute.getCode();
aHS1.acceNumb = ACCENUMB_1;
aHS1.genus = GENUS_1;
AccessionHeaderJson aHS2 = new AccessionHeaderJson();
aHS2.instCode = institute.getCode();
aHS2.acceNumb = ACCENUMB_2;
aHS2.genus = GENUS_2;
List<AccessionHeaderJson> headers = new ArrayList<>(2);
headers.add(aHS1);
headers.add(aHS2);
List<AccessionOpResponse> responses = accessionUploader.deleteAccessions(institute, headers);
assertEquals(responses.get(1).getError(), "Accessions with doi cannot be deleted");
AccessionHistoric accession1Historic = accessionHistoricRepository.findOneByUuid(accWithoutDoi.getUuid());
assertNotNull(accession1Historic);
assertEquals(accession1Historic.getAccessionName(), accWithoutDoi.getAccessionName());
Accession accWithDoiOut = accessionRepository.findByInstituteCodeAndAccessionNumber(institute.getCode(), ACCENUMB_2);
assertEquals(accWithDoiOut.getUuid(), accWithDoi.getUuid());
}
/**
* Report invalid field in JSON
*/
......
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