2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.std.test;
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;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.List;
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;
43 import com.fasterxml.jackson.databind.JsonMappingException;
44 import com.fasterxml.jackson.databind.ObjectMapper;
47 * The class <code>NotificationUnMarshalTest</code> contains tests for the class
48 * <code>{@link NotificationUnMarshal}</code>.
50 * @generatedBy CodePro at 6/1/16 1:40 PM
51 * @version $Revision: 1.0 $
53 public class NotificationUnMarshalTest {
54 private static final String EMPTY_STRING = "";
56 private static final String POLICY_NAME = "ONAP";
58 private static final String POLICY_VERSION = "1.0.0";
61 * Run the StdPDPNotification notificationJSON(String) method test.
65 * @generatedBy CodePro at 6/1/16 1:40 PM
67 @Test(expected = JsonMappingException.class)
68 public void testNotificationJSON_EmptyString_ShouldThrowException() throws Exception {
69 final StdPDPNotification result = NotificationUnMarshal.notificationJSON(EMPTY_STRING);
71 assertNotNull(result);
75 public void testNotificationJSON_StdPDPNotificationJsonStringWithEmptyLoadedAndRemovedPolicies_emptyLoadedPolices()
77 final String json = getPDPNotificationAsJsonString(new StdPDPNotification());
79 final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
81 assertNotNull(result);
82 assertTrue(result.getLoadedPolicies().isEmpty());
86 public void testNotificationJSON_StdPDPNotificationJsonStringWithEmptyLoadedpolicy_emptyLoadedPolices()
88 final StdPDPNotification notification = getPDPNotification(
89 Arrays.asList(getRemovedPolicy(POLICY_VERSION, POLICY_NAME)), Collections.emptyList());
90 final String json = getPDPNotificationAsJsonString(notification);
92 final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
94 assertNotNull(result);
95 assertTrue(result.getLoadedPolicies().isEmpty());
99 public void testNotificationJSON_validPDPNotificationJsonStringWithRemovedAndLoadedPolicies_UpdateTypeUpdateAndLoadedPolicyAdded()
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);
105 final String json = getPDPNotificationAsJsonString(notification);
107 final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
109 assertNotNull(result);
110 assertFalse(result.getLoadedPolicies().isEmpty());
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());
119 * Run the StdPDPNotification notificationJSON(String) method test.
123 * @generatedBy CodePro at 6/1/16 1:40 PM
126 public void testNotificationJSON_validPDPNotificationJsonStringWithRemovedAndLoadedPolicies_UpdateTypeNewAndLoadedPolicyAdded()
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);
132 final String json = getPDPNotificationAsJsonString(notification);
134 final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
136 assertNotNull(result);
137 assertFalse(result.getLoadedPolicies().isEmpty());
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());
146 public void testNotificationJSON_validPDPNotificationJsonStringLoadedPoliciesAndNullRemovedPolicies_UpdateTypeNewAndLoadedPolicyAdded()
148 final List<StdLoadedPolicy> loadedPolices = Arrays.asList(getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME));
149 final StdPDPNotification notification = getPDPNotification(null, loadedPolices);
151 final String json = getPDPNotificationAsJsonString(notification);
153 final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
155 assertNotNull(result);
156 assertFalse(result.getLoadedPolicies().isEmpty());
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());
165 * Run the StdPDPNotification notificationJSON(String) method test.
169 * @generatedBy CodePro at 6/1/16 1:40 PM
171 @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class)
172 public void testNotificationJSON_7() throws Exception {
173 final String json = EMPTY_STRING;
175 final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
177 // add additional test code here
178 assertNotNull(result);
182 * Run the StdPDPNotification notificationJSON(String) method test.
186 * @generatedBy CodePro at 6/1/16 1:40 PM
188 @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class)
189 public void testNotificationJSON_8() throws Exception {
190 final String json = EMPTY_STRING;
192 final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
194 // add additional test code here
195 assertNotNull(result);
199 * Run the StdPDPNotification notificationJSON(String) method test.
203 * @generatedBy CodePro at 6/1/16 1:40 PM
205 @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class)
206 public void testNotificationJSON_9() throws Exception {
207 final String json = EMPTY_STRING;
209 final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
211 // add additional test code here
212 assertNotNull(result);
216 * Run the StdPDPNotification notificationJSON(String) method test.
220 * @generatedBy CodePro at 6/1/16 1:40 PM
222 @Test(expected = com.fasterxml.jackson.databind.JsonMappingException.class)
223 public void testNotificationJSON_10() throws Exception {
224 final String json = EMPTY_STRING;
226 final StdPDPNotification result = NotificationUnMarshal.notificationJSON(json);
228 // add additional test code here
229 assertNotNull(result);
232 private String getPDPNotificationAsJsonString(final StdPDPNotification notification) {
233 final ObjectMapper mapper = new ObjectMapper();
236 return mapper.writeValueAsString(notification);
237 } catch (final Exception expection) {
238 throw new RuntimeException(expection);
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;
251 private StdRemovedPolicy getRemovedPolicy(final String version, final String policyName) {
252 return new StdRemovedPolicy() {
255 public String getVersionNo() {
260 public String getPolicyName() {
266 private StdLoadedPolicy getStdLoadedPolicy(final String version, final String policyName) {
267 return new StdLoadedPolicy() {
270 public String getVersionNo() {
275 public String getPolicyName() {
280 public Map<String, String> getMatches() {
281 return Collections.emptyMap();
287 * Perform pre-test initialization.
290 * if the initialization fails for some reason
292 * @generatedBy CodePro at 6/1/16 1:40 PM
295 public void setUp() throws Exception {
296 // add additional set up code here
300 * Perform post-test clean-up.
303 * if the clean-up fails for some reason
305 * @generatedBy CodePro at 6/1/16 1:40 PM
308 public void tearDown() throws Exception {
309 // Add additional tear down code here
316 * the command line arguments
318 * @generatedBy CodePro at 6/1/16 1:40 PM
320 public static void main(final String[] args) {
321 new org.junit.runner.JUnitCore().run(NotificationUnMarshalTest.class);