Commit 41017532 authored by Matija Obreza's avatar Matija Obreza

AccessionOpResponse does not contain uuid when accession is inserted

parent 8f2d9b43
......@@ -100,7 +100,7 @@ public class AccessionRepositoryCustomImpl implements AccessionRepositoryCustom,
List<? extends AccessionIdentifier3> sublist = forUpdate.subList(fromIndex, Math.min(forUpdate.size(), fromIndex + chunkSize));
List<Predicate> restrictions = new ArrayList<Predicate>();
Set<String> uniqueDois = sublist.stream().map(aid -> aid.getDoi()).filter(doi -> doi != null).distinct().collect(Collectors.toSet());
Set<String> uniqueDois = sublist.stream().filter(aid -> aid != null).map(aid -> aid.getDoi()).filter(doi -> doi != null).distinct().collect(Collectors.toSet());
Path<Object> theDoi = root.get("doi");
Path<Object> theInstCode = institute.get("code");
......@@ -114,6 +114,10 @@ public class AccessionRepositoryCustomImpl implements AccessionRepositoryCustom,
// A lot of .. (instCode=? and acceNumb=? and genus=?)
for (AccessionIdentifier3 ah : sublist) {
if (ah == null) {
// ignore incoming nulls
continue;
}
if (useGenus) {
restrictions.add(criteriaBuilder.and(criteriaBuilder.equal(theInstCode, ah.getHoldingInstitute()), criteriaBuilder.equal(theAcceNumb, ah.getAccessionNumber()),
criteriaBuilder.equal(theGenus, ah.getGenus())));
......
......@@ -4,6 +4,7 @@ import java.io.Serializable;
import java.util.UUID;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.service.worker.AccessionOpResponse.UpsertResult.Type;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;
......@@ -30,6 +31,9 @@ public class AccessionOpResponse implements Serializable {
NOOP, INSERT, UPDATE, DELETE, ERROR
}
public UpsertResult() {
}
/**
* @param action UPSERT or INSERT or DELETE
*/
......@@ -54,6 +58,9 @@ public class AccessionOpResponse implements Serializable {
return this;
}
}
public AccessionOpResponse() {
}
public AccessionOpResponse(Accession accession) {
this.instCode = accession.getHoldingInstitute();
......@@ -114,6 +121,17 @@ public class AccessionOpResponse implements Serializable {
public AccessionOpResponse setError(String error) {
this.error = error;
// update existing result
if (error != null && this.result != null) {
if (this.result.getAction() == Type.INSERT) {
// clear UUID as it is not persisted
this.result.setUUID(null);
}
// set result to error
this.result.setAction(UpsertResult.Type.ERROR);
}
return this;
}
}
......@@ -25,6 +25,7 @@ import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.validation.ConstraintViolation;
......@@ -91,7 +92,7 @@ public class AccessionUploader implements InitializingBean {
private static final Logger LOG = LoggerFactory.getLogger(AccessionUploader.class);
private ObjectMapper objectMapper;
@Autowired
private AccessionRepository accessionRepository;
......@@ -162,8 +163,15 @@ public class AccessionUploader implements InitializingBean {
if (accession.getInstituteCode() == null || (accession.getDoi() == null && accession.getAccessionNumber() == null)) {
throw new InvalidApiUsageException("instituteCode, accessionNumber OR doi missing in " + accn);
}
accessions.add(accession);
responses.add(new AccessionOpResponse(accession));
// Sanity check
if (!accession.getInstituteCode().equals(institute.getCode())) {
accessions.add(null); // need to match get(index) with responses and updates
responses.add(new AccessionOpResponse(accn)
.setResult(new UpsertResult(UpsertResult.Type.ERROR)).setError("Accession does not belong to institute " + institute.getCode()));
} else {
accessions.add(accession);
responses.add(new AccessionOpResponse(accession));
}
} catch (IOException e) {
LOG.error("Could not parse input: {}", e.getMessage(), e);
accessions.add(null); // need to match get(index) with responses and updates
......@@ -204,10 +212,12 @@ public class AccessionUploader implements InitializingBean {
if (accession == null) {
// LOG.trace("New accesson");
LOG.trace("New accession {} not in {}", update, existingAccessions);
response.setResult(new UpsertResult(UpsertResult.Type.INSERT));
accession = new Accession();
accession.setAccessionId(new AccessionId());
accession.setInstitute(faoInstituteRepository.findByCode(updateA.getInstituteCode()));
// Assign UUID on insert
accession.getAccessionId().setUuid(UUID.randomUUID());;
accession.setInstitute(institute);
response.setResult(new UpsertResult(UpsertResult.Type.INSERT).setUUID(accession.getUuid()));
} else {
LOG.trace("Updating accession {}", accession);
response.setResult(new UpsertResult(UpsertResult.Type.UPDATE).setUUID(accession.getUuid()));
......@@ -219,7 +229,7 @@ public class AccessionUploader implements InitializingBean {
if (accession.getDoi() != null && updateA.getDoi() == null) {
throw new InvalidApiUsageException("DOI not included for accession with registered DOI=" + accession.getDoi());
}
if (! accession.getInstitute().getId().equals(institute.getId())) {
throw new InvalidApiUsageException("Accession does not belong to institute " + institute.getCode());
}
......@@ -713,7 +723,7 @@ public class AccessionUploader implements InitializingBean {
throw new InvalidApiUsageException("instituteCode, accessionNumber OR doi missing in " + accn);
}
// Security check
// Sanity check
if (!accession.getInstituteCode().equals(institute.getCode())) {
throw new InvalidApiUsageException("Accession does not belong to institute " + institute.getCode());
}
......
......@@ -45,13 +45,16 @@ import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionAlias.AliasType;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.worker.AccessionOpResponse;
import org.genesys2.spring.CSVMessageConverter;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Sets;
......@@ -1002,4 +1005,29 @@ public class AccessionControllerTest extends AbstractAccessionControllerTest {
.andExpect(jsonPath("$.auditAccession").isMap());
/*@formatter:on*/
}
// AccessionOpResponse should contain UUID when accession is inserted
@Test
public void testUpsertReturnsUUID() throws Exception {
ObjectNode accessionJson = setUpAccession();
/*@formatter:off*/
String responseJson = upsert(accessionJson)
.andExpect(jsonPath("$[0].result", notNullValue()))
.andExpect(jsonPath("$[0].result.action", is("INSERT")))
.andExpect(jsonPath("$[0].result.uuid", notNullValue()))
.andReturn().getResponse().getContentAsString();
List<AccessionOpResponse> opResponse = objectMapper.readValue(responseJson, new TypeReference<List<AccessionOpResponse>>() {});
assertThat(accessionRepository.findOneByUuid(opResponse.get(0).getResult().getUuid()), notNullValue());
upsert(accessionJson)
.andExpect(jsonPath("$[0].result", notNullValue()))
.andExpect(jsonPath("$[0].result.action", is("UPDATE")))
.andExpect(jsonPath("$[0].result.uuid", is(opResponse.get(0).getResult().getUuid().toString())));
/*@formatter:on*/
}
}
......@@ -38,6 +38,7 @@ import org.genesys2.server.persistence.Taxonomy2Repository;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.worker.AccessionOpResponse;
import org.genesys2.server.service.worker.AccessionUploader;
import org.genesys2.server.service.worker.AccessionOpResponse.UpsertResult;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -139,6 +140,8 @@ public class AccessionUploaderTest extends AbstractServicesTest {
public void testBasicInsert() {
List<AccessionOpResponse> op = accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_1, GENUS_1, null));
assertThat(op.get(0).getError(), nullValue());
assertThat(op.get(0).getResult().getAction(), is(UpsertResult.Type.INSERT));
assertThat(op.get(0).getResult().getUuid(), notNullValue());
Accession a = accessionRepository.findOne(institute, null, ACCENUMB_1, GENUS_1);
assertThat(accessionRepository.count(), is(1l));
......@@ -159,6 +162,8 @@ public class AccessionUploaderTest extends AbstractServicesTest {
List<AccessionOpResponse> op = accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_1, GENUS_1, null));
assertThat(op.get(0).getError(), nullValue());
assertThat(op.get(0).getResult().getAction(), is(UpsertResult.Type.UPDATE));
assertThat(op.get(0).getResult().getUuid(), is(a1.getUuid()));
Accession a2 = accessionRepository.findOne(institute, null, ACCENUMB_1, GENUS_1);
assertThat(a2, notNullValue());
......@@ -167,7 +172,18 @@ public class AccessionUploaderTest extends AbstractServicesTest {
assertThat(accessionRepository.count(), is(1l));
}
/**
* Institute must match for updates.
*/
@Test
public void testErrorWrongInstCode() {
List<AccessionOpResponse> op = accessionUploader.upsertAccessions(institute, upsertAccessions(institute2.getCode(), ACCENUMB_1, GENUS_1, null));
assertThat(op.get(0).getError(), is("Accession does not belong to institute " + institute.getCode()));
assertThat(op.get(0).getResult().getAction(), is(UpsertResult.Type.ERROR));
assertThat(op.get(0).getResult().getUuid(), nullValue());
}
/**
* Institute must match for updates.
*/
......@@ -179,7 +195,7 @@ public class AccessionUploaderTest extends AbstractServicesTest {
List<AccessionOpResponse> op = accessionUploader.upsertAccessions(institute, upsertAccessions(institute2.getCode(), ACCENUMB_1, GENUS_1, null));
assertThat(op.get(0).getError(), is("Accession does not belong to institute " + institute.getCode()));
}
/**
* Inserting new accession with a DOI must work.
......@@ -202,7 +218,9 @@ public class AccessionUploaderTest extends AbstractServicesTest {
List<AccessionOpResponse> resp_1 = accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_1, GENUS_1, withWhitespaceDOI));
assertNotNull(resp_1.get(0));
assertNotNull(resp_1.get(0).getError());
assertTrue(resp_1.get(0).getError().startsWith("Validation failed: Accession.doi"));
assertNull(resp_1.get(0).getResult().getUuid());
assertThat(resp_1.get(0).getResult().getAction(), is(AccessionOpResponse.UpsertResult.Type.ERROR));
assertTrue(resp_1.get(0).getError().startsWith("Validation failed: Accession.doi"));
assertThat(accessionRepository.count(), is(0L));
Accession accession_1 = accessionRepository.findOne(institute, null, ACCENUMB_1, GENUS_1);
assertNull(accession_1);
......
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