From 6a78e43cc960da790a0ca8bcad6de92646ba77ce Mon Sep 17 00:00:00 2001 From: Leonel Kovacic Date: Tue, 24 Aug 2021 18:02:33 -0300 Subject: [PATCH 1/2] IBP-4552: fixed missing environment parameters in brapi request --- .../api/brapi/StudyServiceBrapiImpl.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/java/org/generationcp/middleware/api/brapi/StudyServiceBrapiImpl.java b/src/main/java/org/generationcp/middleware/api/brapi/StudyServiceBrapiImpl.java index 24b9df3e40..049338cc2b 100644 --- a/src/main/java/org/generationcp/middleware/api/brapi/StudyServiceBrapiImpl.java +++ b/src/main/java/org/generationcp/middleware/api/brapi/StudyServiceBrapiImpl.java @@ -206,6 +206,8 @@ public List getStudyInstancesWithMetadata(final StudySearchFil environmentParameterVariables.addAll(this.createGeolocationVariables(environmentVariables, studyDbId)); + this.addEnvironmentParameterVariablesWithoutValues(environmentParameterVariables, environmentVariables); + final List environmentParameters = environmentParameterVariables.stream() .map(EnvironmentParameter::new).collect(Collectors.toList()); studyInstanceDto.setEnvironmentParameters(environmentParameters); @@ -599,4 +601,29 @@ private List getUsersForEnvironment(final Integer instanceId) { return Collections.emptyList(); } + private void addEnvironmentParameterVariablesWithoutValues(final List environmentParameterVariables, + final List environmentVariables) { + // Filter standard environment factors + final List filterVariables = Lists.newArrayList( + TermId.LOCATION_ID.getId(), + TermId.TRIAL_INSTANCE_FACTOR.getId(), + TermId.EXPERIMENT_DESIGN_FACTOR.getId()); + // filter the already added variables + filterVariables.addAll(environmentParameterVariables.stream().map(MeasurementVariable::getTermId).collect(Collectors.toList())); + + final List variables = environmentVariables + .stream() + .filter(measurementVariable -> !filterVariables.contains(measurementVariable.getTermId())) + .map(measurementVariable -> { + if (measurementVariable.getValue() == null) { + measurementVariable.setValue(""); + } + return measurementVariable; + }) + .collect(Collectors.toList()); + if (!CollectionUtils.isEmpty(variables)) { + environmentParameterVariables.addAll(variables); + } + } + } From 4a7e71fa83961650d1db1adfe3eac9734d5840dc Mon Sep 17 00:00:00 2001 From: Leonel Kovacic Date: Wed, 25 Aug 2021 14:39:46 -0300 Subject: [PATCH 2/2] IBP-4552: fixed unit test --- .../middleware/api/brapi/StudyServiceBrapiImpl.java | 2 +- .../middleware/brapi/StudyServiceBrapiImplTest.java | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/generationcp/middleware/api/brapi/StudyServiceBrapiImpl.java b/src/main/java/org/generationcp/middleware/api/brapi/StudyServiceBrapiImpl.java index 049338cc2b..58cc106965 100644 --- a/src/main/java/org/generationcp/middleware/api/brapi/StudyServiceBrapiImpl.java +++ b/src/main/java/org/generationcp/middleware/api/brapi/StudyServiceBrapiImpl.java @@ -608,7 +608,7 @@ private void addEnvironmentParameterVariablesWithoutValues(final List variables = environmentVariables diff --git a/src/test/java/org/generationcp/middleware/brapi/StudyServiceBrapiImplTest.java b/src/test/java/org/generationcp/middleware/brapi/StudyServiceBrapiImplTest.java index 9ad2231e02..5e795484fa 100644 --- a/src/test/java/org/generationcp/middleware/brapi/StudyServiceBrapiImplTest.java +++ b/src/test/java/org/generationcp/middleware/brapi/StudyServiceBrapiImplTest.java @@ -27,11 +27,13 @@ import org.generationcp.middleware.service.api.study.EnvironmentParameter; import org.generationcp.middleware.service.api.study.StudyDetailsDto; import org.generationcp.middleware.service.api.study.StudyInstanceDto; +import org.generationcp.middleware.service.api.study.StudySearchFilter; import org.generationcp.middleware.utils.test.IntegrationTestDataInitializer; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.PageRequest; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; @@ -182,7 +184,11 @@ public void testSaveStudyInstances_WithValidVariableHavingInvalidVariableValue() .saveStudyInstances(this.cropType.getCropName(), Collections.singletonList(dto), this.testUser.getUserid()).get(0); Assert.assertEquals(dto.getTrialDbId(), savedInstance.getTrialDbId()); Assert.assertEquals(dto.getLocationDbId(), savedInstance.getLocationDbId()); - Assert.assertTrue(CollectionUtils.isEmpty(savedInstance.getEnvironmentParameters())); + + assertThat(savedInstance.getEnvironmentParameters(), hasSize(1)); + final EnvironmentParameter environmentParameter = savedInstance.getEnvironmentParameters().get(0); + assertThat(environmentParameter.getParameterPUI(), is(numericVariable.getCvTermId().toString())); + assertThat(environmentParameter.getValue(), is("")); } @Test