279b9c737a5cc821e74dffc1300b4b2b111c02c5
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / rest / HPACapabilityTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.aai.rest;
21
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;
34
35 import javax.ws.rs.core.Response;
36 import java.util.*;
37
38 import static org.junit.Assert.assertEquals;
39
40 @RunWith(AAIJunitRunner.class)
41 public class HPACapabilityTest {
42
43     private static EELFLogger logger = EELFManager.getInstance().getLogger(HPACapabilityTest.class);
44     private HttpTestUtil httpTestUtil;
45     private Map<String, String> templateValuesMap;
46
47     @Parameterized.Parameter(value = 0)
48     public QueryStyle queryStyle;
49
50     @Parameterized.Parameters(name = "QueryStyle.{0}")
51     public static Collection<Object[]> data() {
52         return Arrays.asList(new Object[][]{
53                 {QueryStyle.TRAVERSAL}
54         });
55     }
56
57     @Before
58     public void setup() {
59         httpTestUtil = new HttpTestUtil(queryStyle);
60         templateValuesMap = new HashMap<>();
61     }
62
63     @Test
64     public void testPutHPACapabilitiesInFlavorAndCheckIfDeleteIsSuccessful() throws Exception {
65
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());
80
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")
85         );
86
87         Response response = httpTestUtil.doPut(cloudRegionUri, cloudRegionPayload);
88         assertEquals("Expected the cloud region to be created", 201, response.getStatus());
89
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#########################");
96
97         JSONAssert.assertEquals(cloudRegionPayload, jsonResponse, false);
98
99         deleteFlavor(cloudRegionUri, templateValuesMap.get("flavor-id1"));
100         deleteFlavor(cloudRegionUri, templateValuesMap.get("flavor-id2"));
101         deleteTenant(cloudRegionUri);
102     }
103
104     private void deleteTenant(String cloudRegionUri) throws Exception {
105         String tenantUri = cloudRegionUri + "/tenants/tenant/" + templateValuesMap.get("tenant-id");
106         deleteVserver(tenantUri);
107
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();
111
112         String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
113
114         tntResponse = httpTestUtil.doDelete(tenantUri, resourceVersion);
115         assertEquals("Expected to DELETE Tenant info from cloud-region", 204, tntResponse.getStatus());
116     }
117
118     private void deleteVserver(String tenantUri) throws Exception {
119         String uri = tenantUri + "/vservers/vserver/" + templateValuesMap.get("vserver-id");
120
121         Response tntResponse = httpTestUtil.doGet(uri);
122         assertEquals("Expected to GET Vserver", 200, tntResponse.getStatus());
123         String responseStr = tntResponse.getEntity().toString();
124
125         String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
126
127         tntResponse = httpTestUtil.doDelete(uri, resourceVersion);
128         assertEquals("Expected to DELETE Vserver", 204, tntResponse.getStatus());
129     }
130
131     private void deleteFlavor(String cloudRegionUri, String flavorId) throws Exception {
132         String flavorUri = cloudRegionUri + "/flavors/flavor/" + flavorId;
133
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();
141
142         List<String> capabilityIds = JsonPath.read(responseStr,
143                 "$.hpa-capabilities.hpa-capability[*].hpa-capability-id");
144         for(String capabilityId : capabilityIds) {
145             deleteHPACapability(flavorUri, capabilityId);
146         }
147
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());
151     }
152
153     private void deleteHPACapability(String flavorUri, String capabilityId) throws Exception {
154         String uri = flavorUri + "/hpa-capabilities/hpa-capability/" + capabilityId;
155
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();
163
164         String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
165
166         response = httpTestUtil.doDelete(uri, resourceVersion);
167         assertEquals("Expected to DELETE HPA info from flavors", 204, response.getStatus());
168     }
169 }