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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.regression;
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;
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;
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;
50 private Response response;
53 * Verify ACM healthcheck.
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)
64 * Verify commissioning.
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)
78 var jsonPath = new JsonPath(response.getBody().asString());
79 if (Boolean.parseBoolean(isMigration)) {
80 targetCompositionId = jsonPath.getString("compositionId");
82 compositionId = jsonPath.getString("compositionId");
87 * Register participants.
89 @When("the register participants endpoint is invoked {string}")
90 public void registerParticipants(String endpoint) {
91 response = RestAssured.given()
92 .auth().basic(username, password)
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)
110 .body(jsonBody.toString())
111 .put(endpoint.replace("{compositionId}",
112 Boolean.parseBoolean(isMigration) ? targetCompositionId : compositionId));
116 * Fetch composition by Id.
118 @When("the ACM composition is fetched {string}")
119 public void fetchCompositionById(String endpoint) {
120 response = RestAssured.given()
121 .auth().basic(username, password)
123 .get(endpoint.replace("{compositionId}", compositionId));
127 * Wait and retry requests until the keyword is present.
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());
137 case "PRIMED", "COMMISSIONED" -> {
138 conditionMet = keyword.equals(jsonPath.getString("state"));
140 Thread.sleep(10000); // Wait for 10 second before retrying
141 fetchCompositionById(endpoint);
144 case "DEPLOYED", "UNDEPLOYED" -> {
145 conditionMet = keyword.equals(jsonPath.getString("deployState"));
147 Thread.sleep(10000); // Wait for 10 second before retrying
148 fetchAcInstanceById(endpoint,
149 Boolean.parseBoolean(isMigration) ? targetCompositionId : compositionId);
157 assertTrue(conditionMet);
162 * Verify AC instantiation.
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)
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");
181 * Verify update property.
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)
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");
201 * Verify AC deployment.
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)
213 .body(jsonBody.toString())
214 .put(endpoint.replace("{compositionId}", compositionId)
215 .replace("{instanceId}", instanceId));
219 * Fetch Ac instance by id.
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)
226 .get(endpoint.replace("{compositionId}",
227 Boolean.parseBoolean(isMigration) ? targetCompositionId : compositionId)
228 .replace("{instanceId}", instanceId));
232 * Verify Ac undeployment.
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)
244 .body(jsonBody.toString())
245 .put(endpoint.replace("{compositionId}", compositionId)
246 .replace("{instanceId}", instanceId));
250 * Verify Ac uninstantiation.
252 @When("the ACM instance is uninstantiated {string}")
253 public void unInstantiate(String endpoint) {
254 response = RestAssured.given()
255 .auth().basic(username, password)
257 .delete(endpoint.replace("{compositionId}", compositionId).replace("{instanceId}", instanceId));
261 * Fetch all Ac instances for the given composition id.
263 @When("all the AC instances are fetched {string}")
264 public void fetchAllAcInstances(String endpoint) {
265 response = RestAssured.given()
266 .auth().basic(username, password)
268 .get(endpoint.replace("{compositionId}", compositionId));
272 * Wait and retry request until the instance list is empty in the response.
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);
283 Thread.sleep(10000); // Wait for 10 second before retrying
284 fetchAllAcInstances(endpoint);
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)
302 .body(jsonBody.toString())
303 .put(endpoint.replace("{compositionId}", compositionId));
307 * Verify deletion of composition defintion.
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)
314 .delete(endpoint.replace("{compositionId}", compositionId));
318 * Verify migration functionality.
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)
331 .replace("COMPOSITIONIDPLACEHOLDER", compositionId)
332 .replace("INSTANCEIDPLACEHOLDER", instanceId)
333 .replace("COMPOSITIONTARGETIDPLACEHOLDER", targetCompositionId))
334 .post(endpoint.replace("{compositionId}", compositionId));
338 * Verify the response status.
340 @Then("the response status code should be {int}")
341 public void theUserEndpointIsUpAndRunning(int expectedStatus) {
342 assertEquals(expectedStatus, response.getStatusCode());
346 * Verify the response contains specific keywords.
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));