4b05daa23d78f36ae8e618212b6315f61f2d3122
[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.Mockito.when;
24
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Map;
30 import org.mockito.Mock;
31 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
32 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
33 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
34 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
35 import org.onap.policy.controlloop.actorserviceprovider.TargetType;
36 import org.onap.policy.controlloop.actorserviceprovider.Util;
37 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
38 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
39 import org.onap.policy.simulators.SoSimulatorJaxRs;
40 import org.onap.policy.so.SoRequest;
41 import org.onap.policy.so.SoRequestParameters;
42 import org.onap.policy.so.SoRequestReferences;
43 import org.onap.policy.so.SoRequestStatus;
44 import org.onap.policy.so.SoResponse;
45
46 /**
47  * Superclass for various operator tests.
48  */
49 public abstract class BasicSoOperation extends BasicHttpOperation {
50     protected static final String[] IGNORE_FIELDS = {"RequestID", "subRequestID", "seconds", "nanos"};
51
52     public static final String MODEL_CUSTOM_ID = "my-model-customization-id";
53     public static final String MODEL_INVAR_ID = "my-model-invariant-id";
54     public static final String MODEL_NAME = "my-model-name";
55     public static final String MODEL_VERSION = "my-model-version";
56     public static final String MODEL_VERS_ID = "my-model-version-id";
57     public static final String SUBSCRIPTION_SVC_TYPE = "my-subscription-service-type";
58     public static final String MY_PATH = "my-path";
59     public static final String POLL_PATH = "my-poll-path/";
60     public static final int MAX_POLLS = 3;
61     public static final int POLL_WAIT_SEC = 20;
62     public static final Integer VF_COUNT = 10;
63
64     @Mock
65     protected HttpPollingConfig config;
66
67     protected TargetType targetType;
68     protected Map<String, String> targetEntities = new HashMap<>();
69     protected SoResponse response;
70
71     /**
72      * Constructs the object using a default actor and operation name.
73      */
74     public BasicSoOperation() {
75         super();
76     }
77
78     /**
79      * Constructs the object.
80      *
81      * @param actor actor name
82      * @param operation operation name
83      */
84     public BasicSoOperation(String actor, String operation) {
85         super(actor, operation);
86     }
87
88     /**
89      * Starts the simulator.
90      */
91     protected static void initBeforeClass() throws Exception {
92         org.onap.policy.simulators.Util.buildSoSim();
93
94         BusTopicParams clientParams = BusTopicParams.builder().clientName(MY_CLIENT).basePath("").hostname("localhost")
95                         .managed(true).port(org.onap.policy.simulators.Util.SOSIM_SERVER_PORT)
96                         .build();
97         HttpClientFactoryInstance.getClientFactory().build(clientParams);
98
99         SoSimulatorJaxRs.setRequirePolling(true);
100     }
101
102     protected static void destroyAfterClass() {
103         SoSimulatorJaxRs.setRequirePolling(false);
104         HttpClientFactoryInstance.getClientFactory().destroy();
105         HttpServletServerFactoryInstance.getServerFactory().destroy();
106     }
107
108     /**
109      * Initializes mocks and sets up.
110      */
111     public void setUp() throws Exception {
112         super.setUpBasic();
113
114         response = new SoResponse();
115
116         SoRequest request = new SoRequest();
117         response.setRequest(request);
118
119         SoRequestStatus status = new SoRequestStatus();
120         request.setRequestStatus(status);
121         status.setRequestState(SoOperation.COMPLETE);
122
123         SoRequestReferences ref = new SoRequestReferences();
124         response.setRequestReferences(ref);
125         ref.setRequestId(REQ_ID.toString());
126
127         when(rawResponse.getStatus()).thenReturn(200);
128         when(rawResponse.readEntity(String.class)).thenReturn(coder.encode(response));
129
130         initConfig();
131     }
132
133     @Override
134     protected void initConfig() {
135         super.initConfig();
136         when(config.getClient()).thenReturn(client);
137         when(config.getPath()).thenReturn(MY_PATH);
138         when(config.getMaxPolls()).thenReturn(MAX_POLLS);
139         when(config.getPollPath()).thenReturn(POLL_PATH);
140         when(config.getPollWaitSec()).thenReturn(POLL_WAIT_SEC);
141     }
142
143     @Override
144     protected void makeContext() {
145         super.makeContext();
146
147         targetType = TargetType.VNF;
148
149         targetEntities = new HashMap<>();
150         targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_CUSTOMIZATION_ID, MODEL_CUSTOM_ID);
151         targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_INVARIANT_ID, MODEL_INVAR_ID);
152         targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_NAME, MODEL_NAME);
153         targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_VERSION, MODEL_VERSION);
154         targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_VERSION_ID, MODEL_VERS_ID);
155
156         params = params.toBuilder().targetType(targetType).targetEntityIds(targetEntities).build();
157     }
158
159     @Override
160     protected Map<String, Object> makePayload() {
161         Map<String, Object> payload = new HashMap<>();
162
163         // request parameters
164         SoRequestParameters reqParams = new SoRequestParameters();
165         reqParams.setSubscriptionServiceType(SUBSCRIPTION_SVC_TYPE);
166         payload.put(SoOperation.REQ_PARAM_NM, Util.translate("", reqParams, String.class));
167
168         // config parameters
169         List<Map<String, String>> config = new LinkedList<>();
170         config.add(Collections.emptyMap());
171         payload.put(SoOperation.CONFIG_PARAM_NM, Util.translate("", config, String.class));
172
173         return payload;
174     }
175 }