2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.appclcm;
23 import static org.junit.Assert.*;
25 import java.util.HashMap;
26 import java.util.UUID;
28 import org.junit.Test;
29 import org.onap.policy.appclcm.util.Serialization;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class AppcLcmTest {
35 private static final Logger logger = LoggerFactory.getLogger(AppcLcmTest.class);
37 private static LCMRequestWrapper dmaapRequest;
38 private static LCMResponseWrapper dmaapResponse;
42 * Construct an APPCLCM Request to be Serialized
44 dmaapRequest = new LCMRequestWrapper();
45 dmaapRequest.setCorrelationId("664be3d2-6c12-4f4b-a3e7-c349acced200" + "-" + "1");
46 dmaapRequest.setRpcName("restart");
47 dmaapRequest.setType("request");
49 dmaapResponse = new LCMResponseWrapper();
50 dmaapResponse.setCorrelationId("664be3d2-6c12-4f4b-a3e7-c349acced200" + "-" + "1");
51 dmaapResponse.setRpcName("restart");
52 dmaapResponse.setType("response");
54 LCMRequest appcRequest = new LCMRequest();
56 appcRequest.setAction("restart");
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");
62 appcRequest.setActionIdentifiers(actionIdentifiers);
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");
69 appcRequest.setCommonHeader(commonHeader);
71 appcRequest.setPayload(null);
73 dmaapRequest.setBody(appcRequest);
76 * Construct an APPCLCM Response to be Serialized
78 LCMResponse appcResponse = new LCMResponse(appcRequest);
79 appcResponse.getStatus().setCode(400);
80 appcResponse.getStatus().setMessage("Restart Successful");
81 appcResponse.setPayload(null);
83 dmaapResponse.setBody(appcResponse);
87 public void testRequestSerialization() {
90 * Use the gson serializer to obtain json
92 String jsonRequest = Serialization.gson.toJson(dmaapRequest, LCMRequestWrapper.class);
93 assertNotNull(jsonRequest);
96 * The serializer should have added an extra
97 * sub-tag called "input" that wraps the request
99 assertTrue(jsonRequest.contains("input"));
102 * The common-header, request-id, and
103 * sub-request-id should exist
105 assertTrue(jsonRequest.contains("common-header"));
106 assertTrue(jsonRequest.contains("request-id"));
107 assertTrue(jsonRequest.contains("sub-request-id"));
110 * action-identifiers should exist and contain
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
129 * so we have a string of json to use for testing
131 String jsonRequest = Serialization.gson.toJson(dmaapRequest, LCMRequestWrapper.class);
134 * Use the serializer to convert the json string
137 LCMRequestWrapper dmaapRequest = Serialization.gson.fromJson(jsonRequest, LCMRequestWrapper.class);
138 assertNotNull(dmaapRequest);
141 * The type of the DMAAP wrapper should be request
143 assertEquals(dmaapRequest.getType(), "request");
146 * The DMAAP wrapper must have a body as that is
147 * the true APPC request
149 assertNotNull(dmaapRequest.getBody());
150 LCMRequest appcRequest = dmaapRequest.getBody();
151 assertNotNull(appcRequest);
154 * The common header should not be null
156 assertNotNull(appcRequest.getCommonHeader());
159 * The action should not be null and should be
162 assertNotNull(appcRequest.getAction());
163 assertEquals(appcRequest.getAction(), "restart");
166 * The action-identifiers should not be null
167 * and should contain a vnf-id
169 assertNotNull(appcRequest.getActionIdentifiers());
170 assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
172 logger.debug("Request as a Java Object: \n" + appcRequest.toString() + "\n\n");
176 public void testResponseSerialization() {
179 * Use the serializer to convert the object into json
181 String jsonResponse = Serialization.gson.toJson(dmaapResponse, LCMResponseWrapper.class);
182 assertNotNull(jsonResponse);
185 * The serializer should have added an extra
186 * sub-tag called "input" that wraps the request
188 assertTrue(jsonResponse.contains("output"));
191 * The response should contain a common-header,
192 * request-id, sub-request-id, and status
194 assertTrue(jsonResponse.contains("common-header"));
195 assertTrue(jsonResponse.contains("request-id"));
196 assertTrue(jsonResponse.contains("sub-request-id"));
197 assertTrue(jsonResponse.contains("status"));
199 logger.debug("Response as JSON: " + jsonResponse + "\n\n");
203 public void testResponseDeserialization() {
205 * Convert the LCM response object into json
206 * so we have a string of json to use for testing
208 String jsonResponse = Serialization.gson.toJson(dmaapResponse, LCMResponseWrapper.class);
211 * Use the serializer to convert the json string
214 LCMResponseWrapper dmaapResponse = Serialization.gson.fromJson(jsonResponse, LCMResponseWrapper.class);
215 assertNotNull(dmaapResponse);
218 * The type of the DMAAP wrapper should be response
220 assertEquals(dmaapResponse.getType(), "response");
223 * The DMAAP wrapper must have a body as that is
224 * the true APPC response
226 assertNotNull(dmaapResponse.getBody());
227 LCMResponse appcResponse = dmaapResponse.getBody();
228 assertNotNull(appcResponse);
231 * The common header should not be null
233 assertNotNull(appcResponse.getCommonHeader());
236 * The status should not be null and the
237 * status code should be 400
239 assertNotNull(appcResponse.getStatus());
240 assertEquals(appcResponse.getStatus().getCode(), 400);
242 logger.debug("Response as a Java Object: \n" + appcResponse.toString() + "\n\n");