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