d2e2872d5b65bc4f9bc7601d9aefed67c10e8750
[policy/models.git] / models-interactions / model-actors / actor.so / src / test / java / org / onap / policy / controlloop / actor / so / BasicSoOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.so;
22
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.when;
25
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31 import org.mockito.Mock;
32 import org.onap.policy.aai.AaiCqResponse;
33 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
34 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
35 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
36 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
37 import org.onap.policy.controlloop.actorserviceprovider.TargetType;
38 import org.onap.policy.controlloop.actorserviceprovider.Util;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
41 import org.onap.policy.simulators.SoSimulatorJaxRs;
42 import org.onap.policy.so.SoRequest;
43 import org.onap.policy.so.SoRequestParameters;
44 import org.onap.policy.so.SoRequestReferences;
45 import org.onap.policy.so.SoRequestStatus;
46 import org.onap.policy.so.SoResponse;
47
48 /**
49  * Superclass for various operator tests.
50  */
51 public abstract class BasicSoOperation extends BasicHttpOperation {
52     protected static final String[] IGNORE_FIELDS = {"RequestID", "subRequestID", "seconds", "nanos"};
53
54     public static final String MODEL_CUSTOM_ID = "my-model-customization-id";
55     public static final String MODEL_INVAR_ID = "my-model-invariant-id";
56     public static final String MODEL_NAME = "my-model-name";
57     public static final String MODEL_VERSION = "my-model-version";
58     public static final String MODEL_VERS_ID = "my-model-version-id";
59     public static final String SUBSCRIPTION_SVC_TYPE = "my-subscription-service-type";
60     public static final String MY_PATH = "my-path";
61     public static final String POLL_PATH = "my-poll-path/";
62     public static final int MAX_POLLS = 3;
63     public static final int POLL_WAIT_SEC = 20;
64     public static final Integer VF_COUNT = 10;
65
66     @Mock
67     protected HttpPollingConfig config;
68
69     protected TargetType targetType;
70     protected Map<String, String> targetEntities = new HashMap<>();
71     protected SoResponse response;
72
73     /**
74      * Constructs the object using a default actor and operation name.
75      */
76     public BasicSoOperation() {
77         super();
78     }
79
80     /**
81      * Constructs the object.
82      *
83      * @param actor actor name
84      * @param operation operation name
85      */
86     public BasicSoOperation(String actor, String operation) {
87         super(actor, operation);
88     }
89
90     /**
91      * Starts the simulator.
92      */
93     protected static void initBeforeClass() throws Exception {
94         org.onap.policy.simulators.Util.buildSoSim();
95
96         BusTopicParams clientParams = BusTopicParams.builder().clientName(MY_CLIENT).basePath("").hostname("localhost")
97                         .managed(true).port(org.onap.policy.simulators.Util.SOSIM_SERVER_PORT)
98                         .build();
99         HttpClientFactoryInstance.getClientFactory().build(clientParams);
100
101         SoSimulatorJaxRs.setRequirePolling(true);
102     }
103
104     protected static void destroyAfterClass() {
105         SoSimulatorJaxRs.setRequirePolling(false);
106         HttpClientFactoryInstance.getClientFactory().destroy();
107         HttpServletServerFactoryInstance.getServerFactory().destroy();
108     }
109
110     /**
111      * Initializes mocks and sets up.
112      */
113     public void setUp() throws Exception {
114         super.setUpBasic();
115
116         response = new SoResponse();
117
118         SoRequest request = new SoRequest();
119         response.setRequest(request);
120
121         SoRequestStatus status = new SoRequestStatus();
122         request.setRequestStatus(status);
123         status.setRequestState(SoOperation.COMPLETE);
124
125         SoRequestReferences ref = new SoRequestReferences();
126         response.setRequestReferences(ref);
127         ref.setRequestId(REQ_ID.toString());
128
129         when(rawResponse.getStatus()).thenReturn(200);
130         when(rawResponse.readEntity(String.class)).thenReturn(coder.encode(response));
131
132         initConfig();
133     }
134
135     @Override
136     protected void initConfig() {
137         super.initConfig();
138         when(config.getClient()).thenReturn(client);
139         when(config.getPath()).thenReturn(MY_PATH);
140         when(config.getMaxPolls()).thenReturn(MAX_POLLS);
141         when(config.getPollPath()).thenReturn(POLL_PATH);
142         when(config.getPollWaitSec()).thenReturn(POLL_WAIT_SEC);
143     }
144
145     @Override
146     protected void makeContext() {
147         super.makeContext();
148
149         targetType = TargetType.VNF;
150
151         targetEntities = new HashMap<>();
152         targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_CUSTOMIZATION_ID, MODEL_CUSTOM_ID);
153         targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_INVARIANT_ID, MODEL_INVAR_ID);
154         targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_NAME, MODEL_NAME);
155         targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_VERSION, MODEL_VERSION);
156         targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_VERSION_ID, MODEL_VERS_ID);
157
158         params = params.toBuilder().targetType(targetType).targetEntityIds(targetEntities).build();
159     }
160
161     @Override
162     protected Map<String, Object> makePayload() {
163         Map<String, Object> payload = new HashMap<>();
164
165         // request parameters
166         SoRequestParameters reqParams = new SoRequestParameters();
167         reqParams.setSubscriptionServiceType(SUBSCRIPTION_SVC_TYPE);
168         payload.put(SoOperation.REQ_PARAM_NM, Util.translate("", reqParams, String.class));
169
170         // config parameters
171         List<Map<String, String>> config = new LinkedList<>();
172         config.add(Collections.emptyMap());
173         payload.put(SoOperation.CONFIG_PARAM_NM, Util.translate("", config, String.class));
174
175         return payload;
176     }
177
178     protected AaiCqResponse makeCqResponse() {
179         when(cqResponse.getVfModuleCount(any(), any(), any())).thenReturn(VF_COUNT);
180         return cqResponse;
181     }
182 }