From e54ce7a34fe5ae1425336aa461d70eee9386d90a Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Tue, 6 Oct 2020 13:50:36 -0400 Subject: Don't always validate restServerParameters Restored original validation code to DmaapSimParameterGroup so it works as it originally worked (i.e., required "restServerParameters"). However, modified SimulatorParameters to validate the relevant fields directly, rather than relying on DmaapSimParameterGroup.validate(), thus eliminating all checks on "restServerParameters" when used within SimulatorParameters. Issue-ID: POLICY-2858 Change-Id: I9a09431ce3056a874bf2dba1c5bd6c418f37f69b Signed-off-by: Jim Hahn --- .../dmaap/parameters/DmaapSimParameterGroup.java | 2 + .../models/simulators/SimulatorParameters.java | 14 ++++++- .../onap/policy/models/simulators/MainTest.java | 2 +- .../models/simulators/SimulatorParametersTest.java | 46 +++++++++++++++++----- .../src/test/resources/emptyRestServer.json | 11 ++++++ .../src/test/resources/invalidDmaapName.json | 10 +++++ .../src/test/resources/invalidDmaapParameters.json | 7 ---- .../src/test/resources/invalidDmaapProvider.json | 7 ++++ .../src/test/resources/invalidGrpcParameters.json | 7 ---- .../src/test/resources/invalidGrpcServer.json | 7 ++++ .../src/test/resources/invalidTopicSweep.json | 10 +++++ 11 files changed, 98 insertions(+), 25 deletions(-) create mode 100644 models-sim/policy-models-simulators/src/test/resources/emptyRestServer.json create mode 100644 models-sim/policy-models-simulators/src/test/resources/invalidDmaapName.json delete mode 100644 models-sim/policy-models-simulators/src/test/resources/invalidDmaapParameters.json create mode 100644 models-sim/policy-models-simulators/src/test/resources/invalidDmaapProvider.json delete mode 100644 models-sim/policy-models-simulators/src/test/resources/invalidGrpcParameters.json create mode 100644 models-sim/policy-models-simulators/src/test/resources/invalidGrpcServer.json create mode 100644 models-sim/policy-models-simulators/src/test/resources/invalidTopicSweep.json diff --git a/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/parameters/DmaapSimParameterGroup.java b/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/parameters/DmaapSimParameterGroup.java index 4e2841a08..9719ae93f 100644 --- a/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/parameters/DmaapSimParameterGroup.java +++ b/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/parameters/DmaapSimParameterGroup.java @@ -25,10 +25,12 @@ import lombok.Getter; import org.onap.policy.common.parameters.ParameterGroupImpl; import org.onap.policy.common.parameters.annotations.Min; import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; /** * Class to hold all parameters needed for the DMaaP simulator component. */ +@NotNull @NotBlank @Getter public class DmaapSimParameterGroup extends ParameterGroupImpl { diff --git a/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/SimulatorParameters.java b/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/SimulatorParameters.java index c22527165..4b60d5e45 100644 --- a/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/SimulatorParameters.java +++ b/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/SimulatorParameters.java @@ -27,6 +27,8 @@ import lombok.Getter; import org.onap.policy.common.endpoints.parameters.TopicParameters; import org.onap.policy.common.parameters.BeanValidationResult; import org.onap.policy.common.parameters.BeanValidator; +import org.onap.policy.common.parameters.ObjectValidationResult; +import org.onap.policy.common.parameters.ValidationStatus; import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup; /** @@ -76,7 +78,17 @@ public class SimulatorParameters { BeanValidationResult result = new BeanValidator().validateTop(containerName, this); if (dmaapProvider != null) { - result.addResult(dmaapProvider.validate()); + // do not want full validation of the provider, so validate the relevant + // fields ourselves + BeanValidationResult subResult = new BeanValidationResult("dmaapProvider", dmaapProvider); + subResult.validateNotNull("name", dmaapProvider.getName()); + if (dmaapProvider.getTopicSweepSec() < 1) { + ObjectValidationResult fieldResult = + new ObjectValidationResult("topicSweepSec", dmaapProvider.getTopicSweepSec(), + ValidationStatus.INVALID, "is below the minimum value: 1"); + subResult.addResult(fieldResult); + } + result.addResult(subResult); } if (grpcServer != null) { diff --git a/models-sim/policy-models-simulators/src/test/java/org/onap/policy/models/simulators/MainTest.java b/models-sim/policy-models-simulators/src/test/java/org/onap/policy/models/simulators/MainTest.java index abc27f428..f5598e2e1 100644 --- a/models-sim/policy-models-simulators/src/test/java/org/onap/policy/models/simulators/MainTest.java +++ b/models-sim/policy-models-simulators/src/test/java/org/onap/policy/models/simulators/MainTest.java @@ -109,7 +109,7 @@ public class MainTest { @Test public void testConstructor() throws Exception { - assertThatIllegalArgumentException().isThrownBy(() -> new Main("invalidDmaapParameters.json")) + assertThatIllegalArgumentException().isThrownBy(() -> new Main("invalidDmaapProvider.json")) .withMessage("invalid simulator parameters"); } diff --git a/models-sim/policy-models-simulators/src/test/java/org/onap/policy/models/simulators/SimulatorParametersTest.java b/models-sim/policy-models-simulators/src/test/java/org/onap/policy/models/simulators/SimulatorParametersTest.java index f8d7433d2..8094ca419 100644 --- a/models-sim/policy-models-simulators/src/test/java/org/onap/policy/models/simulators/SimulatorParametersTest.java +++ b/models-sim/policy-models-simulators/src/test/java/org/onap/policy/models/simulators/SimulatorParametersTest.java @@ -20,6 +20,7 @@ package org.onap.policy.models.simulators; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -34,23 +35,50 @@ public class SimulatorParametersTest { @Test public void testValidate() throws CoderException { - - // everything populated SimulatorParameters params = new StandardCoder().decode(new File("src/test/resources/simParameters.json"), SimulatorParameters.class); assertNull(params.validate("ValidParams").getResult()); + } - // invalid dmaap provider - params = new StandardCoder().decode(new File("src/test/resources/invalidDmaapParameters.json"), - SimulatorParameters.class); + @Test + public void testValidateEmptyRestServer() throws CoderException { + SimulatorParameters params = new StandardCoder() + .decode(new File("src/test/resources/emptyRestServer.json"), SimulatorParameters.class); + assertNull(params.validate("ValidParams").getResult()); + } + + @Test + public void testValidateInvalidDmaapProvider() throws CoderException { + SimulatorParameters params = new StandardCoder() + .decode(new File("src/test/resources/invalidDmaapProvider.json"), SimulatorParameters.class); BeanValidationResult result = params.validate("InvalidDmaapParams"); assertFalse(result.isValid()); assertNotNull(result.getResult()); + } - // invalid grpc server - params = new StandardCoder().decode(new File("src/test/resources/invalidGrpcParameters.json"), - SimulatorParameters.class); - result = params.validate("InvalidGrpcParams"); + @Test + public void testValidateInvalidDmaapName() throws CoderException { + SimulatorParameters params = new StandardCoder().decode( + new File("src/test/resources/invalidDmaapName.json"), SimulatorParameters.class); + BeanValidationResult result = params.validate("InvalidDmaapParams"); + assertFalse(result.isValid()); + assertThat(result.getResult()).contains("item \"name\" value \"null\""); + } + + @Test + public void testValidateInvalidTopicSweep() throws CoderException { + SimulatorParameters params = new StandardCoder().decode( + new File("src/test/resources/invalidTopicSweep.json"), SimulatorParameters.class); + BeanValidationResult result = params.validate("InvalidDmaapParams"); + assertFalse(result.isValid()); + assertThat(result.getResult()).contains("topicSweepSec"); + } + + @Test + public void testValidateInvalidGrpcServer() throws CoderException { + SimulatorParameters params = new StandardCoder() + .decode(new File("src/test/resources/invalidGrpcServer.json"), SimulatorParameters.class); + BeanValidationResult result = params.validate("InvalidGrpcParams"); assertFalse(result.isValid()); assertNotNull(result.getResult()); } diff --git a/models-sim/policy-models-simulators/src/test/resources/emptyRestServer.json b/models-sim/policy-models-simulators/src/test/resources/emptyRestServer.json new file mode 100644 index 000000000..4a86253da --- /dev/null +++ b/models-sim/policy-models-simulators/src/test/resources/emptyRestServer.json @@ -0,0 +1,11 @@ +{ + "dmaapProvider": { + "name": "DMaaP simulator", + "topicSweepSec": 300, + "restServerParameters": {} + }, + "restServers": [], + "topicSinks": [], + "topicSources": [], + "topicServers": [] +} diff --git a/models-sim/policy-models-simulators/src/test/resources/invalidDmaapName.json b/models-sim/policy-models-simulators/src/test/resources/invalidDmaapName.json new file mode 100644 index 000000000..889c4c844 --- /dev/null +++ b/models-sim/policy-models-simulators/src/test/resources/invalidDmaapName.json @@ -0,0 +1,10 @@ +{ + "dmaapProvider": { + "name": null, + "topicSweepSec":300 + }, + "restServers": [], + "topicSinks": [], + "topicSources": [], + "topicServers": [] +} diff --git a/models-sim/policy-models-simulators/src/test/resources/invalidDmaapParameters.json b/models-sim/policy-models-simulators/src/test/resources/invalidDmaapParameters.json deleted file mode 100644 index ff6790144..000000000 --- a/models-sim/policy-models-simulators/src/test/resources/invalidDmaapParameters.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "dmaapProvider": {}, - "restServers": [], - "topicSinks": [], - "topicSources": [], - "topicServers": [] -} diff --git a/models-sim/policy-models-simulators/src/test/resources/invalidDmaapProvider.json b/models-sim/policy-models-simulators/src/test/resources/invalidDmaapProvider.json new file mode 100644 index 000000000..ff6790144 --- /dev/null +++ b/models-sim/policy-models-simulators/src/test/resources/invalidDmaapProvider.json @@ -0,0 +1,7 @@ +{ + "dmaapProvider": {}, + "restServers": [], + "topicSinks": [], + "topicSources": [], + "topicServers": [] +} diff --git a/models-sim/policy-models-simulators/src/test/resources/invalidGrpcParameters.json b/models-sim/policy-models-simulators/src/test/resources/invalidGrpcParameters.json deleted file mode 100644 index 86e425364..000000000 --- a/models-sim/policy-models-simulators/src/test/resources/invalidGrpcParameters.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "grpcServer": {}, - "restServers": [], - "topicSinks": [], - "topicSources": [], - "topicServers": [] -} diff --git a/models-sim/policy-models-simulators/src/test/resources/invalidGrpcServer.json b/models-sim/policy-models-simulators/src/test/resources/invalidGrpcServer.json new file mode 100644 index 000000000..86e425364 --- /dev/null +++ b/models-sim/policy-models-simulators/src/test/resources/invalidGrpcServer.json @@ -0,0 +1,7 @@ +{ + "grpcServer": {}, + "restServers": [], + "topicSinks": [], + "topicSources": [], + "topicServers": [] +} diff --git a/models-sim/policy-models-simulators/src/test/resources/invalidTopicSweep.json b/models-sim/policy-models-simulators/src/test/resources/invalidTopicSweep.json new file mode 100644 index 000000000..11a37d5c4 --- /dev/null +++ b/models-sim/policy-models-simulators/src/test/resources/invalidTopicSweep.json @@ -0,0 +1,10 @@ +{ + "dmaapProvider": { + "name": "DMaaP simulator", + "topicSweepSec":0 + }, + "restServers": [], + "topicSinks": [], + "topicSources": [], + "topicServers": [] +} -- cgit 1.2.3-korg