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