Remove dmaap from models
[policy/models.git] / models-interactions / model-impl / sdnr / src / test / java / org / onap / policy / sdnr / SdnrTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * sdnr
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.sdnr;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28
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;
34
35 public class SdnrTest {
36
37     private static final String CORRELATION_ID = "664be3d2-6c12-4f4b-a3e7-c349acced200";
38
39     private static final Logger logger = LoggerFactory.getLogger(SdnrTest.class);
40
41     private static PciRequestWrapper messageRequest;
42     private static PciResponseWrapper messageResponse;
43
44     static {
45         /*
46          * Construct an SDNR Request to be Serialized
47          */
48         messageRequest = new PciRequestWrapper();
49         messageRequest.setCorrelationId(CORRELATION_ID + "-" + "1");
50         messageRequest.setRpcName("restart");
51         messageRequest.setType("request");
52
53         messageResponse = new PciResponseWrapper();
54         messageResponse.setCorrelationId(CORRELATION_ID + "-" + "1");
55         messageResponse.setRpcName("restart");
56         messageResponse.setType("response");
57
58         PciRequest sdnrRequest = new PciRequest();
59
60         sdnrRequest.setAction("ModifyConfig");
61
62         PciCommonHeader commonHeader = new PciCommonHeader();
63         commonHeader.setRequestId(UUID.fromString(CORRELATION_ID));
64         commonHeader.setSubRequestId("1");
65
66         sdnrRequest.setCommonHeader(commonHeader);
67
68         sdnrRequest.setPayload(null);
69
70         messageRequest.setBody(sdnrRequest);
71
72         /*
73          * Construct an SDNR Response to be Serialized
74          */
75         PciResponse sdnrResponse = new PciResponse(sdnrRequest);
76         sdnrResponse.getStatus().setCode(400);
77         sdnrResponse.getStatus().setValue("Restart Successful");
78         sdnrResponse.setPayload(null);
79
80         messageResponse.setBody(sdnrResponse);
81     }
82
83     @Test
84     public void testRequestSerialization() {
85
86         /*
87          * Use the gson serializer to obtain json
88          */
89         String jsonRequest = Serialization.gson.toJson(messageRequest, PciRequestWrapper.class);
90         assertNotNull(jsonRequest);
91
92         /*
93          * The serializer should have added an extra sub-tag called "input" that wraps the request
94          */
95         assertTrue(jsonRequest.contains("input"));
96
97         /*
98          * The common-header, request-id, and sub-request-id should exist
99          */
100         assertTrue(jsonRequest.contains("CommonHeader"));
101         assertTrue(jsonRequest.contains("RequestID"));
102         assertTrue(jsonRequest.contains("SubRequestID"));
103
104         /*
105          * The action sub-tag should exist
106          */
107         assertTrue(jsonRequest.contains("Action"));
108
109         logger.debug("Request as JSON: " + jsonRequest + "\n\n");
110     }
111
112     @Test
113     public void testRequestDeserialization() {
114
115         /*
116          * Convert the PCI request object into json so we have a string of json to use for testing
117          */
118         String jsonRequest = Serialization.gson.toJson(messageRequest, PciRequestWrapper.class);
119
120         /*
121          * Use the serializer to convert the json string into a java object
122          */
123         PciRequestWrapper pciRequestWrapper = Serialization.gson.fromJson(jsonRequest, PciRequestWrapper.class);
124         assertNotNull(pciRequestWrapper);
125         assertEquals(messageRequest, pciRequestWrapper);
126
127         /*
128          * The type of the Message wrapper should be request
129          */
130         assertEquals("request", messageRequest.getType());
131
132         /*
133          * The Message wrapper must have a body as that is the true SDNR request
134          */
135         assertNotNull(messageRequest.getBody());
136         PciRequest sdnrRequest = messageRequest.getBody();
137         assertNotNull(sdnrRequest);
138
139         /*
140          * The common header should not be null
141          */
142         assertNotNull(sdnrRequest.getCommonHeader());
143
144         /*
145          * The action should not be null and should be set to restart
146          */
147         assertNotNull(sdnrRequest.getAction());
148         assertEquals("ModifyConfig", sdnrRequest.getAction());
149
150         logger.debug("Request as a Java Object: \n" + sdnrRequest.toString() + "\n\n");
151     }
152
153     @Test
154     public void testResponseSerialization() {
155
156         /*
157          * Use the serializer to convert the object into json
158          */
159         String jsonResponse = Serialization.gson.toJson(messageResponse, PciResponseWrapper.class);
160         assertNotNull(jsonResponse);
161
162         /*
163          * The serializer should have added an extra sub-tag called "input" that wraps the request
164          */
165         assertTrue(jsonResponse.contains("output"));
166
167         /*
168          * The response should contain a common-header, request-id, sub-request-id, and status
169          */
170         assertTrue(jsonResponse.contains("CommonHeader"));
171         assertTrue(jsonResponse.contains("RequestID"));
172         assertTrue(jsonResponse.contains("SubRequestID"));
173         assertTrue(jsonResponse.contains("Status"));
174
175         logger.debug("Response as JSON: " + jsonResponse + "\n\n");
176     }
177
178     @Test
179     public void testResponseDeserialization() {
180         /*
181          * Convert the PCI response object into json so we have a string of json to use for testing
182          */
183         String jsonResponse = Serialization.gson.toJson(messageResponse, PciResponseWrapper.class);
184
185         /*
186          * Use the serializer to convert the json string into a java object
187          */
188         PciResponseWrapper pciResponseWrapper = Serialization.gson.fromJson(jsonResponse, PciResponseWrapper.class);
189         assertNotNull(pciResponseWrapper);
190         assertEquals(messageResponse, pciResponseWrapper);
191
192         /*
193          * The type of the Message wrapper should be response
194          */
195         assertEquals("response", messageResponse.getType());
196
197         /*
198          * The Message wrapper must have a body as that is the true SDNR response
199          */
200         assertNotNull(messageResponse.getBody());
201         PciResponse sdnrResponse = messageResponse.getBody();
202         assertNotNull(sdnrResponse);
203
204         /*
205          * The common header should not be null
206          */
207         assertNotNull(sdnrResponse.getCommonHeader());
208
209         /*
210          * The status should not be null and the status code should be 400
211          */
212         assertNotNull(sdnrResponse.getStatus());
213         assertEquals(400, sdnrResponse.getStatus().getCode());
214
215         logger.debug("Response as a Java Object: \n" + sdnrResponse.toString() + "\n\n");
216     }
217 }