@@ -3,7 +3,6 @@ package io.elastic.jdbc.actions
3
3
import io.elastic.api.EventEmitter
4
4
import io.elastic.api.ExecutionParameters
5
5
import io.elastic.api.Message
6
- import io.elastic.jdbc.actions.SelectAction
7
6
import spock.lang.Ignore
8
7
import spock.lang.Shared
9
8
import spock.lang.Specification
@@ -12,7 +11,6 @@ import javax.json.Json
12
11
import javax.json.JsonObject
13
12
import java.sql.Connection
14
13
import java.sql.DriverManager
15
- import java.sql.ResultSet
16
14
17
15
@Ignore
18
16
class SelectMySQLSpec extends Specification {
@@ -27,6 +25,8 @@ class SelectMySQLSpec extends Specification {
27
25
def databaseName = System . getenv(" CONN_DBNAME_MYSQL" )
28
26
@Shared
29
27
def host = System . getenv(" CONN_HOST_MYSQL" )
28
+ @Shared
29
+ def port = System . getenv(" CONN_PORT_MYSQL" )
30
30
31
31
@Shared
32
32
Connection connection
@@ -38,6 +38,8 @@ class SelectMySQLSpec extends Specification {
38
38
@Shared
39
39
EventEmitter.Callback dataCallback
40
40
@Shared
41
+ EventEmitter.Callback onHttpReplyCallback
42
+ @Shared
41
43
EventEmitter.Callback reboundCallback
42
44
@Shared
43
45
EventEmitter emitter
@@ -53,226 +55,62 @@ class SelectMySQLSpec extends Specification {
53
55
}
54
56
55
57
def createAction () {
56
- errorCallback = Mock (EventEmitter.Callback )
57
- snapshotCallback = Mock (EventEmitter.Callback )
58
- dataCallback = Mock (EventEmitter.Callback )
59
- reboundCallback = Mock (EventEmitter.Callback )
60
- emitter = new EventEmitter.Builder (). onData(dataCallback). onSnapshot(snapshotCallback). onError(errorCallback). onRebound(reboundCallback). build()
61
- action = new SelectAction (emitter)
58
+ action = new SelectAction ()
62
59
}
63
60
64
61
def runAction (JsonObject config , JsonObject body , JsonObject snapshot ) {
65
62
Message msg = new Message.Builder (). body(body). build()
66
- ExecutionParameters params = new ExecutionParameters (msg, config, snapshot)
63
+ errorCallback = Mock (EventEmitter.Callback )
64
+ snapshotCallback = Mock (EventEmitter.Callback )
65
+ dataCallback = Mock (EventEmitter.Callback )
66
+ reboundCallback = Mock (EventEmitter.Callback )
67
+ onHttpReplyCallback = Mock (EventEmitter.Callback )
68
+ emitter = new EventEmitter.Builder ()
69
+ .onData(dataCallback)
70
+ .onSnapshot(snapshotCallback)
71
+ .onError(errorCallback)
72
+ .onRebound(reboundCallback)
73
+ .onHttpReplyCallback(onHttpReplyCallback). build()
74
+ ExecutionParameters params = new ExecutionParameters (msg, emitter, config, snapshot)
67
75
action. execute(params);
68
76
}
69
77
70
78
def getStarsConfig () {
71
- JsonObject config = Json . createObjectBuilder(). build();
72
-
73
- config . addProperty( " idColumn " , " id " )
74
- config . addProperty( " tableName " , " stars " )
75
- config . addProperty( " user " , user )
76
- config . addProperty( " password " , password )
77
- config . addProperty( " dbEngine " , " mssql " )
78
- config . addProperty( " host " , host )
79
- config . addProperty( " databaseName " , databaseName )
79
+ JsonObject config = Json . createObjectBuilder()
80
+ .add( " sqlQuery " , " SELECT * from stars where @id:number =id AND name=@name " )
81
+ .add( " user " , user )
82
+ .add( " password " , password )
83
+ .add( " dbEngine " , " mysql " )
84
+ .add( " host " , host )
85
+ .add( " port " , port )
86
+ .add( " databaseName " , databaseName )
87
+ .build( )
80
88
return config;
81
89
}
82
-
83
90
def prepareStarsTable () {
84
- String sql = " DROP TABLE IF EXISTS stars; "
91
+ String sql = " DROP TABLE IF EXISTS stars"
85
92
connection. createStatement(). execute(sql);
86
93
connection. createStatement(). execute(" CREATE TABLE stars (id int, name varchar(255) NOT NULL, date datetime, radius int, destination int)" );
87
- }
88
-
89
- def getRecords (tableName ) {
90
- ArrayList<String > records = new ArrayList<String > ();
91
- String sql = " SELECT * FROM " + tableName;
92
- ResultSet rs = connection. createStatement(). executeQuery(sql);
93
- while (rs. next()) {
94
- records. add(rs. toRowResult(). toString());
95
- }
96
- rs. close();
97
- return records;
94
+ connection. createStatement(). execute(" INSERT INTO stars (id, name) VALUES (1,'Hello')" );
98
95
}
99
96
100
97
def cleanupSpec () {
101
- String sql = " DROP TABLE IF EXISTS persons;"
102
-
103
- connection. createStatement(). execute(sql)
104
- sql = " DROP TABLE IF EXISTS stars;"
98
+ String sql = " DROP TABLE IF EXISTS stars"
105
99
connection. createStatement(). execute(sql)
106
100
connection. close()
107
101
}
108
102
109
- def " one insert" () {
110
-
103
+ def " one select" () {
111
104
prepareStarsTable();
112
-
113
105
JsonObject snapshot = Json . createObjectBuilder(). build();
114
-
115
- JsonObject body = Json . createObjectBuilder(). build();
116
- body. addProperty(" id" , " 1" )
117
- body. addProperty(" name" , " Taurus" )
118
- body. addProperty(" date" , " 2015-02-19 10:10:10.0" )
119
- body. addProperty(" radius" , " 123" )
120
-
106
+ JsonObject body = Json . createObjectBuilder()
107
+ .add(" id" , 1 )
108
+ .add(" name" , " Hello" )
109
+ .build()
110
+ when :
121
111
runAction(getStarsConfig(), body, snapshot)
122
-
123
- ArrayList<String > records = getRecords(" stars" )
124
-
125
- expect :
126
- records. size() == 1
127
- records. get(0 ) == ' {id=1, name=Taurus, date=2015-02-19 10:10:10.0, radius=123, destination=null}'
128
- }
129
-
130
- def " one insert, incorrect value: string in integer field" () {
131
-
132
- prepareStarsTable();
133
-
134
- JsonObject snapshot = Json . createObjectBuilder(). build();
135
-
136
- JsonObject body = Json . createObjectBuilder(). build();
137
- body. addProperty(" id" , " 1" )
138
- body. addProperty(" name" , " Taurus" )
139
- body. addProperty(" radius" , " test" )
140
-
141
- String exceptionClass = " " ;
142
-
143
- try {
144
- runAction(getStarsConfig(), body, snapshot)
145
- } catch (Exception e) {
146
- exceptionClass = e. getClass(). getName();
147
- }
148
-
149
- expect :
150
- exceptionClass. contains(" Exception" )
151
- }
152
-
153
- def " two inserts" () {
154
-
155
- prepareStarsTable();
156
-
157
- JsonObject snapshot = Json . createObjectBuilder(). build()
158
-
159
- JsonObject body1 = Json . createObjectBuilder(). build()
160
- body1. addProperty(" id" , " 1" )
161
- body1. addProperty(" name" , " Taurus" )
162
- body1. addProperty(" radius" , " 123" )
163
-
164
- runAction(getStarsConfig(), body1, snapshot)
165
-
166
- JsonObject body2 = Json . createObjectBuilder(). build()
167
- body2. addProperty(" id" , " 2" )
168
- body2. addProperty(" name" , " Eridanus" )
169
- body2. addProperty(" radius" , " 456" )
170
-
171
- runAction(getStarsConfig(), body2, snapshot)
172
-
173
- ArrayList<String > records = getRecords(" stars" )
174
-
175
- expect :
176
- records. size() == 2
177
- records. get(0 ) == ' {id=1, name=Taurus, date=null, radius=123, destination=null}'
178
- records. get(1 ) == ' {id=2, name=Eridanus, date=null, radius=456, destination=null}'
179
- }
180
-
181
- def " one insert, one update by ID" () {
182
-
183
- prepareStarsTable();
184
-
185
- JsonObject snapshot = Json . createObjectBuilder(). build()
186
-
187
- JsonObject body1 = Json . createObjectBuilder(). build()
188
- body1. addProperty(" id" , " 1" )
189
- body1. addProperty(" name" , " Taurus" )
190
- body1. addProperty(" radius" , " 123" )
191
-
192
- runAction(getStarsConfig(), body1, snapshot)
193
-
194
- JsonObject body2 = Json . createObjectBuilder(). build()
195
- body2. addProperty(" id" , " 1" )
196
- body2. addProperty(" name" , " Eridanus" )
197
-
198
- runAction(getStarsConfig(), body2, snapshot)
199
-
200
- ArrayList<String > records = getRecords(" stars" )
201
-
202
- expect :
203
- records. size() == 1
204
- records. get(0 ) == ' {id=1, name=Eridanus, date=null, radius=123, destination=null}'
112
+ then :
113
+ 0 * errorCallback. receive(_)
205
114
}
206
115
207
-
208
- def getPersonsConfig () {
209
- JsonObject config = Json . createObjectBuilder(). build()
210
- config. addProperty(" idColumn" , " email" )
211
- config. addProperty(" tableName" , " persons" )
212
- config. addProperty(" user" , user)
213
- config. addProperty(" password" , password)
214
- config. addProperty(" dbEngine" , " mssql" )
215
- config. addProperty(" host" , host)
216
- config. addProperty(" databaseName" , databaseName)
217
- return config;
218
- }
219
-
220
- def preparePersonsTable () {
221
- String sql = " DROP TABLE IF EXISTS persons;"
222
- connection. createStatement(). execute(sql);
223
- connection. createStatement(). execute(" CREATE TABLE persons (id int, name varchar(255) NOT NULL, email varchar(255) NOT NULL)" );
224
- }
225
-
226
- def " one insert, name with quote" () {
227
-
228
- preparePersonsTable();
229
-
230
- JsonObject snapshot = Json . createObjectBuilder(). build()
231
-
232
- JsonObject body1 = Json . createObjectBuilder(). build()
233
- body1. addProperty(" id" , " 1" )
234
- body1. addProperty(" name" , " O'Henry" )
235
- body1
. addProperty(
" email" ,
" [email protected] " )
236
- runAction(getPersonsConfig(), body1, snapshot)
237
-
238
- ArrayList<String > records = getRecords(" persons" )
239
-
240
- expect :
241
- records. size() == 1
242
- records
. get(
0 )
== ' {id=1, name=O\' Henry, [email protected] }'
243
- }
244
-
245
- def " two inserts, one update by email" () {
246
-
247
- preparePersonsTable();
248
-
249
- JsonObject snapshot = Json . createObjectBuilder(). build()
250
-
251
- JsonObject body1 = Json . createObjectBuilder(). build()
252
- body1. addProperty(" id" , " 1" )
253
- body1. addProperty(" name" , " User1" )
254
- body1
. addProperty(
" email" ,
" [email protected] " )
255
- runAction(getPersonsConfig(), body1, snapshot)
256
-
257
- JsonObject body2 = Json . createObjectBuilder(). build()
258
- body2. addProperty(" id" , " 2" )
259
- body2. addProperty(" name" , " User2" )
260
- body2
. addProperty(
" email" ,
" [email protected] " )
261
- runAction(getPersonsConfig(), body2, snapshot)
262
-
263
- JsonObject body3 = Json . createObjectBuilder(). build()
264
- body3. addProperty(" id" , " 3" )
265
- body3. addProperty(" name" , " User3" )
266
- body3
. addProperty(
" email" ,
" [email protected] " )
267
- runAction(getPersonsConfig(), body3, snapshot)
268
-
269
- ArrayList<String > records = getRecords(" persons" )
270
-
271
- expect :
272
- records. size() == 2
273
- records
. get(
0 )
== ' {id=1, name=User1, [email protected] }'
274
- records
. get(
1 )
== ' {id=3, name=User3, [email protected] }'
275
- }
276
-
277
-
278
116
}
0 commit comments