2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Wipro Limited Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019, 2024 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;
30 import org.junit.Test;
31 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 String CORRELATION_ID = "664be3d2-6c12-4f4b-a3e7-c349acced200";
39 private static final Logger logger = LoggerFactory.getLogger(SdnrTest.class);
41 private static PciRequestWrapper messageRequest;
42 private static PciResponseWrapper messageResponse;
46 * Construct an SDNR Request to be Serialized
48 messageRequest = new PciRequestWrapper();
49 messageRequest.setCorrelationId(CORRELATION_ID + "-" + "1");
50 messageRequest.setRpcName("restart");
51 messageRequest.setType("request");
53 messageResponse = new PciResponseWrapper();
54 messageResponse.setCorrelationId(CORRELATION_ID + "-" + "1");
55 messageResponse.setRpcName("restart");
56 messageResponse.setType("response");
58 PciRequest sdnrRequest = new PciRequest();
60 sdnrRequest.setAction("ModifyConfig");
62 PciCommonHeader commonHeader = new PciCommonHeader();
63 commonHeader.setRequestId(UUID.fromString(CORRELATION_ID));
64 commonHeader.setSubRequestId("1");
66 sdnrRequest.setCommonHeader(commonHeader);
68 sdnrRequest.setPayload(null);
70 messageRequest.setBody(sdnrRequest);
73 * Construct an SDNR Response to be Serialized
75 PciResponse sdnrResponse = new PciResponse(sdnrRequest);
76 sdnrResponse.getStatus().setCode(400);
77 sdnrResponse.getStatus().setValue("Restart Successful");
78 sdnrResponse.setPayload(null);
80 messageResponse.setBody(sdnrResponse);
84 public void testRequestSerialization() {
87 * Use the gson serializer to obtain json
89 String jsonRequest = Serialization.gson.toJson(messageRequest, PciRequestWrapper.class);
90 assertNotNull(jsonRequest);
93 * The serializer should have added an extra sub-tag called "input" that wraps the request
95 assertTrue(jsonRequest.contains("input"));
98 * The common-header, request-id, and sub-request-id should exist
100 assertTrue(jsonRequest.contains("CommonHeader"));
101 assertTrue(jsonRequest.contains("RequestID"));
102 assertTrue(jsonRequest.contains("SubRequestID"));
105 * The action sub-tag should exist
107 assertTrue(jsonRequest.contains("Action"));
109 logger.debug("Request as JSON: " + jsonRequest + "\n\n");
113 public void testRequestDeserialization() {
116 * Convert the PCI request object into json so we have a string of json to use for testing
118 String jsonRequest = Serialization.gson.toJson(messageRequest, PciRequestWrapper.class);
121 * Use the serializer to convert the json string into a java object
123 PciRequestWrapper pciRequestWrapper = Serialization.gson.fromJson(jsonRequest, PciRequestWrapper.class);
124 assertNotNull(pciRequestWrapper);
125 assertEquals(messageRequest, pciRequestWrapper);
128 * The type of the Message wrapper should be request
130 assertEquals("request", messageRequest.getType());
133 * The Message wrapper must have a body as that is the true SDNR request
135 assertNotNull(messageRequest.getBody());
136 PciRequest sdnrRequest = messageRequest.getBody();
137 assertNotNull(sdnrRequest);
140 * The common header should not be null
142 assertNotNull(sdnrRequest.getCommonHeader());
145 * The action should not be null and should be set to restart
147 assertNotNull(sdnrRequest.getAction());
148 assertEquals("ModifyConfig", sdnrRequest.getAction());
150 logger.debug("Request as a Java Object: \n" + sdnrRequest.toString() + "\n\n");
154 public void testResponseSerialization() {
157 * Use the serializer to convert the object into json
159 String jsonResponse = Serialization.gson.toJson(messageResponse, PciResponseWrapper.class);
160 assertNotNull(jsonResponse);
163 * The serializer should have added an extra sub-tag called "input" that wraps the request
165 assertTrue(jsonResponse.contains("output"));
168 * The response should contain a common-header, request-id, sub-request-id, and status
170 assertTrue(jsonResponse.contains("CommonHeader"));
171 assertTrue(jsonResponse.contains("RequestID"));
172 assertTrue(jsonResponse.contains("SubRequestID"));
173 assertTrue(jsonResponse.contains("Status"));
175 logger.debug("Response as JSON: " + jsonResponse + "\n\n");
179 public void testResponseDeserialization() {
181 * Convert the PCI response object into json so we have a string of json to use for testing
183 String jsonResponse = Serialization.gson.toJson(messageResponse, PciResponseWrapper.class);
186 * Use the serializer to convert the json string into a java object
188 PciResponseWrapper pciResponseWrapper = Serialization.gson.fromJson(jsonResponse, PciResponseWrapper.class);
189 assertNotNull(pciResponseWrapper);
190 assertEquals(messageResponse, pciResponseWrapper);
193 * The type of the Message wrapper should be response
195 assertEquals("response", messageResponse.getType());
198 * The Message wrapper must have a body as that is the true SDNR response
200 assertNotNull(messageResponse.getBody());
201 PciResponse sdnrResponse = messageResponse.getBody();
202 assertNotNull(sdnrResponse);
205 * The common header should not be null
207 assertNotNull(sdnrResponse.getCommonHeader());
210 * The status should not be null and the status code should be 400
212 assertNotNull(sdnrResponse.getStatus());
213 assertEquals(400, sdnrResponse.getStatus().getCode());
215 logger.debug("Response as a Java Object: \n" + sdnrResponse.toString() + "\n\n");