AAI-1523 Batch reformat aai-core
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / rest / TenantTest.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
21 package org.onap.aai.rest;
22
23 import static org.junit.Assert.assertEquals;
24
25 import com.jayway.jsonpath.JsonPath;
26
27 import java.util.*;
28
29 import javax.ws.rs.core.Response;
30
31 import org.junit.Before;
32 import org.junit.ClassRule;
33 import org.junit.Ignore;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.junit.runners.Parameterized;
38 import org.onap.aai.AAISetup;
39 import org.onap.aai.HttpTestUtil;
40 import org.onap.aai.PayloadUtil;
41 import org.onap.aai.serialization.engines.QueryStyle;
42 import org.skyscreamer.jsonassert.JSONAssert;
43 import org.springframework.test.context.junit4.rules.SpringClassRule;
44 import org.springframework.test.context.junit4.rules.SpringMethodRule;
45
46 @RunWith(value = Parameterized.class)
47 public class TenantTest extends AAISetup {
48
49     private HttpTestUtil httpTestUtil;
50     private Map<String, String> templateValuesMap;
51     @ClassRule
52     public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
53
54     @Rule
55     public final SpringMethodRule springMethodRule = new SpringMethodRule();
56
57     @Parameterized.Parameter(value = 0)
58     public QueryStyle queryStyle;
59
60     @Parameterized.Parameters(name = "QueryStyle.{0}")
61     public static Collection<Object[]> data() {
62         return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
63     }
64
65     @Before
66     public void setUp() {
67         httpTestUtil = new HttpTestUtil(queryStyle);
68         templateValuesMap = new HashMap<>();
69     }
70
71     @Ignore("Test is failing due to the deletion of node with children not correct will be fixed soon")
72     @Test
73     public void testCloudRegionTenantDeleteSuccessWithoutDeletingVserver() throws Exception {
74
75         templateValuesMap.put("cloud-region-id", UUID.randomUUID().toString());
76         templateValuesMap.put("cloud-owner", UUID.randomUUID().toString());
77         templateValuesMap.put("tenant-id", UUID.randomUUID().toString());
78         templateValuesMap.put("vserver-id", UUID.randomUUID().toString());
79
80         String cloudRegionPayload = PayloadUtil.getTemplatePayload("cloud-region.json", templateValuesMap);
81         String cloudRegionUri = String.format("/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/%s/%s",
82                 templateValuesMap.get("cloud-owner"), templateValuesMap.get("cloud-region-id"));
83
84         String tenantUri = cloudRegionUri + "/tenants/tenant/" + templateValuesMap.get("tenant-id");
85         String tenantPayload = PayloadUtil.getTemplatePayload("tenant.json", templateValuesMap);
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(tenantUri);
91         assertEquals("Expected the cloud region to be created", 200, response.getStatus());
92         String responseStr = response.getEntity().toString();
93
94         JSONAssert.assertEquals(tenantPayload, responseStr, false);
95         String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
96
97         response = httpTestUtil.doDelete(tenantUri, resourceVersion);
98         assertEquals("Expected the cloud region to be created", 204, response.getStatus());
99
100         response = httpTestUtil.doGet(cloudRegionUri);
101         assertEquals("Expected the cloud region to be created", 200, response.getStatus());
102         responseStr = response.getEntity().toString();
103         resourceVersion = JsonPath.read(responseStr, "$.resource-version");
104
105         response = httpTestUtil.doDelete(cloudRegionUri, resourceVersion);
106         assertEquals("Expected the cloud region to be created", 204, response.getStatus());
107     }
108 }