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 String VNF_ID_KEY = "vnf-id";
40 private static final String RESTART = "restart";
42 private static final String CORRELATION_ID = "664be3d2-6c12-4f4b-a3e7-c349acced200";
44 private static final Logger logger = LoggerFactory.getLogger(AppcLcmTest.class);
46 private static LcmRequestWrapper dmaapRequest;
47 private static LcmResponseWrapper dmaapResponse;
51 * Construct an APPCLCM Request to be Serialized
53 dmaapRequest = new LcmRequestWrapper();
54 dmaapRequest.setCorrelationId(CORRELATION_ID + "-" + "1");
55 dmaapRequest.setRpcName(RESTART);
56 dmaapRequest.setType("request");
58 dmaapResponse = new LcmResponseWrapper();
59 dmaapResponse.setCorrelationId(CORRELATION_ID + "-" + "1");
60 dmaapResponse.setRpcName(RESTART);
61 dmaapResponse.setType("response");
63 LcmRequest appcRequest = new LcmRequest();
65 appcRequest.setAction(RESTART);
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");
71 appcRequest.setActionIdentifiers(actionIdentifiers);
73 LcmCommonHeader commonHeader = new LcmCommonHeader();
74 commonHeader.setRequestId(UUID.fromString(CORRELATION_ID));
75 commonHeader.setSubRequestId("1");
76 commonHeader.setOriginatorId(CORRELATION_ID);
78 appcRequest.setCommonHeader(commonHeader);
80 appcRequest.setPayload(null);
82 dmaapRequest.setBody(appcRequest);
85 * Construct an APPCLCM Response to be Serialized
87 LcmResponse appcResponse = new LcmResponse(appcRequest);
88 appcResponse.getStatus().setCode(400);
89 appcResponse.getStatus().setMessage("Restart Successful");
90 appcResponse.setPayload(null);
92 dmaapResponse.setBody(appcResponse);
96 public void testRequestSerialization() {
99 * Use the gson serializer to obtain json
101 String jsonRequest = Serialization.gson.toJson(dmaapRequest, LcmRequestWrapper.class);
102 assertNotNull(jsonRequest);
105 * The serializer should have added an extra sub-tag called "input" that wraps the request
107 assertTrue(jsonRequest.contains("input"));
110 * The common-header, request-id, and sub-request-id should exist
112 assertTrue(jsonRequest.contains("common-header"));
113 assertTrue(jsonRequest.contains("request-id"));
114 assertTrue(jsonRequest.contains("sub-request-id"));
117 * action-identifiers should exist and contain a vnf-id
119 assertTrue(jsonRequest.contains("action-identifiers"));
120 assertTrue(jsonRequest.contains(VNF_ID_KEY));
123 * The action sub-tag should exist
125 assertTrue(jsonRequest.contains("action"));
127 logger.debug("Request as JSON: " + jsonRequest + "\n\n");
131 public void testRequestDeserialization() {
134 * Convert the LCM request object into json so we have a string of json to use for testing
136 String jsonRequest = Serialization.gson.toJson(dmaapRequest, LcmRequestWrapper.class);
139 * Use the serializer to convert the json string into a java object
141 LcmRequestWrapper req = Serialization.gson.fromJson(jsonRequest, LcmRequestWrapper.class);
145 * The type of the DMAAP wrapper should be request
147 assertEquals("request", req.getType());
150 * The DMAAP wrapper must have a body as that is the true APPC request
152 assertNotNull(req.getBody());
153 LcmRequest appcRequest = req.getBody();
154 assertNotNull(appcRequest);
157 * The common header should not be null
159 assertNotNull(appcRequest.getCommonHeader());
162 * The action should not be null and should be set to restart
164 assertNotNull(appcRequest.getAction());
165 assertEquals(RESTART, appcRequest.getAction());
168 * The action-identifiers should not be null and should contain a vnf-id
170 assertNotNull(appcRequest.getActionIdentifiers());
171 assertNotNull(appcRequest.getActionIdentifiers().get(VNF_ID_KEY));
173 logger.debug("Request as a Java Object: \n" + appcRequest.toString() + "\n\n");
177 public void testResponseSerialization() {
180 * Use the serializer to convert the object into json
182 String jsonResponse = Serialization.gson.toJson(dmaapResponse, LcmResponseWrapper.class);
183 assertNotNull(jsonResponse);
186 * The serializer should have added an extra sub-tag called "input" that wraps the request
188 assertTrue(jsonResponse.contains("output"));
191 * The response should contain a common-header, request-id, sub-request-id, and status
193 assertTrue(jsonResponse.contains("common-header"));
194 assertTrue(jsonResponse.contains("request-id"));
195 assertTrue(jsonResponse.contains("sub-request-id"));
196 assertTrue(jsonResponse.contains("status"));
198 logger.debug("Response as JSON: " + jsonResponse + "\n\n");
202 public void testResponseDeserialization() {
204 * Convert the LCM response object into json so we have a string of json to use for testing
206 String jsonResponse = Serialization.gson.toJson(dmaapResponse, LcmResponseWrapper.class);
209 * Use the serializer to convert the json string into a java object
211 LcmResponseWrapper resp = Serialization.gson.fromJson(jsonResponse, LcmResponseWrapper.class);
215 * The type of the DMAAP wrapper should be response
217 assertEquals("response", resp.getType());
220 * The DMAAP wrapper must have a body as that is the true APPC response
222 assertNotNull(resp.getBody());
223 LcmResponse appcResponse = resp.getBody();
224 assertNotNull(appcResponse);
227 * The common header should not be null
229 assertNotNull(appcResponse.getCommonHeader());
232 * The status should not be null and the status code should be 400
234 assertNotNull(appcResponse.getStatus());
235 assertEquals(400, appcResponse.getStatus().getCode());
237 logger.debug("Response as a Java Object: \n" + appcResponse.toString() + "\n\n");