Improve the performance of resoures microservice
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / rest / ModelElementTest.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 ModelElementTest extends AAISetup {
47
48     private HttpTestUtil httpTestUtil;
49
50     @Parameterized.Parameter(value = 0)
51     public QueryStyle queryStyle;
52
53     private String modelPayload;
54     private String modelElementPayload;
55
56     private String modelUri;
57     private String modelElementUri;
58
59     @Parameterized.Parameters(name = "QueryStyle.{0}")
60     public static Collection<Object[]> data() {
61         return Arrays.asList(new Object[][]{
62             {QueryStyle.TRAVERSAL_URI}
63         });
64     }
65
66     @Before
67     public void setUp() throws IOException {
68         httpTestUtil = new HttpTestUtil(queryStyle);
69         modelPayload = PayloadUtil.getResourcePayload("model.json");
70         modelElementPayload = PayloadUtil.getResourcePayload("model-element.json");
71         modelUri = "/aai/v14/service-design-and-creation/models/model/24c04fc5-f3f8-43ec-8792-c6f940638676-test1";
72         modelElementUri = "/aai/v14/service-design-and-creation/models/model/24c04fc5-f3f8-43ec-8792-c6f940638676-test1/model-vers/model-ver/0c4c59f0-9864-43ca-b0c2-ca38746b72a5-test1/model-elements/model-element/0dc2b8b6-af8d-4213-970b-7861a603fc86-test1/model-constraints/model-constraint/782ba24a-28ab-4fd0-8e69-da10cc5373f3-test1/constrained-element-sets/constrained-element-set/a33e65c3-1198-4d4c-9799-2b521e4c4212-test1/element-choice-sets/element-choice-set/7df27a94-06c8-46ef-9fc2-5900d8cffbb0-test1/model-elements/model-element/acf8b6cf-e051-4c1b-bcad-b24792f826cf-test1";
73     }
74
75     @Test
76     public void testPutModelAndThenModelElementAndItShouldSucceed() throws IOException, AAIException {
77         Response response     = httpTestUtil.doPut(modelUri, modelPayload);
78         assertEquals("Expected the cloud region to be created", 201, response.getStatus());
79
80         response = httpTestUtil.doGet(modelUri);
81         assertEquals("Expected the cloud region to be found", 200, response.getStatus());
82         String jsonResponse = response.getEntity().toString();
83         JSONAssert.assertEquals(modelPayload, jsonResponse, false);
84
85         response     = httpTestUtil.doPut(modelElementUri, modelElementPayload);
86         assertEquals("Expected the cloud region to be created", 201, response.getStatus());
87     }
88
89     @After
90     public void tearDown() throws UnsupportedEncodingException, AAIException {
91         Response response = httpTestUtil.doGet(modelUri);
92         assertEquals("Expected the cloud region to be found", 200, response.getStatus());
93         String jsonResponse = response.getEntity().toString();
94         String resourceVersion = JsonPath.read(jsonResponse, "$.resource-version");
95         response = httpTestUtil.doDelete(modelUri, resourceVersion);
96         assertEquals("Expected the cloud region to be deleted", 204, response.getStatus());
97     }
98 }