aboutsummaryrefslogtreecommitdiffstats
path: root/mdbc-server/src/test/java/org
diff options
context:
space:
mode:
authorEnrique Saurez <enrique.saurez@gmail.com>2018-10-29 10:43:15 -0400
committerEnrique Saurez <enrique.saurez@gmail.com>2018-11-29 18:26:26 -0500
commit9cc93ae782739f4fd637fa8d30a986ce2e14ae3e (patch)
treeedacbf99b11797f90370c7957f0f7a86e2be1820 /mdbc-server/src/test/java/org
parent76d8bc46fdf9b36548dff46b9d1c91bf7c56f6ac (diff)
ownership and relinquish
Change-Id: I625bd61adfac11febdb25b179efbc6134a276f12 Issue-ID: MUSIC-219 Signed-off-by: Enrique Saurez <enrique.saurez@gmail.com>
Diffstat (limited to 'mdbc-server/src/test/java/org')
-rw-r--r--mdbc-server/src/test/java/org/onap/music/mdbc/MdbcConnectionTest.java16
-rwxr-xr-xmdbc-server/src/test/java/org/onap/music/mdbc/TestUtils.java116
-rw-r--r--mdbc-server/src/test/java/org/onap/music/mdbc/mixins/MusicMixinTest.java193
3 files changed, 322 insertions, 3 deletions
diff --git a/mdbc-server/src/test/java/org/onap/music/mdbc/MdbcConnectionTest.java b/mdbc-server/src/test/java/org/onap/music/mdbc/MdbcConnectionTest.java
new file mode 100644
index 0000000..776a06e
--- /dev/null
+++ b/mdbc-server/src/test/java/org/onap/music/mdbc/MdbcConnectionTest.java
@@ -0,0 +1,16 @@
+package org.onap.music.mdbc;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class MdbcConnectionTest {
+
+ @Test
+ public void own() {
+ }
+
+ @Test
+ public void relinquishIfRequired() {
+ }
+} \ No newline at end of file
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 3f8bd65..4d32b83 100755
--- 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,14 +19,124 @@
*/
package org.onap.music.mdbc;
+import com.datastax.driver.core.*;
+import com.datastax.driver.core.exceptions.QueryExecutionException;
+import com.datastax.driver.core.exceptions.SyntaxError;
+import org.onap.music.datastore.CassaDataStore;
+import org.onap.music.logging.EELFLoggerDelegate;
import org.onap.music.main.MusicUtil;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Properties;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.*;
+
+import static junit.framework.TestCase.assertNotNull;
+import static junit.framework.TestCase.fail;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
public class TestUtils {
+ private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(TestUtils.class);
+
+ public static void createKeyspace(String keyspace, Session session) {
+ String queryOp = "CREATE KEYSPACE " +
+ keyspace +
+ " WITH REPLICATION " +
+ "= {'class':'SimpleStrategy', 'replication_factor':1}; ";
+ ResultSet res=null;
+ try {
+ res = session.execute(queryOp);
+ }
+ catch(QueryExecutionException e){
+ fail("Failure executing creation of keyspace with error: " + e.getMessage());
+ } catch(SyntaxError e){
+ fail("Failure executing creation of keyspace with syntax error: " + e.getMessage());
+ }
+ assertTrue("Keyspace "+keyspace+" is already being used, please change it to avoid loosing data",res.wasApplied());
+ }
+
+ public static void deleteKeyspace(String keyspace, Session session){
+ String queryBuilder = "DROP KEYSPACE " +
+ keyspace +
+ ";";
+ ResultSet res = session.execute(queryBuilder);
+ assertTrue("Keyspace "+keyspace+" doesn't exist and it should",res.wasApplied());
+ }
+
+ 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){
+ HashMap<String, DataType> expectedTypes = new HashMap<>();
+ expectedTypes.put("rangeid",DataType.uuid());
+ expectedTypes.put("keys",DataType.set(DataType.text()));
+ ProtocolVersion currentVer = cluster.getConfiguration().getProtocolOptions().getProtocolVersion();
+ assertNotNull("Protocol version for cluster is invalid", currentVer);
+ CodecRegistry registry = cluster.getConfiguration().getCodecRegistry();
+ assertNotNull("Codec registry for cluster is invalid", registry);
+ 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 checkRowsInTable(String keyspace, String tableName, CassaDataStore ds,
+ HashSet<String> expectedColumns, HashMap<String,DataType> expectedTypes){
+ TableMetadata table = ds.returnColumnMetadata(keyspace,tableName);
+ assertNotNull("Error obtaining metadata of table, there may be an error with its creation", table);
+ List<ColumnMetadata> columnsMeta = table.getColumns();
+ checkDataTypeForTable(columnsMeta,expectedColumns,expectedTypes);
+ }
+
+ public static void checkDataTypeForTable(List<ColumnMetadata> columnsMeta, HashSet<String> expectedColumns,
+ HashMap<String,DataType> expectedTypes){
+ for(ColumnMetadata cMeta : columnsMeta){
+ String columnName = cMeta.getName();
+ DataType type = cMeta.getType();
+ assertTrue("Invalid column name: "+columnName,expectedColumns.contains(columnName));
+ assertTrue("Fix the contents of expectedtypes for column: "+columnName,
+ expectedTypes.containsKey(columnName));
+ assertEquals("Invalid type for column: "+columnName,
+ expectedTypes.get(columnName),type);
+ }
+ }
+
+ 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();
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
new file mode 100644
index 0000000..7e0e4c8
--- /dev/null
+++ b/mdbc-server/src/test/java/org/onap/music/mdbc/mixins/MusicMixinTest.java
@@ -0,0 +1,193 @@
+package org.onap.music.mdbc.mixins;
+
+import static org.junit.Assert.*;
+
+import com.datastax.driver.core.Cluster;
+import com.datastax.driver.core.Session;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+import java.util.UUID;
+import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
+
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onap.music.datastore.CassaDataStore;
+import org.onap.music.datastore.MusicLockState;
+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.main.MusicCore;
+import org.onap.music.mdbc.DatabasePartition;
+import org.onap.music.mdbc.Range;
+import org.onap.music.mdbc.tables.MusicRangeInformationRow;
+import org.onap.music.mdbc.tables.MusicTxDigestId;
+
+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;
+
+ @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);
+ assertNotNull("Invalid configuration for cassandra", cluster);
+ session = cluster.connect();
+ assertNotNull("Invalid configuration for cassandra", session);
+ CassaDataStore store = new CassaDataStore(cluster, session);
+ assertNotNull("Invalid configuration for music", store);
+ MusicCore.mDstoreHandle = store;
+ try {
+ Properties properties = new Properties();
+ properties.setProperty(MusicMixin.KEY_MUSIC_NAMESPACE,keyspace);
+ properties.setProperty(MusicMixin.KEY_MY_ID,mdbcServerName);
+ mixin=new MusicMixin(mdbcServerName,properties);
+ } catch (MDBCServiceException e) {
+ fail("error creating music mixin");
+ }
+ }
+
+ @AfterClass
+ public static void close() throws MusicServiceException, MusicQueryException {
+ //TODO: shutdown cassandra
+ session.close();
+ cluster.close();
+ }
+
+ @Test
+ public void own() {
+ final UUID uuid = mixin.generateUniqueKey();
+ List<Range> ranges = new ArrayList<>();
+ ranges.add(new Range("table1"));
+ DatabasePartition dbPartition = new DatabasePartition(ranges,uuid,null);
+ MusicRangeInformationRow newRow = new MusicRangeInformationRow(dbPartition, new ArrayList<>(), "", mdbcServerName);
+ DatabasePartition partition=null;
+ try {
+ partition = mixin.createMusicRangeInformation(newRow);
+ } catch (MDBCServiceException e) {
+ fail("failure when creating new row");
+ }
+ String fullyQualifiedMriKey = keyspace+"."+ mriTableName+"."+partition.getMusicRangeInformationIndex().toString();
+ try {
+ MusicLockState musicLockState = MusicCore.voluntaryReleaseLock(fullyQualifiedMriKey, partition.getLockId());
+ } catch (MusicLockingException e) {
+ fail("failure when releasing lock");
+ }
+ DatabasePartition newPartition = new DatabasePartition();
+ try {
+ mixin.own(ranges,newPartition);
+ } catch (MDBCServiceException e) {
+ fail("failure when running own function");
+ }
+ }
+
+ @Test
+ public void own2() {
+ final UUID uuid = mixin.generateUniqueKey();
+ final UUID uuid2 = mixin.generateUniqueKey();
+ List<Range> ranges = new ArrayList<>();
+ List<Range> ranges2 = new ArrayList<>();
+ ranges.add(new Range("table1"));
+ ranges2.add(new Range("table2"));
+ DatabasePartition dbPartition = new DatabasePartition(ranges,uuid,null);
+ DatabasePartition dbPartition2 = new DatabasePartition(ranges2,uuid2,null);
+ MusicRangeInformationRow newRow = new MusicRangeInformationRow(dbPartition, new ArrayList<>(), "", mdbcServerName);
+ MusicRangeInformationRow newRow2 = new MusicRangeInformationRow(dbPartition2, new ArrayList<>(), "", mdbcServerName);
+ DatabasePartition partition=null;
+ DatabasePartition partition2=null;
+ try {
+ partition = mixin.createMusicRangeInformation(newRow);
+ partition2 = mixin.createMusicRangeInformation(newRow2);
+ } catch (MDBCServiceException e) {
+ fail("failure when creating new row");
+ }
+ String fullyQualifiedMriKey = keyspace+"."+ mriTableName+"."+partition.getMusicRangeInformationIndex().toString();
+ String fullyQualifiedMriKey2 = keyspace+"."+ mriTableName+"."+partition2.getMusicRangeInformationIndex().toString();
+ try {
+ MusicLockState musicLockState = MusicCore.voluntaryReleaseLock(fullyQualifiedMriKey, partition.getLockId());
+ MusicLockState musicLockState2 = MusicCore.voluntaryReleaseLock(fullyQualifiedMriKey2, partition2.getLockId());
+ } catch (MusicLockingException e) {
+ fail("failure when releasing lock");
+ }
+ DatabasePartition newPartition = new DatabasePartition();
+ MusicInterface.OwnershipReturn ownershipReturn=null;
+ try {
+ List<Range> ownRanges = new ArrayList<>();
+ ownRanges.add(new Range("table1"));
+ ownRanges.add(new Range("table2"));
+ ownershipReturn = mixin.own(ownRanges, newPartition);
+ } catch (MDBCServiceException e) {
+ fail("failure when running own function");
+ }
+ assertEquals(2,ownershipReturn.getOldIRangeds().size());
+ assertEquals(ownershipReturn.getOwnerId(),newPartition.getLockId());
+ assertTrue(ownershipReturn.getOldIRangeds().get(0).equals(partition.getMusicRangeInformationIndex())||
+ ownershipReturn.getOldIRangeds().get(1).equals(partition.getMusicRangeInformationIndex()));
+ assertTrue(ownershipReturn.getOldIRangeds().get(0).equals(partition2.getMusicRangeInformationIndex())||
+ ownershipReturn.getOldIRangeds().get(1).equals(partition2.getMusicRangeInformationIndex()));
+ String finalfullyQualifiedMriKey = keyspace+"."+ mriTableName+"."+newPartition.getMusicRangeInformationIndex().toString();
+ try {
+ List<String> lockQueue = MusicCore.getLockingServiceHandle().getLockQueue(keyspace, mriTableName,
+ newPartition.getMusicRangeInformationIndex().toString());
+ assertEquals(1,lockQueue.size());
+ assertEquals(lockQueue.get(0),newPartition.getLockId());
+ } catch (MusicServiceException|MusicQueryException|MusicLockingException e) {
+ fail("failure on getting queue");
+ }
+ MusicRangeInformationRow musicRangeInformation=null;
+ try {
+ musicRangeInformation= mixin.getMusicRangeInformation(newPartition.getMusicRangeInformationIndex());
+ } catch (MDBCServiceException e) {
+ fail("fail to retrieve row");
+ }
+ assertEquals(2,musicRangeInformation.getDBPartition().getSnapshot().size());
+ assertEquals(0,musicRangeInformation.getRedoLog().size());
+ assertEquals(newPartition.getLockId(),musicRangeInformation.getOwnerId());
+ assertEquals(mdbcServerName,musicRangeInformation.getMetricProcessId());
+ List<Range> snapshot = musicRangeInformation.getDBPartition().getSnapshot();
+ boolean containsTable1=false;
+ Range table1Range = new Range("table1");
+ for(Range r:snapshot){
+ if(r.overlaps(table1Range)){
+ containsTable1=true;
+ break;
+ }
+ }
+ assertTrue(containsTable1);
+ boolean containsTable2=false;
+ Range table2Range = new Range("table2");
+ for(Range r:snapshot){
+ if(r.overlaps(table2Range)){
+ containsTable2=true;
+ break;
+ }
+ }
+ assertTrue(containsTable2);
+ }
+
+ @Test
+ public void relinquish() {
+ }
+
+ @Test
+ public void relinquishIfRequired() {
+ }
+} \ No newline at end of file