@@ -8,13 +8,9 @@ import javax.json.Json
8
8
import javax.json.JsonObject
9
9
import java.sql.Connection
10
10
import java.sql.DriverManager
11
- import java.sql.ResultSet
12
11
13
12
@Ignore
14
13
class SelectMSSQLSpec extends Specification {
15
-
16
- @Shared
17
- def connectionString = System . getenv(" CONN_URI_MSSQL" )
18
14
@Shared
19
15
def user = System . getenv(" CONN_USER_MSSQL" )
20
16
@Shared
@@ -23,7 +19,10 @@ class SelectMSSQLSpec extends Specification {
23
19
def databaseName = System . getenv(" CONN_DBNAME_MSSQL" )
24
20
@Shared
25
21
def host = System . getenv(" CONN_HOST_MSSQL" )
26
-
22
+ @Shared
23
+ def port = System . getenv(" CONN_PORT_MSSQL" )
24
+ @Shared
25
+ def connectionString = " jdbc:sqlserver://" + host + " :" + port + " ;database=" + databaseName
27
26
@Shared
28
27
Connection connection
29
28
@@ -36,6 +35,8 @@ class SelectMSSQLSpec extends Specification {
36
35
@Shared
37
36
EventEmitter.Callback reboundCallback
38
37
@Shared
38
+ EventEmitter.Callback onHttpReplyCallback
39
+ @Shared
39
40
EventEmitter emitter
40
41
@Shared
41
42
SelectAction action
@@ -45,34 +46,36 @@ class SelectMSSQLSpec extends Specification {
45
46
}
46
47
47
48
def setup () {
48
- createAction ()
49
+ action = new SelectAction ()
49
50
}
50
51
51
- def createAction () {
52
+ def runAction (JsonObject config , JsonObject body , JsonObject snapshot ) {
53
+ Message msg = new Message.Builder (). body(body). build()
52
54
errorCallback = Mock (EventEmitter.Callback )
53
55
snapshotCallback = Mock (EventEmitter.Callback )
54
56
dataCallback = Mock (EventEmitter.Callback )
55
57
reboundCallback = Mock (EventEmitter.Callback )
56
- emitter = new EventEmitter.Builder (). onData(dataCallback). onSnapshot(snapshotCallback). onError(errorCallback). onRebound(reboundCallback). build()
57
- action = new SelectAction (emitter)
58
- }
59
-
60
- def runAction (JsonObject config , JsonObject body , JsonObject snapshot ) {
61
- Message msg = new Message.Builder (). body(body). build()
62
- ExecutionParameters params = new ExecutionParameters (msg, config, snapshot)
58
+ onHttpReplyCallback = Mock (EventEmitter.Callback )
59
+ emitter = new EventEmitter.Builder ()
60
+ .onData(dataCallback)
61
+ .onSnapshot(snapshotCallback)
62
+ .onError(errorCallback)
63
+ .onRebound(reboundCallback)
64
+ .onHttpReplyCallback(onHttpReplyCallback). build()
65
+ ExecutionParameters params = new ExecutionParameters (msg, emitter, config, snapshot)
63
66
action. execute(params);
64
67
}
65
68
66
69
def getStarsConfig () {
67
- JsonObject config = Json . createObjectBuilder(). build();
68
-
69
- config . addProperty( " idColumn " , " id " )
70
- config . addProperty( " tableName " , " stars " )
71
- config . addProperty( " user " , user )
72
- config . addProperty( " password " , password )
73
- config . addProperty( " dbEngine " , " mssql " )
74
- config . addProperty( " host " , host )
75
- config . addProperty( " databaseName " , databaseName)
70
+ JsonObject config = Json . createObjectBuilder()
71
+ .add( " user " , user)
72
+ .add( " password " , password )
73
+ .add( " dbEngine " , " mssql " )
74
+ .add( " host " , host )
75
+ .add( " port " , port )
76
+ .add( " databaseName " , databaseName )
77
+ .add( " sqlQuery " , " SELECT * from stars " )
78
+ .build();
76
79
return config;
77
80
}
78
81
@@ -81,198 +84,25 @@ class SelectMSSQLSpec extends Specification {
81
84
" DROP TABLE stars;"
82
85
connection. createStatement(). execute(sql);
83
86
connection. createStatement(). execute(" CREATE TABLE stars (id int, name varchar(255) NOT NULL, date datetime, radius int, destination int)" );
84
- }
85
-
86
- def getRecords (tableName ) {
87
- ArrayList<String > records = new ArrayList<String > ();
88
- String sql = " SELECT * FROM " + tableName;
89
- ResultSet rs = connection. createStatement(). executeQuery(sql);
90
- while (rs. next()) {
91
- records. add(rs. toRowResult(). toString());
92
- }
93
- rs. close();
94
- return records;
87
+ connection. createStatement(). execute(" INSERT INTO stars (id, name) VALUES (1,'Hello')" );
95
88
}
96
89
97
90
def cleanupSpec () {
98
- String sql = " IF OBJECT_ID('persons', 'U') IS NOT NULL\n " +
99
- " DROP TABLE persons;"
100
-
101
- connection. createStatement(). execute(sql)
102
- sql = " IF OBJECT_ID('stars', 'U') IS NOT NULL\n " +
91
+ String sql = " IF OBJECT_ID('stars', 'U') IS NOT NULL\n " +
103
92
" DROP TABLE stars;"
104
93
connection. createStatement(). execute(sql)
105
94
connection. close()
106
95
}
107
96
108
- def " one insert " () {
97
+ def " one select " () {
109
98
110
99
prepareStarsTable();
111
-
112
100
JsonObject snapshot = Json . createObjectBuilder(). build();
101
+ JsonObject body = Json . createObjectBuilder(). build()
113
102
114
- JsonObject body = Json . createObjectBuilder(). build();
115
- body. addProperty(" id" , " 1" )
116
- body. addProperty(" name" , " Taurus" )
117
- body. addProperty(" date" , " 2015-02-19 10:10:10.0" )
118
- body. addProperty(" radius" , " 123" )
119
-
103
+ when :
120
104
runAction(getStarsConfig(), body, snapshot)
121
-
122
- ArrayList<String > records = getRecords(" stars" )
123
-
124
- expect :
125
- records. size() == 1
126
- records. get(0 ) == ' {id=1, name=Taurus, date=2015-02-19 10:10:10.0, radius=123, destination=null}'
127
- }
128
-
129
- def " one insert, incorrect value: string in integer field" () {
130
-
131
- prepareStarsTable();
132
-
133
- JsonObject snapshot = Json . createObjectBuilder(). build();
134
-
135
- JsonObject body = Json . createObjectBuilder(). build();
136
- body. addProperty(" id" , " 1" )
137
- body. addProperty(" name" , " Taurus" )
138
- body. addProperty(" radius" , " test" )
139
-
140
- String exceptionClass = " " ;
141
-
142
- try {
143
- runAction(getStarsConfig(), body, snapshot)
144
- } catch (Exception e) {
145
- exceptionClass = e. getClass(). getName();
146
- }
147
-
148
- expect :
149
- exceptionClass. contains(" Exception" )
105
+ then :
106
+ 0 * errorCallback. receive(_)
150
107
}
151
-
152
- def " two inserts" () {
153
-
154
- prepareStarsTable();
155
-
156
- JsonObject snapshot = Json . createObjectBuilder(). build()
157
-
158
- JsonObject body1 = Json . createObjectBuilder(). build()
159
- body1. addProperty(" id" , " 1" )
160
- body1. addProperty(" name" , " Taurus" )
161
- body1. addProperty(" radius" , " 123" )
162
-
163
- runAction(getStarsConfig(), body1, snapshot)
164
-
165
- JsonObject body2 = Json . createObjectBuilder(). build()
166
- body2. addProperty(" id" , " 2" )
167
- body2. addProperty(" name" , " Eridanus" )
168
- body2. addProperty(" radius" , " 456" )
169
-
170
- runAction(getStarsConfig(), body2, snapshot)
171
-
172
- ArrayList<String > records = getRecords(" stars" )
173
-
174
- expect :
175
- records. size() == 2
176
- records. get(0 ) == ' {id=1, name=Taurus, date=null, radius=123, destination=null}'
177
- records. get(1 ) == ' {id=2, name=Eridanus, date=null, radius=456, destination=null}'
178
- }
179
-
180
- def " one insert, one update by ID" () {
181
-
182
- prepareStarsTable();
183
-
184
- JsonObject snapshot = Json . createObjectBuilder(). build()
185
-
186
- JsonObject body1 = Json . createObjectBuilder(). build()
187
- body1. addProperty(" id" , " 1" )
188
- body1. addProperty(" name" , " Taurus" )
189
- body1. addProperty(" radius" , " 123" )
190
-
191
- runAction(getStarsConfig(), body1, snapshot)
192
-
193
- JsonObject body2 = Json . createObjectBuilder(). build()
194
- body2. addProperty(" id" , " 1" )
195
- body2. addProperty(" name" , " Eridanus" )
196
-
197
- runAction(getStarsConfig(), body2, snapshot)
198
-
199
- ArrayList<String > records = getRecords(" stars" )
200
-
201
- expect :
202
- records. size() == 1
203
- records. get(0 ) == ' {id=1, name=Eridanus, date=null, radius=123, destination=null}'
204
- }
205
-
206
-
207
- def getPersonsConfig () {
208
- JsonObject config = Json . createObjectBuilder(). build()
209
- config. addProperty(" idColumn" , " email" )
210
- config. addProperty(" tableName" , " persons" )
211
- config. addProperty(" user" , user)
212
- config. addProperty(" password" , password)
213
- config. addProperty(" dbEngine" , " mssql" )
214
- config. addProperty(" host" , host)
215
- config. addProperty(" databaseName" , databaseName)
216
- return config;
217
- }
218
-
219
- def preparePersonsTable () {
220
- String sql = " IF OBJECT_ID('persons', 'U') IS NOT NULL\n " +
221
- " DROP TABLE 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
108
}
0 commit comments