Merge "Fix more sonar issues in models: yaml to dao"
[policy/models.git] / models-interactions / model-impl / appclcm / src / test / java / org / onap / policy / appclcm / AppcLcmTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * appclcm
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.appclcm;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.util.HashMap;
29 import java.util.UUID;
30
31 import org.junit.Test;
32 import org.onap.policy.appclcm.util.Serialization;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class AppcLcmTest {
37
38     private static final String VNF_ID_KEY = "vnf-id";
39
40     private static final String RESTART = "restart";
41
42     private static final String CORRELATION_ID = "664be3d2-6c12-4f4b-a3e7-c349acced200";
43
44     private static final Logger logger = LoggerFactory.getLogger(AppcLcmTest.class);
45
46     private static LcmRequestWrapper dmaapRequest;
47     private static LcmResponseWrapper dmaapResponse;
48
49     static {
50         /*
51          * Construct an APPCLCM Request to be Serialized
52          */
53         dmaapRequest = new LcmRequestWrapper();
54         dmaapRequest.setCorrelationId(CORRELATION_ID + "-" + "1");
55         dmaapRequest.setRpcName(RESTART);
56         dmaapRequest.setType("request");
57
58         dmaapResponse = new LcmResponseWrapper();
59         dmaapResponse.setCorrelationId(CORRELATION_ID + "-" + "1");
60         dmaapResponse.setRpcName(RESTART);
61         dmaapResponse.setType("response");
62
63         LcmRequest appcRequest = new LcmRequest();
64
65         appcRequest.setAction(RESTART);
66
67         HashMap<String, String> actionIdentifiers = new HashMap<>();
68         actionIdentifiers.put(VNF_ID_KEY, "trial-vnf-003");
69         actionIdentifiers.put("vserver-id", "08f6c1f9-99e7-49f3-a662-c62b9f200d79");
70
71         appcRequest.setActionIdentifiers(actionIdentifiers);
72
73         LcmCommonHeader commonHeader = new LcmCommonHeader();
74         commonHeader.setRequestId(UUID.fromString(CORRELATION_ID));
75         commonHeader.setSubRequestId("1");
76         commonHeader.setOriginatorId(CORRELATION_ID);
77
78         appcRequest.setCommonHeader(commonHeader);
79
80         appcRequest.setPayload(null);
81
82         dmaapRequest.setBody(appcRequest);
83
84         /*
85          * Construct an APPCLCM Response to be Serialized
86          */
87         LcmResponse appcResponse = new LcmResponse(appcRequest);
88         appcResponse.getStatus().setCode(400);
89         appcResponse.getStatus().setMessage("Restart Successful");
90         appcResponse.setPayload(null);
91
92         dmaapResponse.setBody(appcResponse);
93     }
94
95     @Test
96     public void testRequestSerialization() {
97
98         /*
99          * Use the gson serializer to obtain json
100          */
101         String jsonRequest = Serialization.gson.toJson(dmaapRequest, LcmRequestWrapper.class);
102         assertNotNull(jsonRequest);
103
104         /*
105          * The serializer should have added an extra sub-tag called "input" that wraps the request
106          */
107         assertTrue(jsonRequest.contains("input"));
108
109         /*
110          * The common-header, request-id, and sub-request-id should exist
111          */
112         assertTrue(jsonRequest.contains("common-header"));
113         assertTrue(jsonRequest.contains("request-id"));
114         assertTrue(jsonRequest.contains("sub-request-id"));
115
116         /*
117          * action-identifiers should exist and contain a vnf-id
118          */
119         assertTrue(jsonRequest.contains("action-identifiers"));
120         assertTrue(jsonRequest.contains(VNF_ID_KEY));
121
122         /*
123          * The action sub-tag should exist
124          */
125         assertTrue(jsonRequest.contains("action"));
126
127         logger.debug("Request as JSON: " + jsonRequest + "\n\n");
128     }
129
130     @Test
131     public void testRequestDeserialization() {
132
133         /*
134          * Convert the LCM request object into json so we have a string of json to use for testing
135          */
136         String jsonRequest = Serialization.gson.toJson(dmaapRequest, LcmRequestWrapper.class);
137
138         /*
139          * Use the serializer to convert the json string into a java object
140          */
141         LcmRequestWrapper req = Serialization.gson.fromJson(jsonRequest, LcmRequestWrapper.class);
142         assertNotNull(req);
143
144         /*
145          * The type of the DMAAP wrapper should be request
146          */
147         assertEquals("request", req.getType());
148
149         /*
150          * The DMAAP wrapper must have a body as that is the true APPC request
151          */
152         assertNotNull(req.getBody());
153         LcmRequest appcRequest = req.getBody();
154         assertNotNull(appcRequest);
155
156         /*
157          * The common header should not be null
158          */
159         assertNotNull(appcRequest.getCommonHeader());
160
161         /*
162          * The action should not be null and should be set to restart
163          */
164         assertNotNull(appcRequest.getAction());
165         assertEquals(RESTART, appcRequest.getAction());
166
167         /*
168          * The action-identifiers should not be null and should contain a vnf-id
169          */
170         assertNotNull(appcRequest.getActionIdentifiers());
171         assertNotNull(appcRequest.getActionIdentifiers().get(VNF_ID_KEY));
172
173         logger.debug("Request as a Java Object: \n" + appcRequest.toString() + "\n\n");
174     }
175
176     @Test
177     public void testResponseSerialization() {
178
179         /*
180          * Use the serializer to convert the object into json
181          */
182         String jsonResponse = Serialization.gson.toJson(dmaapResponse, LcmResponseWrapper.class);
183         assertNotNull(jsonResponse);
184
185         /*
186          * The serializer should have added an extra sub-tag called "input" that wraps the request
187          */
188         assertTrue(jsonResponse.contains("output"));
189
190         /*
191          * The response should contain a common-header, request-id, sub-request-id, and status
192          */
193         assertTrue(jsonResponse.contains("common-header"));
194         assertTrue(jsonResponse.contains("request-id"));
195         assertTrue(jsonResponse.contains("sub-request-id"));
196         assertTrue(jsonResponse.contains("status"));
197
198         logger.debug("Response as JSON: " + jsonResponse + "\n\n");
199     }
200
201     @Test
202     public void testResponseDeserialization() {
203         /*
204          * Convert the LCM response object into json so we have a string of json to use for testing
205          */
206         String jsonResponse = Serialization.gson.toJson(dmaapResponse, LcmResponseWrapper.class);
207
208         /*
209          * Use the serializer to convert the json string into a java object
210          */
211         LcmResponseWrapper resp = Serialization.gson.fromJson(jsonResponse, LcmResponseWrapper.class);
212         assertNotNull(resp);
213
214         /*
215          * The type of the DMAAP wrapper should be response
216          */
217         assertEquals("response", resp.getType());
218
219         /*
220          * The DMAAP wrapper must have a body as that is the true APPC response
221          */
222         assertNotNull(resp.getBody());
223         LcmResponse appcResponse = resp.getBody();
224         assertNotNull(appcResponse);
225
226         /*
227          * The common header should not be null
228          */
229         assertNotNull(appcResponse.getCommonHeader());
230
231         /*
232          * The status should not be null and the status code should be 400
233          */
234         assertNotNull(appcResponse.getStatus());
235         assertEquals(400, appcResponse.getStatus().getCode());
236
237         logger.debug("Response as a Java Object: \n" + appcResponse.toString() + "\n\n");
238     }
239 }