2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.rest;
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import com.jayway.jsonpath.JsonPath;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.Parameterized;
29 import org.onap.aai.AAISetup;
30 import org.onap.aai.HttpTestUtil;
31 import org.onap.aai.PayloadUtil;
32 import org.onap.aai.introspection.*;
34 import org.onap.aai.serialization.engines.QueryStyle;
35 import org.skyscreamer.jsonassert.JSONAssert;
37 import javax.ws.rs.core.Response;
38 import java.util.Arrays;
39 import java.util.Collection;
40 import java.util.HashMap;
43 import static org.junit.Assert.assertEquals;
44 import static org.junit.Assert.assertNotNull;
46 @RunWith(value = Parameterized.class)
47 public class PserverTest extends AAISetup{
49 private static EELFLogger logger = EELFManager.getInstance().getLogger(PserverTest.class);
50 private HttpTestUtil httpTestUtil;
51 private Map<String, String> relationshipMap;
53 @Parameterized.Parameter(value = 0)
54 public QueryStyle queryStyle;
56 @Parameterized.Parameters(name = "QueryStyle.{0}")
57 public static Collection<Object[]> data() {
58 return Arrays.asList(new Object[][]{
59 {QueryStyle.TRAVERSAL},
60 {QueryStyle.TRAVERSAL_URI}
67 httpTestUtil = new HttpTestUtil(queryStyle);
68 relationshipMap = new HashMap<>();
72 public void testPutPServerCreateGetAndDeleteAndCreateRelationshipBetweenPserverAndCloudRegion() throws Exception {
74 logger.info("Starting the pserver testPutServerCreateGetAndDelete");
76 String pserverUri = "/aai/v12/cloud-infrastructure/pservers/pserver/test-pserver";
77 String cloudRegionUri = "/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/test1/test2";
78 String cloudRegionRelationshipUri = cloudRegionUri + "/relationship-list/relationship";
80 Response response = httpTestUtil.doGet(pserverUri);
81 assertNotNull("Expected the response to be not null", response);
82 assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
83 logger.info("Verifying that the pserver is already not in the database successfully");
85 Map<String, String> templateValueMap = new HashMap<>();
86 templateValueMap.put("hostname", "test-pserver");
87 String pserverPayload = PayloadUtil.getTemplatePayload("pserver.json", templateValueMap);
89 response = httpTestUtil.doPut(pserverUri, pserverPayload);
90 assertNotNull("Expected the response to be not null", response);
91 assertEquals("Expecting the pserver to be created", 201, response.getStatus());
92 logger.info("Successfully created the pserver into db");
94 response = httpTestUtil.doGet(pserverUri);
95 assertNotNull("Expected the response to be not null", response);
96 assertEquals("Expecting the pserver to be found", 200, response.getStatus());
98 JSONAssert.assertEquals(pserverPayload, response.getEntity().toString(), false);
99 logger.info("Successfully retrieved the created pserver from db and verified with put data");
101 response = httpTestUtil.doPut(cloudRegionUri, "{}");
102 assertNotNull("Expected the response to be not null", response);
103 assertEquals("Expect the cloud region to be created", 201, response.getStatus());
104 logger.info("Successfully able to create the cloud region with payload that has no keys to be retrieved from uri");
106 response = httpTestUtil.doGet(cloudRegionUri);
107 assertNotNull("Expected the response to be not null", response);
108 assertEquals("Expecting the cloud region to be found", 200, response.getStatus());
109 logger.info("Successfully retrieved the cloud region from db");
111 relationshipMap.put("related-to", "pserver");
112 relationshipMap.put("related-link", pserverUri);
114 String pserverRelationshipPayload = PayloadUtil.getTemplatePayload("relationship.json", relationshipMap);
115 // Creates the relationship between cloud region and pserver
116 response = httpTestUtil.doPut(cloudRegionRelationshipUri, pserverRelationshipPayload);
117 assertNotNull("Expected the response to be not null", response);
118 assertEquals("Expect the cloud region relationship to pserver to be created", 200, response.getStatus());
119 logger.info("Successfully created the relationship between cloud region and pserver");
121 response = httpTestUtil.doGet(cloudRegionUri);
122 assertNotNull("Expected the response to be not null", response);
123 assertEquals("Expect the cloud region to be created", 200, response.getStatus());
124 logger.info("Successfully retrieved the cloud region from db");
126 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
127 Introspector in = loader.unmarshal("cloud-region", response.getEntity().toString());
129 String resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
131 response = httpTestUtil.doDelete(cloudRegionUri, resourceVersion);
132 assertNotNull("Expected the response to be not null", response);
133 assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
134 logger.info("Successfully deleted the cloud region from db");
136 response = httpTestUtil.doGet(pserverUri);
137 assertNotNull("Expected the response to be not null", response);
138 assertEquals("Expecting the pserver to be not found", 200, response.getStatus());
139 resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
140 logger.info("Successfully retrieved the cloud region from db to get the latest resource version");
142 response = httpTestUtil.doDelete(pserverUri, resourceVersion);
143 assertNotNull("Expected the response to be not null", response);
144 assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
145 logger.info("Successfully deleted the pserver from db");
147 logger.info("Ending the pserver testPutServerCreateGetAndDelete");