Re-format source code
[policy/engine.git] / PolicyEngineUtils / src / test / java / org / onap / policy / utils / test / BackUpMonitorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineUtils
4  * ================================================================================
5  * Copyright (C) 2017,2019 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.onap.policy.utils.test;
22
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
26
27 import com.fasterxml.jackson.core.JsonProcessingException;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Properties;
37 import java.util.concurrent.TimeUnit;
38
39 import javax.persistence.EntityManager;
40 import javax.persistence.EntityTransaction;
41 import javax.persistence.Persistence;
42 import javax.persistence.PersistenceException;
43 import javax.persistence.Query;
44
45 import org.apache.commons.io.FileUtils;
46 import org.eclipse.persistence.config.PersistenceUnitProperties;
47 import org.junit.After;
48 import org.junit.AfterClass;
49 import org.junit.Test;
50 import org.onap.policy.api.NotificationType;
51 import org.onap.policy.api.UpdateType;
52 import org.onap.policy.jpa.BackUpMonitorEntity;
53 import org.onap.policy.std.NotificationStore;
54 import org.onap.policy.std.StdLoadedPolicy;
55 import org.onap.policy.std.StdPDPNotification;
56 import org.onap.policy.std.StdRemovedPolicy;
57 import org.onap.policy.utils.BackUpMonitor;
58 import org.onap.policy.utils.BackUpMonitor.ResourceNode;
59 import org.onap.policy.utils.BackUpMonitorException;
60 import org.onap.policy.utils.PolicyUtils;
61
62 public class BackUpMonitorTest {
63     @Test(expected = PersistenceException.class)
64     public void backUpMonitorTestFail() throws Exception {
65         Properties properties = new Properties();
66         properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
67         properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk");
68         properties.setProperty("javax.persistence.jdbc.user", "policy_user");
69         properties.setProperty("javax.persistence.jdbc.password", "");
70         BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test", properties,
71                 new DummyBackUpHandler());
72     }
73
74     @Test
75     public void backUpMonitorTestFailNoUser() throws Exception {
76         Properties properties = new Properties();
77         properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
78         properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk");
79         properties.setProperty("javax.persistence.jdbc.user", "");
80         properties.setProperty("javax.persistence.jdbc.password", "password");
81         BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test",
82                 properties, new DummyBackUpHandler());
83         assertNull(bum);
84     }
85
86     @Test
87     public void backUpMonitorTestFailNoUrl() throws Exception {
88         Properties properties = new Properties();
89         properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
90         properties.setProperty("javax.persistence.jdbc.url", "");
91         properties.setProperty("javax.persistence.jdbc.user", "test");
92         properties.setProperty("javax.persistence.jdbc.password", "password");
93         properties.setProperty("ping_interval", "500");
94         BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test",
95                 properties, new DummyBackUpHandler());
96         assertNull(bum);
97     }
98
99     @Test
100     public void backUpMonitorTestFailNoDriver() throws Exception {
101         Properties properties = new Properties();
102         properties.setProperty("javax.persistence.jdbc.driver", "");
103         properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk");
104         properties.setProperty("javax.persistence.jdbc.user", "test");
105         properties.setProperty("javax.persistence.jdbc.password", "password");
106         properties.setProperty("ping_interval", "500");
107         BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test",
108                 properties, new DummyBackUpHandler());
109         assertNull(bum);
110     }
111
112     @Test
113     public void backUpMonitorTestFailNoNode() throws Exception {
114         Properties properties = new Properties();
115         properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
116         properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk");
117         properties.setProperty("javax.persistence.jdbc.user", "test");
118         properties.setProperty("javax.persistence.jdbc.password", "password");
119         properties.setProperty("ping_interval", "");
120         BackUpMonitor bum = BackUpMonitor.getInstance(null, "brms_test", properties, new DummyBackUpHandler());
121         assertNull(bum);
122     }
123
124     @Test
125     public void backUpMonitorTestFailNoResource() throws Exception {
126         Properties properties = new Properties();
127         properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
128         properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk");
129         properties.setProperty("javax.persistence.jdbc.user", "test");
130         properties.setProperty("javax.persistence.jdbc.password", "password");
131         BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), null, properties,
132                 new DummyBackUpHandler());
133         assertNull(bum);
134     }
135
136     @Test
137     public void backUpMonitorTestFailNoProperties() throws Exception {
138         BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test", null,
139                 new DummyBackUpHandler());
140         assertNull(bum);
141     }
142
143     @Test
144     public void backUpMonitorTestFailNoHandler() throws Exception {
145         Properties properties = new Properties();
146         properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
147         properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk");
148         properties.setProperty("javax.persistence.jdbc.user", "test");
149         properties.setProperty("javax.persistence.jdbc.password", "password");
150         properties.setProperty("ping_interval", "500");
151         BackUpMonitor bum =
152                 BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test", properties, null);
153         assertNull(bum);
154     }
155
156     @Test
157     public void backUpMonitorPingError() throws BackUpMonitorException {
158         Properties properties = new Properties();
159         properties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
160         properties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:backUpMonitorPingError");
161         properties.setProperty("javax.persistence.jdbc.user", "sa");
162         properties.setProperty("javax.persistence.jdbc.password", "");
163         properties.setProperty("ping_interval", "123a");
164         properties.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistencePUtest.xml");
165         BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test",
166                 properties, new DummyBackUpHandler());
167         assertTrue(bum.getFlag());
168     }
169
170     @Test
171     public void backUpMonitorMasterTest() throws BackUpMonitorException, InterruptedException, JsonProcessingException {
172         Properties properties = new Properties();
173         // Master Check. Initial Run.
174         properties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
175         properties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:BackupMonitorMasterTest");
176         properties.setProperty("javax.persistence.jdbc.user", "sa");
177         properties.setProperty("javax.persistence.jdbc.password", "");
178         properties.setProperty("ping_interval", "500");
179         properties.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistencePUtest.xml");
180         BackUpMonitor buMonitor = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test",
181                 properties, new DummyBackUpHandler());
182         createPolicyNotification();
183         assertTrue(buMonitor.getFlag());
184         // Start a slave check.
185         startSlave(properties);
186         updatePolicyNotification();
187         TimeUnit.MILLISECONDS.sleep(1500);
188         assertFalse(buMonitor.getFlag());
189         // Get Back to Master test
190         TimeUnit.MILLISECONDS.sleep(2000);
191         assertTrue(buMonitor.getFlag());
192         // No Master check.
193         changeAll(properties, "SLAVE");
194         TimeUnit.MILLISECONDS.sleep(2000);
195         assertTrue(buMonitor.getFlag());
196         // No Master check.
197         changeAll(properties, "MASTER");
198         TimeUnit.MILLISECONDS.sleep(2000);
199         assertTrue(buMonitor.getFlag());
200
201     }
202
203     private void updatePolicyNotification() {
204         StdPDPNotification notification = new StdPDPNotification();
205         notification.setNotificationType(NotificationType.BOTH);
206         StdLoadedPolicy loadedPolicy = new StdLoadedPolicy();
207         loadedPolicy.setPolicyName("com.testing");
208         loadedPolicy.setUpdateType(UpdateType.UPDATE);
209         loadedPolicy.setVersionNo("2");
210         Map<String, String> matches = new HashMap<>();
211         matches.put("test", "test");
212         loadedPolicy.setMatches(matches);
213         List<StdLoadedPolicy> loadedPolicies = new ArrayList<>();
214         loadedPolicies.add(loadedPolicy);
215         notification.setLoadedPolicies(loadedPolicies);
216         List<StdRemovedPolicy> removedPolicies = new ArrayList<>();
217         StdRemovedPolicy removedPolicy = new StdRemovedPolicy();
218         removedPolicy.setPolicyName("com.testing");
219         removedPolicy.setVersionNo("1");
220         notification.setRemovedPolicies(removedPolicies);
221         NotificationStore.recordNotification(notification);
222     }
223
224     private void createPolicyNotification() {
225         StdPDPNotification notification = new StdPDPNotification();
226         notification.setNotificationType(NotificationType.UPDATE);
227         StdLoadedPolicy loadedPolicy = new StdLoadedPolicy();
228         loadedPolicy.setPolicyName("com.testing");
229         loadedPolicy.setUpdateType(UpdateType.NEW);
230         loadedPolicy.setVersionNo("1");
231         Map<String, String> matches = new HashMap<>();
232         matches.put("test", "test");
233         loadedPolicy.setMatches(matches);
234         List<StdLoadedPolicy> loadedPolicies = new ArrayList<>();
235         loadedPolicies.add(loadedPolicy);
236         notification.setLoadedPolicies(loadedPolicies);
237         NotificationStore.recordNotification(notification);
238     }
239
240     private void changeAll(Properties properties, String flag) {
241         EntityManager em =
242                 Persistence.createEntityManagerFactory("PolicyEngineUtils", properties).createEntityManager();
243         EntityTransaction et = em.getTransaction();
244         et.begin();
245         Query query = em.createQuery("select b from BackUpMonitorEntity b where b.resourceNodeName = :nn");
246         query.setParameter("nn", ResourceNode.BRMS.toString());
247         for (Object bmValue : query.getResultList()) {
248             BackUpMonitorEntity bmEntity = (BackUpMonitorEntity) bmValue;
249             bmEntity.setFlag(flag);
250             bmEntity.setTimeStamp(new Date());
251         }
252         em.flush();
253         et.commit();
254     }
255
256     private void startSlave(Properties properties) throws JsonProcessingException {
257         EntityManager em =
258                 Persistence.createEntityManagerFactory("PolicyEngineUtils", properties).createEntityManager();
259         EntityTransaction et = em.getTransaction();
260         et.begin();
261         Query query = em.createQuery("select b from BackUpMonitorEntity b where b.resourceNodeName = :nn");
262         query.setParameter("nn", ResourceNode.BRMS.toString());
263         List<?> bmList = query.getResultList();
264         BackUpMonitorEntity origBm = (BackUpMonitorEntity) bmList.get(0);
265         origBm.setFlag("SLAVE");
266         origBm.setTimeStamp(new Date());
267         BackUpMonitorEntity bmEntity = new BackUpMonitorEntity();
268         bmEntity.setResourceNodeName(ResourceNode.BRMS.toString());
269         bmEntity.setResourceName("brms_test2");
270         bmEntity.setFlag("MASTER");
271         bmEntity.setTimeStamp(new Date());
272         StdPDPNotification notification = new StdPDPNotification();
273         notification.setNotificationType(NotificationType.UPDATE);
274         StdLoadedPolicy loadedPolicy = new StdLoadedPolicy();
275         loadedPolicy.setPolicyName("com.test");
276         loadedPolicy.setUpdateType(UpdateType.NEW);
277         loadedPolicy.setVersionNo("1");
278         Map<String, String> matches = new HashMap<>();
279         matches.put("test", "test");
280         loadedPolicy.setMatches(matches);
281         List<StdLoadedPolicy> loadedPolicies = new ArrayList<>();
282         loadedPolicies.add(loadedPolicy);
283         notification.setLoadedPolicies(loadedPolicies);
284         bmEntity.setNotificationRecord(PolicyUtils.objectToJsonString(notification));
285         em.persist(bmEntity);
286         em.persist(origBm);
287         em.flush();
288         et.commit();
289     }
290
291     @Test(expected = BackUpMonitorException.class)
292     public void testException() throws InterruptedException, BackUpMonitorException {
293         BackUpMonitor.stop();
294         new BackUpMonitorException();
295         new BackUpMonitorException(new Exception());
296         new BackUpMonitorException("error");
297         new BackUpMonitorException("error", new Exception());
298         throw new BackUpMonitorException("error", new Exception(), false, false);
299     }
300
301     @After
302     public void setup() throws InterruptedException {
303         BackUpMonitor.stop();
304     }
305
306     /**
307      * Cleanup.
308      *
309      * @throws IOException Signals that an I/O exception has occurred.
310      */
311     @AfterClass
312     public static void cleanup() throws IOException {
313         FileUtils.deleteQuietly(new File("src/test/resources/META-INF/generatedCreate.ddl"));
314         FileUtils.deleteQuietly(new File("src/test/resources/META-INF/generatedDrop.ddl"));
315     }
316 }