-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug/bi 1852 #294
Merged
Merged
Bug/bi 1852 #294
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7a28a60
[BI-1852] check for duplicate breeding methods
davedrp 24e99f0
[BI-1852] respond with 400 errer with exception's text
davedrp 3212e8a
[BI-1852]fixed some existing 'Problems' in BreedingMethodController.java
davedrp 177eeb3
[BI-1852] fixed error message
davedrp cc4ddf6
[BI-1852] Improved error messages. fixed bug.
davedrp 45d54d7
[BI-1852]changed returned type 'HttpResponse' to 'HttpResponse<?>' to…
davedrp 070e182
[BI-1852] explisitly declaired 'protected' methods
davedrp ed84630
Merge branch 'develop' into bug/BI-1852
davedrp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package org.breedinginsight.services.impl; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.brapi.client.v2.model.exceptions.ApiException; | ||
import org.breedinginsight.brapi.v2.services.BrAPIGermplasmService; | ||
|
@@ -14,8 +13,9 @@ | |
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.lang.String.format; | ||
|
||
@Singleton | ||
@Slf4j | ||
public class BreedingMethodServiceImpl implements BreedingMethodService { | ||
|
||
private final BreedingMethodDAO breedingMethodDAO; | ||
|
@@ -60,28 +60,39 @@ public List<ProgramBreedingMethodEntity> fetchBreedingMethodsInUse(UUID programI | |
} | ||
|
||
@Override | ||
public ProgramBreedingMethodEntity createBreedingMethod(ProgramBreedingMethodEntity breedingMethod, UUID programId, UUID userId) throws BadRequestException, ApiException { | ||
if(!validateBreedingMethod(breedingMethod)) { | ||
throw new BadRequestException("Missing required data"); | ||
} | ||
public ProgramBreedingMethodEntity createBreedingMethod(ProgramBreedingMethodEntity breedingMethod, UUID programId, UUID userId) throws BadRequestException { | ||
validate(breedingMethod, programId); | ||
|
||
return dsl.transactionResult(() -> breedingMethodDAO.createProgramMethod(breedingMethod, programId, userId)); | ||
} | ||
|
||
|
||
@Override | ||
public ProgramBreedingMethodEntity updateBreedingMethod(ProgramBreedingMethodEntity breedingMethod, UUID programId, UUID userId) throws BadRequestException, ApiException { | ||
if(!validateBreedingMethod(breedingMethod)) { | ||
throw new BadRequestException("Missing required data"); | ||
} | ||
|
||
List<ProgramBreedingMethodEntity> inUseMethods = fetchBreedingMethodsInUse(programId); | ||
if(inUseMethods.stream().anyMatch(method -> method.getId().equals(breedingMethod.getId()))) { | ||
throw new BadRequestException("Breeding method is not allowed to be edited"); | ||
} | ||
validate(breedingMethod, programId); | ||
|
||
return dsl.transactionResult(() -> breedingMethodDAO.updateProgramMethod(breedingMethod, programId, userId)); | ||
} | ||
|
||
private void validate(ProgramBreedingMethodEntity breedingMethod, UUID programId) throws BadRequestException { | ||
if (isMissingRequiredFields(breedingMethod)) { | ||
throw new BadRequestException("Missing required data"); | ||
} | ||
|
||
List<ProgramBreedingMethodEntity> programAndSystemMethods = getBreedingMethods(programId); | ||
if( isDuplicateMethodNameFoundOnList(breedingMethod, programAndSystemMethods)){ | ||
throw new BadRequestException(format("A breeding method with the name '%s' is already defined in the system.", breedingMethod.getName())); | ||
} | ||
if( isDuplicateMethodAbbreviationFoundOnList(breedingMethod, programAndSystemMethods)){ | ||
throw new BadRequestException(format("A breeding method with the abbreviation '%s' is already defined in the system.", breedingMethod.getAbbreviation())); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public void enableSystemMethods(List<UUID> systemBreedingMethods, UUID programId, UUID userId) throws ApiException, BadRequestException { | ||
List<ProgramBreedingMethodEntity> inUseMethods = fetchBreedingMethodsInUse(programId); | ||
|
@@ -111,10 +122,70 @@ public void deleteBreedingMethod(UUID programId, UUID breedingMethodId) throws A | |
dsl.transaction(() -> breedingMethodDAO.deleteProgramMethod(programId, breedingMethodId)); | ||
} | ||
|
||
private boolean validateBreedingMethod(ProgramBreedingMethodEntity method) { | ||
return StringUtils.isNotBlank(method.getName()) | ||
&& StringUtils.isNotBlank(method.getAbbreviation()) | ||
&& StringUtils.isNotBlank(method.getCategory()) | ||
&& StringUtils.isNotBlank(method.getGeneticDiversity()); | ||
//'protected' instead of 'private' so it is accessible to unit test. | ||
protected boolean isMissingRequiredFields(ProgramBreedingMethodEntity method) { | ||
return StringUtils.isBlank(method.getName()) | ||
|| StringUtils.isBlank(method.getAbbreviation()) | ||
|| StringUtils.isBlank(method.getCategory()) | ||
|| StringUtils.isBlank(method.getGeneticDiversity()); | ||
} | ||
|
||
//'protected' instead of 'private' so it is accessible to unit test. | ||
protected boolean isDuplicateMethodNameFoundOnList(ProgramBreedingMethodEntity method, List<ProgramBreedingMethodEntity> programBreedingMethodEntityList) { | ||
boolean foundDup = false; | ||
for (ProgramBreedingMethodEntity testMethod: programBreedingMethodEntityList) { | ||
if(isDuplicateName(testMethod, method)){ | ||
foundDup = true; | ||
break; | ||
} | ||
} | ||
return foundDup; | ||
} | ||
|
||
//'protected' instead of 'private' so it is accessible to unit test. | ||
protected boolean isDuplicateMethodAbbreviationFoundOnList(ProgramBreedingMethodEntity method, List<ProgramBreedingMethodEntity> programBreedingMethodEntityList) { | ||
boolean foundDup = false; | ||
for (ProgramBreedingMethodEntity testMethod: programBreedingMethodEntityList) { | ||
if(isDuplicateAbbreviation(testMethod, method)){ | ||
foundDup = true; | ||
break; | ||
} | ||
} | ||
return foundDup; | ||
} | ||
|
||
//'protected' instead of 'private' so it is accessible to unit test. | ||
protected boolean isDuplicateName(ProgramBreedingMethodEntity testMethod, ProgramBreedingMethodEntity method) { | ||
// SPECIAL CASE: If the two methods are the same method, then they are not duplicates | ||
if( (testMethod.getId()!=null) && testMethod.getId().equals(method.getId()) ){ | ||
return false; | ||
} | ||
|
||
boolean isDup = false; | ||
if(testMethod.getName()!= null && testMethod.getName().equalsIgnoreCase(method.getName())){ | ||
isDup = true; | ||
} | ||
else if(testMethod.getName()==null && method.getName()==null ){ | ||
isDup = true; | ||
} | ||
return isDup; | ||
} | ||
|
||
//'protected' instead of 'private' so it is accessible to unit test. | ||
protected boolean isDuplicateAbbreviation(ProgramBreedingMethodEntity testMethod, ProgramBreedingMethodEntity method) { | ||
// SPECIAL CASE: If the two methods are the same method, then they are not duplicates | ||
if( (testMethod.getId()!=null) && testMethod.getId().equals(method.getId()) ){ | ||
return false; | ||
} | ||
|
||
boolean isDup = false; | ||
if(testMethod.getAbbreviation()!= null && testMethod.getAbbreviation().equalsIgnoreCase(method.getAbbreviation())){ | ||
isDup = true; | ||
} | ||
else if(testMethod.getAbbreviation()==null && method.getAbbreviation()==null ){ | ||
isDup = true; | ||
} | ||
Comment on lines
+185
to
+187
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this not needed because a validation error would have already been thrown for the abbreviation not being supplied? |
||
return isDup; | ||
} | ||
|
||
} |
156 changes: 156 additions & 0 deletions
156
src/test/java/org/breedinginsight/services/impl/BreedingMethodServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package org.breedinginsight.services.impl; | ||
|
||
import lombok.SneakyThrows; | ||
import org.breedinginsight.brapi.v2.services.BrAPIGermplasmService; | ||
import org.breedinginsight.dao.db.tables.pojos.ProgramBreedingMethodEntity; | ||
import org.breedinginsight.daos.BreedingMethodDAO; | ||
import org.jooq.DSLContext; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
public class BreedingMethodServiceImplTest { | ||
BreedingMethodServiceImpl breedingMethodService; | ||
BreedingMethodDAO breedingMethodDAO = null; | ||
BrAPIGermplasmService germplasmService = null; | ||
DSLContext dsl = null; | ||
|
||
@BeforeAll | ||
public void setup() { | ||
breedingMethodService = new BreedingMethodServiceImpl(breedingMethodDAO, germplasmService, dsl); | ||
} | ||
|
||
@Test | ||
public void hasRequiredFields(){ | ||
ProgramBreedingMethodEntity method= new ProgramBreedingMethodEntity(); | ||
assertTrue(breedingMethodService.isMissingRequiredFields(method),"the method has blanks"); | ||
|
||
method.setName("Dave"); | ||
method.setAbbreviation("DRP"); | ||
method.setCategory("human"); | ||
method.setGeneticDiversity("none"); | ||
assertFalse(breedingMethodService.isMissingRequiredFields(method),"the method has all required fields"); | ||
} | ||
|
||
@Test | ||
@SneakyThrows | ||
public void isDuplicateMethodNameFoundOnList() { | ||
List<ProgramBreedingMethodEntity> programBreedingMethodEntityList = new ArrayList<>(); | ||
programBreedingMethodEntityList.add(makeMethod("1", null)); | ||
programBreedingMethodEntityList.add(makeMethod("2", null)); | ||
|
||
boolean isDup = breedingMethodService.isDuplicateMethodNameFoundOnList(makeMethod("1", null), programBreedingMethodEntityList); | ||
assertTrue(isDup, "Duplicate name found in list."); | ||
} | ||
|
||
@Test | ||
@SneakyThrows | ||
public void isDuplicateMethodAbbreviationFoundOnList() { | ||
List<ProgramBreedingMethodEntity> programBreedingMethodEntityList = new ArrayList<>(); | ||
programBreedingMethodEntityList.add(makeMethod(null, "1")); | ||
programBreedingMethodEntityList.add(makeMethod(null, "2")); | ||
|
||
boolean isDup = breedingMethodService.isDuplicateMethodAbbreviationFoundOnList(makeMethod(null, "1"), programBreedingMethodEntityList); | ||
assertTrue(isDup, "Duplicate abbreviation found in list."); | ||
isDup = breedingMethodService.isDuplicateMethodAbbreviationFoundOnList(makeMethod(null, "unique"), programBreedingMethodEntityList); | ||
assertFalse(isDup, "Duplicate abbreviation NOT found in list."); | ||
} | ||
|
||
|
||
|
||
@Test | ||
@SneakyThrows | ||
public void isDuplicateName() { | ||
ProgramBreedingMethodEntity method = makeMethod("ABC", null); | ||
ProgramBreedingMethodEntity testMethod = makeMethod("ABC", null); | ||
ProgramBreedingMethodEntity testMethodLowerCase = makeMethod("abc", null); | ||
|
||
|
||
|
||
assertTrue( | ||
breedingMethodService.isDuplicateName(testMethod, method), | ||
"Method Names are Equal" | ||
); | ||
assertTrue( | ||
breedingMethodService.isDuplicateName(method, testMethod), | ||
"Method Names are Equal (switched order)" | ||
); | ||
assertTrue( | ||
breedingMethodService.isDuplicateName(method, testMethodLowerCase), | ||
"Method Names are Equal (one is LowerCase)" | ||
); | ||
testMethod.setId(method.getId()); | ||
assertFalse( | ||
breedingMethodService.isDuplicateName(testMethod, method), | ||
"The methods are the same method (not duplicate data)" | ||
); | ||
|
||
|
||
testMethod.setName("misatch Name"); | ||
testMethod.setId(UUID.randomUUID()); | ||
assertFalse( | ||
breedingMethodService.isDuplicateName(testMethod, method), | ||
"Method Names are not Equal" | ||
); | ||
testMethod.setName(null); | ||
assertFalse( | ||
breedingMethodService.isDuplicateName(testMethod, method), | ||
"Method Names are not Equal (one is null)" | ||
); | ||
|
||
testMethod.setName("name"); | ||
method.setName(null); | ||
assertFalse( | ||
breedingMethodService.isDuplicateName(testMethod, method), | ||
"Method Names are not Equal (the other is null)" | ||
); | ||
} | ||
|
||
@Test | ||
@SneakyThrows | ||
public void isDuplicateAbbreviation() { | ||
ProgramBreedingMethodEntity method = makeMethod("ABC", "ABC"); | ||
ProgramBreedingMethodEntity testMethod = makeMethod("mismatch_name", "ABC"); | ||
ProgramBreedingMethodEntity testMethodLowerCase = makeMethod("mismatch_name", "abc"); | ||
assertTrue( | ||
breedingMethodService.isDuplicateAbbreviation(testMethod, method), | ||
"Method Abbreviations are Equal" | ||
); | ||
assertTrue( | ||
breedingMethodService.isDuplicateAbbreviation(method, testMethod), | ||
"Method Abbreviations are Equal (switched order)" | ||
); | ||
assertTrue( | ||
breedingMethodService.isDuplicateAbbreviation(testMethodLowerCase, method), | ||
"Method Abbreviations are Equal (one is lower case)" | ||
); | ||
testMethod.setAbbreviation("misatch_abbreviation"); | ||
assertFalse( | ||
breedingMethodService.isDuplicateAbbreviation(method, testMethod), | ||
"Method Abbreviations are not equal." | ||
); | ||
testMethod.setAbbreviation(null); | ||
assertFalse( | ||
breedingMethodService.isDuplicateAbbreviation(method, testMethod), | ||
"Method Abbreviations are not equal (one is null)." | ||
); | ||
} | ||
|
||
// Helper Methods // | ||
private ProgramBreedingMethodEntity makeMethod(String nameSuffix, String abbrevSuffix){ | ||
ProgramBreedingMethodEntity method = new ProgramBreedingMethodEntity(); | ||
|
||
method.setName("Name" + ( nameSuffix != null? nameSuffix : "junk" )); | ||
method.setAbbreviation("Abbreviation" + ( abbrevSuffix != null? abbrevSuffix : "junk" )); | ||
method.setId(UUID.randomUUID()); | ||
return method; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this not needed because a validation error would have already been thrown for the name not being supplied?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left it as is to be extra-safe.