diff options
13 files changed, 110 insertions, 17 deletions
diff --git a/models-examples/src/main/resources/policytypes/onap.policies.native.Drools.yaml b/models-examples/src/main/resources/policytypes/onap.policies.native.Drools.yaml index 382999f10..95c87e129 100644 --- a/models-examples/src/main/resources/policytypes/onap.policies.native.Drools.yaml +++ b/models-examples/src/main/resources/policytypes/onap.policies.native.Drools.yaml @@ -111,7 +111,7 @@ data_types: onap.datatypes.drools.controller.relation: derived_from: tosca.datatypes.Root properties: - controllerName: + name: type: string required: true description: the name of drools controller policy diff --git a/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/CdsSimulator.java b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/CdsSimulator.java index 3418780bc..6c505b3d9 100644 --- a/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/CdsSimulator.java +++ b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/CdsSimulator.java @@ -39,8 +39,13 @@ import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceIn import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput; import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput.Builder; import org.onap.policy.common.utils.resources.ResourceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class CdsSimulator { + + private static final Logger LOGGER = LoggerFactory.getLogger(CdsSimulator.class); + @Getter private final int port; @@ -83,6 +88,7 @@ public class CdsSimulator { @Override public void onNext(final ExecutionServiceInput executionServiceInput) { + LOGGER.info("Received request input to CDS: {}", executionServiceInput); try { String responseString = getResponseString(executionServiceInput, countOfSuccesfulEvents); Builder builder = ExecutionServiceOutput.newBuilder(); @@ -141,11 +147,14 @@ public class CdsSimulator { } else { resourceName = resourceName + ".json"; } + LOGGER.info("Fetching response from {}", resourceName); String responseString = ResourceUtils.getResourceAsString(resourceLocation + resourceName); if (responseString == null) { + LOGGER.info("Expected response file {} not found in {}", resourceName, resourceLocation); responseString = ResourceUtils.getResourceAsString(resourceLocation + "DefaultResponseEvent.json"); } + LOGGER.debug("Returning response from CDS Simulator: {}", responseString); return responseString; } } diff --git a/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/Util.java b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/Util.java index efc40367b..d9fb52bd9 100644 --- a/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/Util.java +++ b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/Util.java @@ -52,7 +52,7 @@ public class Util { private static final String CANNOT_PROCESS_PARAMETERS = "cannot parse parameters "; private static final String CANNOT_CONNECT = "cannot connect to port "; - private static final String LOCALHOST = "localhost"; + public static final String LOCALHOST = "localhost"; private Util() { // Prevent instantiation of this class diff --git a/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/CdsSimulatorTest.java b/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/CdsSimulatorTest.java index fea03e3e4..cd4f3b593 100644 --- a/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/CdsSimulatorTest.java +++ b/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/CdsSimulatorTest.java @@ -24,6 +24,7 @@ package org.onap.policy.simulators; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import com.google.protobuf.util.JsonFormat; import io.grpc.ManagedChannel; import io.grpc.internal.DnsNameResolverProvider; import io.grpc.internal.PickFirstLoadBalancerProvider; @@ -42,9 +43,11 @@ import org.junit.Test; import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc; import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceStub; import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput; +import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput.Builder; import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.common.utils.resources.ResourceUtils; public class CdsSimulatorTest { @@ -52,9 +55,15 @@ public class CdsSimulatorTest { private CdsSimulator sim; + /** + * Starts the simulator, allocating a unique port for each test so we don't have to + * wait for the prior server to shut down. + */ @Before public void setUp() throws Exception { - sim = Util.buildCdsSim(); + int port = NetworkUtil.allocPort(); + sim = new CdsSimulator(Util.LOCALHOST, port); + sim.start(); } @After @@ -65,8 +74,10 @@ public class CdsSimulatorTest { @Test public void test() throws Exception { String reqstr = IOUtils.toString(getClass().getResource("cds/cds.request.json"), StandardCharsets.UTF_8); - ExecutionServiceInput request = coder.decode(reqstr, ExecutionServiceInput.class); - ManagedChannel channel = NettyChannelBuilder.forAddress("localhost", sim.getPort()) + Builder builder = ExecutionServiceInput.newBuilder(); + JsonFormat.parser().ignoringUnknownFields().merge(reqstr, builder); + ExecutionServiceInput request = builder.build(); + ManagedChannel channel = NettyChannelBuilder.forAddress(Util.LOCALHOST, sim.getPort()) .nameResolverFactory(new DnsNameResolverProvider()) .loadBalancerFactory(new PickFirstLoadBalancerProvider()).usePlaintext().build(); @@ -118,7 +129,7 @@ public class CdsSimulatorTest { @Test public void testGetResponseString() throws IOException, CoderException, ParseException { - CdsSimulator cdsSimulator = new CdsSimulator("localhost", sim.getPort()); + CdsSimulator cdsSimulator = new CdsSimulator(Util.LOCALHOST, sim.getPort()); String reqstr = ResourceUtils.getResourceAsString( "org/onap/policy/simulators/cds/cds.request.json"); String responseqstr = ResourceUtils.getResourceAsString( 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/invalidDmaapProvider.json index ff6790144..ff6790144 100644 --- a/models-sim/policy-models-simulators/src/test/resources/invalidDmaapParameters.json +++ b/models-sim/policy-models-simulators/src/test/resources/invalidDmaapProvider.json diff --git a/models-sim/policy-models-simulators/src/test/resources/invalidGrpcParameters.json b/models-sim/policy-models-simulators/src/test/resources/invalidGrpcServer.json index 86e425364..86e425364 100644 --- a/models-sim/policy-models-simulators/src/test/resources/invalidGrpcParameters.json +++ b/models-sim/policy-models-simulators/src/test/resources/invalidGrpcServer.json 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": [] +} |