diff options
Diffstat (limited to 'models-interactions/model-actors/actor.test/src')
11 files changed, 499 insertions, 1 deletions
diff --git a/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicActor.java b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicActor.java new file mode 100644 index 000000000..1b11d0d32 --- /dev/null +++ b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicActor.java @@ -0,0 +1,182 @@ +/*- + * ============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.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.FileNotFoundException; +import java.util.Map; +import lombok.Getter; +import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager; +import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; +import org.onap.policy.common.endpoints.http.client.HttpClientConfigException; +import org.onap.policy.common.endpoints.http.client.HttpClientFactory; +import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; +import org.onap.policy.common.endpoints.parameters.TopicParameterGroup; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.BeanValidator; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardYamlCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.controlloop.actorserviceprovider.ActorService; +import org.onap.policy.controlloop.actorserviceprovider.Operator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Superclass for various Actor tests. + */ +public class BasicActor { + private static final Logger logger = LoggerFactory.getLogger(BasicActor.class); + private static final Coder yamlCoder = new StandardYamlCoder(); + + /** + * Reads a YAML configuration file, configures the specified topics and HTTP clients, + * and then runs the specified actor through its paces: configure(), start(), stop(), + * and shutdown(). Finally, it destroys the topics and HTTP clients. + * + * @param actorName name of the actor to be tested. + * @param yamlConfigFile YAML configuration file name + * @throws IllegalArgumentException if an error occurs + */ + protected void verifyActorService(String actorName, String yamlConfigFile) { + ActorService service = new ActorService() {}; + + // ensure the actor was loaded + assertNotNull(service.getActor(actorName)); + + try { + MyConfig config = readConfig(yamlConfigFile); + config.validate(); + + startOtherServices(config); + + // configure and verify + service.configure(config.getActors()); + for (Operator operator : service.getActor(actorName).getOperators()) { + assertTrue(operator.isConfigured()); + } + + // start and verify + service.start(); + for (Operator operator : service.getActor(actorName).getOperators()) { + assertTrue(operator.isAlive()); + } + + // stop and verify + service.stop(); + for (Operator operator : service.getActor(actorName).getOperators()) { + assertFalse(operator.isAlive()); + } + + // shut down and verify + service.shutdown(); + for (Operator operator : service.getActor(actorName).getOperators()) { + assertFalse(operator.isAlive()); + } + + } catch (HttpClientConfigException e) { + logger.error("failed to configure HTTP client(s) for actor: {}", actorName); + throw new IllegalArgumentException(e); + + } finally { + stopOtherServices(); + } + } + + /** + * Reads a YAML configuration from a file. + * + * @param yamlConfigFile YAML configuration file name + * @return the configuration that was read from the file + * @throws AssertionError if an error occurs + * @throws CoderException if an error occurs + */ + private MyConfig readConfig(String yamlConfigFile) { + try { + String yaml = ResourceUtils.getResourceAsString(yamlConfigFile); + if (yaml == null) { + throw new FileNotFoundException(yamlConfigFile); + } + + return yamlCoder.decode(yaml, MyConfig.class); + + } catch (CoderException | FileNotFoundException e) { + logger.error("cannot decode YAML file {}", yamlConfigFile); + throw new IllegalArgumentException(e); + } + } + + /** + * Starts the Topic and HTTP clients. + * + * @param config configuration + * @throws HttpClientConfigException if an error occurs + */ + private void startOtherServices(MyConfig config) throws HttpClientConfigException { + stopOtherServices(); + + if (config.getHttpClients() != null) { + HttpClientFactory factory = HttpClientFactoryInstance.getClientFactory(); + for (BusTopicParams params : config.getHttpClients()) { + factory.build(params); + } + } + + if (config.getTopics() != null) { + TopicEndpointManager.getManager().addTopics(config.getTopics()); + } + } + + /** + * Stops the Topic and HTTP clients. + */ + private void stopOtherServices() { + TopicEndpointManager.getManager().shutdown(); + HttpClientFactoryInstance.getClientFactory().destroy(); + } + + @Getter + public static class MyConfig { + private BusTopicParams[] httpClients; + private TopicParameterGroup topics; + + @NotNull + private Map<String, Map<String, Object>> actors; + + /** + * Validates the config. + */ + public void validate() { + BeanValidationResult result = new BeanValidator().validateTop(BasicActor.class.getSimpleName(), this); + if (topics != null) { + result.addResult(topics.validate()); + } + if (!result.isValid()) { + throw new IllegalArgumentException(result.getResult()); + } + } + } +} diff --git a/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java index f027c1c18..989f6a7c3 100644 --- a/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java +++ b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java @@ -21,6 +21,8 @@ package org.onap.policy.controlloop.actor.test; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; import java.util.Map; import java.util.TreeMap; @@ -29,6 +31,8 @@ import java.util.concurrent.CompletableFuture; import javax.ws.rs.core.Response; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.onap.policy.aai.AaiConstants; +import org.onap.policy.aai.AaiCqResponse; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; @@ -36,9 +40,14 @@ import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.common.utils.time.PseudoExecutor; import org.onap.policy.controlloop.VirtualControlLoopEvent; import org.onap.policy.controlloop.actorserviceprovider.ActorService; +import org.onap.policy.controlloop.actorserviceprovider.Operation; import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.Operator; import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; +import org.onap.policy.controlloop.actorserviceprovider.impl.OperationPartial; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.spi.Actor; +import org.onap.policy.controlloop.policy.PolicyResult; /** * Superclass for various Operation tests. @@ -55,7 +64,22 @@ public class BasicOperation { @Mock protected ActorService service; + @Mock + protected Actor guardActor; + @Mock + protected Operator guardOperator; + @Mock + protected Operation guardOperation; + @Mock + protected Actor cqActor; + @Mock + protected Operator cqOperator; + @Mock + protected Operation cqOperation; + @Mock + protected AaiCqResponse cqResponse; + protected CompletableFuture<OperationOutcome> cqFuture; protected CompletableFuture<Response> future; protected ControlLoopOperationParams params; protected Map<String, String> enrichment; @@ -89,12 +113,28 @@ public class BasicOperation { public void setUpBasic() { MockitoAnnotations.initMocks(this); + cqFuture = new CompletableFuture<>(); future = new CompletableFuture<>(); executor = new PseudoExecutor(); makeContext(); + when(service.getActor(OperationPartial.GUARD_ACTOR_NAME)).thenReturn(guardActor); + when(guardActor.getOperator(OperationPartial.GUARD_OPERATION_NAME)).thenReturn(guardOperator); + when(guardOperator.buildOperation(any())).thenReturn(guardOperation); + + outcome = params.makeOutcome(); + outcome.setResult(PolicyResult.SUCCESS); + when(guardOperation.start()).thenReturn(CompletableFuture.completedFuture(outcome)); + + when(service.getActor(AaiConstants.ACTOR_NAME)).thenReturn(cqActor); + when(cqActor.getOperator("CustomQuery")).thenReturn(cqOperator); + when(cqOperator.buildOperation(any())).thenReturn(cqOperation); + + when(cqOperation.start()).thenReturn(cqFuture); + + // get a fresh outcome outcome = params.makeOutcome(); } @@ -133,7 +173,7 @@ public class BasicOperation { * * @return payload data */ - protected Map<String, String> makePayload() { + protected Map<String, Object> makePayload() { return null; } @@ -162,4 +202,16 @@ public class BasicOperation { assertEquals(expected, json); } + + /** + * Provides a response to a custom query. + * + * @param cq response to provide + */ + protected void provideCqResponse(AaiCqResponse cq) { + context.setProperty(AaiCqResponse.CONTEXT_KEY, cq); + OperationOutcome outcome2 = params.makeOutcome(); + outcome2.setResult(PolicyResult.SUCCESS); + cqFuture.complete(outcome2); + } } diff --git a/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/BasicActorTest.java b/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/BasicActorTest.java new file mode 100644 index 000000000..ef9b37ba6 --- /dev/null +++ b/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/BasicActorTest.java @@ -0,0 +1,47 @@ +/*- + * ============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.test; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.Test; + +public class BasicActorTest extends BasicActor { + + @Test + public void testVerifyActorService_testStartOtherServices_testStopOtherServices() { + // mostly empty service + verifyActorService(DummyActor.NAME, "service.yaml"); + + // service with Topics and HTTP Clients + verifyActorService(DummyActor.NAME, "serviceFull.yaml"); + + assertThatIllegalArgumentException() + .isThrownBy(() -> verifyActorService(DummyActor.NAME, "serviceInvalidHttp.yaml")); + + assertThatIllegalArgumentException() + .isThrownBy(() -> verifyActorService(DummyActor.NAME, "serviceMissingActors.yaml")); + + // config file not found + assertThatThrownBy(() -> verifyActorService(DummyActor.NAME, "file-not-found.yaml")); + } +} diff --git a/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/BasicOperationTest.java b/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/BasicOperationTest.java index 60bb00892..5eb35e9d0 100644 --- a/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/BasicOperationTest.java +++ b/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/BasicOperationTest.java @@ -27,11 +27,16 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import java.util.Map; +import java.util.concurrent.CompletableFuture; import org.junit.Before; import org.junit.Test; +import org.onap.policy.aai.AaiCqResponse; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.actorserviceprovider.impl.OperationPartial; +import org.onap.policy.controlloop.policy.PolicyResult; public class BasicOperationTest { private static final String ACTOR = "my-actor"; @@ -65,6 +70,12 @@ public class BasicOperationTest { assertNotNull(oper.context); assertNotNull(oper.outcome); assertNotNull(oper.executor); + assertNotNull(oper.guardOperation); + + CompletableFuture<OperationOutcome> future = oper.service.getActor(OperationPartial.GUARD_ACTOR_NAME) + .getOperator(OperationPartial.GUARD_OPERATION_NAME).buildOperation(null).start(); + assertTrue(future.isDone()); + assertEquals(PolicyResult.SUCCESS, future.get().getResult()); } @Test @@ -97,4 +108,14 @@ public class BasicOperationTest { Map<String, Object> map = Util.translateToMap("", ResourceUtils.getResourceAsString("actual.json")); oper.verifyRequest("expected.json", map, "svc-request-id", "vnf-id"); } + + @Test + public void testProvideCqResponse() throws Exception { + AaiCqResponse cq = new AaiCqResponse("{}"); + oper.provideCqResponse(cq); + + assertSame(cq, oper.context.getProperty(AaiCqResponse.CONTEXT_KEY)); + assertTrue(oper.cqFuture.isDone()); + assertEquals(PolicyResult.SUCCESS, oper.cqFuture.get().getResult()); + } } diff --git a/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/DummyActor.java b/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/DummyActor.java new file mode 100644 index 000000000..c862a18b7 --- /dev/null +++ b/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/DummyActor.java @@ -0,0 +1,39 @@ +/*- + * ============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.test; + +import org.onap.policy.controlloop.actorserviceprovider.impl.ActorImpl; + +public class DummyActor extends ActorImpl { + public static final String NAME = "MyActor"; + public static final String MY_OPERATION1 = "MyOperationA"; + public static final String MY_OPERATION2 = "MyOperationB"; + + /** + * Constructs the object. + */ + public DummyActor() { + super(NAME); + + addOperator(new DummyOperator(NAME, MY_OPERATION1)); + addOperator(new DummyOperator(NAME, MY_OPERATION2)); + } +} diff --git a/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/DummyOperator.java b/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/DummyOperator.java new file mode 100644 index 000000000..efd3b6500 --- /dev/null +++ b/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/DummyOperator.java @@ -0,0 +1,37 @@ +/*- + * ============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.test; + +import org.onap.policy.controlloop.actorserviceprovider.Operation; +import org.onap.policy.controlloop.actorserviceprovider.impl.OperatorPartial; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; + +public class DummyOperator extends OperatorPartial { + + public DummyOperator(String actorName, String name) { + super(actorName, name); + } + + @Override + public Operation buildOperation(ControlLoopOperationParams params) { + return null; + } +} diff --git a/models-interactions/model-actors/actor.test/src/test/resources/META-INF/services/org.onap.policy.controlloop.actorserviceprovider.spi.Actor b/models-interactions/model-actors/actor.test/src/test/resources/META-INF/services/org.onap.policy.controlloop.actorserviceprovider.spi.Actor new file mode 100644 index 000000000..f9c3bddfd --- /dev/null +++ b/models-interactions/model-actors/actor.test/src/test/resources/META-INF/services/org.onap.policy.controlloop.actorserviceprovider.spi.Actor @@ -0,0 +1 @@ +org.onap.policy.controlloop.actor.test.DummyActor
\ No newline at end of file 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 new file mode 100644 index 000000000..49de7da51 --- /dev/null +++ b/models-interactions/model-actors/actor.test/src/test/resources/service.yaml @@ -0,0 +1,23 @@ +# +# ============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======================================================== +# +actors: + MyActor: + MyOperationA: {} + MyOperationB: {}
\ No newline at end of file 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 new file mode 100644 index 000000000..9818215be --- /dev/null +++ b/models-interactions/model-actors/actor.test/src/test/resources/serviceFull.yaml @@ -0,0 +1,42 @@ +# +# ============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======================================================== +# +httpClients: +- clientName: my-client + hostname: localhost + port: 80 + basePath: base-url + managed: true +topics: + topicSources: + - topicCommInfrastructure: NOOP + topic: my-source + servers: + - localhost + managed: true + topicSinks: + - topicCommInfrastructure: NOOP + topic: my-sink + servers: + - localhost + managed: true +actors: + MyActor: + MyOperationA: {} + MyOperationB: {}
\ No newline at end of file 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 new file mode 100644 index 000000000..b31e8e81e --- /dev/null +++ b/models-interactions/model-actors/actor.test/src/test/resources/serviceInvalidHttp.yaml @@ -0,0 +1,29 @@ +# +# ============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======================================================== +# +httpClients: +- clientName: my-client + hostname: localhost + port: 80 + serializationProvider: unknown.class.name + managed: true +actors: + MyActor: + MyOperationA: {} + MyOperationB: {}
\ No newline at end of file diff --git a/models-interactions/model-actors/actor.test/src/test/resources/serviceMissingActors.yaml b/models-interactions/model-actors/actor.test/src/test/resources/serviceMissingActors.yaml new file mode 100644 index 000000000..0eec84ea4 --- /dev/null +++ b/models-interactions/model-actors/actor.test/src/test/resources/serviceMissingActors.yaml @@ -0,0 +1,25 @@ +# +# ============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======================================================== +# +httpClients: +- clientName: my-client + hostname: localhost + port: 80 + basePath: base-url + managed: true
\ No newline at end of file |