2 * ============LICENSE_START=======================================================
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
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 com.fasterxml.jackson.core.JsonProcessingException;
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;
36 import java.util.Properties;
37 import java.util.concurrent.TimeUnit;
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;
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;
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());
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());
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());
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());
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());
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());
137 public void backUpMonitorTestFailNoProperties() throws Exception {
138 BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test", null,
139 new DummyBackUpHandler());
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");
152 BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test", properties, null);
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());
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());
193 changeAll(properties, "SLAVE");
194 TimeUnit.MILLISECONDS.sleep(2000);
195 assertTrue(buMonitor.getFlag());
197 changeAll(properties, "MASTER");
198 TimeUnit.MILLISECONDS.sleep(2000);
199 assertTrue(buMonitor.getFlag());
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);
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);
240 private void changeAll(Properties properties, String flag) {
242 Persistence.createEntityManagerFactory("PolicyEngineUtils", properties).createEntityManager();
243 EntityTransaction et = em.getTransaction();
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());
256 private void startSlave(Properties properties) throws JsonProcessingException {
258 Persistence.createEntityManagerFactory("PolicyEngineUtils", properties).createEntityManager();
259 EntityTransaction et = em.getTransaction();
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);
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);
302 public void setup() throws InterruptedException {
303 BackUpMonitor.stop();
309 * @throws IOException Signals that an I/O exception has occurred.
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"));