2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.appclcm;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
28 import java.util.HashMap;
29 import java.util.UUID;
31 import org.junit.Test;
32 import org.onap.policy.appclcm.util.Serialization;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class AppcLcmTest {
38 private static final Logger logger = LoggerFactory.getLogger(AppcLcmTest.class);
40 private static LcmRequestWrapper dmaapRequest;
41 private static LcmResponseWrapper dmaapResponse;
45 * Construct an APPCLCM Request to be Serialized
47 dmaapRequest = new LcmRequestWrapper();
48 dmaapRequest.setCorrelationId("664be3d2-6c12-4f4b-a3e7-c349acced200" + "-" + "1");
49 dmaapRequest.setRpcName("restart");
50 dmaapRequest.setType("request");
52 dmaapResponse = new LcmResponseWrapper();
53 dmaapResponse.setCorrelationId("664be3d2-6c12-4f4b-a3e7-c349acced200" + "-" + "1");
54 dmaapResponse.setRpcName("restart");
55 dmaapResponse.setType("response");
57 LcmRequest appcRequest = new LcmRequest();
59 appcRequest.setAction("restart");
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");
65 appcRequest.setActionIdentifiers(actionIdentifiers);
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");
72 appcRequest.setCommonHeader(commonHeader);
74 appcRequest.setPayload(null);
76 dmaapRequest.setBody(appcRequest);
79 * Construct an APPCLCM Response to be Serialized
81 LcmResponse appcResponse = new LcmResponse(appcRequest);
82 appcResponse.getStatus().setCode(400);
83 appcResponse.getStatus().setMessage("Restart Successful");
84 appcResponse.setPayload(null);
86 dmaapResponse.setBody(appcResponse);
90 public void testRequestSerialization() {
93 * Use the gson serializer to obtain json
95 String jsonRequest = Serialization.gson.toJson(dmaapRequest, LcmRequestWrapper.class);
96 assertNotNull(jsonRequest);
99 * The serializer should have added an extra sub-tag called "input" that wraps the request
101 assertTrue(jsonRequest.contains("input"));
104 * The common-header, request-id, and sub-request-id should exist
106 assertTrue(jsonRequest.contains("common-header"));
107 assertTrue(jsonRequest.contains("request-id"));
108 assertTrue(jsonRequest.contains("sub-request-id"));
111 * action-identifiers should exist and contain a vnf-id
113 assertTrue(jsonRequest.contains("action-identifiers"));
114 assertTrue(jsonRequest.contains("vnf-id"));
117 * The action sub-tag should exist
119 assertTrue(jsonRequest.contains("action"));
121 logger.debug("Request as JSON: " + jsonRequest + "\n\n");
125 public void testRequestDeserialization() {
128 * Convert the LCM request object into json so we have a string of json to use for testing
130 String jsonRequest = Serialization.gson.toJson(dmaapRequest, LcmRequestWrapper.class);
133 * Use the serializer to convert the json string into a java object
135 LcmRequestWrapper dmaapRequest = Serialization.gson.fromJson(jsonRequest, LcmRequestWrapper.class);
136 assertNotNull(dmaapRequest);
139 * The type of the DMAAP wrapper should be request
141 assertEquals("request", dmaapRequest.getType());
144 * The DMAAP wrapper must have a body as that is the true APPC request
146 assertNotNull(dmaapRequest.getBody());
147 LcmRequest appcRequest = dmaapRequest.getBody();
148 assertNotNull(appcRequest);
151 * The common header should not be null
153 assertNotNull(appcRequest.getCommonHeader());
156 * The action should not be null and should be set to restart
158 assertNotNull(appcRequest.getAction());
159 assertEquals("restart", appcRequest.getAction());
162 * The action-identifiers should not be null and should contain a vnf-id
164 assertNotNull(appcRequest.getActionIdentifiers());
165 assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
167 logger.debug("Request as a Java Object: \n" + appcRequest.toString() + "\n\n");
171 public void testResponseSerialization() {
174 * Use the serializer to convert the object into json
176 String jsonResponse = Serialization.gson.toJson(dmaapResponse, LcmResponseWrapper.class);
177 assertNotNull(jsonResponse);
180 * The serializer should have added an extra sub-tag called "input" that wraps the request
182 assertTrue(jsonResponse.contains("output"));
185 * The response should contain a common-header, request-id, sub-request-id, and status
187 assertTrue(jsonResponse.contains("common-header"));
188 assertTrue(jsonResponse.contains("request-id"));
189 assertTrue(jsonResponse.contains("sub-request-id"));
190 assertTrue(jsonResponse.contains("status"));
192 logger.debug("Response as JSON: " + jsonResponse + "\n\n");
196 public void testResponseDeserialization() {
198 * Convert the LCM response object into json so we have a string of json to use for testing
200 String jsonResponse = Serialization.gson.toJson(dmaapResponse, LcmResponseWrapper.class);
203 * Use the serializer to convert the json string into a java object
205 LcmResponseWrapper dmaapResponse = Serialization.gson.fromJson(jsonResponse, LcmResponseWrapper.class);
206 assertNotNull(dmaapResponse);
209 * The type of the DMAAP wrapper should be response
211 assertEquals("response", dmaapResponse.getType());
214 * The DMAAP wrapper must have a body as that is the true APPC response
216 assertNotNull(dmaapResponse.getBody());
217 LcmResponse appcResponse = dmaapResponse.getBody();
218 assertNotNull(appcResponse);
221 * The common header should not be null
223 assertNotNull(appcResponse.getCommonHeader());
226 * The status should not be null and the status code should be 400
228 assertNotNull(appcResponse.getStatus());
229 assertEquals(400, appcResponse.getStatus().getCode());
231 logger.debug("Response as a Java Object: \n" + appcResponse.toString() + "\n\n");