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