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