Initial OpenECOMP policy/engine commit
[policy/engine.git] / PyPDPServer / src / test / java / testpypdp / NotificationControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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 testpypdp;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import org.openecomp.policy.pypdp.notifications.NotificationController;
31
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;
42
43 import com.fasterxml.jackson.databind.ObjectMapper;
44
45 public class NotificationControllerTest {
46         private NotificationController notificationController = new NotificationController();
47         private StdPDPNotification notification;
48
49         @Before
50         public void setUp() {
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);
70         }
71
72         @Test
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")){
91                                 result = true;
92                         }
93                 }
94                 assertEquals(result,true);
95                 /*assertEquals(
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\"}]}");*/
98         }
99
100         @Test
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")){
115                                 result = true;
116                         }
117                 }
118                 assertEquals(result,true);
119                 /*assertEquals(
120                                 NotificationController.record(notification),
121                                 "{\"removedPolicies\":[{\"policyName\":\"Test\",\"versionNo\":\"1\"},{\"policyName\":\"Testing\",\"versionNo\":\"1\"}],\"updatedPolicies\":[]}");*/
122         }
123         
124         public StdPDPNotification jsonStringToNotification(String json) throws Exception{
125                 ObjectMapper mapper = new ObjectMapper();
126                 return notification = mapper.readValue(json, StdPDPNotification.class);
127         }
128
129 }