AAI-1523 Batch reformat aai-core
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / rest / EntitlementTest.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.io.IOException;
28 import java.io.UnsupportedEncodingException;
29 import java.util.Arrays;
30 import java.util.Collection;
31
32 import javax.ws.rs.core.Response;
33
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.junit.runners.Parameterized;
39 import org.onap.aai.AAISetup;
40 import org.onap.aai.HttpTestUtil;
41 import org.onap.aai.PayloadUtil;
42 import org.onap.aai.exceptions.AAIException;
43 import org.onap.aai.serialization.engines.QueryStyle;
44 import org.skyscreamer.jsonassert.JSONAssert;
45 import org.springframework.test.annotation.DirtiesContext;
46
47 @RunWith(value = Parameterized.class)
48 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
49 public class EntitlementTest extends AAISetup {
50
51     private HttpTestUtil httpTestUtil;
52
53     @Parameterized.Parameter(value = 0)
54     public QueryStyle queryStyle;
55
56     private String vnfPayload;
57
58     private String vnfUri;
59
60     @Parameterized.Parameters(name = "QueryStyle.{0}")
61     public static Collection<Object[]> data() {
62         return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL_URI}});
63     }
64
65     @Before
66     public void setUp() throws IOException {
67         httpTestUtil = new HttpTestUtil(queryStyle);
68         vnfPayload = PayloadUtil.getResourcePayload("vnf.json");
69         vnfUri = "/aai/v14/network/generic-vnfs/generic-vnf/vnf1";
70     }
71
72     @Test
73     public void testPutGenericVnfAndThenInsertEntitlement() throws IOException, AAIException {
74         String entitlementPayload = PayloadUtil.getResourcePayload("entitlement.json");
75         String entitlementUri = "/aai/v14/network/generic-vnfs/generic-vnf/vnf1/entitlements/entitlement/g1/r1";
76         Response response = httpTestUtil.doPut(vnfUri, vnfPayload);
77         assertEquals("Expected the Generic Vnf to be created", 201, response.getStatus());
78
79         response = httpTestUtil.doGet(vnfUri);
80         assertEquals("Expected the Generic Vnf to be found", 200, response.getStatus());
81         String jsonResponse = response.getEntity().toString();
82         JSONAssert.assertEquals(vnfPayload, jsonResponse, false);
83
84         response = httpTestUtil.doPut(entitlementUri, entitlementPayload);
85         assertEquals("Expected the Entitlement to be created", 201, response.getStatus());
86     }
87
88     @After
89     public void tearDown() throws UnsupportedEncodingException, AAIException {
90         Response response = httpTestUtil.doGet(vnfUri);
91         assertEquals("Expected the Generic Vnf to be found", 200, response.getStatus());
92         String jsonResponse = response.getEntity().toString();
93         String resourceVersion = JsonPath.read(jsonResponse, "$.resource-version");
94         response = httpTestUtil.doDelete(vnfUri, resourceVersion);
95         assertEquals("Expected the cloud region to be deleted", 204, response.getStatus());
96     }
97 }