From 498da4fe6f9ce5506be981eb2b1846f59f6b3547 Mon Sep 17 00:00:00 2001 From: Chris T Date: Mon, 13 Jun 2022 14:20:31 -0400 Subject: [PATCH] trait create/update: account for null method description in method name --- .../org/breedinginsight/daos/TraitDAO.java | 11 ++- .../TraitControllerIntegrationTest.java | 76 +++++++++++++++++++ 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/breedinginsight/daos/TraitDAO.java b/src/main/java/org/breedinginsight/daos/TraitDAO.java index 443694d58..d320244f2 100644 --- a/src/main/java/org/breedinginsight/daos/TraitDAO.java +++ b/src/main/java/org/breedinginsight/daos/TraitDAO.java @@ -36,6 +36,7 @@ import org.breedinginsight.services.brapi.BrAPIProvider; import org.breedinginsight.utilities.BrAPIDAOUtil; import org.jooq.*; +import org.jooq.tools.StringUtils; import javax.inject.Inject; import javax.inject.Singleton; @@ -285,7 +286,7 @@ public List createTraitsBrAPI(List traits, User actingUser, Progra .referenceID(trait.getMethod().getId().toString()) .referenceSource(referenceSource); BrAPIMethod brApiMethod = new BrAPIMethod() - .methodName(String.format("%s %s [%s]", trait.getMethod().getDescription(), trait.getMethod().getMethodClass(), program.getKey())) + .methodName(constructMethodName(trait, program)) .externalReferences(List.of(methodReference)) .methodClass(trait.getMethod().getMethodClass()) .description(trait.getMethod().getDescription()) @@ -405,7 +406,7 @@ public Trait updateTraitBrAPI(Trait trait, Program program) { BrAPIObservationVariable existingVariable = getBrAPIVariable(variablesAPI, trait.getId()); // Change method - existingVariable.getMethod().setMethodName(String.format("%s %s [%s]", trait.getMethod().getDescription(), trait.getMethod().getMethodClass(), program.getKey())); + existingVariable.getMethod().setMethodName(constructMethodName(trait, program)); existingVariable.getMethod().setMethodClass(trait.getMethod().getMethodClass()); existingVariable.getMethod().setDescription(trait.getMethod().getDescription()); existingVariable.getMethod().setFormula(trait.getMethod().getFormula()); @@ -453,6 +454,12 @@ public Trait updateTraitBrAPI(Trait trait, Program program) { return updatedTrait; } + private String constructMethodName(Trait trait, Program program) { + return !StringUtils.isBlank(trait.getMethod().getDescription()) ? + String.format("%s %s [%s]", trait.getMethod().getDescription(), trait.getMethod().getMethodClass(), program.getKey()) : + String.format("%s [%s]", trait.getMethod().getMethodClass(), program.getKey()); + } + private BrAPIObservationVariable getBrAPIVariable(ObservationVariablesApi variablesAPI, UUID traitId) { BrAPIObservationVariable existingVariable; diff --git a/src/test/java/org/breedinginsight/api/v1/controller/TraitControllerIntegrationTest.java b/src/test/java/org/breedinginsight/api/v1/controller/TraitControllerIntegrationTest.java index 300a5eee1..899ee71e8 100644 --- a/src/test/java/org/breedinginsight/api/v1/controller/TraitControllerIntegrationTest.java +++ b/src/test/java/org/breedinginsight/api/v1/controller/TraitControllerIntegrationTest.java @@ -455,6 +455,82 @@ public void checkBrapiValues() { } + @Test + @SneakyThrows + @Order(4) + public void postNullMethodDescriptionNotInMethodName() { + + Trait trait1 = new Trait(); + trait1.setTraitDescription("trait 1 description"); + trait1.setEntity("entity1"); + trait1.setObservationVariableName("Test M Desc"); + trait1.setProgramObservationLevel(ProgramObservationLevel.builder().name("Plant").build()); + Scale scale1 = new Scale(); + scale1.setScaleName("Test Scale"); + scale1.setDataType(DataType.TEXT); + Method method1 = new Method(); + trait1.setScale(scale1); + trait1.setMethod(method1); + + // Set the brapi properties + setBrAPIProperties(trait1); + + // set the synonyms + trait1.setSynonyms(List.of("Test Trait", "test1", "test2")); + + // Set the tags + trait1.setTags(List.of("leaf trait")); + + // Set method description to null + method1.setDescription(null); + + List traits = List.of(trait1); + + // Call endpoint + Flowable> call = client.exchange( + POST("/programs/" + validProgram.getId() + "/traits", traits) + .contentType(MediaType.APPLICATION_JSON) + .cookie(new NettyCookie("phylo-token", "test-registered-user")), String.class + ); + HttpResponse res = call.blockingFirst(); + assertEquals(HttpStatus.OK, res.getStatus()); + + JsonObject result = JsonParser.parseString(res.getBody().get()).getAsJsonObject().getAsJsonObject("result"); + JsonArray data = result.getAsJsonArray("data"); + + for (JsonElement traitJson: data) { + JsonObject trait = (JsonObject) traitJson; + String traitName = trait.get("observationVariableName").getAsString(); + if (traitName.equals(trait1.getObservationVariableName())) { + trait1.setId(UUID.fromString(trait.get("id").getAsString())); + } + } + + String validProgramKey = validProgram.getKey(); + BrAPIObservationVariable variable = getBrapiVariable(trait1.getObservationVariableName(), "Test Program"); + + String methodName = String.format("%s", trait1.getMethod().getMethodClass()); + assertEquals(String.format("%s [%s]", methodName, validProgramKey), + variable.getMethod().getMethodName(), "Unexpected method name"); + + // update + trait1.getMethod().setMethodClass("Observation"); + + Flowable> call2 = client.exchange( + PUT("/programs/" + validProgram.getId() + "/traits", traits) + .contentType(MediaType.APPLICATION_JSON) + .cookie(new NettyCookie("phylo-token", "test-registered-user")), String.class + ); + HttpResponse response2 = call2.blockingFirst(); + assertEquals(HttpStatus.OK, response2.getStatus()); + + variable = getBrapiVariable(trait1.getObservationVariableName(), "Test Program"); + + methodName = String.format("%s", trait1.getMethod().getMethodClass()); + assertEquals(String.format("%s [%s]", methodName, validProgramKey), + variable.getMethod().getMethodName(), "Unexpected method name"); + } + private BrAPIObservationVariable getBrapiVariable(String variableName, String programName) throws ApiException { BrAPIClient client2 = new BrAPIClient(getProperties().get("brapi.server.pheno-url")); ObservationVariablesApi variablesApi = new ObservationVariablesApi(client2);