MySQL JUnit test 16/73616/1
authorTschaen, Brendan <ctschaen@att.com>
Tue, 27 Nov 2018 03:42:22 +0000 (22:42 -0500)
committerTschaen, Brendan <ctschaen@att.com>
Tue, 27 Nov 2018 03:43:36 +0000 (22:43 -0500)
and code cleanup, renaming variable for clarity, updating stale interfaces

Change-Id: I766267c442b7b037b41fe9f2f33092a1c01669ca
Issue-ID: MUSIC-205
Signed-off-by: Tschaen, Brendan <ctschaen@att.com>
mdbc-server/src/main/java/org/onap/music/mdbc/MdbcServerLogic.java
mdbc-server/src/main/java/org/onap/music/mdbc/StateManager.java
mdbc-server/src/main/java/org/onap/music/mdbc/mixins/MixinFactory.java
mdbc-server/src/main/java/org/onap/music/mdbc/mixins/MusicInterface.java
mdbc-server/src/main/java/org/onap/music/mdbc/mixins/MusicMixin.java
mdbc-server/src/main/java/org/onap/music/mdbc/tables/MusicTxDigest.java
mdbc-server/src/test/java/org/onap/music/mdbc/MySQLMixinTest.java [new file with mode: 0644]
pom.xml

index 757b468..989b6e4 100755 (executable)
@@ -50,7 +50,6 @@ public class MdbcServerLogic extends JdbcMeta{
        private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MdbcServerLogic.class);
 
        StateManager manager;
-       String name;
 
        //TODO: Delete this properties after debugging
        private final Properties info;
@@ -58,8 +57,7 @@ public class MdbcServerLogic extends JdbcMeta{
 
        public MdbcServerLogic(String Url, Properties info, NodeConfiguration config) throws SQLException, MDBCServiceException {
                super(Url,info);
-               this.name = config.nodeName;
-               this.manager = new StateManager(Url,info,config.partition,"test"); //FIXME: db name should not be passed in ahead of time
+               this.manager = new StateManager(Url,info,config.partition,config.nodeName, "test"); //FIXME: db name should not be passed in ahead of time
                this.info = info;
         int concurrencyLevel = Integer.parseInt(
                 info.getProperty(ConnectionCacheSettings.CONCURRENCY_LEVEL.key(),
index 22ddee1..c13fe16 100755 (executable)
@@ -62,23 +62,27 @@ public class StateManager {
      */
     private TxCommitProgress transactionInfo;
     private Map<String,MdbcConnection> mdbcConnections;
-    private String sqlDatabase;
-    private String url;
+    private String sqlDBName;
+    private String sqlDBUrl;
     
     String musicmixin;
     String cassandraUrl;
     private Properties info;
     
+    /** Identifier for this server instance */
+    private String mdbcServerName;
+    
     @SuppressWarnings("unused")
        private DatabasePartition ranges;
 
-       public StateManager(String url, Properties info, DatabasePartition ranges, String sqlDatabase) throws MDBCServiceException {
-        this.sqlDatabase = sqlDatabase;
+       public StateManager(String sqlDBUrl, Properties info, DatabasePartition ranges, String mdbcServerName, String sqlDBName) throws MDBCServiceException {
+        this.sqlDBName = sqlDBName;
         this.ranges = ranges;
-        this.url = url;
+        this.sqlDBUrl = sqlDBUrl;
         this.info = info;
+        this.mdbcServerName = mdbcServerName;
         this.transactionInfo = new TxCommitProgress();
-        //\fixme this is not really used, delete!
+        //\fixme this might not be used, delete?
         try {
                        info.load(this.getClass().getClassLoader().getResourceAsStream("music.properties"));
                        info.putAll(MDBCUtils.getMdbcProperties());
@@ -98,20 +102,13 @@ public class StateManager {
     }
 
     /**
-     * Initialize the 
+     * Initialize the connections to music, set up any necessary tables in music
      * @param mixin
      * @param cassandraUrl
      * @throws MDBCServiceException
      */
     protected void initMusic() throws MDBCServiceException {
-        this.musicInterface = MixinFactory.createMusicInterface(musicmixin, cassandraUrl, info);
-        this.musicInterface.createKeyspace();
-        try {
-            this.musicInterface.initializeMetricDataStructures();
-        } catch (MDBCServiceException e) {
-            logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
-            throw(e);
-        }
+        this.musicInterface = MixinFactory.createMusicInterface(musicmixin, mdbcServerName, info);
         this.mdbcConnections = new HashMap<>();
     }
     
@@ -125,9 +122,9 @@ public class StateManager {
             return;
         }
         try {
-            Connection sqlConnection = DriverManager.getConnection(this.url, this.info);
+            Connection sqlConnection = DriverManager.getConnection(this.sqlDBUrl, this.info);
             StringBuilder sql = new StringBuilder("CREATE DATABASE IF NOT EXISTS ")
-                    .append(sqlDatabase)
+                    .append(sqlDBName)
                     .append(";");
             Statement stmt = sqlConnection.createStatement();
             stmt.execute(sql.toString());
@@ -186,14 +183,14 @@ public class StateManager {
                return;
            }
            try {
-               sqlConnection = DriverManager.getConnection(this.url+"/"+this.sqlDatabase, this.info);
+               sqlConnection = DriverManager.getConnection(this.sqlDBUrl+"/"+this.sqlDBName, this.info);
            } catch (SQLException e) {
                logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.QUERYERROR, ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
                sqlConnection = null;
            }
            //Create MDBC connection
            try {
-               newConnection = new MdbcConnection(id, this.url+"/"+this.sqlDatabase, sqlConnection, info, this.musicInterface, transactionInfo,ranges);
+               newConnection = new MdbcConnection(id, this.sqlDBUrl+"/"+this.sqlDBName, sqlConnection, info, this.musicInterface, transactionInfo,ranges);
            } catch (MDBCServiceException e) {
                logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
                newConnection = null;
@@ -235,7 +232,7 @@ public class StateManager {
 
         //Create connection to local SQL DB
                try {
-                       sqlConnection = DriverManager.getConnection(this.url+"/"+this.sqlDatabase, this.info);
+                       sqlConnection = DriverManager.getConnection(this.sqlDBUrl+"/"+this.sqlDBName, this.info);
                } catch (SQLException e) {
                    logger.error("sql connection was not created correctly");
                        logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.QUERYERROR, ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
@@ -243,7 +240,7 @@ public class StateManager {
                }
                //Create MDBC connection
        try {
-                       newConnection = new MdbcConnection(id,this.url+"/"+this.sqlDatabase, sqlConnection, info, this.musicInterface, transactionInfo,ranges);
+                       newConnection = new MdbcConnection(id,this.sqlDBUrl+"/"+this.sqlDBName, sqlConnection, info, this.musicInterface, transactionInfo,ranges);
                } catch (MDBCServiceException e) {
                        logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
                        newConnection = null;
index 0f05734..1edb38d 100755 (executable)
@@ -74,13 +74,11 @@ public class MixinFactory {
         * Look for a class in CLASSPATH that implements the {@link MusicInterface} interface, and has the mixin name <i>name</i>.
         * If one is found, construct and return it, using the other arguments for the constructor.
         * @param name the name of the Mixin
-        * @param msm the MusicSqlManager to use as an argument to the constructor
-        * @param dbi the DBInterface to use as an argument to the constructor
-        * @param url the URL to use as an argument to the constructor
+        * @param mdbcServerName the name of this mdbcServer instance
         * @param info the Properties to use as an argument to the constructor
         * @return the newly constructed MusicInterface, or null if one cannot be found.
         */
-       public static MusicInterface createMusicInterface(String name, String url, Properties info) {
+       public static MusicInterface createMusicInterface(String name, String mdbcServerName, Properties info) {
                for (Class<?> cl : Utils.getClassesImplementing(MusicInterface.class)) {
                        try {
                                Constructor<?> con = cl.getConstructor();
@@ -92,7 +90,7 @@ public class MixinFactory {
                                                con = cl.getConstructor(String.class, Properties.class);
                                                if (con != null) {
                                                        logger.info(EELFLoggerDelegate.applicationLogger,"Found match: "+miname);
-                                                       return (MusicInterface) con.newInstance(url, info);
+                                                       return (MusicInterface) con.newInstance(mdbcServerName, info);
                                                }
                                        }
                                }
index 5b10a73..f4231d8 100755 (executable)
@@ -43,11 +43,6 @@ import org.onap.music.mdbc.tables.TxCommitProgress;
  * @author Robert P. Eby
  */
 public interface MusicInterface {      
-       /**
-        * This function is used to created all the required data structures, both local  
-        * \TODO Check if this function is required in the MUSIC interface or could be just created on the constructor
-        */
-       void initializeMetricDataStructures() throws MDBCServiceException;
        /**
         * Get the name of this MusicInterface mixin object.
         * @return the name
index 258ea4f..8ffb6af 100755 (executable)
@@ -155,7 +155,7 @@ public class MusicMixin implements MusicInterface {
                this.allReplicaIds  = null;
     }
 
-       public MusicMixin(String url, Properties info) throws MDBCServiceException {
+       public MusicMixin(String mdbcServerName, Properties info) throws MDBCServiceException {
                // Default values -- should be overridden in the Properties
                // Default to using the host_ids of the various peers as the replica IDs (this is probably preferred)
                this.musicAddress   = info.getProperty(KEY_MUSIC_ADDRESS, DEFAULT_MUSIC_ADDRESS);
@@ -173,7 +173,8 @@ public class MusicMixin implements MusicInterface {
 
                this.music_ns       = info.getProperty(KEY_MUSIC_NAMESPACE,DEFAULT_MUSIC_NAMESPACE);
                logger.info(EELFLoggerDelegate.applicationLogger,"MusicSqlManager: music_ns="+music_ns);
-        createKeyspace();
+               
+               initializeMetricTables();
     }
 
        /**
@@ -234,8 +235,12 @@ public class MusicMixin implements MusicInterface {
                        musicSession = null;
                }
        }
-       @Override
-       public void initializeMetricDataStructures() throws MDBCServiceException {
+       
+       /**
+        * This function is used to created all the required data structures, both local  
+        */
+       private void initializeMetricTables() throws MDBCServiceException {
+               createKeyspace();
            try {
             createMusicTxDigest();//\TODO If we start partitioning the data base, we would need to use the redotable number
                        createMusicRangeInformationTable();
index 210cb9e..7057172 100644 (file)
@@ -95,9 +95,9 @@ public class MusicTxDigest {
        
        /**
         * Replay the digest for a given partition
-        * @param mi
-        * @param partitionId
-        * @param dbi
+        * @param mi music interface
+        * @param partitionId the partition to be replayed
+        * @param dbi interface to the database that will replay the operations
         * @throws MDBCServiceException
         */
        public void replayDigestForPartition(MusicInterface mi, UUID partitionId, DBInterface dbi) throws MDBCServiceException {
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
new file mode 100644 (file)
index 0000000..458f70f
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * ============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 static org.junit.Assert.*;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onap.music.mdbc.mixins.MySQLMixin;
+
+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" +
+            "    PersonID int,\n" +
+            "    LastName varchar(255),\n" +
+            "    FirstName varchar(255),\n" +
+            "    Address varchar(255),\n" +
+            "    City varchar(255),\n" +
+            "    PRIMARY KEY (PersonID,LastName)" +
+            ");";
+       
+       
+       Connection conn;
+       MySQLMixin mysqlMixin;
+       
+       
+       
+       @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);
+       }
+       
+       @AfterClass
+       public static void close() throws Exception {
+               
+       }
+       
+       @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);
+       }
+       
+       @Test
+       public void testGetDataBaseName() throws SQLException {
+               assertEquals(DATABASE, mysqlMixin.getDatabaseName());
+       }
+
+}
diff --git a/pom.xml b/pom.xml
index d2f7477..51dbd87 100755 (executable)
--- a/pom.xml
+++ b/pom.xml
                 </exclusion>
             </exclusions> -->
         </dependency>
+        <dependency>
+            <groupId>ch.vorburger.mariaDB4j</groupId>
+            <artifactId>mariaDB4j</artifactId>
+            <version>2.2.3</version>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>com.github.jsqlparser</groupId>
             <artifactId>jsqlparser</artifactId>