Initial OpenECOMP policy/engine commit
[policy/engine.git] / PolicyEngineAPI / src / test / java / org / openecomp / policy / test / PolicyEngineTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
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.openecomp.policy.test;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.fail;
26
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.junit.Test;
34 import org.openecomp.policy.api.NotificationScheme;
35 import org.openecomp.policy.api.PolicyEngine;
36 import org.openecomp.policy.api.PolicyEngineException;
37
38 import org.openecomp.policy.common.logging.flexlogger.*; 
39
40 public class PolicyEngineTest {
41
42         private static final Logger logger = FlexLogger.getLogger(PolicyEngineTest.class);
43         private PolicyEngine policyEngine = null;
44         private String filePath = null;
45         
46         @Test
47         public void testPolicyEngineForFail() {
48                 filePath = null;
49                 try {
50                         policyEngine = new PolicyEngine(filePath);
51                 } catch (PolicyEngineException e) {
52                         logger.warn(e.getMessage());
53                 }
54                 assertNull(policyEngine);
55                 // Test even for this case.
56                 filePath = "NotNull";
57                 try {
58                         policyEngine = new PolicyEngine(filePath);
59                 } catch (PolicyEngineException e) {
60                         logger.warn(e.getMessage());
61                 }
62                 assertNull(policyEngine);
63         }
64         
65         @Test
66         public void testPolicyEngineforPropertyFileError() {
67                 filePath = "Test/config_error.property";
68                 isFileAvailable(filePath);
69                 try {
70                         policyEngine = new PolicyEngine(filePath);
71                 } catch (PolicyEngineException e) {
72                         logger.warn(e.getMessage());
73                 }
74                 assertNull(policyEngine);
75         }
76         
77         @Test
78         public void testPolicyEngineforPDPURLError() {
79                 String filePath = "Test/config_fail.properties";
80                 isFileAvailable(filePath);
81                 try {
82                         policyEngine = new PolicyEngine(filePath);
83                 } catch (PolicyEngineException e) {
84                         logger.warn(e.getMessage());
85                 }
86                 assertNull(policyEngine);
87         }
88         
89         @Test
90         public void testPolicyEngineForPass() {
91                 String filePath = "Test/config_pass.properties";
92                 isFileAvailable(filePath);
93                 try {
94                         policyEngine = new PolicyEngine(filePath);
95                 } catch (PolicyEngineException e) {
96                         logger.warn(e.getMessage());
97                 }
98                 assertNotNull(policyEngine);
99         }
100         
101         @Test
102         public void testPolicyEngineForUEBPass() {
103                 String filePath = "Test/config_UEB_pass.properties";
104                 isFileAvailable(filePath);
105                 try {
106                         policyEngine = new PolicyEngine(filePath);
107                 } catch (PolicyEngineException e) {
108                         logger.warn(e.getMessage());
109                 }
110                 assertNotNull(policyEngine);
111         }
112         
113         
114         @Test
115         public void testPolicyEngineForUEBBadType() {
116                 String filePath = "Test/config_UEB_bad_type.properties";
117                 isFileAvailable(filePath);
118                 try {
119                         policyEngine = new PolicyEngine(filePath);
120                 } catch (PolicyEngineException e) {
121                         logger.warn(e.getMessage());
122                 }
123                 assertNotNull(policyEngine);
124         }
125         
126         @Test
127         public void testPolicyEngineForUEBBadServerType() {
128                 String filePath = "Test/config_UEB_badservers.properties";
129                 isFileAvailable(filePath);
130                 try {
131                         policyEngine = new PolicyEngine(filePath);
132                 } catch (PolicyEngineException e) {
133                         logger.warn(e.getMessage());
134                 }
135                 assertNotNull(policyEngine);
136         }
137         
138         @Test
139         public void testPolicyEngineNotficationAutoUEB() {
140                 String filePath = "Test/config_UEB_pass.properties";
141                 isFileAvailable(filePath);
142                 try {
143                         policyEngine = new PolicyEngine(filePath);
144                         policyEngine.setScheme(NotificationScheme.AUTO_ALL_NOTIFICATIONS);
145                 } catch (PolicyEngineException e) {
146                         logger.warn(e.getMessage());
147                 }
148                 assertNotNull(policyEngine);
149         }
150         
151         @Test
152         public void testPolicyEngineNotficationAuto() {
153                 String filePath = "Test/config_pass.properties";
154                 isFileAvailable(filePath);
155                 try {
156                         policyEngine = new PolicyEngine(filePath);
157                         policyEngine.setScheme(NotificationScheme.AUTO_ALL_NOTIFICATIONS);
158                         //policyEngine.getNotification();
159                 } catch (PolicyEngineException e) {
160                         logger.warn(e.getMessage());
161                 }
162                 assertNotNull(policyEngine);
163         }
164
165         public void isFileAvailable(String filePath) {
166                 Path file = Paths.get(filePath);
167                 if (Files.notExists(file)) {
168                         logger.error("File Doesn't Exist "+ file.toString());
169                         fail("File: " +filePath + " Not found");
170                 }
171         }
172 }