From abba1c2f8fc792b63392302fec99ee27147362aa Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 10 Jun 2020 15:44:29 -0400 Subject: Default ActorImpl should have an "operations" property Added "operations" level to actor properties. Also renamed CommonActorParams to ActorParams, in the process. Note: This impacts the properties specified in drools-apps. Issue-ID: POLICY-2629 Change-Id: I155bcb6ced73cb54e656d34e0b184fb2ed6250d1 Signed-off-by: Jim Hahn --- .../actor.cds/src/test/resources/service.yaml | 13 +- .../actor/guard/GuardActorParamsTest.java | 6 +- .../controlloop/actor/so/SoActorParamsTest.java | 6 +- .../actor.test/src/test/resources/service.yaml | 5 +- .../actor.test/src/test/resources/serviceFull.yaml | 5 +- .../src/test/resources/serviceInvalidHttp.yaml | 5 +- .../actor.vfc/src/test/resources/service.yaml | 3 +- .../actorserviceprovider/impl/ActorImpl.java | 11 +- .../parameters/ActorParams.java | 106 ++++++++++++++++ .../parameters/BidirectionalTopicActorParams.java | 2 +- .../parameters/CommonActorParams.java | 106 ---------------- .../parameters/HttpActorParams.java | 2 +- .../actorserviceprovider/ActorServiceTest.java | 10 +- .../actorserviceprovider/impl/ActorImplTest.java | 9 +- .../parameters/ActorParamsTest.java | 138 +++++++++++++++++++++ .../BidirectionalTopicActorParamsTest.java | 4 +- .../parameters/CommonActorParamsTest.java | 138 --------------------- .../parameters/HttpActorParamsTest.java | 4 +- 18 files changed, 292 insertions(+), 281 deletions(-) create mode 100644 models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ActorParams.java delete mode 100644 models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParams.java create mode 100644 models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ActorParamsTest.java delete mode 100644 models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParamsTest.java (limited to 'models-interactions') diff --git a/models-interactions/model-actors/actor.cds/src/test/resources/service.yaml b/models-interactions/model-actors/actor.cds/src/test/resources/service.yaml index 0d3c76f48..19ff698a3 100644 --- a/models-interactions/model-actors/actor.cds/src/test/resources/service.yaml +++ b/models-interactions/model-actors/actor.cds/src/test/resources/service.yaml @@ -19,9 +19,10 @@ # actors: CDS: - any: - timeout: 10 - port: 6700 - host: my-host - username: my-user - password: my-pass + operations: + any: + timeout: 10 + port: 6700 + host: my-host + username: my-user + password: my-pass diff --git a/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardActorParamsTest.java b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardActorParamsTest.java index 0fef3bd29..71c75b8cf 100644 --- a/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardActorParamsTest.java +++ b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardActorParamsTest.java @@ -32,7 +32,7 @@ import org.junit.Before; import org.junit.Test; import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.controlloop.actorserviceprovider.Util; -import org.onap.policy.controlloop.actorserviceprovider.parameters.CommonActorParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ActorParams; public class GuardActorParamsTest { private static final String CONTAINER = "my-container"; @@ -75,14 +75,14 @@ public class GuardActorParamsTest { assertTrue(params.validate(CONTAINER).isValid()); // only a few fields are required - GuardActorParams sparse = Util.translate(CONTAINER, Map.of(CommonActorParams.OPERATIONS_FIELD, operations), + GuardActorParams sparse = Util.translate(CONTAINER, Map.of(ActorParams.OPERATIONS_FIELD, operations), GuardActorParams.class); assertTrue(sparse.validate(CONTAINER).isValid()); assertEquals(GuardActorParams.DEFAULT_ACTION, sparse.getAction()); // check fields from superclass - testValidateField(CommonActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null)); + testValidateField(ActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null)); testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1)); } diff --git a/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/SoActorParamsTest.java b/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/SoActorParamsTest.java index 5fb647253..93f305550 100644 --- a/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/SoActorParamsTest.java +++ b/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/SoActorParamsTest.java @@ -31,7 +31,7 @@ import org.junit.Before; import org.junit.Test; import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.controlloop.actorserviceprovider.Util; -import org.onap.policy.controlloop.actorserviceprovider.parameters.CommonActorParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ActorParams; public class SoActorParamsTest { @@ -68,7 +68,7 @@ public class SoActorParamsTest { assertTrue(params.validate(CONTAINER).isValid()); // only a few fields are required - SoActorParams sparse = Util.translate(CONTAINER, Map.of(CommonActorParams.OPERATIONS_FIELD, operations), + SoActorParams sparse = Util.translate(CONTAINER, Map.of(ActorParams.OPERATIONS_FIELD, operations), SoActorParams.class); assertTrue(sparse.validate(CONTAINER).isValid()); @@ -76,7 +76,7 @@ public class SoActorParamsTest { testValidateField("waitSecGet", "minimum", params2 -> params2.setWaitSecGet(0)); // check fields from superclass - testValidateField(CommonActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null)); + testValidateField(ActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null)); testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1)); // check edge cases diff --git a/models-interactions/model-actors/actor.test/src/test/resources/service.yaml b/models-interactions/model-actors/actor.test/src/test/resources/service.yaml index 49de7da51..1d1604034 100644 --- a/models-interactions/model-actors/actor.test/src/test/resources/service.yaml +++ b/models-interactions/model-actors/actor.test/src/test/resources/service.yaml @@ -19,5 +19,6 @@ # actors: MyActor: - MyOperationA: {} - MyOperationB: {} \ No newline at end of file + operations: + MyOperationA: {} + MyOperationB: {} diff --git a/models-interactions/model-actors/actor.test/src/test/resources/serviceFull.yaml b/models-interactions/model-actors/actor.test/src/test/resources/serviceFull.yaml index 9818215be..8c6271e85 100644 --- a/models-interactions/model-actors/actor.test/src/test/resources/serviceFull.yaml +++ b/models-interactions/model-actors/actor.test/src/test/resources/serviceFull.yaml @@ -38,5 +38,6 @@ topics: managed: true actors: MyActor: - MyOperationA: {} - MyOperationB: {} \ No newline at end of file + operations: + MyOperationA: {} + MyOperationB: {} diff --git a/models-interactions/model-actors/actor.test/src/test/resources/serviceInvalidHttp.yaml b/models-interactions/model-actors/actor.test/src/test/resources/serviceInvalidHttp.yaml index b31e8e81e..416126893 100644 --- a/models-interactions/model-actors/actor.test/src/test/resources/serviceInvalidHttp.yaml +++ b/models-interactions/model-actors/actor.test/src/test/resources/serviceInvalidHttp.yaml @@ -25,5 +25,6 @@ httpClients: managed: true actors: MyActor: - MyOperationA: {} - MyOperationB: {} \ No newline at end of file + operations: + MyOperationA: {} + MyOperationB: {} diff --git a/models-interactions/model-actors/actor.vfc/src/test/resources/service.yaml b/models-interactions/model-actors/actor.vfc/src/test/resources/service.yaml index 26192e013..f6988517b 100644 --- a/models-interactions/model-actors/actor.vfc/src/test/resources/service.yaml +++ b/models-interactions/model-actors/actor.vfc/src/test/resources/service.yaml @@ -25,7 +25,8 @@ httpClients: managed: true actors: VFC: - Restart: + operations: + Restart: clientName: my-client pathGet: jobs maxGets: 20 diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImpl.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImpl.java index 6113f9391..c3c815e80 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImpl.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImpl.java @@ -29,6 +29,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import org.onap.policy.controlloop.actorserviceprovider.Operator; import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ActorParams; import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException; import org.onap.policy.controlloop.actorserviceprovider.spi.Actor; import org.slf4j.Logger; @@ -156,7 +157,8 @@ public class ActorImpl extends StartConfigPartial> implement /** * Extracts the operator parameters from the actor parameters, for a given operator. - * This method assumes each operation has its own set of parameters. + * This method translates the parameters to an {@link ActorParams} and then creates a function + * that will extract operator-specific parameters. * * @param actorParameters actor parameters * @return a function to extract the operator parameters from the actor parameters. @@ -164,8 +166,13 @@ public class ActorImpl extends StartConfigPartial> implement * the given operation name */ protected Function> makeOperatorParameters(Map actorParameters) { + String actorName = getName(); - return operName -> Util.translateToMap(getName() + "." + operName, actorParameters.get(operName)); + // @formatter:off + return Util.translate(actorName, actorParameters, ActorParams.class) + .doValidation(actorName) + .makeOperationParameters(actorName); + // @formatter:on } /** diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ActorParams.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ActorParams.java new file mode 100644 index 000000000..948677fa4 --- /dev/null +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ActorParams.java @@ -0,0 +1,106 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop.actorserviceprovider.parameters; + +import java.util.Map; +import java.util.TreeMap; +import java.util.function.Function; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import org.onap.policy.common.parameters.BeanValidator; +import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.controlloop.actorserviceprovider.Util; + +/** + * Superclass for Actor parameters that have default values in "this" object, and + * operation-specific values in {@link #operations}. + */ +@Getter +@Setter +@EqualsAndHashCode +public class ActorParams { + /** + * Name of the "operations" field contained within actor parameters. + */ + public static final String OPERATIONS_FIELD = "operations"; + + /** + * Maps the operation name to its parameters. + */ + @NotNull + protected Map> operations; + + + /** + * Extracts a specific operation's parameters from "this". + * + * @param name name of the item containing "this" + * @return a function to extract an operation's parameters from "this". Note: the + * returned function is not thread-safe + */ + public Function> makeOperationParameters(String name) { + + Map defaultParams = Util.translateToMap(name, this); + defaultParams.remove(OPERATIONS_FIELD); + + return operationName -> { + Map specificParams = operations.get(operationName); + if (specificParams == null) { + return null; + } + + // start with a copy of defaults and overlay with specific + Map subparams = new TreeMap<>(defaultParams); + subparams.putAll(specificParams); + + return Util.translateToMap(name + "." + operationName, subparams); + }; + } + + /** + * Validates the parameters. + * + * @param name name of the object containing these parameters + * @return "this" + * @throws IllegalArgumentException if the parameters are invalid + */ + public ActorParams doValidation(String name) { + ValidationResult result = validate(name); + if (!result.isValid()) { + throw new ParameterValidationRuntimeException("invalid parameters", result); + } + + return this; + } + + /** + * Validates the parameters. + * + * @param resultName name of the result + * + * @return the validation result + */ + public ValidationResult validate(String resultName) { + return new BeanValidator().validateTop(resultName, this); + } +} diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicActorParams.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicActorParams.java index 291aeeb23..3e65e4729 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicActorParams.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicActorParams.java @@ -31,7 +31,7 @@ import org.onap.policy.common.parameters.annotations.Min; @Getter @Setter @EqualsAndHashCode(callSuper = true) -public class BidirectionalTopicActorParams extends CommonActorParams { +public class BidirectionalTopicActorParams extends ActorParams { /* * Optional, default values that are used if missing from the operation-specific diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParams.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParams.java deleted file mode 100644 index b8218e53b..000000000 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParams.java +++ /dev/null @@ -1,106 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.controlloop.actorserviceprovider.parameters; - -import java.util.Map; -import java.util.TreeMap; -import java.util.function.Function; -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.Setter; -import org.onap.policy.common.parameters.BeanValidator; -import org.onap.policy.common.parameters.ValidationResult; -import org.onap.policy.common.parameters.annotations.NotNull; -import org.onap.policy.controlloop.actorserviceprovider.Util; - -/** - * Superclass for Actor parameters that have default values in "this" object, and - * operation-specific values in {@link #operations}. - */ -@Getter -@Setter -@EqualsAndHashCode -public class CommonActorParams { - /** - * Name of the "operations" field contained within actor parameters. - */ - public static final String OPERATIONS_FIELD = "operations"; - - /** - * Maps the operation name to its parameters. - */ - @NotNull - protected Map> operations; - - - /** - * Extracts a specific operation's parameters from "this". - * - * @param name name of the item containing "this" - * @return a function to extract an operation's parameters from "this". Note: the - * returned function is not thread-safe - */ - public Function> makeOperationParameters(String name) { - - Map defaultParams = Util.translateToMap(name, this); - defaultParams.remove(OPERATIONS_FIELD); - - return operationName -> { - Map specificParams = operations.get(operationName); - if (specificParams == null) { - return null; - } - - // start with a copy of defaults and overlay with specific - Map subparams = new TreeMap<>(defaultParams); - subparams.putAll(specificParams); - - return Util.translateToMap(name + "." + operationName, subparams); - }; - } - - /** - * Validates the parameters. - * - * @param name name of the object containing these parameters - * @return "this" - * @throws IllegalArgumentException if the parameters are invalid - */ - public CommonActorParams doValidation(String name) { - ValidationResult result = validate(name); - if (!result.isValid()) { - throw new ParameterValidationRuntimeException("invalid parameters", result); - } - - return this; - } - - /** - * Validates the parameters. - * - * @param resultName name of the result - * - * @return the validation result - */ - public ValidationResult validate(String resultName) { - return new BeanValidator().validateTop(resultName, this); - } -} diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpActorParams.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpActorParams.java index d589e1d7e..616f674c5 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpActorParams.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpActorParams.java @@ -31,7 +31,7 @@ import org.onap.policy.common.parameters.annotations.Min; @Getter @Setter @EqualsAndHashCode(callSuper = true) -public class HttpActorParams extends CommonActorParams { +public class HttpActorParams extends ActorParams { /* * Optional, default values that are used if missing from the operation-specific diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/ActorServiceTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/ActorServiceTest.java index 0f282f662..97f6bbacf 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/ActorServiceTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/ActorServiceTest.java @@ -36,6 +36,7 @@ import static org.mockito.Mockito.when; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; @@ -46,6 +47,7 @@ import org.junit.Test; import org.onap.policy.common.parameters.ObjectValidationResult; import org.onap.policy.common.parameters.ValidationStatus; import org.onap.policy.controlloop.actorserviceprovider.impl.ActorImpl; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ActorParams; import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException; import org.onap.policy.controlloop.actorserviceprovider.spi.Actor; @@ -80,10 +82,10 @@ public class ActorServiceTest { actor3 = spy(new ActorImpl(ACTOR3)); actor4 = spy(new ActorImpl(ACTOR4)); - sub1 = Map.of("sub A", "value A"); - sub2 = Map.of("sub B", "value B"); - sub3 = Map.of("sub C", "value C"); - sub4 = Map.of("sub D", "value D"); + sub1 = Map.of("sub A", "value A", ActorParams.OPERATIONS_FIELD, Collections.emptyMap()); + sub2 = Map.of("sub B", "value B", ActorParams.OPERATIONS_FIELD, Collections.emptyMap()); + sub3 = Map.of("sub C", "value C", ActorParams.OPERATIONS_FIELD, Collections.emptyMap()); + sub4 = Map.of("sub D", "value D", ActorParams.OPERATIONS_FIELD, Collections.emptyMap()); params = Map.of(ACTOR1, sub1, ACTOR2, sub2, ACTOR3, sub3, ACTOR4, sub4); diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImplTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImplTest.java index 681378de4..cf07d3b2b 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImplTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/impl/ActorImplTest.java @@ -36,7 +36,6 @@ import static org.mockito.Mockito.when; import java.util.Iterator; import java.util.Map; -import java.util.TreeMap; import java.util.stream.Collectors; import org.junit.Before; import org.junit.Test; @@ -44,6 +43,7 @@ import org.onap.policy.common.parameters.ObjectValidationResult; import org.onap.policy.common.parameters.ValidationStatus; import org.onap.policy.controlloop.actorserviceprovider.Operation; import org.onap.policy.controlloop.actorserviceprovider.Operator; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ActorParams; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException; @@ -84,7 +84,7 @@ public class ActorImplTest { sub3 = Map.of("sub C", "value C"); sub4 = Map.of("sub D", "value D"); - params = Map.of(OPER1, sub1, OPER2, sub2, OPER3, sub3, OPER4, sub4); + params = Map.of(ActorParams.OPERATIONS_FIELD, Map.of(OPER1, sub1, OPER2, sub2, OPER3, sub3, OPER4, sub4)); actor = makeActor(oper1, oper2, oper3, oper4); } @@ -285,15 +285,12 @@ public class ActorImplTest { */ @Test public void testDoConfigureConfigure() { - // need mutable parameters - params = new TreeMap<>(params); - // configure one operator oper1.configure(sub1); // configure another and remove its parameters oper2.configure(sub2); - params.remove(OPER2); + params = Map.of(ActorParams.OPERATIONS_FIELD, Map.of(OPER1, sub1, OPER3, sub3, OPER4, sub4)); // create a new, unconfigured actor Operator oper5 = spy(new MyOper("UNCONFIGURED")); diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ActorParamsTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ActorParamsTest.java new file mode 100644 index 000000000..0304ef3be --- /dev/null +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/ActorParamsTest.java @@ -0,0 +1,138 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop.actorserviceprovider.parameters; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.Map; +import java.util.TreeMap; +import java.util.function.Consumer; +import java.util.function.Function; +import lombok.Setter; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.controlloop.actorserviceprovider.Util; + +public class ActorParamsTest { + + private static final String CONTAINER = "my-container"; + + private static final String PATH1 = "path #1"; + private static final String PATH2 = "path #2"; + private static final String URI1 = "uri #1"; + private static final String URI2 = "uri #2"; + private static final String TEXT1 = "hello"; + private static final String TEXT2 = "world"; + private static final String TEXT2B = "bye"; + + private Map> operations; + private ActorParams params; + + /** + * Initializes {@link #operations} with two items and {@link params} with a fully + * populated object. + */ + @Before + public void setUp() { + operations = new TreeMap<>(); + operations.put(PATH1, Map.of("path", URI1)); + operations.put(PATH2, Map.of("path", URI2, "text2", TEXT2B)); + + params = makeActorParams(); + } + + @Test + public void testMakeOperationParameters() { + Function> maker = params.makeOperationParameters(CONTAINER); + assertNull(maker.apply("unknown-operation")); + + Map subparam = maker.apply(PATH1); + assertNotNull(subparam); + assertEquals("{path=uri #1, text1=hello, text2=world}", new TreeMap<>(subparam).toString()); + + subparam = maker.apply(PATH2); + assertNotNull(subparam); + assertEquals("{path=uri #2, text1=hello, text2=bye}", new TreeMap<>(subparam).toString()); + } + + @Test + public void testDoValidation() { + assertThatCode(() -> params.doValidation(CONTAINER)).doesNotThrowAnyException(); + + // invalid param + params.setOperations(null); + assertThatThrownBy(() -> params.doValidation(CONTAINER)) + .isInstanceOf(ParameterValidationRuntimeException.class); + } + + @Test + public void testValidate() { + assertTrue(params.validate(CONTAINER).isValid()); + + // only a few fields are required + ActorParams sparse = Util.translate(CONTAINER, + Map.of(ActorParams.OPERATIONS_FIELD, operations, "timeoutSec", 1), + ActorParams.class); + assertTrue(sparse.validate(CONTAINER).isValid()); + + testValidateField(ActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null)); + } + + private void testValidateField(String fieldName, String expected, Consumer makeInvalid) { + + // original params should be valid + ValidationResult result = params.validate(CONTAINER); + assertTrue(fieldName, result.isValid()); + + // make invalid params + ActorParams params2 = makeActorParams(); + makeInvalid.accept(params2); + result = params2.validate(CONTAINER); + assertFalse(fieldName, result.isValid()); + assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected); + } + + private ActorParams makeActorParams() { + MyParams params2 = new MyParams(); + params2.setOperations(operations); + params2.setText1(TEXT1); + params2.setText2(TEXT2); + + return params2; + } + + @Setter + public static class MyParams extends ActorParams { + @SuppressWarnings("unused") + private String text1; + + @SuppressWarnings("unused") + private String text2; + } +} diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicActorParamsTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicActorParamsTest.java index 1276950a5..769e0fca5 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicActorParamsTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/BidirectionalTopicActorParamsTest.java @@ -77,11 +77,11 @@ public class BidirectionalTopicActorParamsTest { // only a few fields are required BidirectionalTopicActorParams sparse = - Util.translate(CONTAINER, Map.of(CommonActorParams.OPERATIONS_FIELD, operMap, "timeoutSec", 1), + Util.translate(CONTAINER, Map.of(ActorParams.OPERATIONS_FIELD, operMap, "timeoutSec", 1), BidirectionalTopicActorParams.class); assertTrue(sparse.validate(CONTAINER).isValid()); - testValidateField(CommonActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null)); + testValidateField(ActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null)); testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1)); // check edge cases diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParamsTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParamsTest.java deleted file mode 100644 index 99b441064..000000000 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/CommonActorParamsTest.java +++ /dev/null @@ -1,138 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.controlloop.actorserviceprovider.parameters; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatCode; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.util.Map; -import java.util.TreeMap; -import java.util.function.Consumer; -import java.util.function.Function; -import lombok.Setter; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.common.parameters.ValidationResult; -import org.onap.policy.controlloop.actorserviceprovider.Util; - -public class CommonActorParamsTest { - - private static final String CONTAINER = "my-container"; - - private static final String PATH1 = "path #1"; - private static final String PATH2 = "path #2"; - private static final String URI1 = "uri #1"; - private static final String URI2 = "uri #2"; - private static final String TEXT1 = "hello"; - private static final String TEXT2 = "world"; - private static final String TEXT2B = "bye"; - - private Map> operations; - private CommonActorParams params; - - /** - * Initializes {@link #operations} with two items and {@link params} with a fully - * populated object. - */ - @Before - public void setUp() { - operations = new TreeMap<>(); - operations.put(PATH1, Map.of("path", URI1)); - operations.put(PATH2, Map.of("path", URI2, "text2", TEXT2B)); - - params = makeCommonActorParams(); - } - - @Test - public void testMakeOperationParameters() { - Function> maker = params.makeOperationParameters(CONTAINER); - assertNull(maker.apply("unknown-operation")); - - Map subparam = maker.apply(PATH1); - assertNotNull(subparam); - assertEquals("{path=uri #1, text1=hello, text2=world}", new TreeMap<>(subparam).toString()); - - subparam = maker.apply(PATH2); - assertNotNull(subparam); - assertEquals("{path=uri #2, text1=hello, text2=bye}", new TreeMap<>(subparam).toString()); - } - - @Test - public void testDoValidation() { - assertThatCode(() -> params.doValidation(CONTAINER)).doesNotThrowAnyException(); - - // invalid param - params.setOperations(null); - assertThatThrownBy(() -> params.doValidation(CONTAINER)) - .isInstanceOf(ParameterValidationRuntimeException.class); - } - - @Test - public void testValidate() { - assertTrue(params.validate(CONTAINER).isValid()); - - // only a few fields are required - CommonActorParams sparse = Util.translate(CONTAINER, - Map.of(CommonActorParams.OPERATIONS_FIELD, operations, "timeoutSec", 1), - CommonActorParams.class); - assertTrue(sparse.validate(CONTAINER).isValid()); - - testValidateField(CommonActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null)); - } - - private void testValidateField(String fieldName, String expected, Consumer makeInvalid) { - - // original params should be valid - ValidationResult result = params.validate(CONTAINER); - assertTrue(fieldName, result.isValid()); - - // make invalid params - CommonActorParams params2 = makeCommonActorParams(); - makeInvalid.accept(params2); - result = params2.validate(CONTAINER); - assertFalse(fieldName, result.isValid()); - assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected); - } - - private CommonActorParams makeCommonActorParams() { - MyParams params2 = new MyParams(); - params2.setOperations(operations); - params2.setText1(TEXT1); - params2.setText2(TEXT2); - - return params2; - } - - @Setter - public static class MyParams extends CommonActorParams { - @SuppressWarnings("unused") - private String text1; - - @SuppressWarnings("unused") - private String text2; - } -} diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpActorParamsTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpActorParamsTest.java index 99bf63305..5812b0437 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpActorParamsTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/parameters/HttpActorParamsTest.java @@ -65,10 +65,10 @@ public class HttpActorParamsTest { // only a few fields are required HttpActorParams sparse = Util.translate(CONTAINER, - Map.of(CommonActorParams.OPERATIONS_FIELD, operations, "timeoutSec", 1), HttpActorParams.class); + Map.of(ActorParams.OPERATIONS_FIELD, operations, "timeoutSec", 1), HttpActorParams.class); assertTrue(sparse.validate(CONTAINER).isValid()); - testValidateField(CommonActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null)); + testValidateField(ActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null)); testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1)); // check edge cases -- cgit 1.2.3-korg