774bf8a7e6093db2c0eff4c54f843687e75e1411
[policy/engine.git] / PolicyEngineUtils / src / test / java / org / onap / policy / utils / test / NotificationStoreTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineUtils
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.utils.test;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.junit.Test;
32 import org.onap.policy.api.NotificationType;
33 import org.onap.policy.api.UpdateType;
34 import org.onap.policy.std.NotificationStore;
35 import org.onap.policy.std.StdLoadedPolicy;
36 import org.onap.policy.std.StdPDPNotification;
37 import org.onap.policy.std.StdRemovedPolicy;
38 import org.onap.policy.utils.PolicyUtils;
39
40 public class NotificationStoreTest {
41     
42     @Test
43     public void notificationTest() throws IOException{
44         // Notification Delta test first. 
45         NotificationStore.recordNotification(new StdPDPNotification());
46         assertEquals("{\"removedPolicies\":[],\"loadedPolicies\":[],\"notificationType\":null}", PolicyUtils.objectToJsonString(NotificationStore.getDeltaNotification(new StdPDPNotification())));
47         // Initialize test 
48         StdPDPNotification notification = new StdPDPNotification();
49         notification.setNotificationType(NotificationType.BOTH);
50         List<StdLoadedPolicy> loadedPolicies = new ArrayList<>();
51         StdLoadedPolicy loadedPolicy = new StdLoadedPolicy();
52         loadedPolicy.setPolicyName("com.testing");
53         loadedPolicy.setUpdateType(UpdateType.UPDATE);
54         loadedPolicy.setVersionNo("2");
55         Map<String, String> matches = new HashMap<>();
56         matches.put("test", "test");
57         loadedPolicy.setMatches(matches);
58         loadedPolicies.add(loadedPolicy);
59         notification.setLoadedPolicies(loadedPolicies);
60         List<StdRemovedPolicy> removedPolicies = new ArrayList<>();
61         StdRemovedPolicy removedPolicy = new StdRemovedPolicy();
62         removedPolicy.setPolicyName("com.testing");
63         removedPolicy.setVersionNo("1");
64         removedPolicies.add(removedPolicy);
65         notification.setRemovedPolicies(removedPolicies);
66         NotificationStore.recordNotification(notification);
67         assertEquals("{\"removedPolicies\":[{\"policyName\":\"com.testing\",\"versionNo\":\"1\"}],\"loadedPolicies\":[{\"policyName\":\"com.testing\",\"versionNo\":\"2\",\"matches\":{\"test\":\"test\"},\"updateType\":\"UPDATE\"}],\"notificationType\":\"BOTH\"}", PolicyUtils.objectToJsonString(NotificationStore.getNotificationRecord()));
68         // Add new Notifications. 
69         notification = new StdPDPNotification();
70         notification.setNotificationType(NotificationType.BOTH);
71         loadedPolicies = new ArrayList<>();
72         loadedPolicy = new StdLoadedPolicy();
73         loadedPolicy.setPolicyName("com.test.3.xml");
74         loadedPolicy.setUpdateType(UpdateType.NEW);
75         loadedPolicy.setVersionNo("3");
76         matches = new HashMap<>();
77         matches.put("test", "test");
78         loadedPolicy.setMatches(matches);
79         loadedPolicies.add(loadedPolicy);
80         notification.setLoadedPolicies(loadedPolicies);
81         removedPolicies = new ArrayList<>();
82         removedPolicy = new StdRemovedPolicy();
83         removedPolicy.setPolicyName("com.testing.2.xml");
84         removedPolicy.setVersionNo("2");
85         removedPolicies.add(removedPolicy);
86         notification.setRemovedPolicies(removedPolicies);
87         NotificationStore.recordNotification(notification);
88         StdPDPNotification check = NotificationStore.getDeltaNotification(PolicyUtils.jsonStringToObject("{\"removedPolicies\":[{\"policyName\":\"com.testing\",\"versionNo\":\"1\"},{\"policyName\":\"com.testing\",\"versionNo\":\"2\"}],\"loadedPolicies\":[{\"policyName\":\"com.test\",\"versionNo\":\"3\",\"matches\":{\"test\":\"test\"},\"updateType\":\"NEW\"}],\"notificationType\":\"BOTH\"}", StdPDPNotification.class));
89         assertEquals("{\"removedPolicies\":[],\"loadedPolicies\":[],\"notificationType\":null}", PolicyUtils.objectToJsonString(check));
90         // Remove Notifications. 
91         notification = new StdPDPNotification();
92         notification.setNotificationType(NotificationType.REMOVE);
93         removedPolicies = new ArrayList<>();
94         removedPolicy = new StdRemovedPolicy();
95         removedPolicy.setPolicyName("com.test.3.xml");
96         removedPolicy.setVersionNo("3");
97         removedPolicies.add(removedPolicy);
98         notification.setRemovedPolicies(removedPolicies);
99         NotificationStore.recordNotification(notification);
100         check = NotificationStore.getDeltaNotification(PolicyUtils.jsonStringToObject("{\"removedPolicies\":[{\"policyName\":\"com.test\",\"versionNo\":\"3\"},{\"policyName\":\"com.testing\",\"versionNo\":\"1\"},{\"policyName\":\"com.testing\",\"versionNo\":\"2\"}],\"loadedPolicies\":[],\"notificationType\":\"REMOVE\"}", StdPDPNotification.class));
101         assertEquals("{\"removedPolicies\":[],\"loadedPolicies\":[],\"notificationType\":null}", PolicyUtils.objectToJsonString(check));
102         // Remove on remove duplicate  Notifications. 
103         notification = new StdPDPNotification();
104         notification.setNotificationType(NotificationType.REMOVE);
105         removedPolicies = new ArrayList<>();
106         removedPolicy = new StdRemovedPolicy();
107         removedPolicy.setPolicyName("com.test.3.xml");
108         removedPolicy.setVersionNo("3");
109         removedPolicies.add(removedPolicy);
110         notification.setRemovedPolicies(removedPolicies);
111         NotificationStore.recordNotification(notification);
112         check = NotificationStore.getDeltaNotification(PolicyUtils.jsonStringToObject("{\"removedPolicies\":[{\"policyName\":\"com.test\",\"versionNo\":\"3\"},{\"policyName\":\"com.testing\",\"versionNo\":\"1\"},{\"policyName\":\"com.testing\",\"versionNo\":\"2\"}],\"loadedPolicies\":[],\"notificationType\":\"REMOVE\"}", StdPDPNotification.class));
113         assertEquals("{\"removedPolicies\":[],\"loadedPolicies\":[],\"notificationType\":null}", PolicyUtils.objectToJsonString(check));
114         // Update  Notification 
115         notification = new StdPDPNotification();
116         notification.setNotificationType(NotificationType.UPDATE);
117         loadedPolicies = new ArrayList<>();
118         loadedPolicy = new StdLoadedPolicy();
119         loadedPolicy.setPolicyName("com.test.3.xml");
120         loadedPolicy.setUpdateType(UpdateType.NEW);
121         loadedPolicy.setVersionNo("3");
122         matches = new HashMap<>();
123         matches.put("test", "test");
124         loadedPolicy.setMatches(matches);
125         loadedPolicies.add(loadedPolicy);
126         notification.setLoadedPolicies(loadedPolicies);
127         NotificationStore.recordNotification(notification);
128         check = NotificationStore.getDeltaNotification(PolicyUtils.jsonStringToObject("{\"removedPolicies\":[{\"policyName\":\"com.testing\",\"versionNo\":\"1\"},{\"policyName\":\"com.testing\",\"versionNo\":\"2\"}],\"loadedPolicies\":[{\"policyName\":\"com.test\",\"versionNo\":\"3\",\"matches\":{\"test\":\"test\"},\"updateType\":\"NEW\"}],\"notificationType\":\"BOTH\"}", StdPDPNotification.class));
129         assertEquals("{\"removedPolicies\":[],\"loadedPolicies\":[],\"notificationType\":null}", PolicyUtils.objectToJsonString(check));
130         // Update  on update duplicate Notification 
131         notification = new StdPDPNotification();
132         notification.setNotificationType(NotificationType.UPDATE);
133         loadedPolicies = new ArrayList<>();
134         loadedPolicy = new StdLoadedPolicy();
135         loadedPolicy.setPolicyName("com.test.3.xml");
136         loadedPolicy.setUpdateType(UpdateType.NEW);
137         loadedPolicy.setVersionNo("3");
138         matches = new HashMap<>();
139         matches.put("test", "test");
140         loadedPolicy.setMatches(matches);
141         loadedPolicies.add(loadedPolicy);
142         notification.setLoadedPolicies(loadedPolicies);
143         NotificationStore.recordNotification(notification);
144         check = NotificationStore.getDeltaNotification(PolicyUtils.jsonStringToObject("{\"removedPolicies\":[{\"policyName\":\"com.testing\",\"versionNo\":\"1\"},{\"policyName\":\"com.testing\",\"versionNo\":\"2\"}],\"loadedPolicies\":[{\"policyName\":\"com.test\",\"versionNo\":\"3\",\"matches\":{\"test\":\"test\"},\"updateType\":\"NEW\"}],\"notificationType\":\"BOTH\"}", StdPDPNotification.class));
145         assertEquals("{\"removedPolicies\":[],\"loadedPolicies\":[],\"notificationType\":null}", PolicyUtils.objectToJsonString(check));
146         // 
147         // Notification Delta Tests
148         //
149         notification = new StdPDPNotification();
150         notification.setNotificationType(NotificationType.BOTH);
151         loadedPolicies = new ArrayList<>();
152         loadedPolicy = new StdLoadedPolicy();
153         loadedPolicy.setPolicyName("com.testing");
154         loadedPolicy.setUpdateType(UpdateType.NEW);
155         loadedPolicy.setVersionNo("3");
156         matches = new HashMap<>();
157         matches.put("test", "test");
158         loadedPolicy.setMatches(matches);
159         loadedPolicies.add(loadedPolicy);
160         notification.setLoadedPolicies(loadedPolicies);
161         removedPolicies = new ArrayList<>();
162         removedPolicy = new StdRemovedPolicy();
163         removedPolicy.setPolicyName("com.test.3.xml");
164         removedPolicy.setVersionNo("3");
165         removedPolicies.add(removedPolicy);
166         notification.setRemovedPolicies(removedPolicies);
167         check = NotificationStore.getDeltaNotification(notification);
168         assertEquals("{\"removedPolicies\":[{\"policyName\":\"com.test\",\"versionNo\":\"3\"}],\"loadedPolicies\":[{\"policyName\":\"com.testing\",\"versionNo\":\"3\",\"matches\":{\"test\":\"test\"},\"updateType\":\"NEW\"}],\"notificationType\":\"BOTH\"}", PolicyUtils.objectToJsonString(check));
169     }
170
171 }