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