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=========================================================
21 package org.onap.aai.rest;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.jayway.jsonpath.JsonPath;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.HashMap;
35 import javax.ws.rs.core.Response;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.junit.runners.Parameterized;
41 import org.onap.aai.AAISetup;
42 import org.onap.aai.HttpTestUtil;
43 import org.onap.aai.PayloadUtil;
44 import org.onap.aai.introspection.*;
45 import org.onap.aai.serialization.engines.QueryStyle;
46 import org.skyscreamer.jsonassert.JSONAssert;
47 import org.springframework.test.annotation.DirtiesContext;
49 @RunWith(value = Parameterized.class)
50 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
51 public class PserverTest extends AAISetup {
53 private static EELFLogger logger = EELFManager.getInstance().getLogger(PserverTest.class);
54 private HttpTestUtil httpTestUtil;
55 private Map<String, String> relationshipMap;
57 @Parameterized.Parameter(value = 0)
58 public QueryStyle queryStyle;
60 @Parameterized.Parameters(name = "QueryStyle.{0}")
61 public static Collection<Object[]> data() {
62 return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {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());
105 "Successfully able to create the cloud region with payload that has no keys to be retrieved from uri");
107 response = httpTestUtil.doGet(cloudRegionUri);
108 assertNotNull("Expected the response to be not null", response);
109 assertEquals("Expecting the cloud region to be found", 200, response.getStatus());
110 logger.info("Successfully retrieved the cloud region from db");
112 relationshipMap.put("related-to", "pserver");
113 relationshipMap.put("related-link", pserverUri);
115 String pserverRelationshipPayload = PayloadUtil.getTemplatePayload("relationship.json", relationshipMap);
116 // Creates the relationship between cloud region and pserver
117 response = httpTestUtil.doPut(cloudRegionRelationshipUri, pserverRelationshipPayload);
118 assertNotNull("Expected the response to be not null", response);
119 assertEquals("Expect the cloud region relationship to pserver to be created", 200, response.getStatus());
120 logger.info("Successfully created the relationship between cloud region and pserver");
122 response = httpTestUtil.doGet(cloudRegionUri);
123 assertNotNull("Expected the response to be not null", response);
124 assertEquals("Expect the cloud region to be created", 200, response.getStatus());
125 logger.info("Successfully retrieved the cloud region from db");
127 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
128 Introspector in = loader.unmarshal("cloud-region", response.getEntity().toString());
130 String resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
132 response = httpTestUtil.doDelete(cloudRegionUri, resourceVersion);
133 assertNotNull("Expected the response to be not null", response);
134 assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
135 logger.info("Successfully deleted the cloud region from db");
137 response = httpTestUtil.doGet(pserverUri);
138 assertNotNull("Expected the response to be not null", response);
139 assertEquals("Expecting the pserver to be not found", 200, response.getStatus());
140 resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
141 logger.info("Successfully retrieved the cloud region from db to get the latest resource version");
143 response = httpTestUtil.doDelete(pserverUri, resourceVersion);
144 assertNotNull("Expected the response to be not null", response);
145 assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
146 logger.info("Successfully deleted the pserver from db");
148 logger.info("Ending the pserver testPutServerCreateGetAndDelete");