JUnit test for policy/engine PolicyEngineAPI
[policy/engine.git] / PolicyEngineAPI / src / test / java / org / onap / policy / std / test / NotificationUnMarshalTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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.std.test;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.policy.api.UpdateType;
38 import org.onap.policy.std.NotificationUnMarshal;
39 import org.onap.policy.std.StdLoadedPolicy;
40 import org.onap.policy.std.StdPDPNotification;
41 import org.onap.policy.std.StdRemovedPolicy;
42
43 import com.fasterxml.jackson.databind.JsonMappingException;
44 import com.fasterxml.jackson.databind.ObjectMapper;
45
46 /**
47  * The class <code>NotificationUnMarshalTest</code> contains tests for the class
48  * <code>{@link NotificationUnMarshal}</code>.
49  *
50  * @generatedBy CodePro at 6/1/16 1:40 PM
51  * @version $Revision: 1.0 $
52  */
53 public class NotificationUnMarshalTest {
54     private static final String EMPTY_STRING = "";
55
56     private static final String POLICY_NAME = "ONAP";
57
58     private static final String POLICY_VERSION = "1.0.0";
59
60     /**
61      * Run the StdPDPNotification notificationJSON(String) method test.
62      *
63      * @throws Exception
64      *
65      * @generatedBy CodePro at 6/1/16 1:40 PM
66      */
67     @Test(expected = JsonMappingException.class)
68     public void testNotificationJSON_EmptyString_ShouldThrowException() throws Exception {
69         final StdPDPNotification result = NotificationUnMarshal.notificationJSON(EMPTY_STRING);
70
71         assertNotNull(result);
72     }
73
74     @Test
75     public void testNotificationJSON_StdPDPNotificationJsonStringWithEmptyLoadedAndRemovedPolicies_emptyLoadedPolices()
76             throws Exception {
77         final String json = getPDPNotificationAsJsonString(new StdPDPNotification());
78
79         final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
80
81         assertNotNull(result);
82         assertTrue(result.getLoadedPolicies().isEmpty());
83     }
84
85     @Test
86     public void testNotificationJSON_StdPDPNotificationJsonStringWithEmptyLoadedpolicy_emptyLoadedPolices()
87             throws Exception {
88         final StdPDPNotification notification = getPDPNotification(
89                 Arrays.asList(getRemovedPolicy(POLICY_VERSION, POLICY_NAME)), Collections.emptyList());
90         final String json = getPDPNotificationAsJsonString(notification);
91
92         final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
93
94         assertNotNull(result);
95         assertTrue(result.getLoadedPolicies().isEmpty());
96     }
97
98     @Test
99     public void testNotificationJSON_validPDPNotificationJsonStringWithRemovedAndLoadedPolicies_UpdateTypeUpdateAndLoadedPolicyAdded()
100             throws Exception {
101         final List<StdRemovedPolicy> removedPolicies = Arrays.asList(getRemovedPolicy(POLICY_VERSION, POLICY_NAME));
102         final List<StdLoadedPolicy> loadedPolicies = Arrays.asList(getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME));
103         final StdPDPNotification notification = getPDPNotification(removedPolicies, loadedPolicies);
104
105         final String json = getPDPNotificationAsJsonString(notification);
106
107         final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
108
109         assertNotNull(result);
110         assertFalse(result.getLoadedPolicies().isEmpty());
111
112         final StdLoadedPolicy actualPolicy = (StdLoadedPolicy) result.getLoadedPolicies().iterator().next();
113         assertEquals(POLICY_VERSION, actualPolicy.getVersionNo());
114         assertEquals(POLICY_NAME, actualPolicy.getPolicyName());
115         assertEquals(UpdateType.UPDATE, actualPolicy.getUpdateType());
116     }
117
118     /**
119      * Run the StdPDPNotification notificationJSON(String) method test.
120      *
121      * @throws Exception
122      *
123      * @generatedBy CodePro at 6/1/16 1:40 PM
124      */
125     @Test
126     public void testNotificationJSON_validPDPNotificationJsonStringWithRemovedAndLoadedPolicies_UpdateTypeNewAndLoadedPolicyAdded()
127             throws Exception {
128         final List<StdRemovedPolicy> removedPolicies = Arrays.asList(getRemovedPolicy(POLICY_VERSION, "Something"));
129         final List<StdLoadedPolicy> loadedPolices = Arrays.asList(getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME));
130         final StdPDPNotification notification = getPDPNotification(removedPolicies, loadedPolices);
131
132         final String json = getPDPNotificationAsJsonString(notification);
133
134         final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
135
136         assertNotNull(result);
137         assertFalse(result.getLoadedPolicies().isEmpty());
138
139         final StdLoadedPolicy actualPolicy = (StdLoadedPolicy) result.getLoadedPolicies().iterator().next();
140         assertEquals(POLICY_VERSION, actualPolicy.getVersionNo());
141         assertEquals(POLICY_NAME, actualPolicy.getPolicyName());
142         assertEquals(UpdateType.NEW, actualPolicy.getUpdateType());
143     }
144
145     @Test
146     public void testNotificationJSON_validPDPNotificationJsonStringLoadedPoliciesAndNullRemovedPolicies_UpdateTypeNewAndLoadedPolicyAdded()
147             throws Exception {
148         final List<StdLoadedPolicy> loadedPolices = Arrays.asList(getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME));
149         final StdPDPNotification notification = getPDPNotification(null, loadedPolices);
150
151         final String json = getPDPNotificationAsJsonString(notification);
152
153         final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
154
155         assertNotNull(result);
156         assertFalse(result.getLoadedPolicies().isEmpty());
157
158         final StdLoadedPolicy actualPolicy = (StdLoadedPolicy) result.getLoadedPolicies().iterator().next();
159         assertEquals(POLICY_VERSION, actualPolicy.getVersionNo());
160         assertEquals(POLICY_NAME, actualPolicy.getPolicyName());
161         assertEquals(UpdateType.NEW, actualPolicy.getUpdateType());
162     }
163
164     /**
165      * Run the StdPDPNotification notificationJSON(String) method test.
166      *
167      * @throws Exception
168      *
169      * @generatedBy CodePro at 6/1/16 1:40 PM
170      */
171     @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class)
172     public void testNotificationJSON_7() throws Exception {
173         final String json = EMPTY_STRING;
174
175         final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
176
177         // add additional test code here
178         assertNotNull(result);
179     }
180
181     /**
182      * Run the StdPDPNotification notificationJSON(String) method test.
183      *
184      * @throws Exception
185      *
186      * @generatedBy CodePro at 6/1/16 1:40 PM
187      */
188     @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class)
189     public void testNotificationJSON_8() throws Exception {
190         final String json = EMPTY_STRING;
191
192         final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
193
194         // add additional test code here
195         assertNotNull(result);
196     }
197
198     /**
199      * Run the StdPDPNotification notificationJSON(String) method test.
200      *
201      * @throws Exception
202      *
203      * @generatedBy CodePro at 6/1/16 1:40 PM
204      */
205     @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class)
206     public void testNotificationJSON_9() throws Exception {
207         final String json = EMPTY_STRING;
208
209         final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
210
211         // add additional test code here
212         assertNotNull(result);
213     }
214
215     /**
216      * Run the StdPDPNotification notificationJSON(String) method test.
217      *
218      * @throws Exception
219      *
220      * @generatedBy CodePro at 6/1/16 1:40 PM
221      */
222     @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class)
223     public void testNotificationJSON_10() throws Exception {
224         final String json = EMPTY_STRING;
225
226         final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
227
228         // add additional test code here
229         assertNotNull(result);
230     }
231
232     private String getPDPNotificationAsJsonString(final StdPDPNotification notification) {
233         final ObjectMapper mapper = new ObjectMapper();
234
235         try {
236             return mapper.writeValueAsString(notification);
237         } catch (final Exception expection) {
238             throw new RuntimeException(expection);
239         }
240
241     }
242
243     private StdPDPNotification getPDPNotification(final Collection<StdRemovedPolicy> removedPolicies,
244             final Collection<StdLoadedPolicy> loadedPolicies) {
245         final StdPDPNotification oldNotification = new StdPDPNotification();
246         oldNotification.setLoadedPolicies(loadedPolicies);
247         oldNotification.setRemovedPolicies(removedPolicies);
248         return oldNotification;
249     }
250
251     private StdRemovedPolicy getRemovedPolicy(final String version, final String policyName) {
252         return new StdRemovedPolicy() {
253
254             @Override
255             public String getVersionNo() {
256                 return version;
257             }
258
259             @Override
260             public String getPolicyName() {
261                 return policyName;
262             }
263         };
264     }
265
266     private StdLoadedPolicy getStdLoadedPolicy(final String version, final String policyName) {
267         return new StdLoadedPolicy() {
268
269             @Override
270             public String getVersionNo() {
271                 return version;
272             }
273
274             @Override
275             public String getPolicyName() {
276                 return policyName;
277             }
278
279             @Override
280             public Map<String, String> getMatches() {
281                 return Collections.emptyMap();
282             }
283         };
284     }
285
286     /**
287      * Perform pre-test initialization.
288      *
289      * @throws Exception
290      *             if the initialization fails for some reason
291      *
292      * @generatedBy CodePro at 6/1/16 1:40 PM
293      */
294     @Before
295     public void setUp() throws Exception {
296         // add additional set up code here
297     }
298
299     /**
300      * Perform post-test clean-up.
301      *
302      * @throws Exception
303      *             if the clean-up fails for some reason
304      *
305      * @generatedBy CodePro at 6/1/16 1:40 PM
306      */
307     @After
308     public void tearDown() throws Exception {
309         // Add additional tear down code here
310     }
311
312     /**
313      * Launch the test.
314      *
315      * @param args
316      *            the command line arguments
317      *
318      * @generatedBy CodePro at 6/1/16 1:40 PM
319      */
320     public static void main(final String[] args) {
321         new org.junit.runner.JUnitCore().run(NotificationUnMarshalTest.class);
322     }
323 }