aboutsummaryrefslogtreecommitdiffstats
path: root/mdbc-server/src/test/java/org/onap/music/mdbc/mixins/MySQLMixinTest.java
blob: cf233058a2b727cde939f9075dc8810e4ea1d879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * ============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.mixins;

import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.onap.music.mdbc.MDBCUtils;
import org.onap.music.mdbc.MdbcTestUtils;
import org.onap.music.mdbc.MdbcTestUtils.DBType;
import org.onap.music.mdbc.Range;
import org.onap.music.mdbc.mixins.MySQLMixin;
import org.onap.music.mdbc.tables.MriReference;
import org.onap.music.mdbc.tables.MusicTxDigestId;
import ch.vorburger.mariadb4j.DB;

public class MySQLMixinTest {

	public static final String DATABASE = "mdbctest";
	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" +
            "    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");
		MdbcTestUtils.startMariaDb();

	}
	
	@AfterClass
	public static void close() throws Exception {
		MdbcTestUtils.stopDatabase(DBType.MySQL);
	}
	
	@Before
	public void beforeTest() throws SQLException {
		this.conn = MdbcTestUtils.getConnection(DBType.MySQL);
		Properties info = new Properties();
		this.mysqlMixin = new MySQLMixin(null, null, conn, info);
	}
	

    @After
    public void afterEachTest() throws SQLException {
        clearTables();
    }
	
    @Test
    public void testGetDataBaseName() throws SQLException {
        assertEquals(MdbcTestUtils.getMariaDBDBName(), mysqlMixin.getDatabaseName());
    }
    
    @Test
    public void testGetTableSet() throws SQLException {
        Set<Range> rangesAdded = new HashSet<>();
        rangesAdded.add(new Range("TEST.RANGER"));
        rangesAdded.add(new Range("TEST.RANGES"));
        Statement st = conn.createStatement();
        for (Range r: rangesAdded) {
            st.execute("CREATE TABLE " + r + " (name VARCHAR(20));");
        }
        st.close();
        Set<Range> ranges = mysqlMixin.getSQLRangeSet();

        assertTrue(ranges.containsAll(rangesAdded));
        assertTrue(rangesAdded.containsAll(ranges));
    }
    
    @Test
    public void testCkpt() throws SQLException {
        createTables();

        Range r1 = new Range(MdbcTestUtils.mariaDBDatabaseName + ".RANGER");
        MriReference mri1 = new MriReference(MDBCUtils.generateUniqueKey());
        MusicTxDigestId i1 = new MusicTxDigestId(mri1.index, MDBCUtils.generateUniqueKey(), 1);
        Pair<MriReference, MusicTxDigestId> digestId = Pair.of(mri1, i1);
        mysqlMixin.updateCheckpointLocations(r1, digestId);
        
        Range r2 = new Range(MdbcTestUtils.mariaDBDatabaseName + ".RANGES");
        MriReference mri2 = new MriReference(MDBCUtils.generateUniqueKey());
        MusicTxDigestId i2 = new MusicTxDigestId(mri2.index, MDBCUtils.generateUniqueKey(), 2);
        Pair<MriReference, MusicTxDigestId> p2 = Pair.of(mri2, i2);
        mysqlMixin.updateCheckpointLocations(r2, p2);
        
        Map<Range, Pair<MriReference, MusicTxDigestId>> ckptmap = mysqlMixin.getCheckpointLocations();
        assertTrue(ckptmap.containsKey(r1));
        assertEquals(mri1.getIndex(), ckptmap.get(r1).getLeft().getIndex());
        assertEquals(i1.transactionId, ckptmap.get(r1).getRight().transactionId);
        
        assertTrue(ckptmap.containsKey(r2));
        assertEquals(mri2.getIndex(), ckptmap.get(r2).getLeft().getIndex());
        assertEquals(i2.transactionId, ckptmap.get(r2).getRight().transactionId);
    }

    private void createTables() throws SQLException {
        Statement st = conn.createStatement();
        st.execute("CREATE TABLE RANGER (name VARCHAR(20));");
        st.execute("CREATE TABLE RANGES (name VARCHAR(20));");
        st.close();
        //need to re-initiate the tables
        this.mysqlMixin.initTables();
    }

    
    private void clearTables() throws SQLException {
        Set<Range> ranges = mysqlMixin.getSQLRangeSet();
        Statement st = conn.createStatement();
        for (Range r: ranges) {
            try {
                st.execute("DROP TABLE " + r + ";");
            } catch (SQLException e) {
                System.out.println("Trouble dropping: " + r);
                e.printStackTrace();
            }
        }
        st.close();
    }

}