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=========================================================
23 import static org.junit.Assert.assertEquals;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashMap;
30 import org.openecomp.policy.pypdp.notifications.NotificationController;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.openecomp.policy.api.LoadedPolicy;
35 import org.openecomp.policy.api.NotificationType;
36 import org.openecomp.policy.api.PDPNotification;
37 import org.openecomp.policy.api.RemovedPolicy;
38 import org.openecomp.policy.api.UpdateType;
39 import org.openecomp.policy.std.StdLoadedPolicy;
40 import org.openecomp.policy.std.StdPDPNotification;
41 import org.openecomp.policy.std.StdRemovedPolicy;
43 import com.fasterxml.jackson.databind.ObjectMapper;
45 public class NotificationControllerTest {
46 private NotificationController notificationController = new NotificationController();
47 private StdPDPNotification notification;
51 notification = new StdPDPNotification();
52 notification.setNotificationType(NotificationType.BOTH);
53 Collection<StdRemovedPolicy> removedPolicies = new ArrayList<StdRemovedPolicy>();
54 Collection<StdLoadedPolicy> loadedPolicies = new ArrayList<StdLoadedPolicy>();
55 StdRemovedPolicy removedPolicy = new StdRemovedPolicy();
56 StdLoadedPolicy updatedPolicy = new StdLoadedPolicy();
57 removedPolicy.setPolicyName("Test");
58 removedPolicy.setVersionNo("1");
59 removedPolicies.add(removedPolicy);
60 updatedPolicy.setPolicyName("Testing");
61 updatedPolicy.setVersionNo("1");
62 updatedPolicy.setUpdateType(UpdateType.NEW);
63 Map<String, String> matches = new HashMap<String, String>();
64 matches.put("key", "value");
65 updatedPolicy.setMatches(matches);
66 loadedPolicies.add(updatedPolicy);
67 notification.setRemovedPolicies(removedPolicies);
68 notification.setLoadedPolicies(loadedPolicies);
69 NotificationController.record(notification);
73 public void notificationReceivedUpdateTest() throws Exception{
74 StdPDPNotification notification = new StdPDPNotification();
75 notification.setNotificationType(NotificationType.UPDATE);
76 Collection<StdLoadedPolicy> loadedPolicies = new ArrayList<StdLoadedPolicy>();
77 StdLoadedPolicy updatedPolicy = new StdLoadedPolicy();
78 updatedPolicy.setPolicyName("Test");
79 updatedPolicy.setVersionNo("1");
80 updatedPolicy.setUpdateType(UpdateType.NEW);
81 Map<String, String> matches = new HashMap<String, String>();
82 matches.put("key", "value");
83 updatedPolicy.setMatches(matches);
84 loadedPolicies.add(updatedPolicy);
85 notification.setLoadedPolicies(loadedPolicies);
86 notificationController.notificationReceived(notification);
87 Boolean result = false;
88 PDPNotification newNotification= jsonStringToNotification(NotificationController.record(notification));
89 for(LoadedPolicy loadedPolicy: newNotification.getLoadedPolicies()){
90 if(loadedPolicy.getPolicyName().equals("Test") && loadedPolicy.getVersionNo().equals("1")){
94 assertEquals(result,true);
96 NotificationController.record(notification),
97 "{\"removedPolicies\":[],\"updatedPolicies\":[{\"policyName\":\"Test\",\"versionNo\":\"1\",\"matches\":{\"key\":\"value\"},\"updateType\":\"NEW\"},{\"policyName\":\"Testing\",\"versionNo\":\"1\",\"matches\":{\"key\":\"value\"},\"updateType\":\"NEW\"}]}");*/
101 public void notificationReceivedRemovedTest() throws Exception{
102 StdPDPNotification notification = new StdPDPNotification();
103 notification.setNotificationType(NotificationType.REMOVE);
104 Collection<StdRemovedPolicy> removedPolicies = new ArrayList<StdRemovedPolicy>();
105 StdRemovedPolicy removedPolicy = new StdRemovedPolicy();
106 removedPolicy.setPolicyName("Testing");
107 removedPolicy.setVersionNo("1");
108 removedPolicies.add(removedPolicy);
109 notification.setRemovedPolicies(removedPolicies);
110 notificationController.notificationReceived(notification);
111 Boolean result = false;
112 PDPNotification newNotification= jsonStringToNotification(NotificationController.record(notification));
113 for(RemovedPolicy removed: newNotification.getRemovedPolicies()){
114 if(removed.getPolicyName().equals("Testing") && removed.getVersionNo().equals("1")){
118 assertEquals(result,true);
120 NotificationController.record(notification),
121 "{\"removedPolicies\":[{\"policyName\":\"Test\",\"versionNo\":\"1\"},{\"policyName\":\"Testing\",\"versionNo\":\"1\"}],\"updatedPolicies\":[]}");*/
124 public StdPDPNotification jsonStringToNotification(String json) throws Exception{
125 ObjectMapper mapper = new ObjectMapper();
126 return notification = mapper.readValue(json, StdPDPNotification.class);