Re-format source code
[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, 2019 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}",
47                 PolicyUtils.objectToJsonString(NotificationStore.getDeltaNotification(new StdPDPNotification())));
48         // Initialize test
49         StdPDPNotification notification = new StdPDPNotification();
50         notification.setNotificationType(NotificationType.BOTH);
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         List<StdLoadedPolicy> loadedPolicies = new ArrayList<>();
59         loadedPolicies.add(loadedPolicy);
60         notification.setLoadedPolicies(loadedPolicies);
61         List<StdRemovedPolicy> removedPolicies = new ArrayList<>();
62         StdRemovedPolicy removedPolicy = new StdRemovedPolicy();
63         removedPolicy.setPolicyName("com.testing");
64         removedPolicy.setVersionNo("1");
65         removedPolicies.add(removedPolicy);
66         notification.setRemovedPolicies(removedPolicies);
67         NotificationStore.recordNotification(notification);
68         assertEquals(
69                 "{\"removedPolicies\":[{\"policyName\":\"com.testing\",\"versionNo\":\"1\"}],\"loadedPolicies\":[{\"policyName\":\"com.testing\",\"versionNo\":\"2\",\"matches\":{\"test\":\"test\"},\"updateType\":\"UPDATE\"}],\"notificationType\":\"BOTH\"}",
70                 PolicyUtils.objectToJsonString(NotificationStore.getNotificationRecord()));
71         // Add new Notifications.
72         notification = new StdPDPNotification();
73         notification.setNotificationType(NotificationType.BOTH);
74         loadedPolicies = new ArrayList<>();
75         loadedPolicy = new StdLoadedPolicy();
76         loadedPolicy.setPolicyName("com.test.3.xml");
77         loadedPolicy.setUpdateType(UpdateType.NEW);
78         loadedPolicy.setVersionNo("3");
79         matches = new HashMap<>();
80         matches.put("test", "test");
81         loadedPolicy.setMatches(matches);
82         loadedPolicies.add(loadedPolicy);
83         notification.setLoadedPolicies(loadedPolicies);
84         removedPolicies = new ArrayList<>();
85         removedPolicy = new StdRemovedPolicy();
86         removedPolicy.setPolicyName("com.testing.2.xml");
87         removedPolicy.setVersionNo("2");
88         removedPolicies.add(removedPolicy);
89         notification.setRemovedPolicies(removedPolicies);
90         NotificationStore.recordNotification(notification);
91         StdPDPNotification check = NotificationStore.getDeltaNotification(PolicyUtils.jsonStringToObject(
92                 "{\"removedPolicies\":[{\"policyName\":\"com.testing\",\"versionNo\":\"1\"},{\"policyName\":\"com.testing\",\"versionNo\":\"2\"}],\"loadedPolicies\":[{\"policyName\":\"com.test\",\"versionNo\":\"3\",\"matches\":{\"test\":\"test\"},\"updateType\":\"NEW\"}],\"notificationType\":\"BOTH\"}",
93                 StdPDPNotification.class));
94         assertEquals("{\"removedPolicies\":[],\"loadedPolicies\":[],\"notificationType\":null}",
95                 PolicyUtils.objectToJsonString(check));
96         // Remove Notifications.
97         notification = new StdPDPNotification();
98         notification.setNotificationType(NotificationType.REMOVE);
99         removedPolicies = new ArrayList<>();
100         removedPolicy = new StdRemovedPolicy();
101         removedPolicy.setPolicyName("com.test.3.xml");
102         removedPolicy.setVersionNo("3");
103         removedPolicies.add(removedPolicy);
104         notification.setRemovedPolicies(removedPolicies);
105         NotificationStore.recordNotification(notification);
106         check = NotificationStore.getDeltaNotification(PolicyUtils.jsonStringToObject(
107                 "{\"removedPolicies\":[{\"policyName\":\"com.test\",\"versionNo\":\"3\"},{\"policyName\":\"com.testing\",\"versionNo\":\"1\"},{\"policyName\":\"com.testing\",\"versionNo\":\"2\"}],\"loadedPolicies\":[],\"notificationType\":\"REMOVE\"}",
108                 StdPDPNotification.class));
109         assertEquals("{\"removedPolicies\":[],\"loadedPolicies\":[],\"notificationType\":null}",
110                 PolicyUtils.objectToJsonString(check));
111         // Remove on remove duplicate Notifications.
112         notification = new StdPDPNotification();
113         notification.setNotificationType(NotificationType.REMOVE);
114         removedPolicies = new ArrayList<>();
115         removedPolicy = new StdRemovedPolicy();
116         removedPolicy.setPolicyName("com.test.3.xml");
117         removedPolicy.setVersionNo("3");
118         removedPolicies.add(removedPolicy);
119         notification.setRemovedPolicies(removedPolicies);
120         NotificationStore.recordNotification(notification);
121         check = NotificationStore.getDeltaNotification(PolicyUtils.jsonStringToObject(
122                 "{\"removedPolicies\":[{\"policyName\":\"com.test\",\"versionNo\":\"3\"},{\"policyName\":\"com.testing\",\"versionNo\":\"1\"},{\"policyName\":\"com.testing\",\"versionNo\":\"2\"}],\"loadedPolicies\":[],\"notificationType\":\"REMOVE\"}",
123                 StdPDPNotification.class));
124         assertEquals("{\"removedPolicies\":[],\"loadedPolicies\":[],\"notificationType\":null}",
125                 PolicyUtils.objectToJsonString(check));
126         // Update Notification
127         notification = new StdPDPNotification();
128         notification.setNotificationType(NotificationType.UPDATE);
129         loadedPolicies = new ArrayList<>();
130         loadedPolicy = new StdLoadedPolicy();
131         loadedPolicy.setPolicyName("com.test.3.xml");
132         loadedPolicy.setUpdateType(UpdateType.NEW);
133         loadedPolicy.setVersionNo("3");
134         matches = new HashMap<>();
135         matches.put("test", "test");
136         loadedPolicy.setMatches(matches);
137         loadedPolicies.add(loadedPolicy);
138         notification.setLoadedPolicies(loadedPolicies);
139         NotificationStore.recordNotification(notification);
140         check = NotificationStore.getDeltaNotification(PolicyUtils.jsonStringToObject(
141                 "{\"removedPolicies\":[{\"policyName\":\"com.testing\",\"versionNo\":\"1\"},{\"policyName\":\"com.testing\",\"versionNo\":\"2\"}],\"loadedPolicies\":[{\"policyName\":\"com.test\",\"versionNo\":\"3\",\"matches\":{\"test\":\"test\"},\"updateType\":\"NEW\"}],\"notificationType\":\"BOTH\"}",
142                 StdPDPNotification.class));
143         assertEquals("{\"removedPolicies\":[],\"loadedPolicies\":[],\"notificationType\":null}",
144                 PolicyUtils.objectToJsonString(check));
145         // Update on update duplicate Notification
146         notification = new StdPDPNotification();
147         notification.setNotificationType(NotificationType.UPDATE);
148         loadedPolicies = new ArrayList<>();
149         loadedPolicy = new StdLoadedPolicy();
150         loadedPolicy.setPolicyName("com.test.3.xml");
151         loadedPolicy.setUpdateType(UpdateType.NEW);
152         loadedPolicy.setVersionNo("3");
153         matches = new HashMap<>();
154         matches.put("test", "test");
155         loadedPolicy.setMatches(matches);
156         loadedPolicies.add(loadedPolicy);
157         notification.setLoadedPolicies(loadedPolicies);
158         NotificationStore.recordNotification(notification);
159         check = NotificationStore.getDeltaNotification(PolicyUtils.jsonStringToObject(
160                 "{\"removedPolicies\":[{\"policyName\":\"com.testing\",\"versionNo\":\"1\"},{\"policyName\":\"com.testing\",\"versionNo\":\"2\"}],\"loadedPolicies\":[{\"policyName\":\"com.test\",\"versionNo\":\"3\",\"matches\":{\"test\":\"test\"},\"updateType\":\"NEW\"}],\"notificationType\":\"BOTH\"}",
161                 StdPDPNotification.class));
162         assertEquals("{\"removedPolicies\":[],\"loadedPolicies\":[],\"notificationType\":null}",
163                 PolicyUtils.objectToJsonString(check));
164         //
165         // Notification Delta Tests
166         //
167         notification = new StdPDPNotification();
168         notification.setNotificationType(NotificationType.BOTH);
169         loadedPolicies = new ArrayList<>();
170         loadedPolicy = new StdLoadedPolicy();
171         loadedPolicy.setPolicyName("com.testing");
172         loadedPolicy.setUpdateType(UpdateType.NEW);
173         loadedPolicy.setVersionNo("3");
174         matches = new HashMap<>();
175         matches.put("test", "test");
176         loadedPolicy.setMatches(matches);
177         loadedPolicies.add(loadedPolicy);
178         notification.setLoadedPolicies(loadedPolicies);
179         removedPolicies = new ArrayList<>();
180         removedPolicy = new StdRemovedPolicy();
181         removedPolicy.setPolicyName("com.test.3.xml");
182         removedPolicy.setVersionNo("3");
183         removedPolicies.add(removedPolicy);
184         notification.setRemovedPolicies(removedPolicies);
185         check = NotificationStore.getDeltaNotification(notification);
186         assertEquals(
187                 "{\"removedPolicies\":[{\"policyName\":\"com.test\",\"versionNo\":\"3\"}],\"loadedPolicies\":[{\"policyName\":\"com.testing\",\"versionNo\":\"3\",\"matches\":{\"test\":\"test\"},\"updateType\":\"NEW\"}],\"notificationType\":\"BOTH\"}",
188                 PolicyUtils.objectToJsonString(check));
189     }
190
191 }