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.onap.aai.AAIJunitRunner;
29 import org.onap.aai.HttpTestUtil;
30 import org.onap.aai.PayloadUtil;
31 import org.onap.aai.introspection.*;
32 import org.skyscreamer.jsonassert.JSONAssert;
34 import javax.ws.rs.core.Response;
35 import java.util.HashMap;
38 import static org.junit.Assert.assertEquals;
39 import static org.junit.Assert.assertNotNull;
41 @RunWith(AAIJunitRunner.class)
42 public class PserverTest {
44 private static EELFLogger logger = EELFManager.getInstance().getLogger(PserverTest.class);
45 private HttpTestUtil httpTestUtil;
46 private Map<String, String> relationshipMap;
50 httpTestUtil = new HttpTestUtil();
51 relationshipMap = new HashMap<>();
55 public void testPutPServerCreateGetAndDeleteAndCreateRelationshipBetweenPserverAndCloudRegion() throws Exception {
57 logger.info("Starting the pserver testPutServerCreateGetAndDelete");
59 String pserverUri = "/aai/v12/cloud-infrastructure/pservers/pserver/test-pserver";
60 String cloudRegionUri = "/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/test1/test2";
61 String cloudRegionRelationshipUri = cloudRegionUri + "/relationship-list/relationship";
63 Response response = httpTestUtil.doGet(pserverUri);
64 assertNotNull("Expected the response to be not null", response);
65 assertEquals("Expecting the pserver to be not found", 404, response.getStatus());
66 logger.info("Verifying that the pserver is already not in the database successfully");
68 Map<String, String> templateValueMap = new HashMap<>();
69 templateValueMap.put("hostname", "test-pserver");
70 String pserverPayload = PayloadUtil.getTemplatePayload("pserver.json", templateValueMap);
72 response = httpTestUtil.doPut(pserverUri, pserverPayload);
73 assertNotNull("Expected the response to be not null", response);
74 assertEquals("Expecting the pserver to be created", 201, response.getStatus());
75 logger.info("Successfully created the pserver into db");
77 response = httpTestUtil.doGet(pserverUri);
78 assertNotNull("Expected the response to be not null", response);
79 assertEquals("Expecting the pserver to be found", 200, response.getStatus());
81 JSONAssert.assertEquals(pserverPayload, response.getEntity().toString(), false);
82 logger.info("Successfully retrieved the created pserver from db and verified with put data");
84 response = httpTestUtil.doPut(cloudRegionUri, "{}");
85 assertNotNull("Expected the response to be not null", response);
86 assertEquals("Expect the cloud region to be created", 201, response.getStatus());
87 logger.info("Successfully able to create the cloud region with payload that has no keys to be retrieved from uri");
89 response = httpTestUtil.doGet(cloudRegionUri);
90 assertNotNull("Expected the response to be not null", response);
91 assertEquals("Expecting the cloud region to be found", 200, response.getStatus());
92 logger.info("Successfully retrieved the cloud region from db");
94 relationshipMap.put("related-to", "pserver");
95 relationshipMap.put("related-link", pserverUri);
97 String pserverRelationshipPayload = PayloadUtil.getTemplatePayload("relationship.json", relationshipMap);
98 // Creates the relationship between cloud region and pserver
99 response = httpTestUtil.doPut(cloudRegionRelationshipUri, pserverRelationshipPayload);
100 assertNotNull("Expected the response to be not null", response);
101 assertEquals("Expect the cloud region relationship to pserver to be created", 200, response.getStatus());
102 logger.info("Successfully created the relationship between cloud region and pserver");
104 response = httpTestUtil.doGet(cloudRegionUri);
105 assertNotNull("Expected the response to be not null", response);
106 assertEquals("Expect the cloud region to be created", 200, response.getStatus());
107 logger.info("Successfully retrieved the cloud region from db");
109 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.getLatest());
110 Introspector in = loader.unmarshal("cloud-region", response.getEntity().toString());
112 System.out.println(in.marshal(true));
113 String resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
115 response = httpTestUtil.doDelete(cloudRegionUri, resourceVersion);
116 assertNotNull("Expected the response to be not null", response);
117 assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
118 logger.info("Successfully deleted the cloud region from db");
120 response = httpTestUtil.doGet(pserverUri);
121 assertNotNull("Expected the response to be not null", response);
122 assertEquals("Expecting the pserver to be not found", 200, response.getStatus());
123 resourceVersion = JsonPath.read(response.getEntity().toString(), "$.resource-version");
124 logger.info("Successfully retrieved the cloud region from db to get the latest resource version");
126 response = httpTestUtil.doDelete(pserverUri, resourceVersion);
127 assertNotNull("Expected the response to be not null", response);
128 assertEquals("Expecting the cloud region to be deleted", 204, response.getStatus());
129 logger.info("Successfully deleted the pserver from db");
131 logger.info("Ending the pserver testPutServerCreateGetAndDelete");