diff options
author | Jim Hahn <jrh3@att.com> | 2020-02-20 09:40:14 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2020-02-20 16:29:08 -0500 |
commit | 467247c7970f9ae83464d78929ed970bbf03c593 (patch) | |
tree | 54b78e6505b664c3a61e532bf6f7c276307d6591 /models-interactions/model-actors/actor.guard/src/test | |
parent | f6da7772d9dc01ce4ddd21a55b0f1c5fb7ad814f (diff) |
More actor clean-up
Currently, Operator classes refer to Operation classes, and
vice versa, creating a dependency cycle. In addition, there is
a slight problem in that if an operator is reconfigured, any
running operation may get inconsistent configuration data.
Modified the code to create Config objects that are passed to
the operation, instead of passing the Operator to the operations.
This solved both issues.
Replaceed makeOperator() with constructors.
Added parameter type to HttpActors.
Modified guard to get "ONAP" properties from its configuration,
as a default.
Changed setUp() to setUpBasic(), so "throws Exception" could be
removed, thus resolving a sonar issue.
Issue-ID: POLICY-1625
Signed-off-by: Jim Hahn <jrh3@att.com>
Change-Id: I21eb8798acfbc636ff1bd8741b21c7278365b6e4
Diffstat (limited to 'models-interactions/model-actors/actor.guard/src/test')
6 files changed, 407 insertions, 10 deletions
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" |