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.Ignore;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.Parameterized;
30 import org.onap.aai.AAIJunitRunner;
31 import org.onap.aai.HttpTestUtil;
32 import org.onap.aai.PayloadUtil;
33 import org.onap.aai.serialization.engines.QueryStyle;
34 import org.skyscreamer.jsonassert.JSONAssert;
36 import javax.ws.rs.core.Response;
39 import static org.junit.Assert.assertEquals;
42 @RunWith(AAIJunitRunner.class)
43 public class HPACapabilityTest {
45 private static EELFLogger logger = EELFManager.getInstance().getLogger(HPACapabilityTest.class);
46 private HttpTestUtil httpTestUtil;
47 private Map<String, String> templateValuesMap;
49 @Parameterized.Parameter(value = 0)
50 public QueryStyle queryStyle;
52 @Parameterized.Parameters(name = "QueryStyle.{0}")
53 public static Collection<Object[]> data() {
54 return Arrays.asList(new Object[][]{
55 {QueryStyle.TRAVERSAL}
61 httpTestUtil = new HttpTestUtil(queryStyle);
62 templateValuesMap = new HashMap<>();
66 public void testPutHPACapabilitiesInFlavorAndCheckIfDeleteIsSuccessful() throws Exception {
68 templateValuesMap.put("cloud-region-id", UUID.randomUUID().toString());
69 templateValuesMap.put("cloud-owner", UUID.randomUUID().toString());
70 templateValuesMap.put("tenant-id", UUID.randomUUID().toString());
71 templateValuesMap.put("vserver-id", UUID.randomUUID().toString());
72 templateValuesMap.put("flavor-id1", UUID.randomUUID().toString());
73 templateValuesMap.put("flavor-id2", UUID.randomUUID().toString());
74 templateValuesMap.put("hpa-capability-id1", UUID.randomUUID().toString());
75 templateValuesMap.put("hpa-capability-id2", UUID.randomUUID().toString());
76 templateValuesMap.put("hpa-capability-id3", UUID.randomUUID().toString());
77 templateValuesMap.put("hpa-capability-id4", UUID.randomUUID().toString());
78 templateValuesMap.put("hpa-capability-id5", UUID.randomUUID().toString());
79 templateValuesMap.put("hpa-capability-id6", UUID.randomUUID().toString());
80 templateValuesMap.put("hpa-capability-id7", UUID.randomUUID().toString());
81 templateValuesMap.put("hpa-capability-id8", UUID.randomUUID().toString());
83 String cloudRegionPayload = PayloadUtil.getTemplatePayload("hpa.json", templateValuesMap);
84 String cloudRegionUri = String.format("/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/%s/%s",
85 templateValuesMap.get("cloud-owner"),
86 templateValuesMap.get("cloud-region-id")
89 Response response = httpTestUtil.doPut(cloudRegionUri, cloudRegionPayload);
90 assertEquals("Expected the cloud region to be created", 201, response.getStatus());
92 response = httpTestUtil.doGet(cloudRegionUri);
93 assertEquals("Expected the cloud region to be found", 200, response.getStatus());
94 String jsonResponse = response.getEntity().toString();
95 System.out.println("#########################jsonResponse#########################");
96 System.out.println(jsonResponse);
97 System.out.println("#########################jsonResponse#########################");
99 JSONAssert.assertEquals(cloudRegionPayload, jsonResponse, false);
101 deleteFlavor(cloudRegionUri, templateValuesMap.get("flavor-id1"));
102 deleteFlavor(cloudRegionUri, templateValuesMap.get("flavor-id2"));
103 deleteTenant(cloudRegionUri);
106 private void deleteTenant(String cloudRegionUri) throws Exception {
107 String tenantUri = cloudRegionUri + "/tenants/tenant/" + templateValuesMap.get("tenant-id");
108 deleteVserver(tenantUri);
110 Response tntResponse = httpTestUtil.doGet(tenantUri);
111 assertEquals("Expected to GET Tenant info from cloud-region", 200, tntResponse.getStatus());
112 String responseStr = tntResponse.getEntity().toString();
114 String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
116 tntResponse = httpTestUtil.doDelete(tenantUri, resourceVersion);
117 assertEquals("Expected to DELETE Tenant info from cloud-region", 204, tntResponse.getStatus());
120 private void deleteVserver(String tenantUri) throws Exception {
121 String uri = tenantUri + "/vservers/vserver/" + templateValuesMap.get("vserver-id");
123 Response tntResponse = httpTestUtil.doGet(uri);
124 assertEquals("Expected to GET Vserver", 200, tntResponse.getStatus());
125 String responseStr = tntResponse.getEntity().toString();
127 String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
129 tntResponse = httpTestUtil.doDelete(uri, resourceVersion);
130 assertEquals("Expected to DELETE Vserver", 204, tntResponse.getStatus());
133 private void deleteFlavor(String cloudRegionUri, String flavorId) throws Exception {
134 String flavorUri = cloudRegionUri + "/flavors/flavor/" + flavorId;
136 Response response = httpTestUtil.doGet(flavorUri);
137 assertEquals("Expected to GET Flavors info from cloud-region", 200, response.getStatus());
138 String jsonResponse = response.getEntity().toString();
139 System.out.println("#########################Flavor Response#########################");
140 System.out.println(jsonResponse);
141 System.out.println("#########################Flavor Response#########################");
142 String responseStr = response.getEntity().toString();
144 List<String> capabilityIds = JsonPath.read(responseStr,
145 "$.hpa-capabilities.hpa-capability[*].hpa-capability-id");
146 for(String capabilityId : capabilityIds) {
147 deleteHPACapability(flavorUri, capabilityId);
150 String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
151 response = httpTestUtil.doDelete(flavorUri, resourceVersion);
152 assertEquals("Expected to DELETE Flavor info from cloud-region", 204, response.getStatus());
155 private void deleteHPACapability(String flavorUri, String capabilityId) throws Exception {
156 String uri = flavorUri + "/hpa-capabilities/hpa-capability/" + capabilityId;
158 Response response = httpTestUtil.doGet(uri);
159 assertEquals("Expected to GET HPA info from flavors", 200, response.getStatus());
160 String jsonResponse = response.getEntity().toString();
161 System.out.println("#########################HPA Response#########################");
162 System.out.println(jsonResponse);
163 System.out.println("#########################HPA Response#########################");
164 String responseStr = response.getEntity().toString();
166 String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
168 response = httpTestUtil.doDelete(uri, resourceVersion);
169 assertEquals("Expected to DELETE HPA info from flavors", 204, response.getStatus());