2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actor.test;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import java.io.FileNotFoundException;
31 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
32 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
33 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
34 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
35 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
36 import org.onap.policy.common.parameters.BeanValidationResult;
37 import org.onap.policy.common.parameters.BeanValidator;
38 import org.onap.policy.common.parameters.annotations.NotNull;
39 import org.onap.policy.common.utils.coder.Coder;
40 import org.onap.policy.common.utils.coder.CoderException;
41 import org.onap.policy.common.utils.coder.StandardYamlCoder;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
44 import org.onap.policy.controlloop.actorserviceprovider.Operator;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * Superclass for various Actor tests.
51 public class BasicActor {
52 private static final Logger logger = LoggerFactory.getLogger(BasicActor.class);
53 private static final Coder yamlCoder = new StandardYamlCoder();
56 * Reads a YAML configuration file, configures the specified topics and HTTP clients,
57 * and then runs the specified actor through its paces: configure(), start(), stop(),
58 * and shutdown(). Finally, it destroys the topics and HTTP clients.
60 * @param actorName name of the actor to be tested.
61 * @param yamlConfigFile YAML configuration file name
62 * @throws IllegalArgumentException if an error occurs
64 protected void verifyActorService(String actorName, String yamlConfigFile) {
65 ActorService service = new ActorService() {};
67 // ensure the actor was loaded
68 assertNotNull(service.getActor(actorName));
71 MyConfig config = readConfig(yamlConfigFile);
74 startOtherServices(config);
76 // configure and verify
77 service.configure(config.getActors());
78 for (Operator operator : service.getActor(actorName).getOperators()) {
79 assertTrue(operator.isConfigured());
84 for (Operator operator : service.getActor(actorName).getOperators()) {
85 assertTrue(operator.isAlive());
90 for (Operator operator : service.getActor(actorName).getOperators()) {
91 assertFalse(operator.isAlive());
94 // shut down and verify
96 for (Operator operator : service.getActor(actorName).getOperators()) {
97 assertFalse(operator.isAlive());
100 } catch (HttpClientConfigException e) {
101 logger.error("failed to configure HTTP client(s) for actor: {}", actorName);
102 throw new IllegalArgumentException(e);
110 * Reads a YAML configuration from a file.
112 * @param yamlConfigFile YAML configuration file name
113 * @return the configuration that was read from the file
114 * @throws AssertionError if an error occurs
116 private MyConfig readConfig(String yamlConfigFile) {
118 var yaml = ResourceUtils.getResourceAsString(yamlConfigFile);
120 throw new FileNotFoundException(yamlConfigFile);
123 return yamlCoder.decode(yaml, MyConfig.class);
125 } catch (CoderException | FileNotFoundException e) {
126 logger.error("cannot decode YAML file {}", yamlConfigFile);
127 throw new IllegalArgumentException(e);
132 * Starts the Topic and HTTP clients.
134 * @param config configuration
135 * @throws HttpClientConfigException if an error occurs
137 private void startOtherServices(MyConfig config) throws HttpClientConfigException {
140 if (config.getHttpClients() != null) {
141 var factory = HttpClientFactoryInstance.getClientFactory();
142 for (BusTopicParams params : config.getHttpClients()) {
143 factory.build(params);
147 if (config.getTopics() != null) {
148 TopicEndpointManager.getManager().addTopics(config.getTopics());
153 * Stops the Topic and HTTP clients.
155 private void stopOtherServices() {
156 TopicEndpointManager.getManager().shutdown();
157 HttpClientFactoryInstance.getClientFactory().destroy();
161 public static class MyConfig {
162 private BusTopicParams[] httpClients;
163 private TopicParameterGroup topics;
166 private Map<String, Object> actors;
169 * Validates the config.
171 public void validate() {
172 BeanValidationResult result = new BeanValidator().validateTop(BasicActor.class.getSimpleName(), this);
173 if (topics != null) {
174 result.addResult(topics.validate());
176 if (!result.isValid()) {
177 throw new IllegalArgumentException(result.getResult());