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=========================================================
21 package org.onap.policy.utils.test;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
27 import java.util.ArrayList;
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.Properties;
33 import java.util.concurrent.TimeUnit;
35 import javax.persistence.EntityManager;
36 import javax.persistence.EntityTransaction;
37 import javax.persistence.Persistence;
38 import javax.persistence.PersistenceException;
39 import javax.persistence.Query;
41 import org.eclipse.persistence.config.PersistenceUnitProperties;
42 import org.junit.After;
43 import org.junit.Test;
44 import org.onap.policy.api.NotificationType;
45 import org.onap.policy.api.UpdateType;
46 import org.onap.policy.jpa.BackUpMonitorEntity;
47 import org.onap.policy.std.NotificationStore;
48 import org.onap.policy.std.StdLoadedPolicy;
49 import org.onap.policy.std.StdPDPNotification;
50 import org.onap.policy.std.StdRemovedPolicy;
51 import org.onap.policy.utils.BackUpMonitor;
52 import org.onap.policy.utils.BackUpMonitor.ResourceNode;
53 import org.onap.policy.utils.BackUpMonitorException;
54 import org.onap.policy.utils.PolicyUtils;
56 import com.fasterxml.jackson.core.JsonProcessingException;
59 public class BackUpMonitorTest {
61 @Test (expected = PersistenceException.class)
62 public void backUpMonitorTestFail() throws Exception{
63 Properties properties = new Properties();
64 properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
65 properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk");
66 properties.setProperty("javax.persistence.jdbc.user", "policy_user");
67 properties.setProperty("javax.persistence.jdbc.password", "");
68 BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new DummyBackUpHandler());
72 public void backUpMonitorTestFailNoUser() throws Exception{
73 Properties properties = new Properties();
74 properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
75 properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk");
76 properties.setProperty("javax.persistence.jdbc.user", "");
77 properties.setProperty("javax.persistence.jdbc.password", "password");
78 BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new DummyBackUpHandler());
83 public void backUpMonitorTestFailNoURL() throws Exception{
84 Properties properties = new Properties();
85 properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
86 properties.setProperty("javax.persistence.jdbc.url", "");
87 properties.setProperty("javax.persistence.jdbc.user", "test");
88 properties.setProperty("javax.persistence.jdbc.password", "password");
89 properties.setProperty("ping_interval", "500");
90 BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new DummyBackUpHandler());
95 public void backUpMonitorTestFailNoDriver() throws Exception{
96 Properties properties = new Properties();
97 properties.setProperty("javax.persistence.jdbc.driver", "");
98 properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk");
99 properties.setProperty("javax.persistence.jdbc.user", "test");
100 properties.setProperty("javax.persistence.jdbc.password", "password");
101 properties.setProperty("ping_interval", "500");
102 BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new DummyBackUpHandler());
107 public void backUpMonitorTestFailNoNode() throws Exception{
108 Properties properties = new Properties();
109 properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
110 properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk");
111 properties.setProperty("javax.persistence.jdbc.user", "test");
112 properties.setProperty("javax.persistence.jdbc.password", "password");
113 properties.setProperty("ping_interval", "");
114 BackUpMonitor bum = BackUpMonitor.getInstance(null, "brms_test" , properties, new DummyBackUpHandler());
119 public void backUpMonitorTestFailNoResource() throws Exception{
120 Properties properties = new Properties();
121 properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
122 properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk");
123 properties.setProperty("javax.persistence.jdbc.user", "test");
124 properties.setProperty("javax.persistence.jdbc.password", "password");
125 BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), null , properties, new DummyBackUpHandler());
130 public void backUpMonitorTestFailNoProperties() throws Exception{
131 BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , null, new DummyBackUpHandler());
136 public void backUpMonitorTestFailNoHandler() throws Exception{
137 Properties properties = new Properties();
138 properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
139 properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk");
140 properties.setProperty("javax.persistence.jdbc.user", "test");
141 properties.setProperty("javax.persistence.jdbc.password", "password");
142 properties.setProperty("ping_interval", "500");
143 BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, null);
148 public void backUpMonitorPingError() throws BackUpMonitorException {
149 Properties properties = new Properties();
150 properties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
151 properties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:file:./sql/xacmlTest");
152 properties.setProperty("javax.persistence.jdbc.user", "sa");
153 properties.setProperty("javax.persistence.jdbc.password", "");
154 properties.setProperty("ping_interval", "123a");
155 properties.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistencePUtest.xml");
156 BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new DummyBackUpHandler());
157 assertTrue(bum.getFlag());
161 public void backUpMonitorMasterTest() throws BackUpMonitorException, InterruptedException, JsonProcessingException {
162 Properties properties = new Properties();
163 // Master Check. Initial Run.
164 properties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
165 properties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:file:./sql/xacmlTest");
166 properties.setProperty("javax.persistence.jdbc.user", "sa");
167 properties.setProperty("javax.persistence.jdbc.password", "");
168 properties.setProperty("ping_interval", "500");
169 properties.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistencePUtest.xml");
170 BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new DummyBackUpHandler());
171 createPolicyNotification();
172 assertTrue(bum.getFlag());
173 // Start a slave check.
174 startSlave(properties);
175 updatePolicyNotification();
176 TimeUnit.MILLISECONDS.sleep(1500);
177 assertFalse(bum.getFlag());
178 // Get Back to Master test
179 TimeUnit.MILLISECONDS.sleep(2000);
180 assertTrue(bum.getFlag());
182 changeALL(properties, "SLAVE");
183 TimeUnit.MILLISECONDS.sleep(2000);
184 assertTrue(bum.getFlag());
186 changeALL(properties, "MASTER");
187 TimeUnit.MILLISECONDS.sleep(2000);
188 assertTrue(bum.getFlag());
192 private void updatePolicyNotification() {
193 StdPDPNotification notification = new StdPDPNotification();
194 notification.setNotificationType(NotificationType.BOTH);
195 List<StdLoadedPolicy> loadedPolicies = new ArrayList<>();
196 StdLoadedPolicy loadedPolicy = new StdLoadedPolicy();
197 loadedPolicy.setPolicyName("com.testing");
198 loadedPolicy.setUpdateType(UpdateType.UPDATE);
199 loadedPolicy.setVersionNo("2");
200 Map<String, String> matches = new HashMap<>();
201 matches.put("test", "test");
202 loadedPolicy.setMatches(matches);
203 loadedPolicies.add(loadedPolicy);
204 notification.setLoadedPolicies(loadedPolicies);
205 List<StdRemovedPolicy> removedPolicies = new ArrayList<>();
206 StdRemovedPolicy removedPolicy = new StdRemovedPolicy();
207 removedPolicy.setPolicyName("com.testing");
208 removedPolicy.setVersionNo("1");
209 notification.setRemovedPolicies(removedPolicies);
210 NotificationStore.recordNotification(notification);
213 private void createPolicyNotification() {
214 StdPDPNotification notification = new StdPDPNotification();
215 notification.setNotificationType(NotificationType.UPDATE);
216 List<StdLoadedPolicy> loadedPolicies = new ArrayList<>();
217 StdLoadedPolicy loadedPolicy = new StdLoadedPolicy();
218 loadedPolicy.setPolicyName("com.testing");
219 loadedPolicy.setUpdateType(UpdateType.NEW);
220 loadedPolicy.setVersionNo("1");
221 Map<String, String> matches = new HashMap<>();
222 matches.put("test", "test");
223 loadedPolicy.setMatches(matches);
224 loadedPolicies.add(loadedPolicy);
225 notification.setLoadedPolicies(loadedPolicies);
226 NotificationStore.recordNotification(notification);
229 private void changeALL(Properties properties, String flag) {
230 EntityManager em = Persistence.createEntityManagerFactory("PolicyEngineUtils", properties).createEntityManager();
231 EntityTransaction et = em.getTransaction();
233 Query query = em.createQuery("select b from BackUpMonitorEntity b where b.resourceNodeName = :nn");
234 query.setParameter("nn", ResourceNode.BRMS.toString());
235 for(Object bMValue: query.getResultList()){
236 BackUpMonitorEntity bmEntity = (BackUpMonitorEntity) bMValue;
237 bmEntity.setFlag(flag);
238 bmEntity.setTimeStamp(new Date());
244 private void startSlave(Properties properties) throws JsonProcessingException {
245 EntityManager em = Persistence.createEntityManagerFactory("PolicyEngineUtils", properties).createEntityManager();
246 EntityTransaction et = em.getTransaction();
248 Query query = em.createQuery("select b from BackUpMonitorEntity b where b.resourceNodeName = :nn");
249 query.setParameter("nn", ResourceNode.BRMS.toString());
250 List<?> bMList = query.getResultList();
251 BackUpMonitorEntity origBM = (BackUpMonitorEntity) bMList.get(0);
252 origBM.setFlag("SLAVE");
253 origBM.setTimeStamp(new Date());
254 BackUpMonitorEntity bMEntity = new BackUpMonitorEntity();
255 bMEntity.setResourceNodeName(ResourceNode.BRMS.toString());
256 bMEntity.setResourceName("brms_test2");
257 bMEntity.setFlag("MASTER");
258 bMEntity.setTimeStamp(new Date());
259 StdPDPNotification notification = new StdPDPNotification();
260 notification.setNotificationType(NotificationType.UPDATE);
261 List<StdLoadedPolicy> loadedPolicies = new ArrayList<>();
262 StdLoadedPolicy loadedPolicy = new StdLoadedPolicy();
263 loadedPolicy.setPolicyName("com.test");
264 loadedPolicy.setUpdateType(UpdateType.NEW);
265 loadedPolicy.setVersionNo("1");
266 Map<String, String> matches = new HashMap<>();
267 matches.put("test", "test");
268 loadedPolicy.setMatches(matches);
269 loadedPolicies.add(loadedPolicy);
270 notification.setLoadedPolicies(loadedPolicies);
271 bMEntity.setNotificationRecord(PolicyUtils.objectToJsonString(notification));
272 em.persist(bMEntity);
278 @Test(expected = BackUpMonitorException.class)
279 public void testException() throws InterruptedException, BackUpMonitorException{
280 BackUpMonitor.stop();
281 new BackUpMonitorException();
282 new BackUpMonitorException(new Exception());
283 new BackUpMonitorException("error");
284 new BackUpMonitorException("error", new Exception());
285 throw new BackUpMonitorException("error", new Exception(), false, false);
289 public void setup() throws InterruptedException{
290 BackUpMonitor.stop();