-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestSuiteParsingTest.xtend
315 lines (260 loc) · 11 KB
/
TestSuiteParsingTest.xtend
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* generated by Xtext 2.22.0
*/
package xtext.http.rest.tests
import com.google.inject.Inject
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.extensions.InjectionExtension
import org.eclipse.xtext.testing.util.ParseHelper
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.^extension.ExtendWith
import xtext.http.rest.testSuite.TestCollection
import xtext.http.rest.testSuite.TestSuiteFactory
import xtext.http.rest.generator.TestSuiteGenerator
import xtext.http.rest.testSuite.EnvironmentVariable
import org.eclipse.emf.common.util.BasicEList
import xtext.http.rest.generator.AssertionGenerator
import xtext.http.rest.generator.RequestGenerator
@ExtendWith(InjectionExtension)
@InjectWith(TestSuiteInjectorProvider)
class TestSuiteParsingTest {
@Inject
ParseHelper<TestCollection> parseHelper;
@Inject
TestSuiteGenerator generator;
@Inject
AssertionGenerator assertionGenerator;
@Inject
RequestGenerator requestGenerator;
@Test
def void loadModel() {
val result = parseHelper.parse('''
TestCollection: CompleteExample
description: "This is an example test collection to show functionality"
Global Resources: // Accessible to all tests
TextResource @ "http://localhost:3000/text/"
Persons @ "http://localhost:3000/persons"
Person @ "http://localhost:3000/persons/{personId}"
Addresses @ "http://localhost:3000/addresses"
Address @ "http://localhost:3000/addresses/{addressId}"
Global Headers: // Accessible to all tests
ApiKey name="Api-Key" value="xxx-api-xxx-key-xxx"
Secret name="secret" value="ThisIsTheSecret"
ClientName name="Client-Name" value= "Norway-Testing"
BehalfOf name="Behalf-of" value= "Sverige-Testing"
Headers included in every test:
ApiKey, Secret
Tests:
// Demonstrating Global resource vs local resource
Test name: Global_resource
GET @ TextResource
assert status code 200
Test name: Local_resource
GET Text @ "http://localhost:3000/text"
assert status code 200
// Demonstrating injecting var into resource url
Test name: Inject_var_into_url
Environment:
var person1Id reference="personId" /*<-- This is the reference in the url*/ value="23a1c108-97bf-4906-be5a-5872a566767b"
GET Person @ "http://localhost:3000/persons/{personId}" inject person1Id // <-- the injection
assert status code 200
Test name: Inject_var_into_subsequent_calls_url
Environment:
Object personBody has fields name = "Jone", job = "Developer"
var person1Id reference="personId"// This is the reference in the url
POST with body personBody @ Persons
save person1Id by ref // <-- saves personId from response body to environment var person1Id
assert status code 200
// The reason is to use an value from an call into the url in the next call:
GET Person @ "http://localhost:3000/persons/{personId}" inject person1Id // <-- the injection
assert status code 200
// Demonstrating different assertions
Test name: Assert_status_code
GET Text @ "http://localhost:3000/text"
assert status code 200
Test name: Assert_text
GET Text @ "http://localhost:3000/text"
assert text "This is the test text"
Test name: Assert_body_with_injected_personId
Environment:
var person1Id reference="personId" value="23a1c108-97bf-4906-be5a-5872a566767b"
Object personBody has fields name = "Ola Norman", job = "Elektriker"
GET @ Person inject person1Id
assert body personBody inject person1Id // <-- injects person id generated from API into test body to be able to assert the whole body
Test name: Assert_headers
Environment:
var headerValue reference="" /*not in use here, due to only demonstration */ value="Header-value"
Header HeaderWithRefToVar name="Header-with-ref" reference=headerValue
GET Text @ "http://localhost:3000/text" with header(s) HeaderWithRefToVar
assert header(s) ApiKey, Secret, xPower name="X-Powered-By" value="Express", HeaderWithRefToVar
Test name: Assert_multiple
GET Text @ "http://localhost:3000/text"
assert text "This is the test text"
assert status code 200
// Demonstrating headers
Test name: Globaly_included_headers
GET @ Persons
assert status code 200
Test name: Global_headers
GET @ Persons with header(s) ClientName, BehalfOf
assert status code 200
Test name: Local_headers
Environment:
Header LocalHeader1 name="Local-header-1" value="local-header-value-1"
Header LocalHeader2 name="Local-header-2" value="local-header-value-2"
GET @ Persons with header(s) LocalHeader1, LocalHeader2
assert status code 200
Test name: Global_and_Local_header
Environment:
Header LocalHeader1 name="Local-header-1" value="local-header-value-1"
GET @ Persons with header(s) ClientName, LocalHeader1
assert status code 200
Test name: Headers_with_reference_to_var
Environment:
var personJob reference="job"
Header PersonJobHeader name="person-job" reference=personJob
// Call to collect the data we want to use in next call
GET PersonWithId @ "http://localhost:3000/persons/23a1c108-97bf-4906-be5a-5872a566767b"
save personJob by ref // Save the value to the value used in the header
assert status code 200
// The reason is to use a saved value in subsequent calls
GET @ Persons with header(s) PersonJobHeader
assert status code 200
// Demonstrating how to include body
Test name: Post_body
Environment:
Object personBody has fields name = "Jone", job = "Developer"
POST with body personBody @ Persons
assert status code 200
Test name: Post_body_containing_body
Environment:
Object addressBody has fields street="Test street", popularity="High"
Object personBody has fields name = "Jone", job = "Developer", address -> addressBody
POST with body personBody @ Persons
assert status code 200
// Demonstrating normal use: Create two dependent objects, change object, delete object
Test name: SeperateAddress_test
Environment:
var addressId reference="addressId"
var person1Id reference="personId"
Object AddressBody has fields street="Test street", popularity="High"
Object PersonBodyWithSeperateAddress has fields name = "Body", tests = "Person without address"
Object personPutBody has fields name = "Ola", job = "CEO"
POST with body AddressBody @ Addresses
save addressId by ref
assert status code 200
POST with body PersonBodyWithSeperateAddress inject addressId @ Persons
save person1Id by ref
assert status code 200
assert body PersonBodyWithSeperateAddress inject addressId, person1Id
PUT with body personPutBody @ Person inject person1Id
assert body personPutBody inject person1Id
assert status code 200
DELETE @ Person inject person1Id
assert status code 200
DELETE @ Address inject addressId
assert status code 200
// Extended url injection testing
Test name: Url1_test
Environment:
Object Url1Body has fields url="/urltest1/value1/test/value2"
var TestId1 reference="firstId" value="value1"
var TestId2 reference="secondId" value="value2"
GET Url @ "http://localhost:3000/urltest1/{firstId}/test/{secondId}" inject TestId1, TestId2
assert status code 200
assert body Url1Body
Test name: Url2_test
Environment:
Object Url2Body has fields url="/urltest2/value1/value2/test"
var TestId1 reference="firstId" value="value1"
var TestId2 reference="secondId" value="value2"
GET UrlTest2 @ "http://localhost:3000/urltest2/{firstId}/{secondId}/test" inject TestId1, TestId2
assert status code 200
assert body Url2Body
// End
''')
Assertions.assertNotNull(result)
val errors = result.eResource.errors
Assertions.assertTrue(errors.isEmpty, '''Unexpected errors: «errors.join(", ")»''')
}
@Test
def void getLocalFields() {
var test = TestSuiteFactory.eINSTANCE.createTest();
val var1 = TestSuiteFactory.eINSTANCE.createEnvironmentVariable();
var1.name = "name1";
var1.value = "value1";
val var2 = TestSuiteFactory.eINSTANCE.createEnvironmentVariable();
var2.name = "name2";
var2.value = "value2";
test.vars.add(var1);
test.vars.add(var2);
var result = generator.getLocalFields(test);
Assertions.assertEquals("String name1 = \"value1\";\nString name2 = \"value2\";", result);
}
@Test
def void getHeaderWithValue() {
val header = TestSuiteFactory.eINSTANCE.createHeader();
header.headerName = "headerName";
header.value = "headerValue";
var nr = 0;
var result = requestGenerator.getHeader(header, nr++);
Assertions.assertEquals("request0.addHeader(\"headerName\", \"headerValue\");", result);
}
@Test
def void getHeaderWithRef() {
val header = TestSuiteFactory.eINSTANCE.createHeader();
header.headerName = "headerName";
val envVar = TestSuiteFactory.eINSTANCE.createEnvironmentVariable();
envVar.name = "envName";
header.ref = envVar;
var nr = 0;
var result = requestGenerator.getHeader(header, nr++);
Assertions.assertEquals("request0.addHeader(\"headerName\", envName);", result);
}
@Test
def void getHttpResource() {
val httpResource1 = TestSuiteFactory.eINSTANCE.createHttpResource();
httpResource1.url = "http://localhost:3000/{inject1}/someurl/{inject2}";
val envVar1 = TestSuiteFactory.eINSTANCE.createEnvironmentVariable();
envVar1.name = "value1";
envVar1.reference = "inject1";
val envVar2 = TestSuiteFactory.eINSTANCE.createEnvironmentVariable();
envVar2.name = "value2";
envVar2.reference = "inject2";
val injectionValues = new BasicEList<EnvironmentVariable>();
injectionValues.add(envVar1);
injectionValues.add(envVar2);
var result = requestGenerator.getHttpResource(httpResource1, injectionValues);
Assertions.assertEquals("\"http://localhost:3000/\" + value1 + \"/someurl/\" + value2", result);
val httpResource2 = TestSuiteFactory.eINSTANCE.createHttpResource();
httpResource2.url = "http://localhost:3000/{inject1}/{inject2}/someurl";
result = requestGenerator.getHttpResource(httpResource2, injectionValues);
Assertions.assertEquals("\"http://localhost:3000/\" + value1 + \"/\" + value2 + \"/someurl\"", result);
}
@Test
def void getTestBody() {
val jsonObject1 = TestSuiteFactory.eINSTANCE.createJsonObject();
val value1 = TestSuiteFactory.eINSTANCE.createValue();
value1.name = "name1";
value1.value = "value1"
val value2 = TestSuiteFactory.eINSTANCE.createValue();
value2.name = "name2";
value2.value = "value2"
jsonObject1.fields.add(value1);
jsonObject1.fields.add(value2);
var result = requestGenerator.getTestBody(jsonObject1);
Assertions.assertEquals("\"{\\\"name1\\\":\\\"value1\\\",\\\"name2\\\":\\\"value2\\\"}\"", result);
val jsonObject2 = TestSuiteFactory.eINSTANCE.createJsonObject();
val value4 = TestSuiteFactory.eINSTANCE.createValue();
value4.name = "name4";
value4.value = "value4";
jsonObject2.fields.add(value4);
val value3 = TestSuiteFactory.eINSTANCE.createValue();
value3.name = "name3";
value3.reference = jsonObject2;
jsonObject1.fields.add(value3);
result = requestGenerator.getTestBody(jsonObject1);
Assertions.assertEquals("\"{\\\"name1\\\":\\\"value1\\\",\\\"name2\\\":\\\"value2\\\",\\\"name3\\\":{\\\"name4\\\":\\\"value4\\\"}}\"", result);
}
}