diff options
Diffstat (limited to 'PolicyEngineUtils/src')
-rw-r--r-- | PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/BackUpMonitorTest.java | 324 |
1 files changed, 173 insertions, 151 deletions
diff --git a/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/BackUpMonitorTest.java b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/BackUpMonitorTest.java index 83362fd0d..5182c5329 100644 --- a/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/BackUpMonitorTest.java +++ b/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/BackUpMonitorTest.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * PolicyEngineUtils * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017,2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -24,6 +24,9 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import com.fasterxml.jackson.core.JsonProcessingException; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -31,15 +34,15 @@ import java.util.List; import java.util.Map; import java.util.Properties; import java.util.concurrent.TimeUnit; - import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import javax.persistence.PersistenceException; import javax.persistence.Query; - +import org.apache.commons.io.FileUtils; import org.eclipse.persistence.config.PersistenceUnitProperties; import org.junit.After; +import org.junit.AfterClass; import org.junit.Test; import org.onap.policy.api.NotificationType; import org.onap.policy.api.UpdateType; @@ -53,146 +56,151 @@ import org.onap.policy.utils.BackUpMonitor.ResourceNode; import org.onap.policy.utils.BackUpMonitorException; import org.onap.policy.utils.PolicyUtils; -import com.fasterxml.jackson.core.JsonProcessingException; - public class BackUpMonitorTest { + @Test(expected = PersistenceException.class) + public void backUpMonitorTestFail() throws Exception { + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); + properties.setProperty("javax.persistence.jdbc.user", "policy_user"); + properties.setProperty("javax.persistence.jdbc.password", ""); + BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test", properties, + new DummyBackUpHandler()); + } - @Test (expected = PersistenceException.class) - public void backUpMonitorTestFail() throws Exception{ - Properties properties = new Properties(); - properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); - properties.setProperty("javax.persistence.jdbc.user", "policy_user"); - properties.setProperty("javax.persistence.jdbc.password", ""); - BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new DummyBackUpHandler()); - } - - @Test - public void backUpMonitorTestFailNoUser() throws Exception{ - Properties properties = new Properties(); - properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); - properties.setProperty("javax.persistence.jdbc.user", ""); - properties.setProperty("javax.persistence.jdbc.password", "password"); - BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new DummyBackUpHandler()); - assertNull(bum); - } - - @Test - public void backUpMonitorTestFailNoURL() throws Exception{ - Properties properties = new Properties(); - properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", ""); - properties.setProperty("javax.persistence.jdbc.user", "test"); - properties.setProperty("javax.persistence.jdbc.password", "password"); - properties.setProperty("ping_interval", "500"); - BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new DummyBackUpHandler()); - assertNull(bum); - } - - @Test - public void backUpMonitorTestFailNoDriver() throws Exception{ - Properties properties = new Properties(); - properties.setProperty("javax.persistence.jdbc.driver", ""); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); - properties.setProperty("javax.persistence.jdbc.user", "test"); - properties.setProperty("javax.persistence.jdbc.password", "password"); - properties.setProperty("ping_interval", "500"); - BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new DummyBackUpHandler()); - assertNull(bum); - } - - @Test - public void backUpMonitorTestFailNoNode() throws Exception{ - Properties properties = new Properties(); - properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); - properties.setProperty("javax.persistence.jdbc.user", "test"); - properties.setProperty("javax.persistence.jdbc.password", "password"); - properties.setProperty("ping_interval", ""); - BackUpMonitor bum = BackUpMonitor.getInstance(null, "brms_test" , properties, new DummyBackUpHandler()); - assertNull(bum); - } - - @Test - public void backUpMonitorTestFailNoResource() throws Exception{ - Properties properties = new Properties(); - properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); - properties.setProperty("javax.persistence.jdbc.user", "test"); - properties.setProperty("javax.persistence.jdbc.password", "password"); - BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), null , properties, new DummyBackUpHandler()); - assertNull(bum); - } - - @Test - public void backUpMonitorTestFailNoProperties() throws Exception{ - BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , null, new DummyBackUpHandler()); - assertNull(bum); - } - - @Test - public void backUpMonitorTestFailNoHandler() throws Exception{ - Properties properties = new Properties(); - properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); - properties.setProperty("javax.persistence.jdbc.user", "test"); - properties.setProperty("javax.persistence.jdbc.password", "password"); - properties.setProperty("ping_interval", "500"); - BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, null); - assertNull(bum); - } - - @Test - public void backUpMonitorPingError() throws BackUpMonitorException { - Properties properties = new Properties(); - properties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:file:./sql/xacmlTest"); + @Test + public void backUpMonitorTestFailNoUser() throws Exception { + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); + properties.setProperty("javax.persistence.jdbc.user", ""); + properties.setProperty("javax.persistence.jdbc.password", "password"); + BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test", + properties, new DummyBackUpHandler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoUrl() throws Exception { + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", ""); + properties.setProperty("javax.persistence.jdbc.user", "test"); + properties.setProperty("javax.persistence.jdbc.password", "password"); + properties.setProperty("ping_interval", "500"); + BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test", + properties, new DummyBackUpHandler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoDriver() throws Exception { + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", ""); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); + properties.setProperty("javax.persistence.jdbc.user", "test"); + properties.setProperty("javax.persistence.jdbc.password", "password"); + properties.setProperty("ping_interval", "500"); + BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test", + properties, new DummyBackUpHandler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoNode() throws Exception { + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); + properties.setProperty("javax.persistence.jdbc.user", "test"); + properties.setProperty("javax.persistence.jdbc.password", "password"); + properties.setProperty("ping_interval", ""); + BackUpMonitor bum = BackUpMonitor.getInstance(null, "brms_test", properties, new DummyBackUpHandler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoResource() throws Exception { + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); + properties.setProperty("javax.persistence.jdbc.user", "test"); + properties.setProperty("javax.persistence.jdbc.password", "password"); + BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), null, properties, + new DummyBackUpHandler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoProperties() throws Exception { + BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test", null, + new DummyBackUpHandler()); + assertNull(bum); + } + + @Test + public void backUpMonitorTestFailNoHandler() throws Exception { + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/onap_sdk"); + properties.setProperty("javax.persistence.jdbc.user", "test"); + properties.setProperty("javax.persistence.jdbc.password", "password"); + properties.setProperty("ping_interval", "500"); + BackUpMonitor bum = + BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test", properties, null); + assertNull(bum); + } + + @Test + public void backUpMonitorPingError() throws BackUpMonitorException { + Properties properties = new Properties(); + properties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:backUpMonitorPingError"); properties.setProperty("javax.persistence.jdbc.user", "sa"); properties.setProperty("javax.persistence.jdbc.password", ""); properties.setProperty("ping_interval", "123a"); properties.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistencePUtest.xml"); - BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new DummyBackUpHandler()); + BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test", + properties, new DummyBackUpHandler()); assertTrue(bum.getFlag()); - } - - @Test - public void backUpMonitorMasterTest() throws BackUpMonitorException, InterruptedException, JsonProcessingException { - Properties properties = new Properties(); - // Master Check. Initial Run. - properties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver"); - properties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:file:./sql/xacmlTest"); + } + + @Test + public void backUpMonitorMasterTest() throws BackUpMonitorException, InterruptedException, JsonProcessingException { + Properties properties = new Properties(); + // Master Check. Initial Run. + properties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver"); + properties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:BackupMonitorMasterTest"); properties.setProperty("javax.persistence.jdbc.user", "sa"); properties.setProperty("javax.persistence.jdbc.password", ""); properties.setProperty("ping_interval", "500"); properties.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistencePUtest.xml"); - BackUpMonitor bum = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test" , properties, new DummyBackUpHandler()); + BackUpMonitor buMonitor = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), "brms_test", + properties, new DummyBackUpHandler()); createPolicyNotification(); - assertTrue(bum.getFlag()); + assertTrue(buMonitor.getFlag()); // Start a slave check. startSlave(properties); updatePolicyNotification(); TimeUnit.MILLISECONDS.sleep(1500); - assertFalse(bum.getFlag()); + assertFalse(buMonitor.getFlag()); // Get Back to Master test TimeUnit.MILLISECONDS.sleep(2000); - assertTrue(bum.getFlag()); - // No Master check. - changeALL(properties, "SLAVE"); + assertTrue(buMonitor.getFlag()); + // No Master check. + changeAll(properties, "SLAVE"); TimeUnit.MILLISECONDS.sleep(2000); - assertTrue(bum.getFlag()); - // No Master check. - changeALL(properties, "MASTER"); + assertTrue(buMonitor.getFlag()); + // No Master check. + changeAll(properties, "MASTER"); TimeUnit.MILLISECONDS.sleep(2000); - assertTrue(bum.getFlag()); - - } - - private void updatePolicyNotification() { - StdPDPNotification notification = new StdPDPNotification(); + assertTrue(buMonitor.getFlag()); + + } + + private void updatePolicyNotification() { + StdPDPNotification notification = new StdPDPNotification(); notification.setNotificationType(NotificationType.BOTH); - List<StdLoadedPolicy> loadedPolicies = new ArrayList<>(); StdLoadedPolicy loadedPolicy = new StdLoadedPolicy(); loadedPolicy.setPolicyName("com.testing"); loadedPolicy.setUpdateType(UpdateType.UPDATE); @@ -200,6 +208,7 @@ public class BackUpMonitorTest { Map<String, String> matches = new HashMap<>(); matches.put("test", "test"); loadedPolicy.setMatches(matches); + List<StdLoadedPolicy> loadedPolicies = new ArrayList<>(); loadedPolicies.add(loadedPolicy); notification.setLoadedPolicies(loadedPolicies); List<StdRemovedPolicy> removedPolicies = new ArrayList<>(); @@ -213,7 +222,6 @@ public class BackUpMonitorTest { private void createPolicyNotification() { StdPDPNotification notification = new StdPDPNotification(); notification.setNotificationType(NotificationType.UPDATE); - List<StdLoadedPolicy> loadedPolicies = new ArrayList<>(); StdLoadedPolicy loadedPolicy = new StdLoadedPolicy(); loadedPolicy.setPolicyName("com.testing"); loadedPolicy.setUpdateType(UpdateType.NEW); @@ -221,19 +229,21 @@ public class BackUpMonitorTest { Map<String, String> matches = new HashMap<>(); matches.put("test", "test"); loadedPolicy.setMatches(matches); + List<StdLoadedPolicy> loadedPolicies = new ArrayList<>(); loadedPolicies.add(loadedPolicy); notification.setLoadedPolicies(loadedPolicies); NotificationStore.recordNotification(notification); } - private void changeALL(Properties properties, String flag) { - EntityManager em = Persistence.createEntityManagerFactory("PolicyEngineUtils", properties).createEntityManager(); + private void changeAll(Properties properties, String flag) { + EntityManager em = + Persistence.createEntityManagerFactory("PolicyEngineUtils", properties).createEntityManager(); EntityTransaction et = em.getTransaction(); et.begin(); Query query = em.createQuery("select b from BackUpMonitorEntity b where b.resourceNodeName = :nn"); query.setParameter("nn", ResourceNode.BRMS.toString()); - for(Object bMValue: query.getResultList()){ - BackUpMonitorEntity bmEntity = (BackUpMonitorEntity) bMValue; + for (Object bmValue : query.getResultList()) { + BackUpMonitorEntity bmEntity = (BackUpMonitorEntity) bmValue; bmEntity.setFlag(flag); bmEntity.setTimeStamp(new Date()); } @@ -242,23 +252,23 @@ public class BackUpMonitorTest { } private void startSlave(Properties properties) throws JsonProcessingException { - EntityManager em = Persistence.createEntityManagerFactory("PolicyEngineUtils", properties).createEntityManager(); - EntityTransaction et = em.getTransaction(); - et.begin(); - Query query = em.createQuery("select b from BackUpMonitorEntity b where b.resourceNodeName = :nn"); - query.setParameter("nn", ResourceNode.BRMS.toString()); - List<?> bMList = query.getResultList(); - BackUpMonitorEntity origBM = (BackUpMonitorEntity) bMList.get(0); - origBM.setFlag("SLAVE"); - origBM.setTimeStamp(new Date()); - BackUpMonitorEntity bMEntity = new BackUpMonitorEntity(); - bMEntity.setResourceNodeName(ResourceNode.BRMS.toString()); - bMEntity.setResourceName("brms_test2"); - bMEntity.setFlag("MASTER"); - bMEntity.setTimeStamp(new Date()); + EntityManager em = + Persistence.createEntityManagerFactory("PolicyEngineUtils", properties).createEntityManager(); + EntityTransaction et = em.getTransaction(); + et.begin(); + Query query = em.createQuery("select b from BackUpMonitorEntity b where b.resourceNodeName = :nn"); + query.setParameter("nn", ResourceNode.BRMS.toString()); + List<?> bmList = query.getResultList(); + BackUpMonitorEntity origBm = (BackUpMonitorEntity) bmList.get(0); + origBm.setFlag("SLAVE"); + origBm.setTimeStamp(new Date()); + BackUpMonitorEntity bmEntity = new BackUpMonitorEntity(); + bmEntity.setResourceNodeName(ResourceNode.BRMS.toString()); + bmEntity.setResourceName("brms_test2"); + bmEntity.setFlag("MASTER"); + bmEntity.setTimeStamp(new Date()); StdPDPNotification notification = new StdPDPNotification(); notification.setNotificationType(NotificationType.UPDATE); - List<StdLoadedPolicy> loadedPolicies = new ArrayList<>(); StdLoadedPolicy loadedPolicy = new StdLoadedPolicy(); loadedPolicy.setPolicyName("com.test"); loadedPolicy.setUpdateType(UpdateType.NEW); @@ -266,17 +276,18 @@ public class BackUpMonitorTest { Map<String, String> matches = new HashMap<>(); matches.put("test", "test"); loadedPolicy.setMatches(matches); + List<StdLoadedPolicy> loadedPolicies = new ArrayList<>(); loadedPolicies.add(loadedPolicy); notification.setLoadedPolicies(loadedPolicies); - bMEntity.setNotificationRecord(PolicyUtils.objectToJsonString(notification)); - em.persist(bMEntity); - em.persist(origBM); + bmEntity.setNotificationRecord(PolicyUtils.objectToJsonString(notification)); + em.persist(bmEntity); + em.persist(origBm); em.flush(); et.commit(); } - + @Test(expected = BackUpMonitorException.class) - public void testException() throws InterruptedException, BackUpMonitorException{ + public void testException() throws InterruptedException, BackUpMonitorException { BackUpMonitor.stop(); new BackUpMonitorException(); new BackUpMonitorException(new Exception()); @@ -286,7 +297,18 @@ public class BackUpMonitorTest { } @After - public void setup() throws InterruptedException{ - BackUpMonitor.stop(); - } + public void setup() throws InterruptedException { + BackUpMonitor.stop(); + } + + /** + * Cleanup. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @AfterClass + public static void cleanup() throws IOException { + FileUtils.deleteQuietly(new File("src/test/resources/META-INF/generatedCreate.ddl")); + FileUtils.deleteQuietly(new File("src/test/resources/META-INF/generatedDrop.ddl")); + } } |