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