2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Wipro Limited Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.sdnr;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
29 import java.util.UUID;
31 import org.junit.Test;
32 import org.onap.policy.sdnr.util.Serialization;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class SdnrTest {
39 private static final String CORRELATION_ID = "664be3d2-6c12-4f4b-a3e7-c349acced200";
41 private static final Logger logger = LoggerFactory.getLogger(SdnrTest.class);
43 private static PciRequestWrapper dmaapRequest;
44 private static PciResponseWrapper dmaapResponse;
48 * Construct an SDNR Request to be Serialized
50 dmaapRequest = new PciRequestWrapper();
51 dmaapRequest.setCorrelationId(CORRELATION_ID + "-" + "1");
52 dmaapRequest.setRpcName("restart");
53 dmaapRequest.setType("request");
55 dmaapResponse = new PciResponseWrapper();
56 dmaapResponse.setCorrelationId(CORRELATION_ID + "-" + "1");
57 dmaapResponse.setRpcName("restart");
58 dmaapResponse.setType("response");
60 PciRequest sdnrRequest = new PciRequest();
62 sdnrRequest.setAction("ModifyConfig");
64 PciCommonHeader commonHeader = new PciCommonHeader();
65 commonHeader.setRequestId(UUID.fromString(CORRELATION_ID));
66 commonHeader.setSubRequestId("1");
68 sdnrRequest.setCommonHeader(commonHeader);
70 sdnrRequest.setPayload(null);
72 dmaapRequest.setBody(sdnrRequest);
75 * Construct an SDNR Response to be Serialized
77 PciResponse sdnrResponse = new PciResponse(sdnrRequest);
78 sdnrResponse.getStatus().setCode(400);
79 sdnrResponse.getStatus().setValue("Restart Successful");
80 sdnrResponse.setPayload(null);
82 dmaapResponse.setBody(sdnrResponse);
86 public void testRequestSerialization() {
89 * Use the gson serializer to obtain json
91 String jsonRequest = Serialization.gson.toJson(dmaapRequest, PciRequestWrapper.class);
92 assertNotNull(jsonRequest);
95 * The serializer should have added an extra sub-tag called "input" that wraps the request
97 assertTrue(jsonRequest.contains("input"));
100 * The common-header, request-id, and sub-request-id should exist
102 assertTrue(jsonRequest.contains("CommonHeader"));
103 assertTrue(jsonRequest.contains("RequestID"));
104 assertTrue(jsonRequest.contains("SubRequestID"));
107 * The action sub-tag should exist
109 assertTrue(jsonRequest.contains("Action"));
111 logger.debug("Request as JSON: " + jsonRequest + "\n\n");
115 public void testRequestDeserialization() {
118 * Convert the PCI request object into json so we have a string of json to use for testing
120 String jsonRequest = Serialization.gson.toJson(dmaapRequest, PciRequestWrapper.class);
123 * Use the serializer to convert the json string into a java object
125 PciRequestWrapper pciRequestWrapper = Serialization.gson.fromJson(jsonRequest, PciRequestWrapper.class);
126 assertNotNull(pciRequestWrapper);
127 assertEquals(dmaapRequest, pciRequestWrapper);
130 * The type of the DMAAP wrapper should be request
132 assertEquals("request", dmaapRequest.getType());
135 * The DMAAP wrapper must have a body as that is the true SDNR request
137 assertNotNull(dmaapRequest.getBody());
138 PciRequest sdnrRequest = dmaapRequest.getBody();
139 assertNotNull(sdnrRequest);
142 * The common header should not be null
144 assertNotNull(sdnrRequest.getCommonHeader());
147 * The action should not be null and should be set to restart
149 assertNotNull(sdnrRequest.getAction());
150 assertEquals("ModifyConfig", sdnrRequest.getAction());
152 logger.debug("Request as a Java Object: \n" + sdnrRequest.toString() + "\n\n");
156 public void testResponseSerialization() {
159 * Use the serializer to convert the object into json
161 String jsonResponse = Serialization.gson.toJson(dmaapResponse, PciResponseWrapper.class);
162 assertNotNull(jsonResponse);
165 * The serializer should have added an extra sub-tag called "input" that wraps the request
167 assertTrue(jsonResponse.contains("output"));
170 * The response should contain a common-header, request-id, sub-request-id, and status
172 assertTrue(jsonResponse.contains("CommonHeader"));
173 assertTrue(jsonResponse.contains("RequestID"));
174 assertTrue(jsonResponse.contains("SubRequestID"));
175 assertTrue(jsonResponse.contains("Status"));
177 logger.debug("Response as JSON: " + jsonResponse + "\n\n");
181 public void testResponseDeserialization() {
183 * Convert the PCI response object into json so we have a string of json to use for testing
185 String jsonResponse = Serialization.gson.toJson(dmaapResponse, PciResponseWrapper.class);
188 * Use the serializer to convert the json string into a java object
190 PciResponseWrapper pciResponseWrapper = Serialization.gson.fromJson(jsonResponse, PciResponseWrapper.class);
191 assertNotNull(pciResponseWrapper);
192 assertEquals(dmaapResponse, pciResponseWrapper);
195 * The type of the DMAAP wrapper should be response
197 assertEquals("response", dmaapResponse.getType());
200 * The DMAAP wrapper must have a body as that is the true SDNR response
202 assertNotNull(dmaapResponse.getBody());
203 PciResponse sdnrResponse = dmaapResponse.getBody();
204 assertNotNull(sdnrResponse);
207 * The common header should not be null
209 assertNotNull(sdnrResponse.getCommonHeader());
212 * The status should not be null and the status code should be 400
214 assertNotNull(sdnrResponse.getStatus());
215 assertEquals(400, sdnrResponse.getStatus().getCode());
217 logger.debug("Response as a Java Object: \n" + sdnrResponse.toString() + "\n\n");