Add ACM regression test suite
[policy/docker.git] / policy-regression-tests / policy-clamp-regression / src / test / java / org.onap.policy.clamp.regression / StepDefinitions.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Nordix Foundation. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.regression;
22
23 import static io.restassured.config.EncoderConfig.encoderConfig;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 import io.cucumber.java.en.Then;
29 import io.cucumber.java.en.When;
30 import io.restassured.RestAssured;
31 import io.restassured.http.ContentType;
32 import io.restassured.path.json.JsonPath;
33 import io.restassured.response.Response;
34 import java.io.IOException;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.util.concurrent.TimeUnit;
38 import org.json.JSONException;
39 import org.json.JSONObject;
40
41
42 public class StepDefinitions {
43     private static final String acmUrl = "http://localhost:30007/";
44     private static final String username = "runtimeUser";
45     private static final String password = "zb!XztG34";
46     private static String compositionId;
47     private static String instanceId;
48     private static String targetCompositionId;
49
50     private Response response;
51
52     /**
53      * Verify ACM healthcheck.
54      */
55     @When("the acm health check endpoint is invoked {string}")
56     public void isAcmEndpointRunning(String endpoint) {
57         response = RestAssured.given()
58                 .auth().basic(username, password)
59                 .baseUri(acmUrl)
60                 .get(endpoint);
61     }
62
63     /**
64      * Verify commissioning.
65      */
66     @When("the ACM commissioning endpoint {string} is invoked with {string} {string}")
67     public void commissioningIsAccepted(String endpoint, String filePath, String isMigration) throws IOException {
68         var jsonContent = new String(Files.readAllBytes(Path.of(filePath)));
69         response = RestAssured.given()
70                 .config(RestAssured.config().encoderConfig(encoderConfig()
71                         .encodeContentTypeAs("", ContentType.JSON)))
72                 .auth().basic(username, password)
73                 .contentType(ContentType.JSON)
74                 .accept(ContentType.JSON)
75                 .baseUri(acmUrl)
76                 .body(jsonContent)
77                 .post(endpoint);
78         var jsonPath = new JsonPath(response.getBody().asString());
79         if (Boolean.parseBoolean(isMigration)) {
80             targetCompositionId = jsonPath.getString("compositionId");
81         } else {
82             compositionId = jsonPath.getString("compositionId");
83         }
84     }
85
86     /**
87      * Register participants.
88      */
89     @When("the register participants endpoint is invoked {string}")
90     public void registerParticipants(String endpoint) {
91         response = RestAssured.given()
92                 .auth().basic(username, password)
93                 .baseUri(acmUrl)
94                 .put(endpoint);
95     }
96
97     /**
98      * Verify priming.
99      */
100     @When("the ACM participants are primed {string} {string}")
101     public void primeTheParticipants(String endpoint, String isMigration) throws JSONException {
102         var jsonBody = new JSONObject().put("primeOrder", "PRIME");
103         response = RestAssured.given()
104                 .config(RestAssured.config().encoderConfig(encoderConfig()
105                         .encodeContentTypeAs("", ContentType.JSON)))
106                 .auth().basic(username, password)
107                 .contentType(ContentType.JSON)
108                 .accept(ContentType.JSON)
109                 .baseUri(acmUrl)
110                 .body(jsonBody.toString())
111                 .put(endpoint.replace("{compositionId}",
112                         Boolean.parseBoolean(isMigration) ? targetCompositionId : compositionId));
113     }
114
115     /**
116      * Fetch composition by Id.
117      */
118     @When("the ACM composition is fetched {string}")
119     public void fetchCompositionById(String endpoint) {
120         response = RestAssured.given()
121                 .auth().basic(username, password)
122                 .baseUri(acmUrl)
123                 .get(endpoint.replace("{compositionId}", compositionId));
124     }
125
126     /**
127      * Wait and retry requests until the keyword is present.
128      */
129     @Then("Wait and retry until the response contains the keyword {string} {string} {string}")
130     public void waitUntilKeyword(String keyword, String endpoint, String isMigration) throws InterruptedException {
131         var startTime = System.currentTimeMillis();
132         var maxWaitTime = TimeUnit.MINUTES.toMillis(2); // 2 minutes in milliseconds
133         var conditionMet = false;
134         while (!conditionMet && System.currentTimeMillis() - startTime < maxWaitTime) {
135             var jsonPath = new JsonPath(response.getBody().asString());
136             switch (keyword) {
137                 case "PRIMED", "COMMISSIONED" -> {
138                     conditionMet = keyword.equals(jsonPath.getString("state"));
139                     if (!conditionMet) {
140                         Thread.sleep(10000); // Wait for 10 second before retrying
141                         fetchCompositionById(endpoint);
142                     }
143                 }
144                 case "DEPLOYED", "UNDEPLOYED" -> {
145                     conditionMet = keyword.equals(jsonPath.getString("deployState"));
146                     if (!conditionMet) {
147                         Thread.sleep(10000); // Wait for 10 second before retrying
148                         fetchAcInstanceById(endpoint,
149                                 Boolean.parseBoolean(isMigration) ? targetCompositionId : compositionId);
150                     }
151                 }
152                 default -> {
153                     break;
154                 }
155             }
156         }
157         assertTrue(conditionMet);
158     }
159
160
161     /**
162      * Verify AC instantiation.
163      */
164     @When("the ACM instance is created {string} with {string}")
165     public void instantiate(String endpoint, String filePath) throws IOException {
166         var jsonContent = new String(Files.readAllBytes(Path.of(filePath)));
167         response = RestAssured.given()
168                 .config(RestAssured.config().encoderConfig(encoderConfig()
169                         .encodeContentTypeAs("", ContentType.JSON)))
170                 .auth().basic(username, password)
171                 .contentType(ContentType.JSON)
172                 .accept(ContentType.JSON)
173                 .baseUri(acmUrl)
174                 .body(jsonContent.replace("COMPOSITIONIDPLACEHOLDER", compositionId))
175                 .post(endpoint.replace("{compositionId}", compositionId));
176         var jsonPath = new JsonPath(response.getBody().asString());
177         instanceId = jsonPath.getString("instanceId");
178     }
179
180     /**
181      * Verify update property.
182      */
183     @When("the AC instance property is updated {string} {string}")
184     public void updateInstanceProperty(String endpoint, String filePath) throws IOException {
185         var jsonContent = new String(Files.readAllBytes(Path.of(filePath)));
186         response = RestAssured.given()
187                 .config(RestAssured.config().encoderConfig(encoderConfig()
188                         .encodeContentTypeAs("", ContentType.JSON)))
189                 .auth().basic(username, password)
190                 .contentType(ContentType.JSON)
191                 .accept(ContentType.JSON)
192                 .baseUri(acmUrl)
193                 .body(jsonContent.replace("COMPOSITIONIDPLACEHOLDER", compositionId)
194                         .replace("INSTANCEIDPLACEHOLDER", instanceId))
195                 .post(endpoint.replace("{compositionId}", compositionId));
196         var jsonPath = new JsonPath(response.getBody().asString());
197         instanceId = jsonPath.getString("instanceId");
198     }
199
200     /**
201      * Verify AC deployment.
202      */
203     @When("the AC instance is deployed {string}")
204     public void deployAcInstance(String endpoint) throws JSONException {
205         var jsonBody = new JSONObject().put("deployOrder", "DEPLOY");
206         response = RestAssured.given()
207                 .config(RestAssured.config().encoderConfig(encoderConfig()
208                         .encodeContentTypeAs("", ContentType.JSON)))
209                 .auth().basic(username, password)
210                 .contentType(ContentType.JSON)
211                 .accept(ContentType.JSON)
212                 .baseUri(acmUrl)
213                 .body(jsonBody.toString())
214                 .put(endpoint.replace("{compositionId}", compositionId)
215                         .replace("{instanceId}", instanceId));
216     }
217
218     /**
219      * Fetch Ac instance by id.
220      */
221     @When("the AC instance is fetched {string} {string}")
222     public void fetchAcInstanceById(String endpoint, String isMigration) {
223         response = RestAssured.given()
224                 .auth().basic(username, password)
225                 .baseUri(acmUrl)
226                 .get(endpoint.replace("{compositionId}",
227                                 Boolean.parseBoolean(isMigration) ? targetCompositionId : compositionId)
228                         .replace("{instanceId}", instanceId));
229     }
230
231     /**
232      * Verify Ac undeployment.
233      */
234     @When("the AC instance is undeployed {string}")
235     public void unDeployAcInstance(String endpoint) throws JSONException {
236         var jsonBody = new JSONObject().put("deployOrder", "UNDEPLOY");
237         response = RestAssured.given()
238                 .config(RestAssured.config().encoderConfig(encoderConfig()
239                         .encodeContentTypeAs("", ContentType.JSON)))
240                 .auth().basic(username, password)
241                 .contentType(ContentType.JSON)
242                 .accept(ContentType.JSON)
243                 .baseUri(acmUrl)
244                 .body(jsonBody.toString())
245                 .put(endpoint.replace("{compositionId}", compositionId)
246                         .replace("{instanceId}", instanceId));
247     }
248
249     /**
250      * Verify Ac uninstantiation.
251      */
252     @When("the ACM instance is uninstantiated {string}")
253     public void unInstantiate(String endpoint) {
254         response = RestAssured.given()
255                 .auth().basic(username, password)
256                 .baseUri(acmUrl)
257                 .delete(endpoint.replace("{compositionId}", compositionId).replace("{instanceId}", instanceId));
258     }
259
260     /**
261      * Fetch all Ac instances for the given composition id.
262      */
263     @When("all the AC instances are fetched {string}")
264     public void fetchAllAcInstances(String endpoint) {
265         response = RestAssured.given()
266                 .auth().basic(username, password)
267                 .baseUri(acmUrl)
268                 .get(endpoint.replace("{compositionId}", compositionId));
269     }
270
271     /**
272      * Wait and retry request until the instance list is empty in the response.
273      */
274     @Then("Wait and retry until the ac instance list is empty {string}")
275     public void waitUntilTheCondition(String endpoint) throws InterruptedException {
276         var startTime = System.currentTimeMillis();
277         var maxWaitTime = TimeUnit.MINUTES.toMillis(2); // 2 minutes in milliseconds
278         var conditionMet = false;
279         while (!conditionMet && System.currentTimeMillis() - startTime < maxWaitTime) {
280             var size = response.getBody().jsonPath().getList("automationCompositionList").size();
281             conditionMet = Integer.valueOf(0).equals(size);
282             if (!conditionMet) {
283                 Thread.sleep(10000); // Wait for 10 second before retrying
284                 fetchAllAcInstances(endpoint);
285             }
286         }
287     }
288
289     /**
290      * Verify de-priming.
291      */
292     @When("the ACM participants are deprimed {string}")
293     public void deprimeTheParticipants(String endpoint) throws JSONException {
294         var jsonBody = new JSONObject().put("primeOrder", "DEPRIME");
295         response = RestAssured.given()
296                 .config(RestAssured.config().encoderConfig(encoderConfig()
297                         .encodeContentTypeAs("", ContentType.JSON)))
298                 .auth().basic(username, password)
299                 .contentType(ContentType.JSON)
300                 .accept(ContentType.JSON)
301                 .baseUri(acmUrl)
302                 .body(jsonBody.toString())
303                 .put(endpoint.replace("{compositionId}", compositionId));
304     }
305
306     /**
307      * Verify deletion of composition defintion.
308      */
309     @When("the AC definition is deleted {string}")
310     public void deleteAcDefinition(String endpoint) throws IOException {
311         response = RestAssured.given()
312                 .auth().basic(username, password)
313                 .baseUri(acmUrl)
314                 .delete(endpoint.replace("{compositionId}", compositionId));
315     }
316
317     /**
318      * Verify migration functionality.
319      */
320     @When("the ACM instance is migrated {string} with {string}")
321     public void migrate(String endpoint, String filePath) throws IOException {
322         var jsonContent = new String(Files.readAllBytes(Path.of(filePath)));
323         response = RestAssured.given()
324                 .config(RestAssured.config().encoderConfig(encoderConfig()
325                         .encodeContentTypeAs("", ContentType.JSON)))
326                 .auth().basic(username, password)
327                 .contentType(ContentType.JSON)
328                 .accept(ContentType.JSON)
329                 .baseUri(acmUrl)
330                 .body(jsonContent
331                         .replace("COMPOSITIONIDPLACEHOLDER", compositionId)
332                         .replace("INSTANCEIDPLACEHOLDER", instanceId)
333                         .replace("COMPOSITIONTARGETIDPLACEHOLDER", targetCompositionId))
334                 .post(endpoint.replace("{compositionId}", compositionId));
335     }
336
337     /**
338      * Verify the response status.
339      */
340     @Then("the response status code should be {int}")
341     public void theUserEndpointIsUpAndRunning(int expectedStatus) {
342         assertEquals(expectedStatus, response.getStatusCode());
343     }
344
345     /**
346      * Verify the response contains specific keywords.
347      */
348     @Then("the response should contain the updated properties {string} {string}")
349     public void theUserEndpointIsUpAndRunning(String value1, String value2) {
350         assertThat(value1, response.getBody().asString().contains(value1));
351         assertThat(value1, response.getBody().asString().contains(value2));
352     }
353
354 }