diff options
author | Brendan Tschaen <ctschaen@att.com> | 2019-05-15 15:08:05 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-05-15 15:08:05 +0000 |
commit | 96933528fdd51e606e48d79941ce43249ffb48b6 (patch) | |
tree | efec05eb72232c030e442eb12afa2260d6b6bd67 /mdbc-server/src/test | |
parent | 8a3db86e697de59a29e712d23e771c8718f9778a (diff) | |
parent | 52159a510dc31ee1dacf612a1e05cc8612a117e5 (diff) |
Merge "Implement postgres, fixes to eventual, and many bug fixes"
Diffstat (limited to 'mdbc-server/src/test')
8 files changed, 711 insertions, 446 deletions
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 4703d0e..87f8445 100755 --- a/mdbc-server/src/test/java/org/onap/music/mdbc/MDBCUtilsTest.java +++ b/mdbc-server/src/test/java/org/onap/music/mdbc/MDBCUtilsTest.java @@ -36,7 +36,7 @@ public class MDBCUtilsTest { public void toStringTest1() { StagingTable table = new StagingTable(); try { - table.addOperation(new Range("TABLE1"),SQLOperation.INSERT,(new JSONObject(new String[]{"test3", "Test4"})).toString(),null); + table.addOperation(new Range("TEST.TABLE1"),SQLOperation.INSERT,(new JSONObject(new String[]{"test3", "Test4"})).toString(),null); } catch (MDBCServiceException e) { fail(); } diff --git a/mdbc-server/src/test/java/org/onap/music/mdbc/MdbcTestUtils.java b/mdbc-server/src/test/java/org/onap/music/mdbc/MdbcTestUtils.java new file mode 100644 index 0000000..72ec8d3 --- /dev/null +++ b/mdbc-server/src/test/java/org/onap/music/mdbc/MdbcTestUtils.java @@ -0,0 +1,280 @@ +/* + * ============LICENSE_START==================================================== + * org.onap.music.mdbc + * ============================================================================= + * Copyright (C) 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END====================================================== + */ + +package org.onap.music.mdbc; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import ch.vorburger.exec.ManagedProcessException; +import ch.vorburger.mariadb4j.DB; +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; +import com.opentable.db.postgres.embedded.EmbeddedPostgres; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; +import javax.sql.DataSource; +import org.cassandraunit.utils.EmbeddedCassandraServerHelper; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; +import org.onap.music.datastore.MusicDataStore; +import org.onap.music.datastore.MusicDataStoreHandle; +import org.onap.music.exceptions.MDBCServiceException; +import org.onap.music.lockingservice.cassandra.CassaLockStore; +import org.onap.music.mdbc.mixins.MusicMixin; +import org.onap.music.mdbc.mixins.PostgresMixin; + +public class MdbcTestUtils { + + // Postgres variables + static EmbeddedPostgres pg=null; + static DataSource postgresDatabase=null; + final private static int postgresPort = 13307; + @Rule + public static TemporaryFolder tf = new TemporaryFolder(); + + // Cassandra variables + //Properties used to connect to music + private static Cluster cluster; + private static Session session; + + //Mdbc variables + final private static String keyspace="metricmusictest"; + final private static String mdbcServerName = "name"; + final private static String mtdTableName = "musictxdigest"; + final private static String eventualMtxdTableName = "musicevetxdigest"; + final private static String mriTableName = "musicrangeinformation"; + final private static String rangeDependencyTableName = "musicrangedependency"; + final private static String nodeInfoTableName = "nodeinfo"; + //Mariadb variables + static DB db=null; + final public static String mariaDBDatabaseName="test"; + final static Integer mariaDbPort=13306; + + + + public enum DBType {POSTGRES, MySQL} + + public static String getCassandraUrl(){ + return cluster.getMetadata().getAllHosts().iterator().next().getAddress().toString(); + + } + + public static String getKeyspace(){ + return keyspace; + } + + public static String getServerName(){ + return mdbcServerName; + } + + public static String getMriTableName(){ + return mriTableName; + } + + public static String getMariaDbPort() { + return mariaDbPort.toString(); + } + + public static String getMariaDBDBName(){ + return mariaDBDatabaseName; + } + + static Connection getPostgreConnection() { + startPostgres(); + Connection conn=null; + try + { + conn = postgresDatabase.getConnection(); + } catch(SQLException e){ + e.printStackTrace(); + fail(); + } + return conn; + } + + static synchronized public void startPostgres(){ + if(pg==null) { + try { + tf.create(); + pg = EmbeddedPostgres.builder().setPort(postgresPort).setDataDirectory(tf.newFolder("tmp")+"/data-dir").start(); + } catch (IOException e) { + e.printStackTrace(); + fail(); + } + } + if(postgresDatabase==null) { + postgresDatabase = pg.getPostgresDatabase(); + } + } + + static public String getPostgresUrl(){ + return getPostgresUrlWithoutDb()+"/postgres"; + } + + static public String getPostgresUrlWithoutDb(){ + return "jdbc:postgresql://localhost:"+Integer.toString(postgresPort); + } + + synchronized static Connection getMariadbConnection(){ + startMariaDb(); + Connection conn = null; + try { + conn = DriverManager + .getConnection(getMariadbUrlWithoutDatabase()+"/"+mariaDBDatabaseName, "root", ""); + } catch (SQLException e) { + e.printStackTrace(); + fail("Error creating mdbc connection"); + } + return conn; + } + + public synchronized static void startMariaDb(){ + if (db == null) { + try { + db=DB.newEmbeddedDB(mariaDbPort); + db.start(); + db.createDB(mariaDBDatabaseName); + } catch (ManagedProcessException e) { + e.printStackTrace(); + fail("error initializing embedded mariadb"); + } + } + } + + static String getMariadbUrlWithoutDatabase(){ + return "jdbc:mariadb://localhost:"+Integer.toString(mariaDbPort); + } + + public static Connection getConnection(DBType type){ + switch(type){ + case MySQL: + return getMariadbConnection(); + case POSTGRES: + return getPostgreConnection(); + default: + fail("Wrong type for creating connection"); + } + return null; + } + + synchronized static void stopPostgres(){ + postgresDatabase=null; + if(pg!=null) { + try { + pg.close(); + pg=null; + } catch (IOException e) { + e.printStackTrace(); + fail("Error closing postgres database"); + } + } + if(tf!=null){ + tf.delete(); + } + } + + static void stopMySql(){ + try { + db.stop(); + } catch (ManagedProcessException e) { + e.printStackTrace(); + fail("Error closing mysql"); + } + } + + public static void cleanDatabase(DBType type){ + switch(type) { + case MySQL: + stopMySql(); + break; + case POSTGRES: + stopPostgres(); + break; + default: + fail("Wrong type for creating connection"); + } + } + + public static void initCassandra(){ + try { + EmbeddedCassandraServerHelper.startEmbeddedCassandra(EmbeddedCassandraServerHelper.CASSANDRA_RNDPORT_YML_FILE); + } catch (Exception e) { + System.out.println(e); + fail("Error starting embedded cassandra"); + } + cluster=EmbeddedCassandraServerHelper.getCluster(); + //cluster = new Cluster.Builder().addContactPoint(cassaHost).withPort(9142).build(); + cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(20000); + assertNotNull("Invalid configuration for cassandra", cluster); + session = EmbeddedCassandraServerHelper.getSession(); + assertNotNull("Invalid configuration for cassandra", session); + + MusicDataStoreHandle.mDstoreHandle = new MusicDataStore(cluster, session); + CassaLockStore store = new CassaLockStore(MusicDataStoreHandle.mDstoreHandle); + assertNotNull("Invalid configuration for music", store); + } + + public static void stopCassandra(){ + try { + EmbeddedCassandraServerHelper.cleanEmbeddedCassandra(); + } + catch(NullPointerException e){ + } + } + + public static Session getSession(){ + return session; + } + + public static MusicMixin getMusicMixin() throws MDBCServiceException { + initNamespaces(); + initTables(); + MusicMixin mixin=null; + try { + Properties properties = new Properties(); + properties.setProperty(MusicMixin.KEY_MY_ID,MdbcTestUtils.getServerName()); + properties.setProperty(MusicMixin.KEY_MUSIC_NAMESPACE,MdbcTestUtils.getKeyspace()); + properties.setProperty(MusicMixin.KEY_MUSIC_RFACTOR,"1"); + properties.setProperty(MusicMixin.KEY_MUSIC_ADDRESS,MdbcTestUtils.getCassandraUrl()); + mixin =new MusicMixin(null, MdbcTestUtils.getServerName(),properties); + } catch (MDBCServiceException e) { + fail("error creating music mixin"); + } + return mixin; + } + + public static void initNamespaces() throws MDBCServiceException{ + MusicMixin.createKeyspace("music_internal",1); + MusicMixin.createKeyspace(keyspace,1); + } + + public static void initTables() throws MDBCServiceException{ + MusicMixin.createMusicRangeInformationTable(keyspace, mriTableName); + MusicMixin.createMusicTxDigest(mtdTableName,keyspace, -1); + MusicMixin.createMusicEventualTxDigest(eventualMtxdTableName,keyspace, -1); + MusicMixin.createMusicNodeInfoTable(nodeInfoTableName,keyspace,-1); + MusicMixin.createMusicRangeDependencyTable(keyspace,rangeDependencyTableName); + } + +} diff --git a/mdbc-server/src/test/java/org/onap/music/mdbc/MySQLMixinTest.java b/mdbc-server/src/test/java/org/onap/music/mdbc/MySQLMixinTest.java index 2d31939..862e600 100644 --- a/mdbc-server/src/test/java/org/onap/music/mdbc/MySQLMixinTest.java +++ b/mdbc-server/src/test/java/org/onap/music/mdbc/MySQLMixinTest.java @@ -20,12 +20,14 @@ package org.onap.music.mdbc; +import java.util.Properties; import org.junit.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; +import org.onap.music.mdbc.MdbcTestUtils.DBType; import org.onap.music.mdbc.mixins.MySQLMixin; import ch.vorburger.mariadb4j.DB; @@ -33,8 +35,8 @@ import ch.vorburger.mariadb4j.DB; public class MySQLMixinTest { public static final String DATABASE = "mdbctest"; - public static final String TABLE= "Persons"; - public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE + " (\n" + + public static final String TABLE= MdbcTestUtils.getMariaDBDBName(); + public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + MdbcTestUtils.getMariaDBDBName()+ " (\n" + " PersonID int,\n" + " LastName varchar(255),\n" + " FirstName varchar(255),\n" + @@ -52,10 +54,8 @@ public class MySQLMixinTest { @BeforeClass public static void init() throws Exception { Class.forName("org.mariadb.jdbc.Driver"); - //start embedded mariadb - DB db = DB.newEmbeddedDB(13306); - db.start(); - db.createDB(DATABASE); + MdbcTestUtils.startMariaDb(); + } @AfterClass @@ -65,13 +65,14 @@ public class MySQLMixinTest { @Before public void beforeTest() throws SQLException { - this.conn = DriverManager.getConnection("jdbc:mariadb://localhost:13306/"+DATABASE, "root", ""); - this.mysqlMixin = new MySQLMixin(null, "localhost:13306/"+DATABASE, conn, null); + this.conn = MdbcTestUtils.getConnection(DBType.MySQL); + Properties info = new Properties(); + this.mysqlMixin = new MySQLMixin(null, null, conn, info); } @Test public void testGetDataBaseName() throws SQLException { - Assert.assertEquals(DATABASE, mysqlMixin.getDatabaseName()); + Assert.assertEquals(MdbcTestUtils.getMariaDBDBName(), mysqlMixin.getDatabaseName()); } } 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 deleted file mode 100755 index e5a3252..0000000 --- a/mdbc-server/src/test/java/org/onap/music/mdbc/TestUtils.java +++ /dev/null @@ -1,213 +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.onap.music.mdbc; - -import com.datastax.driver.core.*; -import org.onap.music.exceptions.MDBCServiceException; -import org.onap.music.exceptions.MusicLockingException; -import org.onap.music.lockingservice.cassandra.MusicLockState; -import org.onap.music.logging.EELFLoggerDelegate; -import org.onap.music.main.MusicCore; -import org.onap.music.main.MusicUtil; - -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.util.*; -import org.onap.music.mdbc.mixins.MusicInterface; -import org.onap.music.mdbc.tables.MusicRangeInformationRow; - -public class TestUtils { - - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(TestUtils.class); - - public static DatabasePartition createBasicRow(Range range, MusicInterface mixin, String mdbcServerName) - throws MDBCServiceException { - final UUID uuid = MDBCUtils.generateTimebasedUniqueKey(); - List<Range> ranges = new ArrayList<>(); - ranges.add(range); - DatabasePartition dbPartition = new DatabasePartition(ranges,uuid,null); - MusicRangeInformationRow newRow = new MusicRangeInformationRow(uuid,dbPartition, new ArrayList<>(), "", - mdbcServerName, true); - DatabasePartition partition=null; - partition = mixin.createMusicRangeInformation(newRow); - return partition; - } - - public static void unlockRow(String keyspace, String mriTableName, DatabasePartition partition) - throws MusicLockingException { - String fullyQualifiedMriKey = keyspace+"."+ mriTableName+"."+partition.getMRIIndex().toString(); - MusicLockState musicLockState = MusicCore.voluntaryReleaseLock(fullyQualifiedMriKey, partition.getLockId()); - } - - public static void createKeyspace(String keyspace, Session session) { - String queryOp = "CREATE KEYSPACE " + - keyspace + - " WITH REPLICATION " + - "= {'class':'SimpleStrategy', 'replication_factor':1}; "; - ResultSet res=null; - res = session.execute(queryOp); - } - - public static void deleteKeyspace(String keyspace, Session session){ - String queryBuilder = "DROP KEYSPACE " + - keyspace + - ";"; - ResultSet res = session.execute(queryBuilder); - } - - public static HashSet<String> getMriColNames(){ - return new HashSet<>( - Arrays.asList("rangeid","keys","txredolog","ownerid","metricprocessid") - ); - } - - public static HashSet<String> getMtdColNames(){ - return new HashSet<>( - Arrays.asList("txid","transactiondigest") - ); - } - - public static HashMap<String, DataType> getMriColTypes(Cluster cluster) throws Exception { - HashMap<String, DataType> expectedTypes = new HashMap<>(); - expectedTypes.put("rangeid",DataType.uuid()); - expectedTypes.put("keys",DataType.set(DataType.text())); - ProtocolVersion currentVer = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); - if(currentVer != null) { - throw new Exception("Protocol version for cluster is invalid"); - } - CodecRegistry registry = cluster.getConfiguration().getCodecRegistry(); - if(registry!= null) { - throw new Exception("Codec registry for cluster is invalid"); - } - expectedTypes.put("txredolog",DataType.list(TupleType.of(currentVer,registry,DataType.text(),DataType.uuid()))); - expectedTypes.put("ownerid",DataType.text()); - expectedTypes.put("metricprocessid",DataType.text()); - return expectedTypes; - } - - public static HashMap<String, DataType> getMtdColTypes(){ - HashMap<String,DataType> expectedTypes = new HashMap<>(); - expectedTypes.put("txid",DataType.uuid()); - expectedTypes.put("transactiondigest",DataType.text()); - return expectedTypes; - } - - public static void checkDataTypeForTable(List<ColumnMetadata> columnsMeta, HashSet<String> expectedColumns, - HashMap<String,DataType> expectedTypes) throws Exception { - for(ColumnMetadata cMeta : columnsMeta){ - String columnName = cMeta.getName(); - DataType type = cMeta.getType(); - if(!expectedColumns.contains(columnName)){ - throw new Exception("Invalid column name: "); - } - if(!expectedTypes.containsKey(columnName)){ - throw new Exception("Fix the contents of expectedtypes for column: "+columnName); - } - if(expectedTypes.get(columnName)!=type) { - throw new Exception("Invalid type for column: "+columnName); - } - } - } - - public static void readPropertiesFile(Properties prop) { - 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()); - } - } - - - public static void populateMusicUtilsWithProperties(Properties prop){ - //TODO: Learn how to do this properly within music - String[] propKeys = MusicUtil.getPropkeys(); - for (int k = 0; k < propKeys.length; k++) { - String key = propKeys[k]; - if (prop.containsKey(key) && prop.get(key) != null) { - switch (key) { - case "cassandra.host": - MusicUtil.setMyCassaHost(prop.getProperty(key)); - break; - case "music.ip": - MusicUtil.setDefaultMusicIp(prop.getProperty(key)); - break; - case "debug": - MusicUtil.setDebug(Boolean - .getBoolean(prop.getProperty(key).toLowerCase())); - break; - case "version": - MusicUtil.setVersion(prop.getProperty(key)); - break; - case "music.rest.ip": - MusicUtil.setMusicRestIp(prop.getProperty(key)); - break; - case "music.properties": - MusicUtil.setMusicPropertiesFilePath(prop.getProperty(key)); - break; - case "lock.lease.period": - MusicUtil.setDefaultLockLeasePeriod( - Long.parseLong(prop.getProperty(key))); - break; - case "my.id": - MusicUtil.setMyId(Integer.parseInt(prop.getProperty(key))); - break; - case "all.ids": - String[] ids = prop.getProperty(key).split(":"); - MusicUtil.setAllIds(new ArrayList<String>(Arrays.asList(ids))); - break; - case "public.ip": - MusicUtil.setPublicIp(prop.getProperty(key)); - break; - case "all.public.ips": - String[] ips = prop.getProperty(key).split(":"); - if (ips.length== 1) { - // Future use - } else if (ips.length > 1) { - MusicUtil.setAllPublicIps( - new ArrayList<String>(Arrays.asList(ips))); - } - break; - case "cassandra.user": - MusicUtil.setCassName(prop.getProperty(key)); - break; - case "cassandra.password": - MusicUtil.setCassPwd(prop.getProperty(key)); - break; - case "aaf.endpoint.url": - MusicUtil.setAafEndpointUrl(prop.getProperty(key)); - break; - default: - System.out.println("No case found for " + key); - } - } - } - - - } -} diff --git a/mdbc-server/src/test/java/org/onap/music/mdbc/mixins/MusicMixinTest.java b/mdbc-server/src/test/java/org/onap/music/mdbc/mixins/MusicMixinTest.java index 41a943e..c8d284e 100644 --- a/mdbc-server/src/test/java/org/onap/music/mdbc/mixins/MusicMixinTest.java +++ b/mdbc-server/src/test/java/org/onap/music/mdbc/mixins/MusicMixinTest.java @@ -31,23 +31,35 @@ import java.util.Map.Entry; import java.util.Properties; import java.util.UUID; import java.util.function.Consumer; + +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static org.junit.Assert.*; + +import com.datastax.driver.core.Session; + +import java.util.*; + + import org.cassandraunit.utils.EmbeddedCassandraServerHelper; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Test; + import org.onap.music.datastore.MusicDataStore; import org.onap.music.datastore.MusicDataStoreHandle; + 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.lockingservice.cassandra.CassaLockStore; import org.onap.music.lockingservice.cassandra.MusicLockState; import org.onap.music.main.MusicCore; import org.onap.music.mdbc.DatabasePartition; import org.onap.music.mdbc.MDBCUtils; import org.onap.music.mdbc.Range; + import org.onap.music.mdbc.StateManager; import org.onap.music.mdbc.proto.ProtoDigest.Digest.CompleteDigest; import org.onap.music.mdbc.tables.MusicRangeInformationRow; @@ -57,37 +69,25 @@ import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Session; import com.google.protobuf.InvalidProtocolBufferException; +import org.onap.music.mdbc.MdbcTestUtils; +import org.onap.music.mdbc.TestUtils; +import org.onap.music.mdbc.ownership.Dag; +import org.onap.music.mdbc.ownership.DagNode; +import org.onap.music.mdbc.tables.MusicRangeInformationRow; + public class MusicMixinTest { - final private static String keyspace="metricmusictest"; - final private static String mriTableName = "musicrangeinformation"; - final private static String mtdTableName = "musictxdigest"; - final private static String mdbcServerName = "name"; + //Properties used to connect to music - private static Cluster cluster; private static Session session; - private static String cassaHost = "localhost"; private static MusicMixin mixin = null; private StateManager stateManager; @BeforeClass - public static void init() throws MusicServiceException { - try { - EmbeddedCassandraServerHelper.startEmbeddedCassandra(); - } catch (Exception e) { - System.out.println(e); - } - cluster=EmbeddedCassandraServerHelper.getCluster(); - //cluster = new Cluster.Builder().addContactPoint(cassaHost).withPort(9142).build(); - cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(20000); - assertNotNull("Invalid configuration for cassandra", cluster); - session = EmbeddedCassandraServerHelper.getSession(); - assertNotNull("Invalid configuration for cassandra", session); + public static void init() throws MDBCServiceException { + MdbcTestUtils.initCassandra(); - MusicDataStoreHandle.mDstoreHandle = new MusicDataStore(cluster, session); - CassaLockStore store = new CassaLockStore(MusicDataStoreHandle.mDstoreHandle); - assertNotNull("Invalid configuration for music", store); } @AfterClass @@ -102,59 +102,52 @@ public class MusicMixinTest { } @Before - public void initTest(){ - session.execute("DROP KEYSPACE IF EXISTS "+keyspace); + public void initTest() throws MDBCServiceException { + session = MdbcTestUtils.getSession(); + session.execute("DROP KEYSPACE IF EXISTS "+ MdbcTestUtils.getKeyspace()); + mixin=MdbcTestUtils.getMusicMixin(); + } + + //@Test(timeout=10000) + @Ignore // TODO: Move ownership tests to OwnershipAndCheckpointTest + @Test + public void own() { + Range range = new Range("TEST.TABLE1"); + List<Range> ranges = new ArrayList<>(); + ranges.add(range); + DatabasePartition partition=null; try { - Properties properties = new Properties(); - properties.setProperty(MusicMixin.KEY_MUSIC_NAMESPACE,keyspace); - properties.setProperty(MusicMixin.KEY_MY_ID,mdbcServerName); - properties.setProperty(MusicMixin.KEY_COMPRESSION, Boolean.toString(true)); - mixin=new MusicMixin(stateManager, mdbcServerName,properties); - } catch (MDBCServiceException e) { - fail("error creating music mixin"); + partition = TestUtils.createBasicRow(range, mixin, MdbcTestUtils.getServerName()); + } + catch(Exception e){ + fail("fail to create partition"); + } + try { + TestUtils.unlockRow(MdbcTestUtils.getKeyspace(),MdbcTestUtils.getMriTableName(),partition); + } catch (MusicLockingException e) { + fail(e.getMessage()); } + DatabasePartition currentPartition = new DatabasePartition(MDBCUtils.generateTimebasedUniqueKey()); + try { + mixin.getStateManager().getOwnAndCheck().own(mixin,ranges,currentPartition, MDBCUtils.generateTimebasedUniqueKey()); + } catch (MDBCServiceException e) { + fail("failure when running own function"); + } } - //Own has been removed from musicMixin -// @Test(timeout=10000) -// public void own() { -// Range range = new Range("TABLE1"); -// List<Range> ranges = new ArrayList<>(); -// ranges.add(range); -// DatabasePartition partition=null; -// try { -// partition = TestUtils.createBasicRow(range, mixin, mdbcServerName); -// } -// catch(Exception e){ -// fail(e.getMessage()); -// } -// try { -// TestUtils.unlockRow(keyspace,mriTableName,partition); -// } catch (MusicLockingException e) { -// fail(e.getMessage()); -// } -// -// DatabasePartition currentPartition = new DatabasePartition(MDBCUtils.generateTimebasedUniqueKey()); -// try { -// mixin.own(ranges,currentPartition, MDBCUtils.generateTimebasedUniqueKey()); -// } catch (MDBCServiceException e) { -// fail("failure when running own function"); -// } -// } - private DatabasePartition addRow(List<Range> ranges,boolean isLatest){ final UUID uuid = MDBCUtils.generateTimebasedUniqueKey(); DatabasePartition dbPartition = new DatabasePartition(ranges,uuid,null); MusicRangeInformationRow newRow = new MusicRangeInformationRow(uuid,dbPartition, new ArrayList<>(), "", - mdbcServerName, isLatest); + MdbcTestUtils.getServerName(), isLatest); DatabasePartition partition=null; try { partition = mixin.createMusicRangeInformation(newRow); } catch (MDBCServiceException e) { fail("failure when creating new row"); } - String fullyQualifiedMriKey = keyspace+"."+ mriTableName+"."+partition.getMRIIndex().toString(); + String fullyQualifiedMriKey = MdbcTestUtils.getKeyspace()+"."+ MdbcTestUtils.getMriTableName()+"."+partition.getMRIIndex().toString(); try { MusicLockState musicLockState = MusicCore.voluntaryReleaseLock(fullyQualifiedMriKey, partition.getLockId()); } catch (MusicLockingException e) { @@ -163,81 +156,81 @@ public class MusicMixinTest { return partition; } - //Own has been removed from musicMixin -// @Test(timeout=10000) -// public void own2() throws InterruptedException, MDBCServiceException { -// List<Range> range12 = new ArrayList<>( Arrays.asList( -// new Range("RANGE1"), -// new Range("RANGE2") -// )); -// List<Range> range34 = new ArrayList<>( Arrays.asList( -// new Range("RANGE3"), -// new Range("RANGE4") -// )); -// List<Range> range24 = new ArrayList<>( Arrays.asList( -// new Range("RANGE2"), -// new Range("RANGE4") -// )); -// List<Range> range123 = new ArrayList<>( Arrays.asList( -// new Range("RANGE1"), -// new Range("RANGE2"), -// new Range("RANGE3") -// )); -// DatabasePartition db1 = addRow(range12, false); -// DatabasePartition db2 = addRow(range34, false); -// MILLISECONDS.sleep(10); -// DatabasePartition db3 = addRow(range12, true); -// DatabasePartition db4 = addRow(range34, true); -// MILLISECONDS.sleep(10); -// DatabasePartition db5 = addRow(range24, true); -// DatabasePartition currentPartition = new DatabasePartition(MDBCUtils.generateTimebasedUniqueKey()); -// MusicInterface.OwnershipReturn own = null; -// try { -// own = mixin.own(range123, currentPartition, MDBCUtils.generateTimebasedUniqueKey()); -// } catch (MDBCServiceException e) { -// fail("failure when running own function"); -// } -// Dag dag = own.getDag(); -// -// DagNode node4 = dag.getNode(db4.getMRIIndex()); -// assertFalse(node4.hasNotIncomingEdges()); -// List<DagNode> outgoingEdges = new ArrayList<>(node4.getOutgoingEdges()); -// assertEquals(1,outgoingEdges.size()); -// -// DagNode missing = outgoingEdges.get(0); -// Set<Range> missingRanges = missing.getRangeSet(); -// assertEquals(2,missingRanges.size()); -// assertTrue(missingRanges.contains(new Range("RANGE1"))); -// assertTrue(missingRanges.contains(new Range("RANGE3"))); -// List<DagNode> outgoingEdges1 = missing.getOutgoingEdges(); -// assertEquals(1,outgoingEdges1.size()); -// -// DagNode finalNode = outgoingEdges1.get(0); -// assertFalse(finalNode.hasNotIncomingEdges()); -// Set<Range> finalSet = finalNode.getRangeSet(); -// assertEquals(3,finalSet.size()); -// assertTrue(finalSet.contains(new Range("RANGE1"))); -// assertTrue(finalSet.contains(new Range("RANGE2"))); -// assertTrue(finalSet.contains(new Range("RANGE3"))); -// -// DagNode node5 = dag.getNode(db5.getMRIIndex()); -// List<DagNode> toRemoveOutEdges = node5.getOutgoingEdges(); -// assertEquals(1,toRemoveOutEdges.size()); -// toRemoveOutEdges.remove(finalNode); -// assertEquals(0,toRemoveOutEdges.size()); -// -// MusicRangeInformationRow row = mixin.getMusicRangeInformation(own.getRangeId()); -// assertTrue(row.getIsLatest()); -// DatabasePartition dbPartition = row.getDBPartition(); -// List<Range> snapshot = dbPartition.getSnapshot(); -// assertEquals(3,snapshot.size()); -// MusicRangeInformationRow node5row = mixin.getMusicRangeInformation(node5.getId()); -// assertFalse(node5row.getIsLatest()); -// MusicRangeInformationRow node4Row = mixin.getMusicRangeInformation(db4.getMRIIndex()); -// assertFalse(node4Row.getIsLatest()); -// MusicRangeInformationRow node3Row = mixin.getMusicRangeInformation(db3.getMRIIndex()); -// assertFalse(node3Row.getIsLatest()); -// } + @Ignore // TODO: Move ownership tests to OwnershipAndCheckpointTest + @Test(timeout=1000) + public void own2() throws InterruptedException, MDBCServiceException { + List<Range> range12 = new ArrayList<>( Arrays.asList( + new Range("TEST.RANGE1"), + new Range("TEST.RANGE2") + )); + List<Range> range34 = new ArrayList<>( Arrays.asList( + new Range("TEST.RANGE3"), + new Range("TEST.RANGE4") + )); + List<Range> range24 = new ArrayList<>( Arrays.asList( + new Range("TEST.RANGE2"), + new Range("TEST.RANGE4") + )); + List<Range> range123 = new ArrayList<>( Arrays.asList( + new Range("TEST.RANGE1"), + new Range("TEST.RANGE2"), + new Range("TEST.RANGE3") + )); + DatabasePartition db1 = addRow(range12, false); + DatabasePartition db2 = addRow(range34, false); + MILLISECONDS.sleep(10); + DatabasePartition db3 = addRow(range12, true); + DatabasePartition db4 = addRow(range34, true); + MILLISECONDS.sleep(10); + DatabasePartition db5 = addRow(range24, true); + DatabasePartition currentPartition = new DatabasePartition(MDBCUtils.generateTimebasedUniqueKey()); + MusicInterface.OwnershipReturn own = null; + try { + own = mixin.getStateManager().getOwnAndCheck().own(mixin,range123, currentPartition, MDBCUtils.generateTimebasedUniqueKey()); + } catch (MDBCServiceException e) { + fail("failure when running own function"); + } + Dag dag = own.getDag(); + + DagNode node4 = dag.getNode(db4.getMRIIndex()); + assertFalse(node4.hasNotIncomingEdges()); + List<DagNode> outgoingEdges = new ArrayList<>(node4.getOutgoingEdges()); + assertEquals(1,outgoingEdges.size()); + + DagNode missing = outgoingEdges.get(0); + Set<Range> missingRanges = missing.getRangeSet(); + assertEquals(2,missingRanges.size()); + assertTrue(missingRanges.contains(new Range("TEST.RANGE1"))); + assertTrue(missingRanges.contains(new Range("TEST.RANGE3"))); + List<DagNode> outgoingEdges1 = missing.getOutgoingEdges(); + assertEquals(1,outgoingEdges1.size()); + + DagNode finalNode = outgoingEdges1.get(0); + assertFalse(finalNode.hasNotIncomingEdges()); + Set<Range> finalSet = finalNode.getRangeSet(); + assertEquals(3,finalSet.size()); + assertTrue(finalSet.contains(new Range("TEST.RANGE1"))); + assertTrue(finalSet.contains(new Range("TEST.RANGE2"))); + assertTrue(finalSet.contains(new Range("TEST.RANGE3"))); + + DagNode node5 = dag.getNode(db5.getMRIIndex()); + List<DagNode> toRemoveOutEdges = node5.getOutgoingEdges(); + assertEquals(1,toRemoveOutEdges.size()); + toRemoveOutEdges.remove(finalNode); + assertEquals(0,toRemoveOutEdges.size()); + + MusicRangeInformationRow row = mixin.getMusicRangeInformation(own.getRangeId()); + assertTrue(row.getIsLatest()); + DatabasePartition dbPartition = row.getDBPartition(); + List<Range> snapshot = dbPartition.getSnapshot(); + assertEquals(3,snapshot.size()); + MusicRangeInformationRow node5row = mixin.getMusicRangeInformation(node5.getId()); + assertFalse(node5row.getIsLatest()); + MusicRangeInformationRow node4Row = mixin.getMusicRangeInformation(db4.getMRIIndex()); + assertFalse(node4Row.getIsLatest()); + MusicRangeInformationRow node3Row = mixin.getMusicRangeInformation(db3.getMRIIndex()); + assertFalse(node3Row.getIsLatest()); + } @Test public void relinquish() { diff --git a/mdbc-server/src/test/java/org/onap/music/mdbc/mixins/PostgresMixinTest.java b/mdbc-server/src/test/java/org/onap/music/mdbc/mixins/PostgresMixinTest.java new file mode 100644 index 0000000..2134a79 --- /dev/null +++ b/mdbc-server/src/test/java/org/onap/music/mdbc/mixins/PostgresMixinTest.java @@ -0,0 +1,220 @@ +/* + * ============LICENSE_START==================================================== + * org.onap.music.mdbc + * ============================================================================= + * Copyright (C) 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END====================================================== + */ + +package org.onap.music.mdbc.mixins; + +import static org.junit.Assert.*; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.Set; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.music.exceptions.MDBCServiceException; +import org.onap.music.mdbc.MdbcTestUtils; +import org.onap.music.mdbc.MdbcTestUtils.DBType; +import org.onap.music.mdbc.Range; +import org.onap.music.mdbc.TableInfo; +import org.onap.music.mdbc.tables.StagingTable; + +public class PostgresMixinTest { + final private static String keyspace="metricmusictest"; + final private static String mdbcServerName = "name"; + + static PostgresMixin mixin; + + private static MusicMixin mi = null; + private static Connection conn; + + @BeforeClass + public static void init() throws MDBCServiceException { + MdbcTestUtils.initCassandra(); + mi=MdbcTestUtils.getMusicMixin(); + try { + conn = MdbcTestUtils.getConnection(DBType.POSTGRES); + Properties info = new Properties(); + mixin = new PostgresMixin(mi, null, conn, info); + } catch (SQLException e) { + e.printStackTrace(); + fail(); + } + } + + @AfterClass + public static void close(){ + //TODO: shutdown cassandra + mixin=null; + MdbcTestUtils.cleanDatabase(DBType.POSTGRES); + MdbcTestUtils.stopCassandra(); + } + + @Test + public void getMixinName() { + final String mixinName = mixin.getMixinName(); + assertEquals(mixinName.toLowerCase(),"postgres"); + } + + @Test + public void getSQLTableSet() { + createTestTable(); + final Set<String> sqlTableSet = mixin.getSQLTableSet(); + assertEquals(1,sqlTableSet.size()); + assertTrue(sqlTableSet.contains("testtable")); + } + + @Test + public void getTableInfo() { + createTestTable(); + final TableInfo tableInfo = mixin.getTableInfo("testtable"); + assertNotNull(tableInfo); + assertEquals(3,tableInfo.columns.size()); + int index=0; + for(String col: tableInfo.columns) { + switch(col.toLowerCase()){ + case "ix": + assertTrue(tableInfo.iskey.get(index)); + assertEquals(Types.INTEGER, tableInfo.coltype.get(index).intValue()); + break; + case "test1": + assertFalse(tableInfo.iskey.get(index)); + assertEquals(Types.CHAR, tableInfo.coltype.get(index).intValue()); + break; + case "test2": + assertFalse(tableInfo.iskey.get(index)); + assertEquals(Types.VARCHAR, tableInfo.coltype.get(index).intValue()); + break; + default: + fail(); + } + index++; + } + } + + private void createTestTable() { + try { + final Statement statement = conn.createStatement(); + statement.execute("CREATE TABLE IF NOT EXISTS testtable (IX SERIAL, test1 CHAR(1), test2 VARCHAR(255), PRIMARY KEY (IX));"); + statement.close(); + } catch (SQLException e) { + e.printStackTrace(); + fail(); + } + } + + private void cleanTestTable() { + try { + final Statement statement = conn.createStatement(); + statement.execute("DELETE FROM testtable;"); + statement.close(); + } catch (SQLException e) { + e.printStackTrace(); + fail(); + } + } + + @Test + public void postStatementHook() { + createTestTable(); + mixin.createSQLTriggers("testtable"); + final String sqlOperation = "INSERT INTO testtable (test1,test2) VALUES ('u','test');"; + Statement stm=null; + try { + stm = conn.createStatement(); + } catch (SQLException e) { + e.printStackTrace(); + fail(); + } + mixin.preStatementHook(sqlOperation); + try { + stm.execute(sqlOperation); + } catch (SQLException e) { + e.printStackTrace(); + fail(); + } + StagingTable st=new StagingTable(); + mixin.postStatementHook(sqlOperation,st); + mixin.preCommitHook(); + assertFalse(st.isEmpty()); + } + + void checkEmptyTestTable(){ + ResultSet resultSet = mixin.executeSQLRead("SELECT * FROM testtable;"); + try { + assertFalse(resultSet.next()); + } catch (SQLException e) { + e.printStackTrace(); + fail(); + } + } + + + void checkOneRowWithContents(String test1Val, String test2Val){ + ResultSet resultSet = mixin.executeSQLRead("SELECT * FROM testtable;"); + try { + assertTrue(resultSet.next()); + assertEquals(test1Val, resultSet.getString("test1")); + assertEquals(test2Val, resultSet.getString("test2")); + assertFalse(resultSet.next()); + } + catch(SQLException e){ + e.printStackTrace(); + fail(); + } + } + + @Test + public void applyTxDigest() { + createTestTable(); + mixin.createSQLTriggers("testtable"); + final String sqlOperation = "INSERT INTO testtable (test1,test2) VALUES ('u','test');"; + Statement stm=null; + try { + stm = conn.createStatement(); + } catch (SQLException e) { + e.printStackTrace(); + fail(); + } + mixin.preStatementHook(sqlOperation); + try { + stm.execute(sqlOperation); + } catch (SQLException e) { + e.printStackTrace(); + fail(); + } + StagingTable st=new StagingTable(); + mixin.postStatementHook(sqlOperation,st); + mixin.preCommitHook(); + assertFalse(st.isEmpty()); + cleanTestTable(); + checkEmptyTestTable(); + List<Range> ranges = new ArrayList<>(); + ranges.add(new Range("public.testtable")); + try { + mixin.applyTxDigest(st,ranges); + } catch (SQLException|MDBCServiceException e) { + e.printStackTrace(); + fail(); + } + checkOneRowWithContents("u","test"); + } +}
\ No newline at end of file diff --git a/mdbc-server/src/test/java/org/onap/music/mdbc/ownership/DagTest.java b/mdbc-server/src/test/java/org/onap/music/mdbc/ownership/DagTest.java index da64595..9e6161a 100644 --- a/mdbc-server/src/test/java/org/onap/music/mdbc/ownership/DagTest.java +++ b/mdbc-server/src/test/java/org/onap/music/mdbc/ownership/DagTest.java @@ -59,7 +59,7 @@ public class DagTest { public void getDag() throws InterruptedException, MDBCServiceException { List<MusicRangeInformationRow> rows = new ArrayList<>(); List<Range> ranges = new ArrayList<>( Arrays.asList( - new Range("range1") + new Range("schema.range1") )); rows.add(createNewRow(new ArrayList<>(ranges),"",false)); MILLISECONDS.sleep(10); @@ -87,14 +87,14 @@ public class DagTest { public void getDag2() throws InterruptedException, MDBCServiceException { List<MusicRangeInformationRow> rows = new ArrayList<>(); List<Range> range1 = new ArrayList<>( Arrays.asList( - new Range("range1") + new Range("schema.range1") )); List<Range> range2 = new ArrayList<>( Arrays.asList( - new Range("range2") + new Range("schema.range2") )); List<Range> ranges = new ArrayList<>( Arrays.asList( - new Range("range2"), - new Range("range1") + new Range("schema.range2"), + new Range("schema.range1") )); rows.add(createNewRow(new ArrayList<>(range1),"",false)); MILLISECONDS.sleep(10); @@ -123,7 +123,7 @@ public class DagTest { public void nextToOwn() throws InterruptedException, MDBCServiceException { List<MusicRangeInformationRow> rows = new ArrayList<>(); List<Range> ranges = new ArrayList<>( Arrays.asList( - new Range("range1") + new Range("schema.range1") )); rows.add(createNewRow(new ArrayList<>(ranges),"",false)); MILLISECONDS.sleep(10); @@ -149,20 +149,20 @@ public class DagTest { public void nextToApply() throws InterruptedException { List<MusicRangeInformationRow> rows = new ArrayList<>(); List<Range> ranges = new ArrayList<>( Arrays.asList( - new Range("range1") + new Range("schema.range1") )); List<MusicTxDigestId> redo1 = new ArrayList<>(Arrays.asList( - new MusicTxDigestId(MDBCUtils.generateUniqueKey(),0) + new MusicTxDigestId(null,MDBCUtils.generateUniqueKey(),0) )); rows.add(createNewRow(new ArrayList<>(ranges),"",false,redo1)); MILLISECONDS.sleep(10); List<MusicTxDigestId> redo2 = new ArrayList<>(Arrays.asList( - new MusicTxDigestId(MDBCUtils.generateUniqueKey(),0) + new MusicTxDigestId(null,MDBCUtils.generateUniqueKey(),0) )); rows.add(createNewRow(new ArrayList<>(ranges),"",false,redo2)); MILLISECONDS.sleep(10); List<MusicTxDigestId> redo3 = new ArrayList<>(Arrays.asList( - new MusicTxDigestId(MDBCUtils.generateUniqueKey(),0) + new MusicTxDigestId(null,MDBCUtils.generateUniqueKey(),0) )); rows.add(createNewRow(new ArrayList<>(ranges),"",true,redo3)); Dag dag = Dag.getDag(rows, ranges); @@ -180,7 +180,7 @@ public class DagTest { assertEquals(0,pair.getKey().index); List<Range> value = pair.getValue(); assertEquals(1,value.size()); - assertEquals(new Range("range1"),value.get(0)); + assertEquals(new Range("schema.range1"),value.get(0)); pair = node.nextNotAppliedTransaction(rangesSet); transactionCounter++; } @@ -195,23 +195,23 @@ public class DagTest { Map<Range, Pair<MriReference, Integer>> alreadyApplied = new HashMap<>(); List<MusicRangeInformationRow> rows = new ArrayList<>(); List<Range> ranges = new ArrayList<>( Arrays.asList( - new Range("range1") + new Range("schema.range1") )); List<MusicTxDigestId> redo1 = new ArrayList<>(Arrays.asList( - new MusicTxDigestId(MDBCUtils.generateUniqueKey(),0) + new MusicTxDigestId(null,MDBCUtils.generateUniqueKey(),0) )); rows.add(createNewRow(new ArrayList<>(ranges),"",false,redo1)); MILLISECONDS.sleep(10); List<MusicTxDigestId> redo2 = new ArrayList<>(Arrays.asList( - new MusicTxDigestId(MDBCUtils.generateUniqueKey(),0), - new MusicTxDigestId(MDBCUtils.generateUniqueKey(),1) + new MusicTxDigestId(null,MDBCUtils.generateUniqueKey(),0), + new MusicTxDigestId(null,MDBCUtils.generateUniqueKey(),1) )); MusicRangeInformationRow newRow = createNewRow(new ArrayList<>(ranges), "", false, redo2); - alreadyApplied.put(new Range("range1"),Pair.of(new MriReference(newRow.getPartitionIndex()), 0)); + alreadyApplied.put(new Range("schema.range1"),Pair.of(new MriReference(newRow.getPartitionIndex()), 0)); rows.add(newRow); MILLISECONDS.sleep(10); List<MusicTxDigestId> redo3 = new ArrayList<>(Arrays.asList( - new MusicTxDigestId(MDBCUtils.generateUniqueKey(),0) + new MusicTxDigestId(null,MDBCUtils.generateUniqueKey(),0) )); rows.add(createNewRow(new ArrayList<>(ranges),"",true,redo3)); Dag dag = Dag.getDag(rows, ranges); @@ -230,7 +230,7 @@ public class DagTest { assertEquals(2-nodeCounter,pair.getKey().index); List<Range> value = pair.getValue(); assertEquals(1,value.size()); - assertEquals(new Range("range1"),value.get(0)); + assertEquals(new Range("schema.range1"),value.get(0)); pair = node.nextNotAppliedTransaction(rangesSet); transactionCounter++; } @@ -244,14 +244,14 @@ public class DagTest { public void isDifferent() throws InterruptedException { List<MusicRangeInformationRow> rows = new ArrayList<>(); List<Range> range1 = new ArrayList<>( Arrays.asList( - new Range("range1") + new Range("schema.range1") )); List<Range> range2 = new ArrayList<>( Arrays.asList( - new Range("range2") + new Range("schema.range2") )); List<Range> ranges = new ArrayList<>( Arrays.asList( - new Range("range2"), - new Range("range1") + new Range("schema.range2"), + new Range("schema.range1") )); rows.add(createNewRow(new ArrayList<>(range1),"",false)); MILLISECONDS.sleep(10); @@ -277,14 +277,14 @@ public class DagTest { public void getOldestDoubles() throws InterruptedException, MDBCServiceException { List<MusicRangeInformationRow> rows = new ArrayList<>(); List<Range> range1 = new ArrayList<>( Arrays.asList( - new Range("range1") + new Range("schema.range1") )); List<Range> range2 = new ArrayList<>( Arrays.asList( - new Range("range2") + new Range("schema.range2") )); List<Range> ranges = new ArrayList<>( Arrays.asList( - new Range("range2"), - new Range("range1") + new Range("schema.range2"), + new Range("schema.range1") )); rows.add(createNewRow(new ArrayList<>(range1),"",false)); MILLISECONDS.sleep(10); @@ -306,15 +306,15 @@ public class DagTest { public void getIncompleteRangesAndDependents() throws InterruptedException, MDBCServiceException { List<MusicRangeInformationRow> rows = new ArrayList<>(); List<Range> range1 = new ArrayList<>( Arrays.asList( - new Range("range1") + new Range("schema.range1") )); List<Range> range2 = new ArrayList<>( Arrays.asList( - new Range("range2"), - new Range("range3") + new Range("schema.range2"), + new Range("schema.range3") )); List<Range> ranges = new ArrayList<>( Arrays.asList( - new Range("range2"), - new Range("range1") + new Range("schema.range2"), + new Range("schema.range1") )); rows.add(createNewRow(new ArrayList<>(range1),"",false)); MILLISECONDS.sleep(10); @@ -330,7 +330,7 @@ public class DagTest { List<Range> incomplete = incompleteRangesAndDependents.getKey(); Set<DagNode> dependents = incompleteRangesAndDependents.getValue(); assertEquals(1,incomplete.size()); - assertTrue(incomplete.contains(new Range("range3"))); + assertTrue(incomplete.contains(new Range("schema.range3"))); assertEquals(1,dependents.size()); assertTrue(dependents.contains(dag.getNode(rows.get(3).getPartitionIndex()))); } @@ -339,16 +339,16 @@ public class DagTest { public void getIncompleteRangesAndDependents2() throws InterruptedException, MDBCServiceException { List<MusicRangeInformationRow> rows = new ArrayList<>(); List<Range> range1 = new ArrayList<>( Arrays.asList( - new Range("range1"), - new Range("range4") + new Range("schema.range1"), + new Range("schema.range4") )); List<Range> range2 = new ArrayList<>( Arrays.asList( - new Range("range2"), - new Range("range3") + new Range("schema.range2"), + new Range("schema.range3") )); List<Range> ranges = new ArrayList<>( Arrays.asList( - new Range("range2"), - new Range("range1") + new Range("schema.range2"), + new Range("schema.range1") )); rows.add(createNewRow(new ArrayList<>(range1),"",false)); MILLISECONDS.sleep(10); @@ -364,8 +364,8 @@ public class DagTest { List<Range> incomplete = incompleteRangesAndDependents.getKey(); Set<DagNode> dependents = incompleteRangesAndDependents.getValue(); assertEquals(2,incomplete.size()); - assertTrue(incomplete.contains(new Range("range3"))); - assertTrue(incomplete.contains(new Range("range4"))); + assertTrue(incomplete.contains(new Range("schema.range3"))); + assertTrue(incomplete.contains(new Range("schema.range4"))); assertEquals(2,dependents.size()); assertTrue(dependents.contains(dag.getNode(rows.get(3).getPartitionIndex()))); assertTrue(dependents.contains(dag.getNode(rows.get(2).getPartitionIndex()))); @@ -375,20 +375,20 @@ public class DagTest { public void addNewNodeWithSearch() throws InterruptedException, MDBCServiceException { List<MusicRangeInformationRow> rows = new ArrayList<>(); List<Range> range1 = new ArrayList<>( Arrays.asList( - new Range("range1") + new Range("schema.range1") )); List<Range> range2 = new ArrayList<>( Arrays.asList( - new Range("range2"), - new Range("range3") + new Range("schema.range2"), + new Range("schema.range3") )); List<Range> ranges = new ArrayList<>( Arrays.asList( - new Range("range2"), - new Range("range1") + new Range("schema.range2"), + new Range("schema.range1") )); List<Range> allRanges = new ArrayList<>( Arrays.asList( - new Range("range2"), - new Range("range3"), - new Range("range1") + new Range("schema.range2"), + new Range("schema.range3"), + new Range("schema.range1") )); rows.add(createNewRow(new ArrayList<>(range1),"",false)); MILLISECONDS.sleep(10); diff --git a/mdbc-server/src/test/java/org/onap/music/mdbc/ownership/OwnershipAndCheckpointTest.java b/mdbc-server/src/test/java/org/onap/music/mdbc/ownership/OwnershipAndCheckpointTest.java index eb01bcd..f2fbd1f 100644 --- a/mdbc-server/src/test/java/org/onap/music/mdbc/ownership/OwnershipAndCheckpointTest.java +++ b/mdbc-server/src/test/java/org/onap/music/mdbc/ownership/OwnershipAndCheckpointTest.java @@ -34,7 +34,6 @@ import org.cassandraunit.utils.EmbeddedCassandraServerHelper; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.mockito.Mock; import org.mockito.Mockito; @@ -46,8 +45,10 @@ import org.onap.music.exceptions.MusicServiceException; import org.onap.music.lockingservice.cassandra.CassaLockStore; import org.onap.music.mdbc.DatabasePartition; import org.onap.music.mdbc.MDBCUtils; +import org.onap.music.mdbc.MdbcTestUtils.DBType; import org.onap.music.mdbc.Range; import org.onap.music.mdbc.StateManager; +import org.onap.music.mdbc.MdbcTestUtils; import org.onap.music.mdbc.TestUtils; import org.onap.music.mdbc.mixins.LockResult; import org.onap.music.mdbc.mixins.MusicInterface.OwnershipReturn; @@ -58,13 +59,8 @@ import org.onap.music.mdbc.tables.StagingTable; import org.onap.music.mdbc.tables.TxCommitProgress; public class OwnershipAndCheckpointTest { - final private static int sqlPort = 13350; - final private static String keyspace="metricmusictest"; - final private static String mriTableName = "musicrangeinformation"; - final private static String mtdTableName = "musictxdigest"; - final private static String mdbcServerName = "name"; - public static final String DATABASE = "mdbcTest"; - public static final String TABLE= "PERSONS"; + public static final String DATABASE = MdbcTestUtils.mariaDBDatabaseName; + public static final String TABLE= MdbcTestUtils.mariaDBDatabaseName+".PERSONS"; public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE + " (\n" + " PersonID int,\n" + " LastName varchar(255),\n" + @@ -75,11 +71,7 @@ public class OwnershipAndCheckpointTest { ");"; public static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE + ";"; //Properties used to connect to music - private static Cluster cluster; - private static Session session; - private static String cassaHost = "localhost"; private static MusicMixin musicMixin = null; - private static DB db; Connection conn; MySQLMixin mysqlMixin; OwnershipAndCheckpoint ownAndCheck; @@ -90,37 +82,18 @@ public class OwnershipAndCheckpointTest { @BeforeClass public static void init() throws MusicServiceException, ClassNotFoundException, ManagedProcessException { - try { - EmbeddedCassandraServerHelper.startEmbeddedCassandra(); - } catch (Exception e) { - fail(e.getMessage()); - } - cluster=EmbeddedCassandraServerHelper.getCluster(); - //cluster = new Cluster.Builder().addContactPoint(cassaHost).withPort(9142).build(); - cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(20000); - assertNotNull("Invalid configuration for cassandra", cluster); - session = EmbeddedCassandraServerHelper.getSession(); - assertNotNull("Invalid configuration for cassandra", session); + MdbcTestUtils.initCassandra(); Class.forName("org.mariadb.jdbc.Driver"); - MusicDataStoreHandle.mDstoreHandle = new MusicDataStore(cluster, session); - CassaLockStore store = new CassaLockStore(MusicDataStoreHandle.mDstoreHandle); - assertNotNull("Invalid configuration for music", store); //start embedded mariadb - db = DB.newEmbeddedDB(sqlPort); - db.start(); - db.createDB(DATABASE); + MdbcTestUtils.startMariaDb(); } @AfterClass public static void close() throws MusicServiceException, MusicQueryException, ManagedProcessException { //TODO: shutdown cassandra musicMixin=null; - db.stop(); - try { - EmbeddedCassandraServerHelper.cleanEmbeddedCassandra(); - } - catch(NullPointerException e){ - } + MdbcTestUtils.cleanDatabase(DBType.MySQL); + MdbcTestUtils.stopCassandra(); } private void dropTable() throws SQLException { @@ -144,25 +117,34 @@ public class OwnershipAndCheckpointTest { @Before public void initTest() throws SQLException { - session.execute("DROP KEYSPACE IF EXISTS "+keyspace); + MdbcTestUtils.getSession().execute("DROP KEYSPACE IF EXISTS "+MdbcTestUtils.getKeyspace()); try { Properties properties = new Properties(); +/* properties.setProperty(MusicMixin.KEY_MY_ID,mdbcServerName); properties.setProperty(MusicMixin.KEY_MUSIC_NAMESPACE,keyspace); properties.setProperty(MusicMixin.KEY_MUSIC_RFACTOR,"1"); //StateManager stateManager = new StateManager("dbUrl", properties, "serverName", "dbName"); ownAndCheck = new OwnershipAndCheckpoint(); musicMixin =new MusicMixin(stateManager, mdbcServerName,properties); +*/ + properties.setProperty(MusicMixin.KEY_MY_ID,MdbcTestUtils.getServerName()); + properties.setProperty(MusicMixin.KEY_MUSIC_NAMESPACE,MdbcTestUtils.getKeyspace()); + properties.setProperty(MusicMixin.KEY_MUSIC_RFACTOR,"1"); + properties.setProperty(MusicMixin.KEY_MUSIC_ADDRESS,MdbcTestUtils.getCassandraUrl()); + ownAndCheck = new OwnershipAndCheckpoint(); + musicMixin =new MusicMixin(stateManager, MdbcTestUtils.getServerName(), properties); } catch (MDBCServiceException e) { fail("error creating music musicMixin " + e.getMessage()); } - this.conn = DriverManager.getConnection("jdbc:mariadb://localhost:"+sqlPort+"/"+DATABASE, "root", ""); - this.mysqlMixin = new MySQLMixin(musicMixin, "localhost:"+sqlPort+"/"+DATABASE, conn, null); + this.conn = MdbcTestUtils.getConnection(DBType.MySQL); + Properties info = new Properties(); + this.mysqlMixin = new MySQLMixin(musicMixin, "localhost:"+MdbcTestUtils.getMariaDbPort()+"/"+DATABASE, conn, info); dropAndCreateTable(); } private void initDatabase(Range range) throws MDBCServiceException, SQLException { - final DatabasePartition partition = TestUtils.createBasicRow(range, musicMixin, mdbcServerName); + final DatabasePartition partition = TestUtils.createBasicRow(range, musicMixin, MdbcTestUtils.getServerName()); String sqlOperation = "INSERT INTO "+TABLE+" (PersonID,LastName,FirstName,Address,City) VALUES "+ "(1,'SAUREZ','ENRIQUE','GATECH','ATLANTA');"; StagingTable stagingTable = new StagingTable(); @@ -171,13 +153,15 @@ public class OwnershipAndCheckpointTest { executeStatement.execute(sqlOperation); this.conn.commit(); mysqlMixin.postStatementHook(sqlOperation,stagingTable); + mysqlMixin.preCommitHook(); executeStatement.close(); String id = MDBCUtils.generateUniqueKey().toString(); TxCommitProgress progressKeeper = new TxCommitProgress(); progressKeeper.createNewTransactionTracker(id ,this.conn); musicMixin.commitLog(partition, null, stagingTable, id, progressKeeper); try { - TestUtils.unlockRow(keyspace, mriTableName, partition); +// TestUtils.unlockRow(keyspace, mriTableName, partition); + TestUtils.unlockRow(MdbcTestUtils.getKeyspace(), MdbcTestUtils.getMriTableName(), partition); } catch(Exception e){ fail(e.getMessage()); |