Skip to content

Commit

Permalink
fix: disable seedlot gen worth saving at the wrong place and time (#1433
Browse files Browse the repository at this point in the history
)
  • Loading branch information
RMCampos authored Jul 26, 2024
1 parent 65e2473 commit 82eda61
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import ca.bc.gov.backendstartapi.config.SparLog;
import ca.bc.gov.backendstartapi.dao.GeneticWorthEntityDao;
import ca.bc.gov.backendstartapi.dto.GeneticWorthTraitsDto;
import ca.bc.gov.backendstartapi.dto.ParentTreeGeneticQualityDto;
import ca.bc.gov.backendstartapi.dto.SeedlotFormParentTreeSmpDto;
import ca.bc.gov.backendstartapi.entity.GeneticWorthEntity;
import ca.bc.gov.backendstartapi.entity.SeedlotGeneticWorth;
import ca.bc.gov.backendstartapi.entity.idclass.SeedlotGeneticWorthId;
import ca.bc.gov.backendstartapi.entity.seedlot.Seedlot;
import ca.bc.gov.backendstartapi.exception.SeedlotConflictDataException;
import ca.bc.gov.backendstartapi.repository.SeedlotGeneticWorthRepository;
import ca.bc.gov.backendstartapi.security.LoggedUserService;
import java.util.ArrayList;
Expand All @@ -29,49 +26,6 @@ public class SeedlotGeneticWorthService {

private final GeneticWorthEntityDao geneticWorthEntityDao;

/**
* Saves a SeedlotParentTree from the Seedlot Form Registration step 5.
*
* @param seedlot The {@link Seedlot} related
* @param seedlotFormParentTreeDtoList A List of {@link SeedlotFormParentTreeSmpDto}
*/
public List<SeedlotGeneticWorth> saveSeedlotFormStep5(
Seedlot seedlot,
List<SeedlotFormParentTreeSmpDto> seedlotFormParentTreeDtoList,
boolean canDelete) {
SparLog.info("Saving SeedlotGeneticWorth for seedlot number {}", seedlot.getId());

List<SeedlotGeneticWorth> sgwList =
seedlotGeneticWorthRepository.findAllBySeedlot_id(seedlot.getId());

List<ParentTreeGeneticQualityDto> sgwInsertList = new ArrayList<>();

if (!sgwList.isEmpty()) {
SparLog.info(
"Deleting {} previous records on the SeedlotGeneticWorth table for seedlot number {}",
sgwList.size(),
seedlot.getId());

List<String> sgwExistingList =
new ArrayList<>(sgwList.stream().map(SeedlotGeneticWorth::getGeneticWorthCode).toList());

List<SeedlotGeneticWorthId> sgwiList = new ArrayList<>();
for (String genWorthCode : sgwExistingList) {
sgwiList.add(new SeedlotGeneticWorthId(seedlot.getId(), genWorthCode));
}

seedlotGeneticWorthRepository.deleteAllById(sgwiList);
} else if (!sgwList.isEmpty() && !canDelete) {
throw new SeedlotConflictDataException(seedlot.getId());
}

for (SeedlotFormParentTreeSmpDto seedlotPtFormDto : seedlotFormParentTreeDtoList) {
sgwInsertList.addAll(seedlotPtFormDto.parentTreeGeneticQualities());
}

return addSeedlotGeneticWorth(seedlot, sgwInsertList);
}

/**
* Override the seedlot genetic worth data.
*
Expand Down Expand Up @@ -140,35 +94,6 @@ private List<SeedlotGeneticWorth> saveSeedlotGenWorthPrivate(
return seedlotGeneticWorthRepository.saveAll(seedlotGenWorthList);
}

private List<SeedlotGeneticWorth> addSeedlotGeneticWorth(
Seedlot seedlot, List<ParentTreeGeneticQualityDto> genWorthCodeToInsert) {
if (genWorthCodeToInsert.isEmpty()) {
SparLog.info(
"No new records to be inserted on the SeedlotGeneticWorth table for seedlot number {}",
seedlot.getId());
return List.of();
}

SparLog.info(
"{} record(s) to be inserted on the SeedlotGeneticWorth table for seedlot number {}",
genWorthCodeToInsert.size(),
seedlot.getId());

List<SeedlotGeneticWorth> seedlotGeneticWorths = new ArrayList<>();
for (ParentTreeGeneticQualityDto ptgqDto : genWorthCodeToInsert) {

GeneticWorthEntity gwEntity =
geneticWorthEntityDao.getGeneticWorthEntity(ptgqDto.geneticWorthCode()).orElseThrow();

SeedlotGeneticWorth sgw =
new SeedlotGeneticWorth(seedlot, gwEntity, loggedUserService.createAuditCurrentUser());
sgw.setGeneticQualityValue(ptgqDto.geneticQualityValue());
seedlotGeneticWorths.add(sgw);
}

return seedlotGeneticWorthRepository.saveAll(seedlotGeneticWorths);
}

/**
* Gets all SeedlotGeneticWorth given a {@link Seedlot} id number.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ public SeedlotStatusResponseDto updateSeedlotWithForm(
setBecValues(seedlot, form.seedlotFormOrchardDto().primaryOrchardId(), inMemoryDto);

if (isFromRegularForm) {
// Update the Seedlot instance only
// Update the Seedlot instance and table seedlot_genetic_worth
// Calculate Ne value (effective population size)
// Calculate Mean GeoSpatial (for SMP Mix, mean latitude, mean longitude, mean elevation)
// Calculate Seedlot GeoSpatial (for Seedlot, mean latitude, mean longitude, mean elevation)
Expand Down Expand Up @@ -1126,8 +1126,6 @@ private void saveSeedlotFormStep5(
seedlotParentTreeService.saveSeedlotFormStep5(seedlot, seedlotFormParentTreeDtoList, canDelete);
seedlotParentTreeGeneticQualityService.saveSeedlotFormStep5(
seedlot, seedlotFormParentTreeDtoList);
seedlotGeneticWorthService.saveSeedlotFormStep5(
seedlot, seedlotFormParentTreeDtoList, canDelete);

// SMP Mix information is optional, so the array may be empty,
// in this case there is no need to save the list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,6 @@ void submitSeedlotForm_smpMixNotFound_shouldFail() {
when(seedlotParentTreeService.saveSeedlotFormStep5(any(), any(), anyBoolean()))
.thenReturn(List.of());
doNothing().when(seedlotParentTreeGeneticQualityService).saveSeedlotFormStep5(any(), any());
when(seedlotGeneticWorthService.saveSeedlotFormStep5(any(), any(), anyBoolean()))
.thenReturn(List.of());
when(smpMixService.saveSeedlotFormStep5(any(), any())).thenReturn(List.of());

doThrow(new SmpMixNotFoundException())
Expand Down Expand Up @@ -362,8 +360,6 @@ void submitSeedlotForm_facilityDescNotFound_shouldFail() {
when(seedlotParentTreeService.saveSeedlotFormStep5(any(), any(), anyBoolean()))
.thenReturn(List.of());
doNothing().when(seedlotParentTreeGeneticQualityService).saveSeedlotFormStep5(any(), any());
when(seedlotGeneticWorthService.saveSeedlotFormStep5(any(), any(), anyBoolean()))
.thenReturn(List.of());
when(smpMixService.saveSeedlotFormStep5(any(), any())).thenReturn(List.of());
doNothing().when(smpMixGeneticQualityService).saveSeedlotFormStep5(any(), any());
doNothing()
Expand Down Expand Up @@ -411,8 +407,6 @@ void submitSeedlotForm_happyPath_shouldSucceed() {
when(seedlotParentTreeService.saveSeedlotFormStep5(any(), any(), anyBoolean()))
.thenReturn(List.of());
doNothing().when(seedlotParentTreeGeneticQualityService).saveSeedlotFormStep5(any(), any());
when(seedlotGeneticWorthService.saveSeedlotFormStep5(any(), any(), anyBoolean()))
.thenReturn(List.of());
when(smpMixService.saveSeedlotFormStep5(any(), any())).thenReturn(List.of());
doNothing().when(smpMixGeneticQualityService).saveSeedlotFormStep5(any(), any());
doNothing()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

import ca.bc.gov.backendstartapi.dao.GeneticWorthEntityDao;
import ca.bc.gov.backendstartapi.dto.GeneticWorthTraitsDto;
import ca.bc.gov.backendstartapi.dto.ParentTreeGeneticQualityDto;
import ca.bc.gov.backendstartapi.dto.SeedlotFormParentTreeSmpDto;
import ca.bc.gov.backendstartapi.entity.GeneticWorthEntity;
import ca.bc.gov.backendstartapi.entity.SeedlotGeneticWorth;
import ca.bc.gov.backendstartapi.entity.embeddable.AuditInformation;
Expand Down Expand Up @@ -37,22 +35,6 @@ class SeedlotGeneticWorthServiceTest {

private SeedlotGeneticWorthService seedlotGeneticWorthService;

private SeedlotFormParentTreeSmpDto createFormDto(Integer parentTreeId) {
ParentTreeGeneticQualityDto parentTreeGenQualityDto =
new ParentTreeGeneticQualityDto("BV", "GVO", new BigDecimal("18"));
return new SeedlotFormParentTreeSmpDto(
"85",
parentTreeId,
"87",
new BigDecimal("1"),
new BigDecimal("5"),
6,
2,
50,
new BigDecimal("100"),
List.of(parentTreeGenQualityDto));
}

@BeforeEach
void setup() {
seedlotGeneticWorthService =
Expand All @@ -74,54 +56,6 @@ private SeedlotGeneticWorth mockSeedlotGenWorth(Seedlot seedlot, String traitCod
return new SeedlotGeneticWorth(seedlot, mockGeneticWorthEntity(traitCode), mockAudit());
}

@Test
@DisplayName("Save Seedlot Genetic Worth first submit")
void saveSeedlotFormStep5_firstSubmit_shouldSucceed() {
when(seedlotGeneticWorthRepository.findAllBySeedlot_id("54321")).thenReturn(List.of());

AuditInformation audit = new AuditInformation("userId");
when(loggedUserService.createAuditCurrentUser()).thenReturn(audit);

Seedlot seedlot = new Seedlot("54321");
GeneticWorthEntity gw = new GeneticWorthEntity();
gw.setGeneticWorthCode("GVO");

when(geneticWorthEntityDao.getGeneticWorthEntity("GVO")).thenReturn(Optional.of(gw));

SeedlotGeneticWorth sgw = new SeedlotGeneticWorth(seedlot, gw, audit);
when(seedlotGeneticWorthRepository.saveAll(any())).thenReturn(List.of(sgw));

SeedlotFormParentTreeSmpDto formStep5 = createFormDto(4023);

List<SeedlotGeneticWorth> list =
seedlotGeneticWorthService.saveSeedlotFormStep5(seedlot, List.of(formStep5), false);

Assertions.assertFalse(list.isEmpty());
Assertions.assertEquals(1, list.size());
}

@Test
@DisplayName("Save Seedlot Genetic Worth with one new method")
void saveSeedlotFormStep5_updateSeedlotAdd_shouldSucceed() {
Seedlot seedlot = new Seedlot("54321");
AuditInformation audit = new AuditInformation("userId");
GeneticWorthEntity gw = new GeneticWorthEntity();
gw.setGeneticWorthCode("GVO");

when(geneticWorthEntityDao.getGeneticWorthEntity("GVO")).thenReturn(Optional.of(gw));
SeedlotGeneticWorth sgw = new SeedlotGeneticWorth(seedlot, gw, audit);

when(seedlotGeneticWorthRepository.findAllBySeedlot_id("54321")).thenReturn(List.of(sgw));
when(loggedUserService.createAuditCurrentUser()).thenReturn(audit);
when(seedlotGeneticWorthRepository.saveAllAndFlush(any())).thenReturn(List.of(sgw));

List<SeedlotGeneticWorth> list =
seedlotGeneticWorthService.saveSeedlotFormStep5(
seedlot, List.of(createFormDto(4025)), false);

Assertions.assertTrue(list.isEmpty());
}

@Test
@DisplayName("Save seedlot genetic worth happy path should succeed")
void saveSeedlotGenWorth_happyPath_shouldSucceed() {
Expand Down Expand Up @@ -189,8 +123,7 @@ void saveSeedlotGenWorth_emptyExisting_shouldSucceed() {
wwdGenWorth.setGeneticQualityValue(new BigDecimal("12.1"));
wwdGenWorth.setTestedParentTreeContributionPercentage(new BigDecimal("96"));

when(seedlotGeneticWorthRepository.findAllBySeedlot_id(seedlot.getId()))
.thenReturn(List.of());
when(seedlotGeneticWorthRepository.findAllBySeedlot_id(seedlot.getId())).thenReturn(List.of());

when(geneticWorthEntityDao.getGeneticWorthEntity("GVO"))
.thenReturn(Optional.of(mockGeneticWorthEntity("GVO")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,8 +775,6 @@ void updateSeedlotWithForm_tscAdmin_happyPath_shouldSucceed() {
.thenReturn(List.of());
doNothing().when(seedlotParentTreeGeneticQualityService).saveSeedlotFormStep5(any(), any());

when(seedlotGeneticWorthService.saveSeedlotFormStep5(seedlot, List.of(parentTreeSmpDto), true))
.thenReturn(List.of());
when(smpMixService.saveSeedlotFormStep5(any(), any())).thenReturn(List.of());
doNothing().when(smpMixGeneticQualityService).saveSeedlotFormStep5(any(), any());
doNothing()
Expand Down

0 comments on commit 82eda61

Please sign in to comment.