7654e005394d084d6d270874fef49aa35fb52bd7
[policy/models.git] / models-interactions / model-simulators / src / test / java / org / onap / policy / simulators / SoSimulatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * simulators
4  * ================================================================================
5  * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019-2020 Nordix Foundation.
7  * Modifications Copyright (C) 2020 Wipro Limited.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.simulators;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
30
31 import java.util.HashMap;
32 import java.util.UUID;
33 import org.apache.commons.lang3.tuple.Pair;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
38 import org.onap.policy.rest.RestManager;
39 import org.onap.policy.so.SoCloudConfiguration;
40 import org.onap.policy.so.SoModelInfo;
41 import org.onap.policy.so.SoRelatedInstance;
42 import org.onap.policy.so.SoRelatedInstanceListElement;
43 import org.onap.policy.so.SoRequest;
44 import org.onap.policy.so.SoRequest3gpp;
45 import org.onap.policy.so.SoRequestDetails;
46 import org.onap.policy.so.SoRequestInfo;
47 import org.onap.policy.so.SoRequestParameters;
48 import org.onap.policy.so.util.Serialization;
49
50 public class SoSimulatorTest {
51
52     /**
53      * Set up test class.
54      */
55     @BeforeClass
56     public static void setUpSimulator() {
57         try {
58             Util.buildSoSim();
59         } catch (final Exception e) {
60             fail(e.getMessage());
61         }
62     }
63
64     @AfterClass
65     public static void tearDownSimulator() {
66         HttpServletServerFactoryInstance.getServerFactory().destroy();
67         SoSimulatorJaxRs.setRequirePolling(false);
68     }
69
70     /**
71      * Create dummy SO request for TestResponse() junit.
72      */
73     private SoRequest createTestRequest() {
74
75         // Construct SO Request
76         final SoRequest request = new SoRequest();
77         request.setRequestId(UUID.randomUUID());
78         request.setRequestDetails(new SoRequestDetails());
79         request.getRequestDetails().setModelInfo(new SoModelInfo());
80         request.getRequestDetails().setCloudConfiguration(new SoCloudConfiguration());
81         request.getRequestDetails().setRequestInfo(new SoRequestInfo());
82         request.getRequestDetails().setRequestParameters(new SoRequestParameters());
83         request.getRequestDetails().getRequestParameters().setUserParams(null);
84         //
85         // cloudConfiguration
86         //
87         request.getRequestDetails().getCloudConfiguration().setLcpCloudRegionId("DFW");
88         request.getRequestDetails().getCloudConfiguration().setTenantId("1015548");
89         //
90         // modelInfo
91         //
92         request.getRequestDetails().getModelInfo().setModelType("vfModule");
93         request.getRequestDetails().getModelInfo().setModelInvariantId("f32568ec-2f1c-458a-864b-0593d53d141a");
94         request.getRequestDetails().getModelInfo().setModelVersionId("69615025-879d-4f0d-afe3-b7d1a7eeed1f");
95         request.getRequestDetails().getModelInfo().setModelName("C15ce9e1E9144c8fB8bb..dnsscaling..module-1");
96         request.getRequestDetails().getModelInfo().setModelVersion("1.0");
97         //
98         // requestInfo
99         //
100         request.getRequestDetails().getRequestInfo()
101                         .setInstanceName("vDNS_Ete_Named90e1ab3-dcd5-4877-9edb-eadfc84e32c8");
102         request.getRequestDetails().getRequestInfo().setSource("POLICY");
103         request.getRequestDetails().getRequestInfo().setSuppressRollback(false);
104         request.getRequestDetails().getRequestInfo().setRequestorId("policy");
105         //
106         // relatedInstanceList
107         //
108         final SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
109         final SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
110         relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
111         relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
112         //
113         relatedInstanceListElement1.getRelatedInstance().setInstanceId("cf8426a6-0b53-4e3d-bfa6-4b2f4d5913a5");
114         relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
115         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
116         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
117                         .setModelInvariantId("4fcbc1c0-7793-46d8-8aa1-fa1c2ed9ec7b");
118         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
119                         .setModelVersionId("5c996219-b2e2-4c76-9b43-7e8672a33c1d");
120         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName("8330e932-2a23-4943-8606");
121         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion("1.0");
122         //
123         relatedInstanceListElement2.getRelatedInstance().setInstanceId("594e2fe0-48b8-41ff-82e2-3d4bab69b192");
124         relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
125         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
126         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
127                         .setModelInvariantId("033a32ed-aa65-4764-a736-36f2942f1aa0");
128         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
129                         .setModelVersionId("d4d072dc-4e21-4a03-9524-628985819a8e");
130         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelName("c15ce9e1-e914-4c8f-b8bb");
131         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion("1");
132         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
133                         .setModelCustomizationName("c15ce9e1-e914-4c8f-b8bb 1");
134         //
135         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
136         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
137
138         return request;
139     }
140
141     @Test
142     public void testPost() {
143         SoSimulatorJaxRs.setRequirePolling(false);
144         String request = Serialization.gsonPretty.toJson(this.createTestRequest());
145         Pair<Integer, String> httpDetails = new RestManager().post(
146                         "http://localhost:6667/serviceInstantiation/v7/serviceInstances/12345/vnfs/12345/vfModules/scaleOut",
147                         "username",
148                         "password", new HashMap<>(), "application/json", request);
149         assertNotNull(httpDetails);
150         assertThat(httpDetails.getRight()).contains("\"COMPLETE\"").contains("requestSelfLink");
151
152         /*
153          * Repeat, but set the flag indicating that the request should yield incomplete.
154          */
155         SoSimulatorJaxRs.setRequirePolling(true);
156
157         request = Serialization.gsonPretty.toJson(this.createTestRequest());
158         httpDetails = new RestManager().post(
159                         "http://localhost:6667/serviceInstantiation/v7/serviceInstances/12345/vnfs/12345/vfModules/scaleOut",
160                         "username",
161                         "password", new HashMap<>(), "application/json", request);
162         assertNotNull(httpDetails);
163         assertThat(httpDetails.getRight()).contains("requestSelfLink").doesNotContain("\"COMPLETE\"");
164
165         // now poll for the response
166         String uri = extractUri(httpDetails.getRight());
167         httpDetails = new RestManager().get(
168                         "http://localhost:6667/orchestrationRequests/v5/" + uri,
169                         "username",
170                         "password", new HashMap<>());
171         assertNotNull(httpDetails);
172         assertThat(httpDetails.getRight()).contains("\"IN_PROGRESS\"").doesNotContain("requestSelfLink");
173
174         // poll again
175         httpDetails = new RestManager().get(
176                         "http://localhost:6667/orchestrationRequests/v5/" + uri,
177                         "username",
178                         "password", new HashMap<>());
179         assertNotNull(httpDetails);
180         assertThat(httpDetails.getRight()).contains("\"COMPLETE\"").doesNotContain("requestSelfLink");
181     }
182
183     @Test
184     public void testDelete() {
185         SoSimulatorJaxRs.setRequirePolling(false);
186         String request = Serialization.gsonPretty.toJson(this.createTestRequest());
187         Pair<Integer, String> httpDetails = new RestManager().delete(
188                         "http://localhost:6667/serviceInstances/v7/12345/vnfs/12345/vfModules/12345",
189                         "username",
190                         "password", new HashMap<>(), "application/json", request);
191         assertNotNull(httpDetails);
192         assertThat(httpDetails.getRight()).contains("\"COMPLETE\"").contains("requestSelfLink");
193
194         /*
195          * Repeat, but set the flag indicating that the request should yield incomplete.
196          */
197         SoSimulatorJaxRs.setRequirePolling(true);
198
199         request = Serialization.gsonPretty.toJson(this.createTestRequest());
200         httpDetails = new RestManager().delete(
201                         "http://localhost:6667/serviceInstances/v7/12345/vnfs/12345/vfModules/12345",
202                         "username",
203                         "password", new HashMap<>(), "application/json", request);
204         assertNotNull(httpDetails);
205         assertThat(httpDetails.getRight()).contains("requestSelfLink").doesNotContain("\"COMPLETE\"");
206
207         // now poll for the response
208         String uri = extractUri(httpDetails.getRight());
209         httpDetails = new RestManager().get(
210                         "http://localhost:6667/orchestrationRequests/v5/" + uri,
211                         "username",
212                         "password", new HashMap<>());
213         assertNotNull(httpDetails);
214         assertThat(httpDetails.getRight()).contains("\"IN_PROGRESS\"").doesNotContain("requestSelfLink");
215
216         // poll again
217         httpDetails = new RestManager().get(
218                         "http://localhost:6667/orchestrationRequests/v5/" + uri,
219                         "username",
220                         "password", new HashMap<>());
221         assertNotNull(httpDetails);
222         assertThat(httpDetails.getRight()).contains("\"COMPLETE\"").doesNotContain("requestSelfLink");
223     }
224
225     @Test
226     public void testModifyNssi() {
227         SoSimulatorJaxRs.setRequirePolling(false);
228         String request = Serialization.gsonPretty.toJson(this.createNssiRequest());
229         Pair<Integer, String> httpDetails = new RestManager().put(
230                 "http://localhost:6667/3gppservices/v7/modify",
231                 "username",
232                 "password", new HashMap<>(), "application/json", request);
233         assertNotNull(httpDetails);
234         assertEquals(200, httpDetails.getLeft().intValue());
235         assertThat(httpDetails.getRight()).contains("jobId").contains("status");
236     }
237
238     private SoRequest3gpp createNssiRequest() {
239         SoRequest3gpp request = new SoRequest3gpp();
240
241         request.setName("URLLC_core1");
242         request.setServiceInstanceId("ff67a209-dc69-4a1a-b89a-c1f55c2a8842");
243         request.setGlobalSubscriberId("5G Customer");
244         request.setSubscriptionServiceType("5G");
245         request.setNetworkType("an");
246         request.setAdditionalProperties(new HashMap<String, Object>());
247
248         return request;
249     }
250
251     private String extractUri(String response) {
252         final String prefix = "\"requestId\": \"";
253
254         int start = response.indexOf(prefix);
255         assertTrue(start >= 0);
256
257         start += prefix.length();
258
259         int end = response.indexOf('"', start);
260         assertTrue(end >= 0);
261
262         return response.substring(start, end);
263     }
264 }