Reformat PolicyEngineAPI test cases
[policy/engine.git] / PolicyEngineAPI / src / test / java / org / onap / policy / std / test / DummyNotificationHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.std.test;
24
25 import java.util.Collection;
26 import org.onap.policy.api.LoadedPolicy;
27 import org.onap.policy.api.NotificationHandler;
28 import org.onap.policy.api.NotificationType;
29 import org.onap.policy.api.PDPNotification;
30 import org.onap.policy.api.PolicyConfig;
31 import org.onap.policy.api.PolicyConfigException;
32 import org.onap.policy.api.PolicyConfigStatus;
33 import org.onap.policy.api.PolicyEngine;
34 import org.onap.policy.api.PolicyEngineException;
35 import org.onap.policy.api.RemovedPolicy;
36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
38
39 public class DummyNotificationHandler implements NotificationHandler {
40
41     private static final Logger LOGGER = FlexLogger.getLogger(DummyNotificationHandler.class);
42
43     @Override
44     public void notificationReceived(PDPNotification notification) {
45         System.out.println("Notification Received...");
46         System.out.println(notification.getNotificationType());
47         if (notification.getNotificationType().equals(NotificationType.REMOVE)) {
48             System.out.println("Removed Policies: \n");
49             for (RemovedPolicy removedPolicy : notification.getRemovedPolicies()) {
50                 System.out.println(removedPolicy.getPolicyName());
51                 System.out.println(removedPolicy.getVersionNo());
52             }
53         } else if (notification.getNotificationType().equals(NotificationType.UPDATE)) {
54             System.out.println("Updated Policies: \n");
55             for (LoadedPolicy updatedPolicy : notification.getLoadedPolicies()) {
56                 System.out.println("policyName : " + updatedPolicy.getPolicyName());
57                 System.out.println("policyVersion :" + updatedPolicy.getVersionNo());
58                 if (updatedPolicy.getPolicyName().contains(".Config_")) {
59                     System.out.println("Matches: " + updatedPolicy.getMatches());
60                     System.out.println("UpdateType: " + updatedPolicy.getUpdateType());
61                     // Checking the Name is correct or not.
62                     try {
63                         PolicyEngine policyEngine = new PolicyEngine("config.properties");
64                         @SuppressWarnings("deprecation")
65                         Collection<PolicyConfig> policyConfigs =
66                                 policyEngine.getConfigByPolicyName(updatedPolicy.getPolicyName());
67                         for (PolicyConfig policyConfig : policyConfigs) {
68                             if (policyConfig.getPolicyConfigStatus()
69                                     .equals(PolicyConfigStatus.CONFIG_RETRIEVED)) {
70                                 System.out.println("Policy Retrieved with this Name notified. ");
71                             } else {
72                                 System.err.println("\n\n Fail to retrieve policy !!!!\n\n");
73                             }
74                             // Also Test this case.
75                             if (policyConfig.getPolicyName()
76                                     .equals(updatedPolicy.getPolicyName())) {
77                                 System.out.println("Policy Name is good. ");
78                             } else {
79                                 System.err.println("\n\n Fail to check Name \n\n");
80                             }
81                         }
82                     } catch (PolicyEngineException e) {
83                         LOGGER.error("Exception Occured" + e);
84                     } catch (PolicyConfigException e) {
85                         LOGGER.error("Exception Occured" + e);
86                     }
87                 }
88             }
89         } else if (notification.getNotificationType().equals(NotificationType.BOTH)) {
90             System.out.println("Both updated and Removed Notification: \n");
91             System.out.println("Removed Policies: \n");
92             for (RemovedPolicy removedPolicy : notification.getRemovedPolicies()) {
93                 System.out.println(removedPolicy.getPolicyName());
94                 System.out.println(removedPolicy.getVersionNo());
95             }
96             System.out.println("Updated Policies: \n");
97             for (LoadedPolicy updatedPolicy : notification.getLoadedPolicies()) {
98                 System.out.println("policyName : " + updatedPolicy.getPolicyName());
99                 System.out.println("policyVersion :" + updatedPolicy.getVersionNo());
100                 System.out.println("Matches: " + updatedPolicy.getMatches());
101                 System.out.println("UpdateType: " + updatedPolicy.getUpdateType());
102                 // Checking the Name is correct or not.
103                 try {
104                     PolicyEngine policyEngine = new PolicyEngine("config.properties");
105                     @SuppressWarnings("deprecation")
106                     Collection<PolicyConfig> policyConfigs =
107                             policyEngine.getConfigByPolicyName(updatedPolicy.getPolicyName());
108                     for (PolicyConfig policyConfig : policyConfigs) {
109                         if (policyConfig.getPolicyConfigStatus()
110                                 .equals(PolicyConfigStatus.CONFIG_RETRIEVED)) {
111                             System.out.println("Policy Retrieved with this Name notified. ");
112                         } else {
113                             System.err.println("\n\n Fail to retrieve policy !!!!\n\n");
114                         }
115                         // Also Test this case.
116                         if (policyConfig.getPolicyName().equals(updatedPolicy.getPolicyName())) {
117                             System.out.println("Policy Name is good. ");
118                         } else {
119                             System.err.println("\n\n Fail to check Name \n\n");
120                         }
121                     }
122                 } catch (PolicyEngineException e) {
123                     LOGGER.error("Exception Occured" + e);
124                 } catch (PolicyConfigException e) {
125                     LOGGER.error("Exception Occured" + e);
126                 }
127             }
128         }
129     }
130 }