Commit 0d687926 authored by Matija Obreza's avatar Matija Obreza
Browse files

Viability: ensure counterCooperator is returned

parent b6819891
......@@ -35,6 +35,7 @@ import javax.persistence.UniqueConstraint;
import javax.validation.constraints.Min;
import org.genesys.blocks.model.Copyable;
import org.hibernate.Hibernate;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
......@@ -386,8 +387,9 @@ public class InventoryViabilityData extends CooperatorOwnedModel implements Copy
@Override
public void lazyLoad() {
Hibernate.initialize(this.inventoryViability);
if (this.counterCooperator != null) {
this.counterCooperator.getId();
Hibernate.initialize(this.counterCooperator);
}
if (dataEnvironmentMaps != null) {
dataEnvironmentMaps.size();
......
......@@ -43,6 +43,7 @@ import org.gringlobal.model.QInventoryViabilityAction;
import org.gringlobal.model.QInventoryViabilityData;
import org.gringlobal.model.Site;
import org.gringlobal.model.community.CommunityCodeValues;
import org.gringlobal.persistence.CooperatorRepository;
import org.gringlobal.persistence.InventoryRepository;
import org.gringlobal.persistence.InventoryViabilityActionRepository;
import org.gringlobal.persistence.InventoryViabilityDataRepository;
......@@ -225,24 +226,23 @@ public class InventoryViabilityServiceImpl extends FilteredCRUDServiceImpl<Inven
@Transactional
@PostAuthorize("@ggceSec.actionAllowed('ViabilityTest', 'CREATE', returnObject.inventory.site)")
public InventoryViability create(InventoryViability source) {
InventoryViability inventoryViability = new InventoryViability();
inventoryViability.apply(source);
inventoryViability.setInventory(inventoryRepository.getOne(inventoryViability.getInventory().getId()));
assert(source.getId() == null);
source.setInventory(inventoryRepository.getOne(source.getInventory().getId()));
var rule = source.getInventoryViabilityRule();
if (rule != null) {
int numberOfReplicates = Optional.ofNullable(rule.getNumberOfReplicates()).orElse(1);
Integer testQuantity = rule.getSeedsPerReplicate() == null ? null : (int) rule.getSeedsPerReplicate() * numberOfReplicates;
inventoryViability.setReplicationCount(numberOfReplicates);
inventoryViability.setTotalTestedCount(testQuantity);
source.setReplicationCount(numberOfReplicates);
source.setTotalTestedCount(testQuantity);
}
if (inventoryViability.getTestedDate() == null) {
inventoryViability.setTestedDate(new Date());
if (source.getTestedDate() == null) {
source.setTestedDate(new Date());
}
inventoryViability.setTestedDateCode(CommunityCodeValues.DATE_FORMAT_DATE.value);
source.setTestedDateCode(CommunityCodeValues.DATE_FORMAT_DATE.value);
var saved = repository.save(inventoryViability);
var saved = repository.save(source);
saved.lazyLoad();
return saved;
......@@ -351,15 +351,17 @@ public class InventoryViabilityServiceImpl extends FilteredCRUDServiceImpl<Inven
@Autowired
private InventoryViabilityRepository inventoryViabilityRepository;
@Autowired
private CooperatorRepository cooperatorRepository;
@Override
@Transactional
@PostAuthorize("@ggceSec.actionAllowed('ViabilityTest', 'WRITE', returnObject.inventoryViability.inventory.site)")
public InventoryViabilityData create(InventoryViabilityData source) {
var created = new InventoryViabilityData();
created.apply(source);
assert(source.getId() == null);
// reload from database
created.setInventoryViability(inventoryViabilityRepository.getOne(created.getInventoryViability().getId()));
var saved = repository.save(created);
source.setInventoryViability(inventoryViabilityRepository.getOne(source.getInventoryViability().getId()));
var saved = repository.save(source);
// Validate sum counts
validateSumOfCounts(saved);
......@@ -377,6 +379,9 @@ public class InventoryViabilityServiceImpl extends FilteredCRUDServiceImpl<Inven
LOG.debug("Update InventoryViabilityData. Input data {}", input);
target.apply(input);
if (target.getCounterCooperator() != null) {
target.setCounterCooperator(cooperatorRepository.getOne(target.getCounterCooperator().getId()));
}
var saved = repository.save(target);
......
......@@ -21,12 +21,14 @@ import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import org.gringlobal.api.v1.impl.InventoryController;
import org.gringlobal.api.v1.impl.InventoryViabilityController;
import org.gringlobal.api.v1.impl.InventoryViabilityController.InventoryViabilityDataController;
import org.gringlobal.model.Cooperator;
import org.gringlobal.model.Inventory;
import org.gringlobal.model.InventoryViability;
......@@ -48,6 +50,8 @@ import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* @author Maxym Borodenko
......@@ -502,6 +506,85 @@ public class InventoryViabilityControllerTest extends AbstractApiV1Test {
/*@formatter:on*/
}
@Test
public void testCreateViabilityData() throws Exception {
var inventory = addInventoryToDB(addAccessionToDB());
var viability = new InventoryViability();
viability.setInventory(new Inventory(inventory.getId()));
viability.setTestedDate(new Date());
viability.setNote("Test");
viability = viabilityRepository.save(viability);
assertThat(viability.getId(), not(nullValue()));
var viabilityData = new InventoryViabilityData();
viabilityData.setInventoryViability(new InventoryViability(viability.getId()));
viabilityData.setReplicationNumber(1);
viabilityData.setCountNumber(1);
viabilityData.setCountDate(new Date());
viabilityData.setCounterCooperator(new Cooperator(cooperator.getId()));
viabilityData.setReplicationCount(20);
viabilityData.setNormalCount(10);
viabilityData.setAbnormalCount(1);
/*@formatter:off*/
var responseJson = mockMvc
.perform(post(InventoryViabilityDataController.API_URL)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.characterEncoding("UTF8")
.content(clientMapper.writeValueAsString(viabilityData).replaceFirst("\\\"inventoryViability\\\":(\\d+),", "\"inventoryViability\":{\"id\":$1},"))
)
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", notNullValue()))
.andExpect(jsonPath("$.id", is(greaterThan(0))))
.andExpect(jsonPath("$.inventoryViability", is(viability.getId().intValue())))
.andExpect(jsonPath("$.countDate").isString())
.andExpect(jsonPath("$.countNumber", is(1)))
.andExpect(jsonPath("$.replicationNumber", is(1)))
.andExpect(jsonPath("$.counterCooperator").isMap())
.andExpect(jsonPath("$.counterCooperator.id", is(cooperator.getId().intValue())))
// .andExpect(jsonPath("$.counterCooperator.firstName").isString())
.andExpect(jsonPath("$.replicationCount", is(20)))
.andExpect(jsonPath("$.normalCount", is(10)))
.andExpect(jsonPath("$.abnormalCount", is(1)))
.andReturn().getResponse().getContentAsString(Charset.forName("UTF8"))
;
/*@formatter:on*/
var response = (ObjectNode) clientMapper.readTree(responseJson);
assertThat(response.get("id"), notNullValue());
viabilityData.setId(response.get("id").asLong());
viabilityData.setNormalCount(9);
/*@formatter:off*/
mockMvc
.perform(put(InventoryViabilityDataController.API_URL)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.characterEncoding("UTF8")
.content(clientMapper.writeValueAsString(viabilityData).replaceFirst("\\\"inventoryViability\\\":(\\d+),", "\"inventoryViability\":{\"id\":$1},"))
)
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", notNullValue()))
.andExpect(jsonPath("$.id", is(greaterThan(0))))
.andExpect(jsonPath("$.inventoryViability", is(viability.getId().intValue())))
.andExpect(jsonPath("$.countDate").isString())
.andExpect(jsonPath("$.countNumber", is(1)))
.andExpect(jsonPath("$.replicationNumber", is(1)))
.andExpect(jsonPath("$.counterCooperator").isMap())
.andExpect(jsonPath("$.counterCooperator.id", is(cooperator.getId().intValue())))
// .andExpect(jsonPath("$.counterCooperator.firstName").isString())
.andExpect(jsonPath("$.replicationCount", is(20)))
.andExpect(jsonPath("$.normalCount", is(9)))
.andExpect(jsonPath("$.abnormalCount", is(1)))
;
/*@formatter:on*/
}
private InventoryViability buildViability() {
return buildViability(addInventoryToDB(addAccessionToDB()));
}
......
......@@ -189,6 +189,7 @@ public class InventoryViabilitySecurityTest extends AbstractSecurityTest {
replicate1.setNormalCount(20);
inventoryViabilityDataService.create(replicate1);
replicate1.setId(null);
replicate1.setCountNumber(2);
replicate1.setCountDate(new Date());
replicate1.setAbnormalCount(10);
......
......@@ -223,6 +223,7 @@ public class InventoryViabilityServiceTest extends AbstractServicesTest {
assertThat(inventoryViabilityData1.getCountNumber(), is(1));
assertThat(inventoryViabilityData1.getCounterCooperator(), not(nullValue()));
inventoryViabilityData1.setId(null);
// Test database constraint violation
assertThrows(DataIntegrityViolationException.class, () -> inventoryViabilityDataService.create(data1));
......@@ -272,10 +273,13 @@ public class InventoryViabilityServiceTest extends AbstractServicesTest {
viabilityData.setReplicationCount(6); // count is 6, sum must be less than 6
assertThrows(InvalidApiUsageException.class, () -> inventoryViabilityDataService.create(viabilityData));
assertThat(inventoryViabilityDataRepository.count(), is(0l));
// set valid
viabilityData.setId(null);
viabilityData.setReplicationCount(10); // 1+2+3+4=10
var inventoryViabilityData1 = inventoryViabilityDataService.create(viabilityData);
assertThat(inventoryViabilityDataRepository.count(), is(1l));
assertThat(inventoryViabilityData1.getInventoryViability().getId(), is(inventoryViability.getId()));
assertThat(inventoryViabilityData1.getReplicationCount(), is(10));
assertThat(inventoryViabilityData1.getCountNumber(), is(1));
......@@ -320,6 +324,7 @@ public class InventoryViabilityServiceTest extends AbstractServicesTest {
assertThat(viabilityDataRep1.sumOfCounts(), is(55));
inventoryViabilityDataService.create(viabilityDataRep1);
// Replication 1, count 2
viabilityDataRep1.setId(null);
viabilityDataRep1.setCountNumber(2);
viabilityDataRep1.setCountDate(new Date());
viabilityDataRep1.setUnknownCount(5);
......@@ -342,6 +347,7 @@ public class InventoryViabilityServiceTest extends AbstractServicesTest {
assertThat(viabilityDataRep2.sumOfCounts(), is(57));
inventoryViabilityDataService.create(viabilityDataRep2);
// Replication 2, count 2
viabilityDataRep2.setId(null);
viabilityDataRep2.setCountNumber(2);
viabilityDataRep2.setCountDate(new Date());
viabilityDataRep2.setUnknownCount(3);
......
Supports Markdown
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