Merge "Add debugging of REST call"
[policy/drools-applications.git] / controlloop / common / model-impl / appclcm / src / test / java / org / onap / policy / appclcm / AppcLcmTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * appclcm
4  * ================================================================================
5  * Copyright (C) 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
21 package org.onap.policy.appclcm;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.HashMap;
28 import java.util.UUID;
29
30 import org.junit.Test;
31 import org.onap.policy.appclcm.util.Serialization;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class AppcLcmTest {
36
37     private static final Logger logger = LoggerFactory.getLogger(AppcLcmTest.class);
38
39     private static LcmRequestWrapper dmaapRequest;
40     private static LcmResponseWrapper dmaapResponse;
41
42     static {
43         /*
44          * Construct an APPCLCM Request to be Serialized
45          */
46         dmaapRequest = new LcmRequestWrapper();
47         dmaapRequest.setCorrelationId("664be3d2-6c12-4f4b-a3e7-c349acced200" + "-" + "1");
48         dmaapRequest.setRpcName("restart");
49         dmaapRequest.setType("request");
50
51         dmaapResponse = new LcmResponseWrapper();
52         dmaapResponse.setCorrelationId("664be3d2-6c12-4f4b-a3e7-c349acced200" + "-" + "1");
53         dmaapResponse.setRpcName("restart");
54         dmaapResponse.setType("response");
55
56         LcmRequest appcRequest = new LcmRequest();
57
58         appcRequest.setAction("restart");
59
60         HashMap<String, String> actionIdentifiers = new HashMap<>();
61         actionIdentifiers.put("vnf-id", "trial-vnf-003");
62         actionIdentifiers.put("vserver-id", "08f6c1f9-99e7-49f3-a662-c62b9f200d79");
63
64         appcRequest.setActionIdentifiers(actionIdentifiers);
65
66         LcmCommonHeader commonHeader = new LcmCommonHeader();
67         commonHeader.setRequestId(UUID.fromString("664be3d2-6c12-4f4b-a3e7-c349acced200"));
68         commonHeader.setSubRequestId("1");
69         commonHeader.setOriginatorId("664be3d2-6c12-4f4b-a3e7-c349acced200");
70
71         appcRequest.setCommonHeader(commonHeader);
72
73         appcRequest.setPayload(null);
74
75         dmaapRequest.setBody(appcRequest);
76
77         /*
78          * Construct an APPCLCM Response to be Serialized
79          */
80         LcmResponse appcResponse = new LcmResponse(appcRequest);
81         appcResponse.getStatus().setCode(400);
82         appcResponse.getStatus().setMessage("Restart Successful");
83         appcResponse.setPayload(null);
84
85         dmaapResponse.setBody(appcResponse);
86     }
87
88     @Test
89     public void testRequestSerialization() {
90
91         /*
92          * Use the gson serializer to obtain json
93          */
94         String jsonRequest = Serialization.gson.toJson(dmaapRequest, LcmRequestWrapper.class);
95         assertNotNull(jsonRequest);
96
97         /*
98          * The serializer should have added an extra sub-tag called "input" that wraps the request
99          */
100         assertTrue(jsonRequest.contains("input"));
101
102         /*
103          * The common-header, request-id, and sub-request-id should exist
104          */
105         assertTrue(jsonRequest.contains("common-header"));
106         assertTrue(jsonRequest.contains("request-id"));
107         assertTrue(jsonRequest.contains("sub-request-id"));
108
109         /*
110          * action-identifiers should exist and contain a vnf-id
111          */
112         assertTrue(jsonRequest.contains("action-identifiers"));
113         assertTrue(jsonRequest.contains("vnf-id"));
114
115         /*
116          * The action sub-tag should exist
117          */
118         assertTrue(jsonRequest.contains("action"));
119
120         logger.debug("Request as JSON: " + jsonRequest + "\n\n");
121     }
122
123     @Test
124     public void testRequestDeserialization() {
125
126         /*
127          * Convert the LCM request object into json so we have a string of json to use for testing
128          */
129         String jsonRequest = Serialization.gson.toJson(dmaapRequest, LcmRequestWrapper.class);
130
131         /*
132          * Use the serializer to convert the json string into a java object
133          */
134         LcmRequestWrapper dmaapRequest = Serialization.gson.fromJson(jsonRequest, LcmRequestWrapper.class);
135         assertNotNull(dmaapRequest);
136
137         /*
138          * The type of the DMAAP wrapper should be request
139          */
140         assertEquals("request", dmaapRequest.getType());
141
142         /*
143          * The DMAAP wrapper must have a body as that is the true APPC request
144          */
145         assertNotNull(dmaapRequest.getBody());
146         LcmRequest appcRequest = dmaapRequest.getBody();
147         assertNotNull(appcRequest);
148
149         /*
150          * The common header should not be null
151          */
152         assertNotNull(appcRequest.getCommonHeader());
153
154         /*
155          * The action should not be null and should be set to restart
156          */
157         assertNotNull(appcRequest.getAction());
158         assertEquals("restart", appcRequest.getAction());
159
160         /*
161          * The action-identifiers should not be null and should contain a vnf-id
162          */
163         assertNotNull(appcRequest.getActionIdentifiers());
164         assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
165
166         logger.debug("Request as a Java Object: \n" + appcRequest.toString() + "\n\n");
167     }
168
169     @Test
170     public void testResponseSerialization() {
171
172         /*
173          * Use the serializer to convert the object into json
174          */
175         String jsonResponse = Serialization.gson.toJson(dmaapResponse, LcmResponseWrapper.class);
176         assertNotNull(jsonResponse);
177
178         /*
179          * The serializer should have added an extra sub-tag called "input" that wraps the request
180          */
181         assertTrue(jsonResponse.contains("output"));
182
183         /*
184          * The response should contain a common-header, request-id, sub-request-id, and status
185          */
186         assertTrue(jsonResponse.contains("common-header"));
187         assertTrue(jsonResponse.contains("request-id"));
188         assertTrue(jsonResponse.contains("sub-request-id"));
189         assertTrue(jsonResponse.contains("status"));
190
191         logger.debug("Response as JSON: " + jsonResponse + "\n\n");
192     }
193
194     @Test
195     public void testResponseDeserialization() {
196         /*
197          * Convert the LCM response object into json so we have a string of json to use for testing
198          */
199         String jsonResponse = Serialization.gson.toJson(dmaapResponse, LcmResponseWrapper.class);
200
201         /*
202          * Use the serializer to convert the json string into a java object
203          */
204         LcmResponseWrapper dmaapResponse = Serialization.gson.fromJson(jsonResponse, LcmResponseWrapper.class);
205         assertNotNull(dmaapResponse);
206
207         /*
208          * The type of the DMAAP wrapper should be response
209          */
210         assertEquals("response", dmaapResponse.getType());
211
212         /*
213          * The DMAAP wrapper must have a body as that is the true APPC response
214          */
215         assertNotNull(dmaapResponse.getBody());
216         LcmResponse appcResponse = dmaapResponse.getBody();
217         assertNotNull(appcResponse);
218
219         /*
220          * The common header should not be null
221          */
222         assertNotNull(appcResponse.getCommonHeader());
223
224         /*
225          * The status should not be null and the status code should be 400
226          */
227         assertNotNull(appcResponse.getStatus());
228         assertEquals(400, appcResponse.getStatus().getCode());
229
230         logger.debug("Response as a Java Object: \n" + appcResponse.toString() + "\n\n");
231     }
232 }