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