2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Wipro Limited 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.sdnr;
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.UUID;
29 import org.junit.Test;
30 import org.onap.policy.sdnr.util.Serialization;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class SdnrTest {
37 private static final Logger logger = LoggerFactory.getLogger(SdnrTest.class);
39 private static PciRequestWrapper dmaapRequest;
40 private static PciResponseWrapper dmaapResponse;
44 * Construct an SDNR Request to be Serialized
46 dmaapRequest = new PciRequestWrapper();
47 dmaapRequest.setCorrelationId("664be3d2-6c12-4f4b-a3e7-c349acced200" + "-" + "1");
48 dmaapRequest.setRpcName("restart");
49 dmaapRequest.setType("request");
51 dmaapResponse = new PciResponseWrapper();
52 dmaapResponse.setCorrelationId("664be3d2-6c12-4f4b-a3e7-c349acced200" + "-" + "1");
53 dmaapResponse.setRpcName("restart");
54 dmaapResponse.setType("response");
56 PciRequest sdnrRequest = new PciRequest();
58 sdnrRequest.setAction("ModifyConfig");
60 PciCommonHeader commonHeader = new PciCommonHeader();
61 commonHeader.setRequestId(UUID.fromString("664be3d2-6c12-4f4b-a3e7-c349acced200"));
62 commonHeader.setSubRequestId("1");
64 sdnrRequest.setCommonHeader(commonHeader);
66 sdnrRequest.setPayload(null);
68 dmaapRequest.setBody(sdnrRequest);
71 * Construct an SDNR Response to be Serialized
73 PciResponse sdnrResponse = new PciResponse(sdnrRequest);
74 sdnrResponse.getStatus().setCode(400);
75 sdnrResponse.getStatus().setValue("Restart Successful");
76 sdnrResponse.setPayload(null);
78 dmaapResponse.setBody(sdnrResponse);
82 public void testRequestSerialization() {
85 * Use the gson serializer to obtain json
87 String jsonRequest = Serialization.gson.toJson(dmaapRequest, PciRequestWrapper.class);
88 assertNotNull(jsonRequest);
91 * The serializer should have added an extra sub-tag called "input" that wraps the request
93 assertTrue(jsonRequest.contains("input"));
96 * The common-header, request-id, and sub-request-id should exist
98 assertTrue(jsonRequest.contains("CommonHeader"));
99 assertTrue(jsonRequest.contains("RequestID"));
100 assertTrue(jsonRequest.contains("SubRequestID"));
103 * The action sub-tag should exist
105 assertTrue(jsonRequest.contains("Action"));
107 logger.debug("Request as JSON: " + jsonRequest + "\n\n");
111 public void testRequestDeserialization() {
114 * Convert the PCI request object into json so we have a string of json to use for testing
116 String jsonRequest = Serialization.gson.toJson(dmaapRequest, PciRequestWrapper.class);
119 * Use the serializer to convert the json string into a java object
121 PciRequestWrapper pciRequestWrapper = Serialization.gson.fromJson(jsonRequest, PciRequestWrapper.class);
122 assertNotNull(pciRequestWrapper);
123 assertEquals(dmaapRequest, pciRequestWrapper);
126 * The type of the DMAAP wrapper should be request
128 assertEquals("request", dmaapRequest.getType());
131 * The DMAAP wrapper must have a body as that is the true SDNR request
133 assertNotNull(dmaapRequest.getBody());
134 PciRequest sdnrRequest = dmaapRequest.getBody();
135 assertNotNull(sdnrRequest);
138 * The common header should not be null
140 assertNotNull(sdnrRequest.getCommonHeader());
143 * The action should not be null and should be set to restart
145 assertNotNull(sdnrRequest.getAction());
146 assertEquals("ModifyConfig", sdnrRequest.getAction());
148 logger.debug("Request as a Java Object: \n" + sdnrRequest.toString() + "\n\n");
152 public void testResponseSerialization() {
155 * Use the serializer to convert the object into json
157 String jsonResponse = Serialization.gson.toJson(dmaapResponse, PciResponseWrapper.class);
158 assertNotNull(jsonResponse);
161 * The serializer should have added an extra sub-tag called "input" that wraps the request
163 assertTrue(jsonResponse.contains("output"));
166 * The response should contain a common-header, request-id, sub-request-id, and status
168 assertTrue(jsonResponse.contains("CommonHeader"));
169 assertTrue(jsonResponse.contains("RequestID"));
170 assertTrue(jsonResponse.contains("SubRequestID"));
171 assertTrue(jsonResponse.contains("Status"));
173 logger.debug("Response as JSON: " + jsonResponse + "\n\n");
177 public void testResponseDeserialization() {
179 * Convert the PCI response object into json so we have a string of json to use for testing
181 String jsonResponse = Serialization.gson.toJson(dmaapResponse, PciResponseWrapper.class);
184 * Use the serializer to convert the json string into a java object
186 PciResponseWrapper pciResponseWrapper = Serialization.gson.fromJson(jsonResponse, PciResponseWrapper.class);
187 assertNotNull(pciResponseWrapper);
188 assertEquals(dmaapResponse, pciResponseWrapper);
191 * The type of the DMAAP wrapper should be response
193 assertEquals("response", dmaapResponse.getType());
196 * The DMAAP wrapper must have a body as that is the true SDNR response
198 assertNotNull(dmaapResponse.getBody());
199 PciResponse sdnrResponse = dmaapResponse.getBody();
200 assertNotNull(sdnrResponse);
203 * The common header should not be null
205 assertNotNull(sdnrResponse.getCommonHeader());
208 * The status should not be null and the status code should be 400
210 assertNotNull(sdnrResponse.getStatus());
211 assertEquals(400, sdnrResponse.getStatus().getCode());
213 logger.debug("Response as a Java Object: \n" + sdnrResponse.toString() + "\n\n");