Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/main/java/org/breedinginsight/daos/TraitDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -285,7 +286,7 @@ public List<Trait> createTraitsBrAPI(List<Trait> 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())
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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()) ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to you: from a reusability perspective, the Utilities.appendProgramKey method could be used to construct the method name

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Trait> traits = List.of(trait1);

// Call endpoint
Flowable<HttpResponse<String>> call = client.exchange(
POST("/programs/" + validProgram.getId() + "/traits", traits)
.contentType(MediaType.APPLICATION_JSON)
.cookie(new NettyCookie("phylo-token", "test-registered-user")), String.class
);
HttpResponse<String> 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<HttpResponse<String>> call2 = client.exchange(
PUT("/programs/" + validProgram.getId() + "/traits", traits)
.contentType(MediaType.APPLICATION_JSON)
.cookie(new NettyCookie("phylo-token", "test-registered-user")), String.class
);
HttpResponse<String> 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);
Expand Down