e031a3269dc0b28ac2500e175adc5ea7efa07a11
[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 package org.onap.aai.rest;
21
22 import com.jayway.jsonpath.JsonPath;
23 import org.junit.Before;
24 import org.junit.ClassRule;
25 import org.junit.Ignore;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.Parameterized;
30 import org.onap.aai.AAISetup;
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;
35 import org.springframework.test.context.junit4.rules.SpringClassRule;
36 import org.springframework.test.context.junit4.rules.SpringMethodRule;
37
38 import javax.ws.rs.core.Response;
39 import java.util.*;
40
41 import static org.junit.Assert.assertEquals;
42
43 @RunWith(value = Parameterized.class)
44 public class TenantTest extends AAISetup {
45
46     private HttpTestUtil httpTestUtil;
47     private Map<String, String> templateValuesMap;
48     @ClassRule
49     public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
50
51     @Rule
52     public final SpringMethodRule springMethodRule = new SpringMethodRule();
53     
54     @Parameterized.Parameter(value = 0)
55     public QueryStyle queryStyle;
56
57     @Parameterized.Parameters(name = "QueryStyle.{0}")
58     public static Collection<Object[]> data() {
59         return Arrays.asList(new Object[][]{
60                 {QueryStyle.TRAVERSAL},
61                 {QueryStyle.TRAVERSAL_URI}
62         });
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"),
83                 templateValuesMap.get("cloud-region-id")
84         );
85
86         String tenantUri = cloudRegionUri + "/tenants/tenant/" + templateValuesMap.get("tenant-id");
87         String tenantPayload = PayloadUtil.getTemplatePayload("tenant.json", templateValuesMap);
88
89         Response response     = httpTestUtil.doPut(cloudRegionUri, cloudRegionPayload);
90         assertEquals("Expected the cloud region to be created", 201, response.getStatus());
91
92         response     = httpTestUtil.doGet(tenantUri);
93         assertEquals("Expected the cloud region to be created", 200, response.getStatus());
94         String responseStr = response.getEntity().toString();
95
96         JSONAssert.assertEquals(tenantPayload, responseStr, false);
97         String resourceVersion = JsonPath.read(responseStr, "$.resource-version");
98
99         response     = httpTestUtil.doDelete(tenantUri, resourceVersion);
100         assertEquals("Expected the cloud region to be created", 204, response.getStatus());
101
102         response     = httpTestUtil.doGet(cloudRegionUri);
103         assertEquals("Expected the cloud region to be created", 200, response.getStatus());
104         responseStr = response.getEntity().toString();
105         resourceVersion = JsonPath.read(responseStr, "$.resource-version");
106
107         response     = httpTestUtil.doDelete(cloudRegionUri, resourceVersion);
108         assertEquals("Expected the cloud region to be created", 204, response.getStatus());
109     }
110 }