Merge "Add SO actor"
[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.controlloop.actor.test.BasicHttpOperation;
32 import org.onap.policy.controlloop.actorserviceprovider.Util;
33 import org.onap.policy.controlloop.policy.Target;
34 import org.onap.policy.so.SoRequest;
35 import org.onap.policy.so.SoRequestParameters;
36 import org.onap.policy.so.SoRequestReferences;
37 import org.onap.policy.so.SoRequestStatus;
38 import org.onap.policy.so.SoResponse;
39
40 /**
41  * Superclass for various operator tests.
42  */
43 public abstract class BasicSoOperation extends BasicHttpOperation<SoRequest> {
44     protected static final String[] IGNORE_FIELDS = {"RequestID", "subRequestID", "seconds", "nanos"};
45
46     public static final String MODEL_CUSTOM_ID = "my-model-customization-id";
47     public static final String MODEL_INVAR_ID = "my-model-invariant-id";
48     public static final String MODEL_NAME = "my-model-name";
49     public static final String MODEL_VERSION = "my-model-version";
50     public static final String MODEL_VERS_ID = "my-model-version-id";
51     public static final String SUBSCRIPTION_SVC_TYPE = "my-subscription-service-type";
52     public static final String PATH_GET = "my-path-get/";
53     public static final int MAX_GETS = 3;
54     public static final int WAIT_SEC_GETS = 20;
55
56     @Mock
57     protected SoOperator soOperator;
58
59     protected Target target;
60     protected SoResponse response;
61
62     /**
63      * Constructs the object using a default actor and operation name.
64      */
65     public BasicSoOperation() {
66         super();
67     }
68
69     /**
70      * Constructs the object.
71      *
72      * @param actor actor name
73      * @param operation operation name
74      */
75     public BasicSoOperation(String actor, String operation) {
76         super(actor, operation);
77     }
78
79     /**
80      * Initializes mocks and sets up.
81      */
82     public void setUp() throws Exception {
83         super.setUp();
84
85         response = new SoResponse();
86
87         SoRequest request = new SoRequest();
88         response.setRequest(request);
89
90         SoRequestStatus status = new SoRequestStatus();
91         request.setRequestStatus(status);
92         status.setRequestState(SoOperation.COMPLETE);
93
94         SoRequestReferences ref = new SoRequestReferences();
95         response.setRequestReferences(ref);
96         ref.setRequestId(REQ_ID.toString());
97
98         when(rawResponse.getStatus()).thenReturn(200);
99         when(rawResponse.readEntity(String.class)).thenReturn(coder.encode(response));
100
101         operator = soOperator;
102
103         initOperator();
104     }
105
106     @Override
107     protected void initOperator() {
108         super.initOperator();
109         when(soOperator.getMaxGets()).thenReturn(MAX_GETS);
110         when(soOperator.getPathGet()).thenReturn(PATH_GET);
111         when(soOperator.getWaitSecGet()).thenReturn(WAIT_SEC_GETS);
112     }
113
114     @Override
115     protected void makeContext() {
116         super.makeContext();
117
118         target = new Target();
119         target.setModelCustomizationId(MODEL_CUSTOM_ID);
120         target.setModelInvariantId(MODEL_INVAR_ID);
121         target.setModelName(MODEL_NAME);
122         target.setModelVersion(MODEL_VERSION);
123         target.setModelVersionId(MODEL_VERS_ID);
124
125         params = params.toBuilder().target(target).build();
126     }
127
128     @Override
129     protected Map<String, String> makePayload() {
130         Map<String, String> payload = new HashMap<>();
131
132         // request parameters
133         SoRequestParameters reqParams = new SoRequestParameters();
134         reqParams.setSubscriptionServiceType(SUBSCRIPTION_SVC_TYPE);
135         payload.put(SoOperation.REQ_PARAM_NM, Util.translate("", reqParams, String.class));
136
137         // config parameters
138         List<Map<String, String>> config = new LinkedList<>();
139         config.add(Collections.emptyMap());
140         payload.put(SoOperation.CONFIG_PARAM_NM, Util.translate("", config, String.class));
141
142         return payload;
143     }
144 }