2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 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.so;
24 import static org.mockito.Mockito.lenient;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.LinkedList;
29 import java.util.List;
31 import org.mockito.Mock;
32 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
33 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
34 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
35 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
36 import org.onap.policy.controlloop.actorserviceprovider.TargetType;
37 import org.onap.policy.controlloop.actorserviceprovider.Util;
38 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
40 import org.onap.policy.simulators.SoSimulatorJaxRs;
41 import org.onap.policy.so.SoRequest;
42 import org.onap.policy.so.SoRequestParameters;
43 import org.onap.policy.so.SoRequestReferences;
44 import org.onap.policy.so.SoRequestStatus;
45 import org.onap.policy.so.SoResponse;
48 * Superclass for various operator tests.
50 public abstract class BasicSoOperation extends BasicHttpOperation {
51 protected static final String[] IGNORE_FIELDS = {"RequestID", "subRequestID", "seconds", "nanos"};
53 public static final String MODEL_CUSTOM_ID = "my-model-customization-id";
54 public static final String MODEL_INVAR_ID = "my-model-invariant-id";
55 public static final String MODEL_NAME = "my-model-name";
56 public static final String MODEL_VERSION = "my-model-version";
57 public static final String MODEL_VERS_ID = "my-model-version-id";
58 public static final String SUBSCRIPTION_SVC_TYPE = "my-subscription-service-type";
59 public static final String MY_PATH = "my-path";
60 public static final String POLL_PATH = "my-poll-path/";
61 public static final int MAX_POLLS = 3;
62 public static final int POLL_WAIT_SEC = 20;
63 public static final Integer VF_COUNT = 10;
66 protected HttpPollingConfig config;
68 protected TargetType targetType;
69 protected Map<String, String> targetEntities = new HashMap<>();
70 protected SoResponse response;
73 * Constructs the object using a default actor and operation name.
75 public BasicSoOperation() {
80 * Constructs the object.
82 * @param actor actor name
83 * @param operation operation name
85 public BasicSoOperation(String actor, String operation) {
86 super(actor, operation);
90 * Starts the simulator.
92 protected static void initBeforeClass() throws Exception {
93 org.onap.policy.simulators.Util.buildSoSim();
95 BusTopicParams clientParams = BusTopicParams.builder().clientName(MY_CLIENT).basePath("").hostname("localhost")
96 .managed(true).port(org.onap.policy.simulators.Util.SOSIM_SERVER_PORT)
98 HttpClientFactoryInstance.getClientFactory().build(clientParams);
100 SoSimulatorJaxRs.setRequirePolling(true);
103 protected static void destroyAfterClass() {
104 SoSimulatorJaxRs.setRequirePolling(false);
105 HttpClientFactoryInstance.getClientFactory().destroy();
106 HttpServletServerFactoryInstance.getServerFactory().destroy();
110 * Initializes mocks and sets up.
112 public void setUp() throws Exception {
115 response = new SoResponse();
117 SoRequest request = new SoRequest();
118 response.setRequest(request);
120 SoRequestStatus status = new SoRequestStatus();
121 request.setRequestStatus(status);
122 status.setRequestState(SoOperation.COMPLETE);
124 SoRequestReferences ref = new SoRequestReferences();
125 response.setRequestReferences(ref);
126 ref.setRequestId(REQ_ID.toString());
128 lenient().when(rawResponse.getStatus()).thenReturn(200);
129 lenient().when(rawResponse.readEntity(String.class)).thenReturn(coder.encode(response));
135 protected void initConfig() {
137 lenient().when(config.getClient()).thenReturn(client);
138 lenient().when(config.getPath()).thenReturn(MY_PATH);
139 lenient().when(config.getMaxPolls()).thenReturn(MAX_POLLS);
140 lenient().when(config.getPollPath()).thenReturn(POLL_PATH);
141 lenient().when(config.getPollWaitSec()).thenReturn(POLL_WAIT_SEC);
145 protected void makeContext() {
148 targetType = TargetType.VNF;
150 targetEntities = new HashMap<>();
151 targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_CUSTOMIZATION_ID, MODEL_CUSTOM_ID);
152 targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_INVARIANT_ID, MODEL_INVAR_ID);
153 targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_NAME, MODEL_NAME);
154 targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_VERSION, MODEL_VERSION);
155 targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_VERSION_ID, MODEL_VERS_ID);
157 params = params.toBuilder().targetType(targetType).targetEntityIds(targetEntities).build();
161 protected Map<String, Object> makePayload() {
162 Map<String, Object> payload = new HashMap<>();
164 // request parameters
165 SoRequestParameters reqParams = new SoRequestParameters();
166 reqParams.setSubscriptionServiceType(SUBSCRIPTION_SVC_TYPE);
167 payload.put(SoOperation.REQ_PARAM_NM, Util.translate("", reqParams, String.class));
170 List<Map<String, String>> config = new LinkedList<>();
171 config.add(Collections.emptyMap());
172 payload.put(SoOperation.CONFIG_PARAM_NM, Util.translate("", config, String.class));