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