diff options
Diffstat (limited to 'models-interactions/model-actors/actor.guard/src')
12 files changed, 630 insertions, 21 deletions
diff --git a/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardActorParams.java b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardActorParams.java new file mode 100644 index 000000000..b8eb85c03 --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardActorParams.java @@ -0,0 +1,43 @@ +/*- + * ============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.actor.guard; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpActorParams; + +@Getter +@Setter +@EqualsAndHashCode(callSuper = true) +public class GuardActorParams extends HttpActorParams { + public static final String DEFAULT_ACTION = "guard"; + + /* + * Optional, default values that are used if missing from the operation-specific + * parameters. + */ + + private String onapName; + private String onapComponent; + private String onapInstance; + private String action = DEFAULT_ACTION; +} diff --git a/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardActorServiceProvider.java b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardActorServiceProvider.java index 104c3830c..ea08d13da 100644 --- a/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardActorServiceProvider.java +++ b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardActorServiceProvider.java @@ -25,7 +25,7 @@ package org.onap.policy.controlloop.actor.guard; import org.onap.policy.controlloop.actorserviceprovider.impl.HttpActor; import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator; -public class GuardActorServiceProvider extends HttpActor { +public class GuardActorServiceProvider extends HttpActor<GuardActorParams> { // actor name public static final String NAME = "GUARD"; @@ -33,9 +33,8 @@ public class GuardActorServiceProvider extends HttpActor { * Constructs the object. */ public GuardActorServiceProvider() { - super(NAME); + super(NAME, GuardActorParams.class); - addOperator(HttpOperator.makeOperator(NAME, GuardOperation.NAME, - GuardOperation::new)); + addOperator(new HttpOperator(NAME, GuardOperation.NAME, GuardOperation::new)); } } diff --git a/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardConfig.java b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardConfig.java new file mode 100644 index 000000000..c6bf32635 --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardConfig.java @@ -0,0 +1,72 @@ +/*- + * ============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.actor.guard; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.Executor; +import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.common.endpoints.http.client.HttpClientFactory; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; + +/** + * Configuration for Guard Operators. + */ +public class GuardConfig extends HttpConfig { + private final Map<String, Object> defaultRequest = new LinkedHashMap<>(); + + /** + * Constructs the object. + * + * @param blockingExecutor executor to be used for tasks that may perform blocking I/O + * @param params operator parameters + * @param clientFactory factory from which to obtain the {@link HttpClient} + */ + public GuardConfig(Executor blockingExecutor, GuardParams params, HttpClientFactory clientFactory) { + super(blockingExecutor, params, clientFactory); + + addProperty("ONAPComponent", params.getOnapComponent()); + addProperty("ONAPInstance", params.getOnapInstance()); + addProperty("ONAPName", params.getOnapName()); + addProperty("action", params.getAction()); + } + + /** + * Adds a property to the default request, if the value is not {@code null}. + * + * @param key property key + * @param value property value, or {@code null} + */ + private void addProperty(String key, String value) { + if (value != null) { + defaultRequest.put(key, value); + } + } + + /** + * Creates a new request, with the default values. + * + * @return a new request map + */ + public Map<String, Object> makeRequest() { + return new LinkedHashMap<>(defaultRequest); + } +} diff --git a/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardOperation.java b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardOperation.java index 941838f00..453a3e377 100644 --- a/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardOperation.java +++ b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardOperation.java @@ -33,8 +33,8 @@ import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; import org.onap.policy.controlloop.actorserviceprovider.Util; import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation; -import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; import org.onap.policy.controlloop.policy.PolicyResult; import org.onap.policy.models.decisions.concepts.DecisionRequest; import org.onap.policy.models.decisions.concepts.DecisionResponse; @@ -74,15 +74,18 @@ public class GuardOperation extends HttpOperation<DecisionResponse> { */ public static final String RESOURCE_PREFIX = "resource."; + private final GuardConfig config; + /** * Constructs the object. * * @param params operation parameters - * @param operator operator that created this operation + * @param config configuration for this operation */ - public GuardOperation(ControlLoopOperationParams params, HttpOperator operator) { - super(params, operator, DecisionResponse.class); + public GuardOperation(ControlLoopOperationParams params, HttpConfig config) { + super(params, config, DecisionResponse.class); + this.config = (GuardConfig) config; } @Override @@ -101,7 +104,7 @@ public class GuardOperation extends HttpOperation<DecisionResponse> { // @formatter:off return handleResponse(outcome, url, - callback -> getOperator().getClient().post(callback, makePath(), entity, headers)); + callback -> getClient().post(callback, makePath(), entity, headers)); // @formatter:on } @@ -121,7 +124,7 @@ public class GuardOperation extends HttpOperation<DecisionResponse> { * appropriate field. */ - Map<String, Object> req = new LinkedHashMap<>(); + Map<String, Object> req = config.makeRequest(); Map<String, Object> resource = new LinkedHashMap<>(); for (Entry<String, String> ent : params.getPayload().entrySet()) { @@ -141,7 +144,6 @@ public class GuardOperation extends HttpOperation<DecisionResponse> { } } - req.putIfAbsent("action", "guard"); req.computeIfAbsent("requestId", key -> UUID.randomUUID().toString()); req.put(RESOURCE, resource); diff --git a/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardOperator.java b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardOperator.java new file mode 100644 index 000000000..b3a47fd71 --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardOperator.java @@ -0,0 +1,52 @@ +/*- + * ============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.actor.guard; + +import java.util.Map; +import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator; +import org.onap.policy.controlloop.actorserviceprovider.impl.OperationMaker; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException; + +/** + * Guard Operator. + */ +public class GuardOperator extends HttpOperator { + + public GuardOperator(String actorName, String name, + @SuppressWarnings("rawtypes") OperationMaker<HttpConfig, HttpOperation> operationMaker) { + super(actorName, name, operationMaker); + } + + @Override + protected HttpConfig makeConfiguration(Map<String, Object> parameters) { + GuardParams params = Util.translate(getFullName(), parameters, GuardParams.class); + ValidationResult result = params.validate(getFullName()); + if (!result.isValid()) { + throw new ParameterValidationRuntimeException("invalid parameters", result); + } + + return new GuardConfig(getBlockingExecutor(), params, getClientFactory()); + } +} diff --git a/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardParams.java b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardParams.java new file mode 100644 index 000000000..251482525 --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardParams.java @@ -0,0 +1,44 @@ +/*- + * ============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.actor.guard; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.SuperBuilder; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams; + +/** + * Default values to be included if not specified in the payload. + */ +@Data +@EqualsAndHashCode(callSuper = true) +@SuperBuilder(toBuilder = true) +public class GuardParams extends HttpParams { + + /* + * Optional, default values that are used if missing from the payload. + */ + + private String onapName; + private String onapComponent; + private String onapInstance; + private String action; +} 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 new file mode 100644 index 000000000..cd9f4e316 --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardActorParamsTest.java @@ -0,0 +1,108 @@ +/*- + * ============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.actor.guard; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Map; +import java.util.TreeMap; +import java.util.function.Consumer; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.controlloop.actorserviceprovider.Util; + +public class GuardActorParamsTest { + private static final String CONTAINER = "my-container"; + private static final String CLIENT = "my-client"; + private static final int TIMEOUT = 10; + private static final String ONAP_NAME = "onap-nap"; + private static final String ONAP_COMP = "onap-component"; + private static final String ONAP_INST = "onap-instance"; + private static final String MY_ACTION = "my-action"; + + 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 Map<String, Map<String, Object>> operations; + private GuardActorParams 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)); + + params = makeGuardActorParams(); + } + + @Test + public void testValidate() { + assertTrue(params.validate(CONTAINER).isValid()); + + // only a few fields are required + GuardActorParams sparse = Util.translate(CONTAINER, Map.of("operation", operations), GuardActorParams.class); + assertTrue(sparse.validate(CONTAINER).isValid()); + + assertEquals(GuardActorParams.DEFAULT_ACTION, sparse.getAction()); + + // check fields from superclass + testValidateField("operation", "null", params2 -> params2.setOperation(null)); + testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1)); + } + + private void testValidateField(String fieldName, String expected, Consumer<GuardActorParams> makeInvalid) { + + // original params should be valid + ValidationResult result = params.validate(CONTAINER); + assertTrue(fieldName, result.isValid()); + + // make invalid params + GuardActorParams params2 = makeGuardActorParams(); + makeInvalid.accept(params2); + result = params2.validate(CONTAINER); + assertFalse(fieldName, result.isValid()); + assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected); + } + + private GuardActorParams makeGuardActorParams() { + GuardActorParams params2 = new GuardActorParams(); + params2.setClientName(CLIENT); + params2.setTimeoutSec(TIMEOUT); + params2.setOperation(operations); + + params2.setOnapName(ONAP_NAME); + params2.setOnapComponent(ONAP_COMP); + params2.setOnapInstance(ONAP_INST); + params2.setAction(MY_ACTION); + + return params2; + } +} diff --git a/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardConfigTest.java b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardConfigTest.java new file mode 100644 index 000000000..f94d122d9 --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardConfigTest.java @@ -0,0 +1,92 @@ +/*- + * ============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.actor.guard; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.when; + +import java.util.concurrent.Executor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.common.endpoints.http.client.HttpClientFactory; +import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.models.decisions.concepts.DecisionRequest; + +public class GuardConfigTest { + private static final String MY_CLIENT = "my-client"; + private static final String PATH = "my-path"; + private static final int TIMEOUT = 10; + private static final String ONAP_NAME = "onap-nap"; + private static final String ONAP_COMP = "onap-component"; + private static final String ONAP_INST = "onap-instance"; + private static final String MY_ACTION = "my-action"; + + @Mock + private HttpClient client; + @Mock + private HttpClientFactory factory; + @Mock + private Executor executor; + + private GuardParams params; + private GuardConfig config; + + /** + * Sets up. + */ + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + when(factory.get(MY_CLIENT)).thenReturn(client); + + params = GuardParams.builder().onapName(ONAP_NAME).onapComponent(ONAP_COMP).onapInstance(ONAP_INST) + .action(MY_ACTION).clientName(MY_CLIENT).path(PATH).timeoutSec(TIMEOUT).build(); + config = new GuardConfig(executor, params, factory); + } + + @Test + public void test() { + DecisionRequest expected = new DecisionRequest(); + expected.setOnapComponent(ONAP_COMP); + expected.setOnapInstance(ONAP_INST); + expected.setOnapName(ONAP_NAME); + expected.setAction(MY_ACTION); + + DecisionRequest actual = Util.translate("", config.makeRequest(), DecisionRequest.class); + assertEquals(expected, actual); + + // check value from superclass + assertSame(executor, config.getBlockingExecutor()); + assertSame(client, config.getClient()); + + // repeat, with minimal parameters + params = GuardParams.builder().clientName(MY_CLIENT).path(PATH).timeoutSec(TIMEOUT).build(); + config = new GuardConfig(executor, params, factory); + + actual = Util.translate("", config.makeRequest(), DecisionRequest.class); + assertEquals(new DecisionRequest(), actual); + } +} diff --git a/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardOperationTest.java b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardOperationTest.java index 505106041..406cdd05d 100644 --- a/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardOperationTest.java +++ b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardOperationTest.java @@ -25,10 +25,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import java.util.LinkedHashMap; import java.util.Map; import java.util.TreeMap; import java.util.concurrent.CompletableFuture; @@ -51,9 +51,15 @@ public class GuardOperationTest extends BasicHttpOperation<DecisionRequest> { */ @Before public void setUp() throws Exception { - super.setUp(); + super.setUpBasic(); - oper = new GuardOperation(params, operator); + GuardConfig cguard = mock(GuardConfig.class); + when(cguard.makeRequest()).thenAnswer(args -> new TreeMap<>(Map.of("action", "guard"))); + + config = cguard; + initConfig(); + + oper = new GuardOperation(params, config); } @Test @@ -78,9 +84,6 @@ public class GuardOperationTest extends BasicHttpOperation<DecisionRequest> { executor.runAll(100); assertTrue(future2.isDone()); - DecisionRequest request = requestCaptor.getValue().getEntity(); - verifyRequest("makeReqStd.json", request, "requestId"); - assertEquals(PolicyResult.SUCCESS, future2.get().getResult()); } @@ -102,7 +105,7 @@ public class GuardOperationTest extends BasicHttpOperation<DecisionRequest> { // null payload - start with fresh parameters and operation params = params.toBuilder().payload(null).build(); - oper = new GuardOperation(params, operator); + oper = new GuardOperation(params, config); assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest()); } @@ -156,7 +159,7 @@ public class GuardOperationTest extends BasicHttpOperation<DecisionRequest> { req.setRequestId("my-request-id"); @SuppressWarnings("unchecked") - Map<String, String> map = Util.translate("", req, LinkedHashMap.class); + Map<String, String> map = Util.translate("", req, TreeMap.class); // add resources map.put(GuardOperation.RESOURCE_PREFIX + "actor", "resource-actor"); diff --git a/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardOperatorTest.java b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardOperatorTest.java new file mode 100644 index 000000000..36263329a --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardOperatorTest.java @@ -0,0 +1,105 @@ +/*- + * ============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.actor.guard; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.common.endpoints.http.client.HttpClientFactory; +import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException; + +public class GuardOperatorTest { + private static final String ACTOR = "my-actor"; + private static final String OPERATION = "my-name"; + private static final String CLIENT = "my-client"; + private static final String PATH = "my-path"; + private static final int TIMEOUT = 10; + private static final String ONAP_NAME = "onap-nap"; + private static final String ONAP_COMP = "onap-component"; + private static final String ONAP_INST = "onap-instance"; + private static final String MY_ACTION = "my-action"; + + @Mock + private HttpClient client; + + @Mock + private HttpClientFactory factory; + + + private GuardOperator oper; + + /** + * Initializes fields, including {@link #oper}, and resets the static fields used by + * the REST server. + */ + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + when(factory.get(CLIENT)).thenReturn(client); + + oper = new MyOperator(); + + GuardParams params = GuardParams.builder().onapName(ONAP_NAME).onapComponent(ONAP_COMP).onapInstance(ONAP_INST) + .action(MY_ACTION).clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build(); + Map<String, Object> paramMap = Util.translateToMap(OPERATION, params); + oper.configure(paramMap); + + assertTrue(oper.makeConfiguration(paramMap) instanceof GuardConfig); + } + + @Test + public void testConstructor() { + assertEquals(ACTOR, oper.getActorName()); + assertEquals(OPERATION, oper.getName()); + assertEquals(ACTOR + "." + OPERATION, oper.getFullName()); + } + + @Test + public void testDoConfigure_testGetters() { + assertTrue(oper.getCurrentConfig() instanceof GuardConfig); + + // test invalid parameters + Map<String, Object> paramMap2 = Util.translateToMap(OPERATION, GuardParams.builder().build()); + assertThatThrownBy(() -> oper.configure(paramMap2)).isInstanceOf(ParameterValidationRuntimeException.class); + } + + + private class MyOperator extends GuardOperator { + public MyOperator() { + super(ACTOR, OPERATION, null); + } + + @Override + protected HttpClientFactory getClientFactory() { + return factory; + } + } +} diff --git a/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardParamsTest.java b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardParamsTest.java new file mode 100644 index 000000000..b4148857a --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardParamsTest.java @@ -0,0 +1,89 @@ +/*- + * ============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.actor.guard; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.function.Function; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.controlloop.actor.guard.GuardParams.GuardParamsBuilder; +import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams.HttpParamsBuilder; + +public class GuardParamsTest { + private static final String CONTAINER = "my-container"; + private static final String CLIENT = "my-client"; + private static final String PATH = "my-path"; + private static final int TIMEOUT = 10; + private static final String ONAP_NAME = "onap-nap"; + private static final String ONAP_COMP = "onap-component"; + private static final String ONAP_INST = "onap-instance"; + private static final String MY_ACTION = "my-action"; + + private GuardParams params; + + @Before + public void setUp() { + params = GuardParams.builder().onapName(ONAP_NAME).onapComponent(ONAP_COMP).onapInstance(ONAP_INST) + .action(MY_ACTION).clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build(); + } + + @Test + public void testValidate() { + assertTrue(params.validate(CONTAINER).isValid()); + + // validate one of the superclass fields + testValidateField("clientName", "null", bldr -> bldr.clientName(null)); + + // validate with mostly empty params + params = GuardParams.builder().clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build(); + assertTrue(params.validate(CONTAINER).isValid()); + } + + @Test + public void testBuilder_testToBuilder() { + assertEquals(CLIENT, params.getClientName()); + + assertEquals(ONAP_NAME, params.getOnapName()); + assertEquals(ONAP_COMP, params.getOnapComponent()); + assertEquals(ONAP_INST, params.getOnapInstance()); + assertEquals(MY_ACTION, params.getAction()); + + assertEquals(params, params.toBuilder().build()); + } + + private void testValidateField(String fieldName, String expected, + @SuppressWarnings("rawtypes") Function<GuardParamsBuilder, HttpParamsBuilder> makeInvalid) { + + // original params should be valid + ValidationResult result = params.validate(CONTAINER); + assertTrue(fieldName, result.isValid()); + + // make invalid params + result = makeInvalid.apply(params.toBuilder()).build().validate(CONTAINER); + assertFalse(fieldName, result.isValid()); + assertThat(result.getResult()).contains(fieldName).contains(expected); + } +} diff --git a/models-interactions/model-actors/actor.guard/src/test/resources/makeReqStd.json b/models-interactions/model-actors/actor.guard/src/test/resources/makeReqStd.json index 5437ae896..6ae886726 100644 --- a/models-interactions/model-actors/actor.guard/src/test/resources/makeReqStd.json +++ b/models-interactions/model-actors/actor.guard/src/test/resources/makeReqStd.json @@ -1,9 +1,9 @@ { - "ONAPName": "my-onap-name", "ONAPComponent": "my-onap-component", "ONAPInstance": "my-onap-instance", - "requestId": "abcdefghi", + "ONAPName": "my-onap-name", "action": "my-action", + "requestId": "abcdefghi", "resource": { "actor": "resource-actor", "operation": "resource-operation" |