2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.rest;
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import com.jayway.jsonpath.JsonPath;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.Parameterized;
29 import org.onap.aai.AAIJunitRunner;
30 import org.onap.aai.HttpTestUtil;
31 import org.onap.aai.PayloadUtil;
32 import org.onap.aai.serialization.engines.QueryStyle;
33 import org.skyscreamer.jsonassert.JSONAssert;
35 import javax.ws.rs.core.Response;
38 import static org.junit.Assert.assertEquals;
40 @RunWith(AAIJunitRunner.class)
41 public class HPACapabilityTest {
43 private static EELFLogger logger = EELFManager.getInstance().getLogger(HPACapabilityTest.class);
44 private HttpTestUtil httpTestUtil;
45 private Map<String, String> templateValuesMap;
47 @Parameterized.Parameter(value = 0)
48 public QueryStyle queryStyle;
50 @Parameterized.Parameters(name = "QueryStyle.{0}")
51 public static Collection<Object[]> data() {
52 return Arrays.asList(new Object[][]{
53 {QueryStyle.TRAVERSAL}
59 httpTestUtil = new HttpTestUtil(queryStyle);
60 templateValuesMap = new HashMap<>();
64 public void testPutHPACapabilitiesInFlavorAndCheckIfDeleteIsSuccessful() throws Exception {
66 templateValuesMap.put("cloud-region-id", UUID.randomUUID().toString());
67 templateValuesMap.put("cloud-owner", UUID.randomUUID().toString());
68 templateValuesMap.put("tenant-id", UUID.randomUUID().toString());
69 templateValuesMap.put("vserver-id", UUID.randomUUID().toString());
70 templateValuesMap.put("flavor-id1", UUID.randomUUID().toString());
71 templateValuesMap.put("flavor-id2", UUID.randomUUID().toString());
72 templateValuesMap.put("hpa-capability-id1", UUID.randomUUID().toString());
73 templateValuesMap.put("hpa-capability-id2", UUID.randomUUID().toString());
74 templateValuesMap.put("hpa-capability-id3", UUID.randomUUID().toString());
75 templateValuesMap.put("hpa-capability-id4", UUID.randomUUID().toString());
76 templateValuesMap.put("hpa-capability-id5", UUID.randomUUID().toString());
77 templateValuesMap.put("hpa-capability-id6", UUID.randomUUID().toString());
78 templateValuesMap.put("hpa-capability-id7", UUID.randomUUID().toString());
79 templateValuesMap.put("hpa-capability-id8", UUID.randomUUID().toString());
81 String cloudRegionPayload = PayloadUtil.getTemplatePayload("hpa.json", templateValuesMap);
82 String cloudRegionUri = String.format("/aai/v13/cloud-infrastructure/cloud-regions/cloud-region/%s/%s",
83 templateValuesMap.get("cloud-owner"),
84 templateValuesMap.get("cloud-region-id")
87 Response response = httpTestUtil.doPut(cloudRegionUri, cloudRegionPayload);
88 assertEquals("Expected the cloud region to be created", 201, response.getStatus());
90 response = httpTestUtil.doGet(cloudRegionUri);
91 assertEquals("Expected the cloud region to be found", 200, response.getStatus());
92 String jsonResponse = response.getEntity().toString();
93 System.out.println("#########################jsonResponse#########################");
94 System.out.println(jsonResponse);
95 System.out.println("#########################jsonResponse#########################");
97 JSONAssert.assertEquals(cloudRegionPayload, jsonResponse, false);
99 deleteFlavor(cloudRegionUri, templateValuesMap.get("flavor-id1"));
100 deleteFlavor(cloudRegionUri, templateValuesMap.get("flavor-id2"));
101 deleteTenant(cloudRegionUri);
104 private void deleteTenant(String cloudRegionUri) throws Exception {
105 String tenantUri = cloudRegionUri + "/tenants/tenant/" + templateValuesMap.get("tenant-id");
106 deleteVserver(tenantUri);
108 Response tntResponse = httpTestUtil.doGet(tenantUri);
109 assertEquals("Expected to GET Tenant info from cloud-region", 200, tntResponse.getStatus());
110 String responseStr = tntResponse.getEntity().toString();
112 String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
114 tntResponse = httpTestUtil.doDelete(tenantUri, resourceVersion);
115 assertEquals("Expected to DELETE Tenant info from cloud-region", 204, tntResponse.getStatus());
118 private void deleteVserver(String tenantUri) throws Exception {
119 String uri = tenantUri + "/vservers/vserver/" + templateValuesMap.get("vserver-id");
121 Response tntResponse = httpTestUtil.doGet(uri);
122 assertEquals("Expected to GET Vserver", 200, tntResponse.getStatus());
123 String responseStr = tntResponse.getEntity().toString();
125 String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
127 tntResponse = httpTestUtil.doDelete(uri, resourceVersion);
128 assertEquals("Expected to DELETE Vserver", 204, tntResponse.getStatus());
131 private void deleteFlavor(String cloudRegionUri, String flavorId) throws Exception {
132 String flavorUri = cloudRegionUri + "/flavors/flavor/" + flavorId;
134 Response response = httpTestUtil.doGet(flavorUri);
135 assertEquals("Expected to GET Flavors info from cloud-region", 200, response.getStatus());
136 String jsonResponse = response.getEntity().toString();
137 System.out.println("#########################Flavor Response#########################");
138 System.out.println(jsonResponse);
139 System.out.println("#########################Flavor Response#########################");
140 String responseStr = response.getEntity().toString();
142 List<String> capabilityIds = JsonPath.read(responseStr,
143 "$.hpa-capabilities.hpa-capability[*].hpa-capability-id");
144 for(String capabilityId : capabilityIds) {
145 deleteHPACapability(flavorUri, capabilityId);
148 String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
149 response = httpTestUtil.doDelete(flavorUri, resourceVersion);
150 assertEquals("Expected to DELETE Flavor info from cloud-region", 204, response.getStatus());
153 private void deleteHPACapability(String flavorUri, String capabilityId) throws Exception {
154 String uri = flavorUri + "/hpa-capabilities/hpa-capability/" + capabilityId;
156 Response response = httpTestUtil.doGet(uri);
157 assertEquals("Expected to GET HPA info from flavors", 200, response.getStatus());
158 String jsonResponse = response.getEntity().toString();
159 System.out.println("#########################HPA Response#########################");
160 System.out.println(jsonResponse);
161 System.out.println("#########################HPA Response#########################");
162 String responseStr = response.getEntity().toString();
164 String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
166 response = httpTestUtil.doDelete(uri, resourceVersion);
167 assertEquals("Expected to DELETE HPA info from flavors", 204, response.getStatus());