Commit 1f670cc7 authored by Matija Obreza's avatar Matija Obreza

Merge branch '519-return-uuid-on-accession-insert' into 'master'

Resolve "Return UUID on accession insert"

Closes #519

See merge request genesys-pgr/genesys-server!546
parents 1d7cf30a 02097acf
......@@ -93,8 +93,9 @@ dockerize genesys branch:
stage: dockerize
dependencies:
- package genesys branch
# when: manual
allow_failure: false
when: manual
# If we set allow_failure it will block the pipeline
# allow_failure: true
except:
- master
- tags
......@@ -194,6 +195,8 @@ deploy to production:
only:
- /^production-.*/
when: manual
# Don't terminate the pipeline!
allow_failure: false
variables:
GIT_STRATEGY: none
script:
......
......@@ -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;
......@@ -31,6 +32,9 @@ public class AccessionOpResponse implements Serializable {
NOOP, INSERT, UPDATE, DELETE, ERROR
}
public UpsertResult() {
}
/**
* @param action UPSERT or INSERT or DELETE
*/
......@@ -55,6 +59,9 @@ public class AccessionOpResponse implements Serializable {
return this;
}
}
public AccessionOpResponse() {
}
public AccessionOpResponse(Accession accession) {
this.instCode = accession.getHoldingInstitute();
......@@ -121,6 +128,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;
......@@ -225,10 +226,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());
// 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()));
......
......@@ -44,13 +44,16 @@ import org.genesys2.server.model.genesys.AccessionAlias.AliasType;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.impl.Crop;
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;
......@@ -1310,4 +1313,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*/
}
}
......@@ -40,6 +40,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;
......@@ -168,6 +169,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(1).getError(), nullValue());
assertThat(op.get(1).getResult().getAction(), is(UpsertResult.Type.INSERT));
assertThat(op.get(1).getResult().getUuid(), notNullValue());
Accession a = accessionRepository.findOne(institute, null, ACCENUMB_1, GENUS_1);
assertThat(accessionRepository.count(), is(3l));
......@@ -188,6 +191,8 @@ public class AccessionUploaderTest extends AbstractServicesTest {
List<AccessionOpResponse> op = accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_1, GENUS_1, null));
assertThat(op.get(1).getError(), nullValue());
assertThat(op.get(1).getResult().getAction(), is(UpsertResult.Type.UPDATE));
assertThat(op.get(1).getResult().getUuid(), is(a1.getUuid()));
Accession a2 = accessionRepository.findOne(institute, null, ACCENUMB_1, GENUS_1);
assertThat(a2, notNullValue());
......@@ -196,7 +201,18 @@ public class AccessionUploaderTest extends AbstractServicesTest {
assertThat(accessionRepository.count(), is(3l));
}
/**
* 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(1).getError(), is("Accession does not belong to institute " + institute.getCode()));
assertThat(op.get(1).getResult().getAction(), is(UpsertResult.Type.ERROR));
assertThat(op.get(1).getResult().getUuid(), nullValue());
}
/**
* Institute must match for updates.
*/
......@@ -231,6 +247,8 @@ public class AccessionUploaderTest extends AbstractServicesTest {
List<AccessionOpResponse> resp_1 = accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_1, GENUS_1, withWhitespaceDOI));
assertNotNull(resp_1.get(1));
assertNotNull(resp_1.get(1).getError());
assertNull(resp_1.get(1).getResult().getUuid());
assertThat(resp_1.get(1).getResult().getAction(), is(AccessionOpResponse.UpsertResult.Type.ERROR));
assertTrue(resp_1.get(1).getError().startsWith("Validation failed: Accession.doi"));
assertThat(accessionRepository.count(), is(2L));
Accession accession_1 = accessionRepository.findOne(institute, null, ACCENUMB_1, GENUS_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