aboutsummaryrefslogtreecommitdiffstats
path: root/models-interactions/model-actors/actor.test/src/main
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2020-02-20 19:08:55 -0500
committerJim Hahn <jrh3@att.com>2020-02-21 20:33:06 -0500
commit7f69c5ca0a6f6018166f8fee3e811edf4dee1eb8 (patch)
treee57ab3105adbe0752b30e0e0a60ea43588cfb384 /models-interactions/model-actors/actor.test/src/main
parent3c8c5b2994e3f132385f0341283bc271e13cdb25 (diff)
Change payload to Map<String,Object> so it's more versatile
This was supposed to be two separate commits, but I goofed something. Added guard query to Operation superclass. Modified VfModuleCreate to store the VF count, pass it to the guard, and bump it once the create completes successfully. Added code to check Actors for proper plug-in to ActorService. Renamed "operation" property to "operations", to be more consistent with other parameters (e.g., TopicParameterGroup). The META-INF/services files for the actors had mixed case, which did not match the package name of the Actor class, preventing the ServiceLoader from recognizing them. Also modified the ActorService to skip any that cannot actually be loaded, for whatever reason (e.g., not in the classpath). Issue-ID: POLICY-1625 Signed-off-by: Jim Hahn <jrh3@att.com> Change-Id: Ifa97744543f2866cc553138ec5ec644b033de780
Diffstat (limited to 'models-interactions/model-actors/actor.test/src/main')
-rw-r--r--models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicActor.java182
-rw-r--r--models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java54
2 files changed, 235 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);
+ }
}