-
Notifications
You must be signed in to change notification settings - Fork 1
/
RestapiTestBase.groovy
109 lines (91 loc) · 2.95 KB
/
RestapiTestBase.groovy
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
package org.cristalise.restapi.test
import static io.restassured.RestAssured.*
import static org.hamcrest.Matchers.*
import org.cristalise.kernel.process.AbstractMain
import org.cristalise.kernel.utils.Logger
import org.json.JSONArray
import org.junit.BeforeClass
import org.junit.Test
import groovy.transform.CompileStatic
import io.restassured.http.ContentType
import io.restassured.http.Cookie
import io.restassured.response.Response
@CompileStatic
class RestapiTestBase {
static String apiUri
static String user = 'user'
static String pwd = 'test'
String userUuid
Cookie cauthCookie
static final int STATUS_OK = 200
@BeforeClass
public static void init() {
Logger.addLogStream(System.out, 5)
Properties props = AbstractMain.readPropertyFiles("src/main/bin/client.conf", "src/main/bin/integTest.clc", null)
apiUri = props.get('REST.URI')
}
def login() {
Response loginResp =
given()
.accept(ContentType.JSON)
.params('user', user, 'pass', pwd )
.when()
.get(apiUri+"/login")
.then()
.cookie('cauth')
.statusCode(STATUS_OK)
.extract().response()
cauthCookie = loginResp.getDetailedCookie('cauth');
userUuid = loginResp.body().jsonPath().getString('Login.uuid.value')
assert userUuid
}
def logout(String reason) {
if (reason) {
given()
.accept(ContentType.JSON)
.cookie(cauthCookie)
.params('reason', reason)
.when()
.get(apiUri+"/logout")
.then()
.statusCode(STATUS_OK)
}
else {
given()
.accept(ContentType.JSON)
.cookie(cauthCookie)
.when()
.get(apiUri+"/logout")
.then()
.statusCode(STATUS_OK)
}
}
int checkEvent(Integer id, String name) {
if (id == null) {
String histBody =
given()
.accept(ContentType.JSON)
.cookie(cauthCookie)
.when()
.get(apiUri+"/item/$userUuid/history")
.then()
.statusCode(STATUS_OK)
.extract().response().body().asString()
def histJson = new JSONArray(histBody)
def event = histJson.getJSONObject(histJson.length()-1)
assert event && event.getJSONObject('activity').get('name') == name
return event.getInt('id')
}
else {
given()
.accept(ContentType.JSON)
.cookie(cauthCookie)
.when()
.get(apiUri+"/item/$userUuid/history/$id")
.then()
.statusCode(STATUS_OK)
.body('activity.name', equalTo(name))
return id
}
}
}