forked from apache/incubator-seata
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MySQLInsertOrUpdateExecutorTest.java
279 lines (250 loc) · 11.3 KB
/
MySQLInsertOrUpdateExecutorTest.java
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright 1999-2019 Seata.io Group.
*
* 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.
*/
package io.seata.rm.datasource.exec;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Collections;
import com.google.common.collect.Lists;
import io.seata.rm.datasource.ConnectionProxy;
import io.seata.rm.datasource.PreparedStatementProxy;
import io.seata.rm.datasource.StatementProxy;
import io.seata.rm.datasource.exec.mysql.MySQLInsertOrUpdateExecutor;
import io.seata.rm.datasource.sql.struct.ColumnMeta;
import io.seata.rm.datasource.sql.struct.IndexMeta;
import io.seata.rm.datasource.sql.struct.IndexType;
import io.seata.rm.datasource.sql.struct.TableMeta;
import io.seata.rm.datasource.sql.struct.TableRecords;
import io.seata.sqlparser.SQLInsertRecognizer;
import io.seata.sqlparser.util.JdbcConstants;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author: yangyicong
*/
public class MySQLInsertOrUpdateExecutorTest {
private static final String ID_COLUMN = "id";
private static final String USER_ID_COLUMN = "user_id";
private static final String USER_NAME_COLUMN = "user_name";
private static final String USER_STATUS_COLUMN = "user_status";
private static final Integer PK_VALUE = 100;
private StatementProxy statementProxy;
private SQLInsertRecognizer sqlInsertRecognizer;
private TableMeta tableMeta;
private MySQLInsertOrUpdateExecutor insertOrUpdateExecutor;
private final int pkIndex = 0;
private HashMap<String,Integer> pkIndexMap;
@BeforeEach
public void init() {
ConnectionProxy connectionProxy = mock(ConnectionProxy.class);
when(connectionProxy.getDbType()).thenReturn(JdbcConstants.MYSQL);
statementProxy = mock(PreparedStatementProxy.class);
when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy);
StatementCallback statementCallback = mock(StatementCallback.class);
sqlInsertRecognizer = mock(SQLInsertRecognizer.class);
tableMeta = mock(TableMeta.class);
insertOrUpdateExecutor = Mockito.spy(new MySQLInsertOrUpdateExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
pkIndexMap = new HashMap<String,Integer>(){
{
put(ID_COLUMN, pkIndex);
}
};
}
@Test
public void TestBuildImageParameters(){
mockParameters();
List<String> insertParamsList = new ArrayList<>();
insertParamsList.add("?,?,?,?");
insertParamsList.add("?,?,?,?");
when(sqlInsertRecognizer.getInsertParamsValue()).thenReturn(insertParamsList);
mockInsertColumns();
Map<String, ArrayList<Object>> imageParameterMap = insertOrUpdateExecutor.buildImageParameters(sqlInsertRecognizer);
Assertions.assertEquals(imageParameterMap.toString(),mockImageParameterMap().toString());
}
@Test
public void TestBuildImageParameters_contain_constant(){
mockImageParameterMap_contain_constant();
List<String> insertParamsList = new ArrayList<>();
insertParamsList.add("?,?,?,userStatus1");
insertParamsList.add("?,?,?,userStatus2");
when(sqlInsertRecognizer.getInsertParamsValue()).thenReturn(insertParamsList);
mockInsertColumns();
Map<String, ArrayList<Object>> imageParameterMap = insertOrUpdateExecutor.buildImageParameters(sqlInsertRecognizer);
Assertions.assertEquals(imageParameterMap.toString(),mockImageParameterMap().toString());
}
@Test
public void testBuildImageSQL(){
String selectSQLStr = "SELECT * FROM null WHERE (user_id = ? ) OR (id = ? ) OR (user_id = ? ) OR (id = ? ) ";
String paramAppenderListStr = "[[userId1, 100], [userId2, 101]]";
mockImageParameterMap_contain_constant();
List<String> insertParamsList = new ArrayList<>();
insertParamsList.add("?,?,?,userStatus1");
insertParamsList.add("?,?,?,userStatus2");
when(sqlInsertRecognizer.getInsertParamsValue()).thenReturn(insertParamsList);
mockInsertColumns();
mockAllIndexes();
String selectSQL = insertOrUpdateExecutor.buildImageSQL(tableMeta);
Assertions.assertEquals(selectSQLStr,selectSQL);
Assertions.assertEquals(paramAppenderListStr,insertOrUpdateExecutor.getParamAppenderList().toString());
}
@Test
public void testBeforeImage(){
mockImageParameterMap_contain_constant();
List<String> insertParamsList = new ArrayList<>();
insertParamsList.add("?,?,?,userStatus1");
insertParamsList.add("?,?,?,userStatus2");
when(sqlInsertRecognizer.getInsertParamsValue()).thenReturn(insertParamsList);
mockInsertColumns();
mockAllIndexes();
doReturn(tableMeta).when(insertOrUpdateExecutor).getTableMeta();
try {
TableRecords tableRecords = new TableRecords();
String selectSQL = insertOrUpdateExecutor.buildImageSQL(tableMeta);
ArrayList<List<Object>> paramAppenderList = insertOrUpdateExecutor.getParamAppenderList();
doReturn(tableRecords).when(insertOrUpdateExecutor).buildTableRecords2(tableMeta,selectSQL,paramAppenderList, Collections.emptyList());
TableRecords tableRecordsResult = insertOrUpdateExecutor.beforeImage();
Assertions.assertEquals(tableRecords,tableRecordsResult);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
private void mockAllIndexes(){
Map<String, IndexMeta> allIndex = new HashMap<>();
IndexMeta primary = new IndexMeta();
primary.setIndextype(IndexType.PRIMARY);
ColumnMeta columnMeta = new ColumnMeta();
columnMeta.setColumnName("id");
primary.setValues(Lists.newArrayList(columnMeta));
allIndex.put("id", primary);
IndexMeta unique = new IndexMeta();
unique.setIndextype(IndexType.PRIMARY);
ColumnMeta columnMetaUnique = new ColumnMeta();
columnMetaUnique.setColumnName("user_id");
unique.setValues(Lists.newArrayList(columnMetaUnique));
allIndex.put("user_id", unique);
when(tableMeta.getAllIndexes()).thenReturn(allIndex);
}
private List<String> mockInsertColumns() {
List<String> columns = new ArrayList<>();
columns.add(ID_COLUMN);
columns.add(USER_ID_COLUMN);
columns.add(USER_NAME_COLUMN);
columns.add(USER_STATUS_COLUMN);
when(sqlInsertRecognizer.getInsertColumns()).thenReturn(columns);
return columns;
}
/**
* all insert params is variable
* {1=[100], 2=[userId1], 3=[userName1], 4=[userStatus1], 5=[101], 6=[userId2], 7=[userName2], 8=[userStatus2]}
*/
private void mockParameters() {
Map<Integer,ArrayList<Object>> paramters = new HashMap<>(4);
ArrayList arrayList10 = new ArrayList<>();
arrayList10.add(PK_VALUE);
ArrayList arrayList11 = new ArrayList<>();
arrayList11.add("userId1");
ArrayList arrayList12 = new ArrayList<>();
arrayList12.add("userName1");
ArrayList arrayList13 = new ArrayList<>();
arrayList13.add("userStatus1");
paramters.put(1, arrayList10);
paramters.put(2, arrayList11);
paramters.put(3, arrayList12);
paramters.put(4, arrayList13);
ArrayList arrayList20 = new ArrayList<>();
arrayList20.add(PK_VALUE+1);
ArrayList arrayList21 = new ArrayList<>();
arrayList21.add("userId2");
ArrayList arrayList22 = new ArrayList<>();
arrayList22.add("userName2");
ArrayList arrayList23 = new ArrayList<>();
arrayList23.add("userStatus2");
paramters.put(5, arrayList20);
paramters.put(6, arrayList21);
paramters.put(7, arrayList22);
paramters.put(8, arrayList23);
PreparedStatementProxy psp = (PreparedStatementProxy) this.statementProxy;
when(psp.getParameters()).thenReturn(paramters);
}
/**
* exist insert parms is constant
* {1=[100], 2=[userId1], 3=[userName1], 4=[101], 5=[userId2], 6=[userName2]}
*/
private void mockImageParameterMap_contain_constant() {
Map<Integer,ArrayList<Object>> paramters = new HashMap<>(4);
ArrayList arrayList10 = new ArrayList<>();
arrayList10.add(PK_VALUE);
ArrayList arrayList11 = new ArrayList<>();
arrayList11.add("userId1");
ArrayList arrayList12 = new ArrayList<>();
arrayList12.add("userName1");
paramters.put(1, arrayList10);
paramters.put(2, arrayList11);
paramters.put(3, arrayList12);
ArrayList arrayList20 = new ArrayList<>();
arrayList20.add(PK_VALUE+1);
ArrayList arrayList21 = new ArrayList<>();
arrayList21.add("userId2");
ArrayList arrayList22 = new ArrayList<>();
arrayList22.add("userName2");
paramters.put(4, arrayList20);
paramters.put(5, arrayList21);
paramters.put(6, arrayList22);
PreparedStatementProxy psp = (PreparedStatementProxy) this.statementProxy;
when(psp.getParameters()).thenReturn(paramters);
}
private Map<String, ArrayList<Object>> mockImageParameterMap(){
Map<String, ArrayList<Object>> imageParameterMap = new HashMap<>();
ArrayList<Object> idList = new ArrayList<>();
idList.add("100");
idList.add("101");
imageParameterMap.put("id",idList);
ArrayList<Object> user_idList = new ArrayList<>();
user_idList.add("userId1");
user_idList.add("userId2");
imageParameterMap.put("user_id",user_idList);
ArrayList<Object> user_nameList = new ArrayList<>();
user_nameList.add("userName1");
user_nameList.add("userName2");
imageParameterMap.put("user_name",user_nameList);
ArrayList<Object> user_statusList = new ArrayList<>();
user_statusList.add("userStatus1");
user_statusList.add("userStatus2");
imageParameterMap.put("user_status",user_statusList);
return imageParameterMap;
}
private void mockParametersOfOnePk() {
Map<Integer,ArrayList<Object>> paramters = new HashMap<>(4);
ArrayList arrayList1 = new ArrayList<>();
arrayList1.add(PK_VALUE);
paramters.put(1, arrayList1);
PreparedStatementProxy psp = (PreparedStatementProxy) this.statementProxy;
when(psp.getParameters()).thenReturn(paramters);
}
private void mockInsertRows() {
List<List<Object>> rows = new ArrayList<>();
rows.add(Arrays.asList("?", "?", "?", "?"));
when(sqlInsertRecognizer.getInsertRows(pkIndexMap.values())).thenReturn(rows);
}
}