2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
27 import java.util.HashMap;
28 import java.util.UUID;
30 import org.junit.Test;
31 import org.onap.policy.appclcm.util.Serialization;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class AppcLcmTest {
37 private static final Logger logger = LoggerFactory.getLogger(AppcLcmTest.class);
39 private static LcmRequestWrapper dmaapRequest;
40 private static LcmResponseWrapper dmaapResponse;
44 * Construct an APPCLCM Request to be Serialized
46 dmaapRequest = new LcmRequestWrapper();
47 dmaapRequest.setCorrelationId("664be3d2-6c12-4f4b-a3e7-c349acced200" + "-" + "1");
48 dmaapRequest.setRpcName("restart");
49 dmaapRequest.setType("request");
51 dmaapResponse = new LcmResponseWrapper();
52 dmaapResponse.setCorrelationId("664be3d2-6c12-4f4b-a3e7-c349acced200" + "-" + "1");
53 dmaapResponse.setRpcName("restart");
54 dmaapResponse.setType("response");
56 LcmRequest appcRequest = new LcmRequest();
58 appcRequest.setAction("restart");
60 HashMap<String, String> actionIdentifiers = new HashMap<>();
61 actionIdentifiers.put("vnf-id", "trial-vnf-003");
62 actionIdentifiers.put("vserver-id", "08f6c1f9-99e7-49f3-a662-c62b9f200d79");
64 appcRequest.setActionIdentifiers(actionIdentifiers);
66 LcmCommonHeader commonHeader = new LcmCommonHeader();
67 commonHeader.setRequestId(UUID.fromString("664be3d2-6c12-4f4b-a3e7-c349acced200"));
68 commonHeader.setSubRequestId("1");
69 commonHeader.setOriginatorId("664be3d2-6c12-4f4b-a3e7-c349acced200");
71 appcRequest.setCommonHeader(commonHeader);
73 appcRequest.setPayload(null);
75 dmaapRequest.setBody(appcRequest);
78 * Construct an APPCLCM Response to be Serialized
80 LcmResponse appcResponse = new LcmResponse(appcRequest);
81 appcResponse.getStatus().setCode(400);
82 appcResponse.getStatus().setMessage("Restart Successful");
83 appcResponse.setPayload(null);
85 dmaapResponse.setBody(appcResponse);
89 public void testRequestSerialization() {
92 * Use the gson serializer to obtain json
94 String jsonRequest = Serialization.gson.toJson(dmaapRequest, LcmRequestWrapper.class);
95 assertNotNull(jsonRequest);
98 * The serializer should have added an extra sub-tag called "input" that wraps the request
100 assertTrue(jsonRequest.contains("input"));
103 * The common-header, request-id, and 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 a vnf-id
112 assertTrue(jsonRequest.contains("action-identifiers"));
113 assertTrue(jsonRequest.contains("vnf-id"));
116 * The action sub-tag should exist
118 assertTrue(jsonRequest.contains("action"));
120 logger.debug("Request as JSON: " + jsonRequest + "\n\n");
124 public void testRequestDeserialization() {
127 * Convert the LCM request object into json so we have a string of json to use for testing
129 String jsonRequest = Serialization.gson.toJson(dmaapRequest, LcmRequestWrapper.class);
132 * Use the serializer to convert the json string into a java object
134 LcmRequestWrapper dmaapRequest = Serialization.gson.fromJson(jsonRequest, LcmRequestWrapper.class);
135 assertNotNull(dmaapRequest);
138 * The type of the DMAAP wrapper should be request
140 assertEquals("request", dmaapRequest.getType());
143 * The DMAAP wrapper must have a body as that is the true APPC request
145 assertNotNull(dmaapRequest.getBody());
146 LcmRequest appcRequest = dmaapRequest.getBody();
147 assertNotNull(appcRequest);
150 * The common header should not be null
152 assertNotNull(appcRequest.getCommonHeader());
155 * The action should not be null and should be set to restart
157 assertNotNull(appcRequest.getAction());
158 assertEquals("restart", appcRequest.getAction());
161 * The action-identifiers should not be null and should contain a vnf-id
163 assertNotNull(appcRequest.getActionIdentifiers());
164 assertNotNull(appcRequest.getActionIdentifiers().get("vnf-id"));
166 logger.debug("Request as a Java Object: \n" + appcRequest.toString() + "\n\n");
170 public void testResponseSerialization() {
173 * Use the serializer to convert the object into json
175 String jsonResponse = Serialization.gson.toJson(dmaapResponse, LcmResponseWrapper.class);
176 assertNotNull(jsonResponse);
179 * The serializer should have added an extra sub-tag called "input" that wraps the request
181 assertTrue(jsonResponse.contains("output"));
184 * The response should contain a common-header, request-id, sub-request-id, and status
186 assertTrue(jsonResponse.contains("common-header"));
187 assertTrue(jsonResponse.contains("request-id"));
188 assertTrue(jsonResponse.contains("sub-request-id"));
189 assertTrue(jsonResponse.contains("status"));
191 logger.debug("Response as JSON: " + jsonResponse + "\n\n");
195 public void testResponseDeserialization() {
197 * Convert the LCM response object into json so we have a string of json to use for testing
199 String jsonResponse = Serialization.gson.toJson(dmaapResponse, LcmResponseWrapper.class);
202 * Use the serializer to convert the json string into a java object
204 LcmResponseWrapper dmaapResponse = Serialization.gson.fromJson(jsonResponse, LcmResponseWrapper.class);
205 assertNotNull(dmaapResponse);
208 * The type of the DMAAP wrapper should be response
210 assertEquals("response", dmaapResponse.getType());
213 * The DMAAP wrapper must have a body as that is the true APPC response
215 assertNotNull(dmaapResponse.getBody());
216 LcmResponse appcResponse = dmaapResponse.getBody();
217 assertNotNull(appcResponse);
220 * The common header should not be null
222 assertNotNull(appcResponse.getCommonHeader());
225 * The status should not be null and the status code should be 400
227 assertNotNull(appcResponse.getStatus());
228 assertEquals(400, appcResponse.getStatus().getCode());
230 logger.debug("Response as a Java Object: \n" + appcResponse.toString() + "\n\n");