2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Wipro Limited 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.sdnr;
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.UUID;
30 import org.junit.Test;
31 import org.onap.policy.sdnr.util.Serialization;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class SdnrTest {
38 private static final Logger logger = LoggerFactory.getLogger(SdnrTest.class);
40 private static PciRequestWrapper dmaapRequest;
41 private static PciResponseWrapper dmaapResponse;
45 * Construct an SDNR Request to be Serialized
47 dmaapRequest = new PciRequestWrapper();
48 dmaapRequest.setCorrelationId("664be3d2-6c12-4f4b-a3e7-c349acced200" + "-" + "1");
49 dmaapRequest.setRpcName("restart");
50 dmaapRequest.setType("request");
52 dmaapResponse = new PciResponseWrapper();
53 dmaapResponse.setCorrelationId("664be3d2-6c12-4f4b-a3e7-c349acced200" + "-" + "1");
54 dmaapResponse.setRpcName("restart");
55 dmaapResponse.setType("response");
57 PciRequest sdnrRequest = new PciRequest();
59 sdnrRequest.setAction("ModifyConfig");
61 PciCommonHeader commonHeader = new PciCommonHeader();
62 commonHeader.setRequestId(UUID.fromString("664be3d2-6c12-4f4b-a3e7-c349acced200"));
63 commonHeader.setSubRequestId("1");
65 sdnrRequest.setCommonHeader(commonHeader);
67 sdnrRequest.setPayload(null);
69 dmaapRequest.setBody(sdnrRequest);
72 * Construct an SDNR Response to be Serialized
74 PciResponse sdnrResponse = new PciResponse(sdnrRequest);
75 sdnrResponse.getStatus().setCode(400);
76 sdnrResponse.getStatus().setValue("Restart Successful");
77 sdnrResponse.setPayload(null);
79 dmaapResponse.setBody(sdnrResponse);
83 public void testRequestSerialization() {
86 * Use the gson serializer to obtain json
88 String jsonRequest = Serialization.gson.toJson(dmaapRequest, PciRequestWrapper.class);
89 assertNotNull(jsonRequest);
92 * The serializer should have added an extra sub-tag called "input" that wraps the request
94 assertTrue(jsonRequest.contains("input"));
97 * The common-header, request-id, and sub-request-id should exist
99 assertTrue(jsonRequest.contains("CommonHeader"));
100 assertTrue(jsonRequest.contains("RequestID"));
101 assertTrue(jsonRequest.contains("SubRequestID"));
104 * The action sub-tag should exist
106 assertTrue(jsonRequest.contains("Action"));
108 logger.debug("Request as JSON: " + jsonRequest + "\n\n");
112 public void testRequestDeserialization() {
115 * Convert the PCI request object into json so we have a string of json to use for testing
117 String jsonRequest = Serialization.gson.toJson(dmaapRequest, PciRequestWrapper.class);
120 * Use the serializer to convert the json string into a java object
122 PciRequestWrapper pciRequestWrapper = Serialization.gson.fromJson(jsonRequest, PciRequestWrapper.class);
123 assertNotNull(pciRequestWrapper);
124 assertEquals(dmaapRequest, pciRequestWrapper);
127 * The type of the DMAAP wrapper should be request
129 assertEquals("request", dmaapRequest.getType());
132 * The DMAAP wrapper must have a body as that is the true SDNR request
134 assertNotNull(dmaapRequest.getBody());
135 PciRequest sdnrRequest = dmaapRequest.getBody();
136 assertNotNull(sdnrRequest);
139 * The common header should not be null
141 assertNotNull(sdnrRequest.getCommonHeader());
144 * The action should not be null and should be set to restart
146 assertNotNull(sdnrRequest.getAction());
147 assertEquals("ModifyConfig", sdnrRequest.getAction());
149 logger.debug("Request as a Java Object: \n" + sdnrRequest.toString() + "\n\n");
153 public void testResponseSerialization() {
156 * Use the serializer to convert the object into json
158 String jsonResponse = Serialization.gson.toJson(dmaapResponse, PciResponseWrapper.class);
159 assertNotNull(jsonResponse);
162 * The serializer should have added an extra sub-tag called "input" that wraps the request
164 assertTrue(jsonResponse.contains("output"));
167 * The response should contain a common-header, request-id, sub-request-id, and status
169 assertTrue(jsonResponse.contains("CommonHeader"));
170 assertTrue(jsonResponse.contains("RequestID"));
171 assertTrue(jsonResponse.contains("SubRequestID"));
172 assertTrue(jsonResponse.contains("Status"));
174 logger.debug("Response as JSON: " + jsonResponse + "\n\n");
178 public void testResponseDeserialization() {
180 * Convert the PCI response object into json so we have a string of json to use for testing
182 String jsonResponse = Serialization.gson.toJson(dmaapResponse, PciResponseWrapper.class);
185 * Use the serializer to convert the json string into a java object
187 PciResponseWrapper pciResponseWrapper = Serialization.gson.fromJson(jsonResponse, PciResponseWrapper.class);
188 assertNotNull(pciResponseWrapper);
189 assertEquals(dmaapResponse, pciResponseWrapper);
192 * The type of the DMAAP wrapper should be response
194 assertEquals("response", dmaapResponse.getType());
197 * The DMAAP wrapper must have a body as that is the true SDNR response
199 assertNotNull(dmaapResponse.getBody());
200 PciResponse sdnrResponse = dmaapResponse.getBody();
201 assertNotNull(sdnrResponse);
204 * The common header should not be null
206 assertNotNull(sdnrResponse.getCommonHeader());
209 * The status should not be null and the status code should be 400
211 assertNotNull(sdnrResponse.getStatus());
212 assertEquals(400, sdnrResponse.getStatus().getCode());
214 logger.debug("Response as a Java Object: \n" + sdnrResponse.toString() + "\n\n");