diff options
author | Tschaen, Brendan <ctschaen@att.com> | 2018-11-06 18:23:25 -0500 |
---|---|---|
committer | Tschaen, Brendan <ctschaen@att.com> | 2018-11-06 21:05:01 -0500 |
commit | f63139f3b6d16611cea21c7b3754b2cba833378e (patch) | |
tree | ec4d01a01bfed3d7428498250ae9bf8abff66aae /mdbc-server/src/test/java/org/onap/music | |
parent | 71b2712a0c9d968c36ef59bcfad1b0add8e82592 (diff) |
Self contain the unit tests
Change-Id: Ib160116a075a1634f9d9cb6bf2527a010fc0e573
Issue-ID: MUSIC-177
Signed-off-by: Tschaen, Brendan <ctschaen@att.com>
Diffstat (limited to 'mdbc-server/src/test/java/org/onap/music')
3 files changed, 48 insertions, 73 deletions
diff --git a/mdbc-server/src/test/java/org/onap/music/mdbc/DatabaseOperationsTest.java b/mdbc-server/src/test/java/org/onap/music/mdbc/DatabaseOperationsTest.java index b9a929c..e2395ee 100644 --- a/mdbc-server/src/test/java/org/onap/music/mdbc/DatabaseOperationsTest.java +++ b/mdbc-server/src/test/java/org/onap/music/mdbc/DatabaseOperationsTest.java @@ -23,22 +23,26 @@ import com.datastax.driver.core.*; import com.datastax.driver.core.exceptions.QueryExecutionException; import com.datastax.driver.core.exceptions.SyntaxError; import org.apache.commons.lang3.tuple.Pair; -import org.apache.zookeeper.ZooKeeper; +import org.cassandraunit.utils.EmbeddedCassandraServerHelper; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; +import org.junit.Ignore; import org.onap.music.datastore.CassaDataStore; +import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.exceptions.MDBCServiceException; import org.onap.music.exceptions.MusicLockingException; import org.onap.music.exceptions.MusicQueryException; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.logging.EELFLoggerDelegate; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.main.ReturnType; import org.onap.music.mdbc.tables.*; + import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; @@ -52,64 +56,60 @@ import java.util.concurrent.locks.ReentrantLock; import static org.junit.Assert.*; public class DatabaseOperationsTest { - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DatabaseOperationsTest.class); final private String keyspace="metricmusictest"; final private String mriTableName = "musicrangeinformation"; final private String mtdTableName = "musictxdigest"; - - // Lock and cojndition variable used to test connection to zookeeper - final private Lock lock = new ReentrantLock(); - final private Condition ready = lock.newCondition(); - //Flag used to detect connection failures before running any tests in metric - private boolean first=true; //Properties used to connect to music - private Properties prop= new Properties(); - private Cluster cluster; - private Session session; + private static Cluster cluster; + private static Session session; + private static String cassaHost = "localhost"; + + @BeforeClass + public static void init() throws MusicServiceException { + try { + EmbeddedCassandraServerHelper.startEmbeddedCassandra(); + } catch (Exception e) { + System.out.println(e); + } + + cluster = new Cluster.Builder().addContactPoint(cassaHost).withPort(9142).build(); + cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(20000); + session = cluster.connect(); + + assertNotNull("Invalid configuration for cassandra", cluster); + session = cluster.connect(); + assertNotNull("Invalid configuration for cassandra", session); +// TestUtils.populateMusicUtilsWithProperties(prop); + CassaDataStore store = new CassaDataStore(cluster, session); + assertNotNull("Invalid configuration for music", store); + MusicCore.mDstoreHandle = store; + + } + + @AfterClass + public static void close() throws MusicServiceException, MusicQueryException { + + //TODO: shutdown cassandra + + } + @Before public void setUp() throws Exception { // System.out.println("TEST 1: Getting ready for testing connection to Cassandra"); -// - if(first) { - //Read properties file to access cassandra and zookeeper - readPropertiesFile(); - //Test cassandra is correctly running - String cassaHost = prop.getProperty("cassandra.host",MusicUtil.getMyCassaHost()); - String cassaUser = prop.getProperty("cassandra.user",MusicUtil.getCassName()); - String cassaPwd = prop.getProperty("cassandra.password",MusicUtil.getCassPwd()); - cluster = Cluster.builder().addContactPoints(cassaHost) - .withCredentials(cassaUser,cassaPwd).build(); - assertNotNull("Invalid configuration for cassandra", cluster); - session = cluster.connect(); - assertNotNull("Invalid configuration for cassandra", session); - TestUtils.populateMusicUtilsWithProperties(prop); - //Test zookeeper is correctly running - String zookeeperHost = MusicUtil.getMyZkHost(); - assertTrue(!zookeeperHost.isEmpty()); - ZooKeeper zk = new ZooKeeper(zookeeperHost+":2181",3000, - we -> { - lock.lock(); - ready.signalAll(); - lock.unlock(); - }); - lock.lock(); - ready.await(10, TimeUnit.SECONDS); - assertEquals(zk.getState(), ZooKeeper.States.CONNECTED); - assertNotNull("Invalid configuration for zookeper", zk); - long sessionId = zk.getSessionId(); - assertNotEquals(sessionId,0); - zk.close(); - CassaDataStore store = MusicCore.getDSHandle(); - assertNotNull("Invalid configuration for music", store); - first = false; - } //Create keyspace + + createKeyspace(); useKeyspace(); } + @After + public void tearDown() { + deleteKeyspace(); + } + private void createKeyspace() { String queryOp = "CREATE KEYSPACE " + keyspace + @@ -143,26 +143,6 @@ public class DatabaseOperationsTest { assertTrue("Keyspace "+keyspace+" doesn't exist and it should",res.wasApplied()); } - private void readPropertiesFile() { - try { - String fileLocation = MusicUtil.getMusicPropertiesFilePath(); - InputStream fstream = new FileInputStream(fileLocation); - prop.load(fstream); - fstream.close(); - } catch (FileNotFoundException e) { - logger.error("Configuration file not found"); - - } catch (IOException e) { - // TODO Auto-generated catch block - logger.error("Exception when reading file: "+e.toString()); - } - } - - @After - public void tearDown() { - deleteKeyspace(); - } - private void CreateMTD(){ try { DatabaseOperations.createMusicTxDigest(keyspace, mtdTableName); diff --git a/mdbc-server/src/test/java/org/onap/music/mdbc/MDBCUtilsTest.java b/mdbc-server/src/test/java/org/onap/music/mdbc/MDBCUtilsTest.java index 2c26aed..8bdce12 100644 --- a/mdbc-server/src/test/java/org/onap/music/mdbc/MDBCUtilsTest.java +++ b/mdbc-server/src/test/java/org/onap/music/mdbc/MDBCUtilsTest.java @@ -88,4 +88,5 @@ public class MDBCUtilsTest { assertTrue(output!=null); assertTrue(!output.isEmpty()); } + } diff --git a/mdbc-server/src/test/java/org/onap/music/mdbc/TestUtils.java b/mdbc-server/src/test/java/org/onap/music/mdbc/TestUtils.java index 111b65c..3f8bd65 100644 --- a/mdbc-server/src/test/java/org/onap/music/mdbc/TestUtils.java +++ b/mdbc-server/src/test/java/org/onap/music/mdbc/TestUtils.java @@ -19,7 +19,6 @@ */ package org.onap.music.mdbc; -import org.onap.music.logging.EELFLoggerDelegate; import org.onap.music.main.MusicUtil; import java.util.ArrayList; @@ -27,7 +26,6 @@ import java.util.Arrays; import java.util.Properties; public class TestUtils { - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(TestUtils.class); public static void populateMusicUtilsWithProperties(Properties prop){ //TODO: Learn how to do this properly within music @@ -36,9 +34,6 @@ public class TestUtils { String key = propKeys[k]; if (prop.containsKey(key) && prop.get(key) != null) { switch (key) { - case "zookeeper.host": - MusicUtil.setMyZkHost(prop.getProperty(key)); - break; case "cassandra.host": MusicUtil.setMyCassaHost(prop.getProperty(key)); break; @@ -91,8 +86,7 @@ public class TestUtils { MusicUtil.setAafEndpointUrl(prop.getProperty(key)); break; default: - logger.error(EELFLoggerDelegate.errorLogger, - "No case found for " + key); + System.out.println("No case found for " + key); } } } |