From f63139f3b6d16611cea21c7b3754b2cba833378e Mon Sep 17 00:00:00 2001 From: "Tschaen, Brendan" Date: Tue, 6 Nov 2018 18:23:25 -0500 Subject: Self contain the unit tests Change-Id: Ib160116a075a1634f9d9cb6bf2527a010fc0e573 Issue-ID: MUSIC-177 Signed-off-by: Tschaen, Brendan --- .../onap/music/mdbc/DatabaseOperationsTest.java | 112 ++++------ .../java/org/onap/music/mdbc/MDBCUtilsTest.java | 1 + .../test/java/org/onap/music/mdbc/TestUtils.java | 8 +- .../sdnc/sli/resource/dblib/CachedDataSource.java | 26 --- .../sdnc/sli/resource/dblib/DBResourceManager.java | 106 --------- .../sdnc/sli/resource/dblib/StressTest.java | 244 --------------------- 6 files changed, 48 insertions(+), 449 deletions(-) delete mode 100755 mdbc-server/src/test/java/org/openecomp/sdnc/sli/resource/dblib/CachedDataSource.java delete mode 100755 mdbc-server/src/test/java/org/openecomp/sdnc/sli/resource/dblib/DBResourceManager.java delete mode 100755 mdbc-server/src/test/java/org/openecomp/sdnc/sli/resource/dblib/StressTest.java (limited to 'mdbc-server/src/test') 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); } } } diff --git a/mdbc-server/src/test/java/org/openecomp/sdnc/sli/resource/dblib/CachedDataSource.java b/mdbc-server/src/test/java/org/openecomp/sdnc/sli/resource/dblib/CachedDataSource.java deleted file mode 100755 index ecac610..0000000 --- a/mdbc-server/src/test/java/org/openecomp/sdnc/sli/resource/dblib/CachedDataSource.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * ============LICENSE_START==================================================== - * org.onap.music.mdbc - * ============================================================================= - * Copyright (C) 2018 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. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END====================================================== - */ -package org.openecomp.sdnc.sli.resource.dblib; - -public class CachedDataSource { - public String getDbConnectionName() { - return "name"; - } -} diff --git a/mdbc-server/src/test/java/org/openecomp/sdnc/sli/resource/dblib/DBResourceManager.java b/mdbc-server/src/test/java/org/openecomp/sdnc/sli/resource/dblib/DBResourceManager.java deleted file mode 100755 index 43d4cdf..0000000 --- a/mdbc-server/src/test/java/org/openecomp/sdnc/sli/resource/dblib/DBResourceManager.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * ============LICENSE_START==================================================== - * org.onap.music.mdbc - * ============================================================================= - * Copyright (C) 2018 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. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END====================================================== - */ -package org.openecomp.sdnc.sli.resource.dblib; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.List; -import java.util.Properties; -import java.util.Queue; -import java.util.concurrent.LinkedBlockingQueue; - -import javax.sql.rowset.CachedRowSet; -import javax.sql.rowset.RowSetProvider; - -public class DBResourceManager { - public static final String DB_CONNECTION = "jdbc:mdbc:file:/tmp/stresstest"; // "jdbc:h2:mem:db1"; - @SuppressWarnings("unused") - private Properties p; - private Queue conns; - - private DBResourceManager(Properties p) { - this.p = p; - this.conns = new LinkedBlockingQueue(); - } - public static DBResourceManager create(Properties props) throws Exception { - DBResourceManager dbmanager = new DBResourceManager(props); - return dbmanager; - } - public Connection getConnection() throws SQLException { - if (conns.size() > 0) { - return conns.remove(); - } else { - Properties driver_info = new Properties(); - return DriverManager.getConnection(DB_CONNECTION, driver_info); - } - } - public void cleanUp() { - try { - while (conns.size() > 0) { - Connection conn = conns.remove(); - conn.close(); - } - } catch (SQLException e) { - } - } - public boolean isActive() { - return true; - } - public boolean writeData(String statement, List arguments, String preferredDS) throws SQLException { - Connection conn = getConnection(); - PreparedStatement ps = conn.prepareStatement(statement); - for (int i = 1; i <= arguments.size(); i++) { - ps.setObject(i, arguments.get(i-1)); - } - ps.executeUpdate(); - ps.close(); - conns.add(conn); - return true; - } - public CachedRowSet getData(String statement, List arguments, String preferredDS) throws SQLException { - CachedRowSet data = null; - ResultSet rs = null; - Connection conn = null; - try { - data = RowSetProvider.newFactory().createCachedRowSet(); - conn = getConnection(); - PreparedStatement ps = conn.prepareStatement(statement); - if(arguments != null) { - for (int i = 0; i < arguments.size(); i++) { - ps.setObject(i+1, arguments.get(i)); - } - } - rs = ps.executeQuery(); - data.populate(rs); - } catch (Throwable exc) { - throw (SQLException)exc; - } finally { - if (conn != null) - conns.add(conn); - } - return data; - } - CachedDataSource findMaster() throws Exception { - return new CachedDataSource(); - } -} diff --git a/mdbc-server/src/test/java/org/openecomp/sdnc/sli/resource/dblib/StressTest.java b/mdbc-server/src/test/java/org/openecomp/sdnc/sli/resource/dblib/StressTest.java deleted file mode 100755 index d6b5717..0000000 --- a/mdbc-server/src/test/java/org/openecomp/sdnc/sli/resource/dblib/StressTest.java +++ /dev/null @@ -1,244 +0,0 @@ -/* - * ============LICENSE_START==================================================== - * org.onap.music.mdbc - * ============================================================================= - * Copyright (C) 2018 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. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END====================================================== - */ -package org.openecomp.sdnc.sli.resource.dblib; - -import static org.junit.Assert.*; - -import java.net.URL; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Properties; -import java.util.Set; -import java.util.concurrent.atomic.AtomicInteger; - -import javax.sql.rowset.CachedRowSet; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.anarsoft.vmlens.concurrent.junit.ConcurrentTestRunner; -import com.anarsoft.vmlens.concurrent.junit.ThreadCount; - -//@FixMethodOrder(MethodSorters.NAME_ASCENDING) -@RunWith(ConcurrentTestRunner.class) -public class StressTest { - -// static { -// System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "INFO"); -// System.setProperty(org.slf4j.impl.SimpleLogger.LOG_FILE_KEY, String.format("ComparativeAnalysisTest-%d.log", System.currentTimeMillis())); -// } - private static final Logger LOG = LoggerFactory.getLogger(StressTest.class); - private static Properties props; - private static DBResourceManager jdbcDataSource = null; - @SuppressWarnings("unused") - private static final int MAX_TREADS = 1; - @SuppressWarnings("unused") - private static final int MAX_ITERATIONS = 10; - - private final AtomicInteger count= new AtomicInteger(); - - Set runningThreads = new HashSet(); - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - props = new Properties(); - URL url = StressTest.class.getResource("/dblib.properties"); - assertNotNull(url); - LOG.info("Property file is: " + url.toString()); - props.load(url.openStream()); - - try { - jdbcDataSource = DBResourceManager.create(props); - Connection conn = jdbcDataSource.getConnection(); - - // --------------- - // CREATE TABLE - String sql = - "CREATE TABLE IF NOT EXISTS `AIC_SITE` (" + - "`name` varchar(100) DEFAULT NULL, "+ - "`aic_site_id` varchar(100) NOT NULL, "+ - "`vcenter_url` varchar(200) DEFAULT NULL, "+ - "`vcenter_username` varchar(40) DEFAULT NULL, "+ - "`vcenter_passwd` varchar(255) DEFAULT NULL, "+ - "`city` varchar(100) DEFAULT NULL, "+ - "`state` varchar(2) DEFAULT NULL, "+ - "`operational_status` varchar(20) DEFAULT NULL, "+ - "`oam_gateway_addr` varchar(20) DEFAULT '', "+ - "PRIMARY KEY (`aic_site_id`) "+ - ") ; "; - Statement stmt = conn.createStatement(); - stmt.execute(sql); - // --------------- - - conn.close(); - } catch (Throwable exc) { - LOG.error("", exc); - } - assertNotNull(jdbcDataSource); - if (((DBResourceManager)jdbcDataSource).isActive()){ - LOG.warn( "DBLIB: JDBC DataSource has been initialized."); - } else { - LOG.warn( "DBLIB: JDBC DataSource did not initialize successfully."); - } - } - - @AfterClass - public static void tearDownAfterClass() throws Exception { - jdbcDataSource.cleanUp(); - } - - @Before - public void setUp() throws Exception { - - } - - @After - public void tearDown() throws Exception { - - } - -// @Test - public void test01() { - LOG.info("TEST 1: Verify primary db selection"); - checkPrimaryDatabase(); - } - - - @Test - @ThreadCount(10) - public void test0X() { - int id = count.incrementAndGet(); - - String siteid = String.format("Councurrent-tester-%02d", id); - for(int i=0; i<40; i++){ - String site = String.format("%s_%04d", siteid, i); - insertTestData(site); - queryTestData(site); - removeTestData(site); - try { - Thread.sleep(0); - } catch (Exception e) { - LOG.warn("", e); - } - } - } - - private void removeTestData(String site) { - ArrayList delete = new ArrayList(); - delete.add(site); - try { - long startTime = System.currentTimeMillis(); - boolean success = jdbcDataSource.writeData("delete from AIC_SITE where aic_site_id=?", delete, null); - logRequest(site, "DELETE", startTime, System.currentTimeMillis() - startTime); - assertTrue(success); - } catch (SQLException e) { - LOG.warn("", e); - - } - } - - private boolean queryTestData(String site) { - ArrayList identifier = new ArrayList(); - identifier.add(site); - try { - int rowcount = 0; - long startTime = System.currentTimeMillis(); - CachedRowSet data = jdbcDataSource.getData("select * from AIC_SITE where aic_site_id=?", identifier, null); - logRequest(site, "QUERY", startTime, System.currentTimeMillis() - startTime); - while(data.next()) { - rowcount ++; - } - return rowcount!=0; -// assertTrue(success); - } catch (SQLException e) { - LOG.warn("", e); - return false; - } - } - - - private void insertTestData(String site) { - ArrayList data = new ArrayList(); - data.add(site); - data.add(site); - data.add("Sample03"); - data.add("Sample04"); - data.add("Sample05"); - - boolean success; - try { - long startTime = System.currentTimeMillis(); - success = jdbcDataSource.writeData("insert into AIC_SITE (name, aic_site_id, vcenter_url, vcenter_username, vcenter_passwd) values (?,?,?,?,?)", data, null); - logRequest(site, "INSERT", startTime, System.currentTimeMillis() - startTime); - assertTrue(success); - } catch (SQLException e) { - LOG.warn("", e); - } - } - - private void checkPrimaryDatabase() { - Connection conn = null; - PreparedStatement statement = null; - ResultSet rs = null; - - try { - conn = jdbcDataSource.getConnection(); - statement = conn.prepareStatement("SELECT 1 FROM DUAL"); - rs = statement.executeQuery(); - int value = -1; - while(rs.next()) { - value = rs.getInt(1); - } - LOG.info("Value returned is: " + value); - conn.close(); - } catch (SQLException e) { - LOG.warn("transaction failed", e); - } finally { - try { - if(rs != null) { rs.close(); } - if(conn != null){ conn.close(); } - if(conn != null){ conn.close(); } - } catch (SQLException e) { - LOG.warn("transaction failed", e); - } - } - CachedDataSource ds = null; - try { - ds = jdbcDataSource.findMaster(); - } catch (Throwable e) { - LOG.warn("", e); - } - LOG.info("Primary DS is " + ds.getDbConnectionName()); - } - private static void logRequest(String site, String command, long timestamp, long duration) { - LOG.info(String.format("%s|%s|%d|%d", site, command, timestamp, duration)); - } -} -- cgit 1.2.3-korg