AbstractJDBCImporterTest.java 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

18
package org.apache.shardingsphere.scaling.core.execute.executor.importer;
19

20
import com.google.common.collect.Lists;
21 22
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
A
avalon5666 已提交
23
import org.apache.shardingsphere.scaling.core.config.ImporterConfiguration;
24
import org.apache.shardingsphere.scaling.core.config.ScalingDataSourceConfiguration;
25 26 27 28 29 30
import org.apache.shardingsphere.scaling.core.datasource.DataSourceManager;
import org.apache.shardingsphere.scaling.core.execute.executor.channel.Channel;
import org.apache.shardingsphere.scaling.core.execute.executor.record.Column;
import org.apache.shardingsphere.scaling.core.execute.executor.record.DataRecord;
import org.apache.shardingsphere.scaling.core.execute.executor.record.FinishedRecord;
import org.apache.shardingsphere.scaling.core.execute.executor.record.Record;
31
import org.apache.shardingsphere.scaling.core.job.position.NopPosition;
32 33 34 35 36
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
37 38

import javax.sql.DataSource;
39 40 41 42 43
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
44 45
import java.util.Map;
import java.util.Set;
46 47 48 49 50

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
51
public final class AbstractJDBCImporterTest {
52 53 54 55 56
    
    private static final String TABLE_NAME = "test_table";
    
    private static final String INSERT_SQL = "INSERT INTO test_table (id,user,status) VALUES(?,?,?)";
    
57
    private static final String DELETE_SQL = "DELETE FROM test_table WHERE id = ? and user = ?";
58
    
59
    private static final String UPDATE_SQL = "UPDATE test_table SET user = ?,status = ? WHERE id = ? and user = ?";
60 61 62 63 64
    
    @Mock
    private DataSourceManager dataSourceManager;
    
    @Mock
65
    private AbstractSQLBuilder sqlBuilder;
66 67
    
    @Mock
68
    private ScalingDataSourceConfiguration dataSourceConfig;
69 70 71 72 73 74 75 76 77 78 79 80 81
    
    @Mock
    private Channel channel;
    
    @Mock
    private DataSource dataSource;
    
    @Mock
    private Connection connection;
    
    @Mock
    private PreparedStatement preparedStatement;
    
82
    private AbstractJDBCImporter jdbcImporter;
83 84
    
    @Before
L
Liang Zhang 已提交
85
    public void setUp() throws SQLException {
A
avalon5666 已提交
86
        jdbcImporter = new AbstractJDBCImporter(getImporterConfiguration(), dataSourceManager) {
87 88
            
            @Override
89
            protected AbstractSQLBuilder createSQLBuilder(final Map<String, Set<String>> shardingColumnsMap) {
90 91 92
                return sqlBuilder;
            }
        };
93
        jdbcImporter.setChannel(channel);
94
        when(dataSourceManager.getDataSource(dataSourceConfig)).thenReturn(dataSource);
95 96 97 98
        when(dataSource.getConnection()).thenReturn(connection);
    }
    
    @Test
99
    public void assertInsertDataRecord() throws SQLException {
L
Liang Zhang 已提交
100
        DataRecord insertRecord = getDataRecord("INSERT");
101
        when(sqlBuilder.buildInsertSQL(insertRecord)).thenReturn(new PreparedSQL(INSERT_SQL, Lists.newArrayList(0, 1, 2)));
102 103
        when(connection.prepareStatement(INSERT_SQL)).thenReturn(preparedStatement);
        when(channel.fetchRecords(100, 3)).thenReturn(mockRecords(insertRecord));
104
        jdbcImporter.run();
105 106
        verify(preparedStatement).setObject(1, 1);
        verify(preparedStatement).setObject(2, 10);
L
Liang Zhang 已提交
107
        verify(preparedStatement).setObject(3, "INSERT");
108 109 110 111 112
        verify(preparedStatement).execute();
    }
    
    @Test
    public void assertDeleteDataRecord() throws SQLException {
L
Liang Zhang 已提交
113
        DataRecord deleteRecord = getDataRecord("DELETE");
114
        when(sqlBuilder.buildDeleteSQL(deleteRecord)).thenReturn(new PreparedSQL(DELETE_SQL, Lists.newArrayList(0, 1)));
115 116
        when(connection.prepareStatement(DELETE_SQL)).thenReturn(preparedStatement);
        when(channel.fetchRecords(100, 3)).thenReturn(mockRecords(deleteRecord));
117
        jdbcImporter.run();
118
        verify(preparedStatement).setObject(1, 1);
119
        verify(preparedStatement).setObject(2, 10);
120 121 122 123 124
        verify(preparedStatement).execute();
    }
    
    @Test
    public void assertUpdateDataRecord() throws SQLException {
L
Liang Zhang 已提交
125
        DataRecord updateRecord = getDataRecord("UPDATE");
126
        when(sqlBuilder.buildUpdateSQL(updateRecord)).thenReturn(new PreparedSQL(UPDATE_SQL, Lists.newArrayList(1, 2, 0, 1)));
127 128
        when(connection.prepareStatement(UPDATE_SQL)).thenReturn(preparedStatement);
        when(channel.fetchRecords(100, 3)).thenReturn(mockRecords(updateRecord));
129
        jdbcImporter.run();
130
        verify(preparedStatement).setObject(1, 10);
L
Liang Zhang 已提交
131
        verify(preparedStatement).setObject(2, "UPDATE");
132
        verify(preparedStatement).setObject(3, 1);
133
        verify(preparedStatement).setObject(4, 10);
134 135 136 137 138 139
        verify(preparedStatement).execute();
    }
    
    private List<Record> mockRecords(final DataRecord dataRecord) {
        List<Record> result = new LinkedList<>();
        result.add(dataRecord);
140
        result.add(new FinishedRecord(new NopPosition()));
141 142 143 144
        return result;
    }
    
    private DataRecord getDataRecord(final String recordType) {
145
        DataRecord result = new DataRecord(new NopPosition(), 3);
146 147 148 149 150 151 152 153
        result.setTableName(TABLE_NAME);
        result.setType(recordType);
        result.addColumn(new Column("id", 1, false, true));
        result.addColumn(new Column("user", 10, true, false));
        result.addColumn(new Column("status", recordType, true, false));
        return result;
    }
    
A
avalon5666 已提交
154 155
    private ImporterConfiguration getImporterConfiguration() {
        ImporterConfiguration result = new ImporterConfiguration();
156
        result.setDataSourceConfiguration(dataSourceConfig);
157 158 159
        Map<String, Set<String>> shardingColumnsMap = Maps.newHashMap();
        shardingColumnsMap.put("test_table", Sets.newHashSet("user"));
        result.setShardingColumnsMap(shardingColumnsMap);
160 161 162
        return result;
    }
}