-
Notifications
You must be signed in to change notification settings - Fork 18
/
ExceptionHandlerR2RSpec.scala
235 lines (171 loc) · 6.73 KB
/
ExceptionHandlerR2RSpec.scala
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
/*
* Copyright © 2021 - 2024 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.e2e
import org.apache.pekko
import dsp.errors.*
import org.knora.webapi.*
import org.knora.webapi.http.handler
import pekko.http.scaladsl.model.*
import pekko.http.scaladsl.server.Directives.*
import pekko.http.scaladsl.server.Route
/**
* Route (R2R) test specification for testing exception handling.
*/
class ExceptionHandlerR2RSpec extends R2RSpec {
private val nfe = path(("v2" | "admin") / "nfe") {
get {
throw NotFoundException("not found")
}
}
private val fe = path(("v2" | "admin") / "fe") {
get {
throw ForbiddenException("forbidden")
}
}
private val bce = path(("v2" | "admin") / "bce") {
get {
throw BadCredentialsException("bad credentials")
}
}
private val dve = path(("v2" | "admin") / "dve") {
get {
throw DuplicateValueException("duplicate value")
}
}
private val oce = path(("v2" | "admin") / "oce") {
get {
throw OntologyConstraintException("ontology constraint")
}
}
private val ece = path(("v2" | "admin") / "ece") {
get {
throw EditConflictException("edit conflict")
}
}
private val sse = path(("v2" | "admin") / "sse") {
get {
throw GravsearchException("sparql search")
}
}
private val unpe = path(("v2" | "admin") / "unpe") {
get {
throw UpdateNotPerformedException("update not performed")
}
}
private val ae = path(("v2" | "admin") / "ae") {
get {
throw AuthenticationException("authentication exception")
}
}
private val route: Route = Route.seal(handleExceptions(handler.KnoraExceptionHandler(appConfig)) {
nfe ~ fe ~ bce ~ dve ~ oce ~ ece ~ sse ~ unpe ~ ae
})
"The Exception Handler" should {
"return correct status code and response for 'NotFoundException'" in {
Get("/v2/nfe") ~> route ~> check {
status should be(StatusCodes.NotFound)
responseAs[String] should be(
"{\"knora-api:error\":\"dsp.errors.NotFoundException: not found\",\"@context\":{\"knora-api\":\"http://api.knora.org/ontology/knora-api/v2#\"}}"
)
}
Get("/admin/nfe") ~> route ~> check {
status should be(StatusCodes.NotFound)
responseAs[String] should be("{\"error\":\"dsp.errors.NotFoundException: not found\"}")
}
}
"return correct status code and response for 'ForbiddenException'" in {
Get("/v2/fe") ~> route ~> check {
status should be(StatusCodes.Forbidden)
responseAs[String] should be(
"{\"knora-api:error\":\"dsp.errors.ForbiddenException: forbidden\",\"@context\":{\"knora-api\":\"http://api.knora.org/ontology/knora-api/v2#\"}}"
)
}
Get("/admin/fe") ~> route ~> check {
status should be(StatusCodes.Forbidden)
responseAs[String] should be("{\"error\":\"dsp.errors.ForbiddenException: forbidden\"}")
}
}
"return correct status code and response for 'DuplicateValueException'" in {
Get("/v2/dve") ~> route ~> check {
status should be(StatusCodes.BadRequest)
responseAs[String] should be(
"{\"knora-api:error\":\"dsp.errors.DuplicateValueException: duplicate value\",\"@context\":{\"knora-api\":\"http://api.knora.org/ontology/knora-api/v2#\"}}"
)
}
Get("/admin/dve") ~> route ~> check {
status should be(StatusCodes.BadRequest)
responseAs[String] should be(
"{\"error\":\"dsp.errors.DuplicateValueException: duplicate value\"}"
)
}
}
"return correct status code and response for 'OntologyConstraintException'" in {
Get("/v2/oce") ~> route ~> check {
status should be(StatusCodes.BadRequest)
responseAs[String] should be(
"{\"knora-api:error\":\"dsp.errors.OntologyConstraintException: ontology constraint\",\"@context\":{\"knora-api\":\"http://api.knora.org/ontology/knora-api/v2#\"}}"
)
}
Get("/admin/oce") ~> route ~> check {
status should be(StatusCodes.BadRequest)
responseAs[String] should be(
"{\"error\":\"dsp.errors.OntologyConstraintException: ontology constraint\"}"
)
}
}
"return correct status code and response for 'EditConflictException'" in {
Get("/v2/ece") ~> route ~> check {
status should be(StatusCodes.Conflict)
responseAs[String] should be(
"{\"knora-api:error\":\"dsp.errors.EditConflictException: edit conflict\",\"@context\":{\"knora-api\":\"http://api.knora.org/ontology/knora-api/v2#\"}}"
)
}
Get("/admin/ece") ~> route ~> check {
status should be(StatusCodes.Conflict)
responseAs[String] should be("{\"error\":\"dsp.errors.EditConflictException: edit conflict\"}")
}
}
"return correct status code and response for 'GravsearchException'" in {
Get("/v2/sse") ~> route ~> check {
status should be(StatusCodes.BadRequest)
responseAs[String] should be(
"{\"knora-api:error\":\"dsp.errors.GravsearchException: sparql search\",\"@context\":{\"knora-api\":\"http://api.knora.org/ontology/knora-api/v2#\"}}"
)
}
Get("/admin/sse") ~> route ~> check {
status should be(StatusCodes.BadRequest)
responseAs[String] should be("{\"error\":\"dsp.errors.GravsearchException: sparql search\"}")
}
}
"return correct status code and response for 'UpdateNotPerformedException'" in {
Get("/v2/unpe") ~> route ~> check {
status should be(StatusCodes.Conflict)
responseAs[String] should be(
"{\"knora-api:error\":\"dsp.errors.UpdateNotPerformedException: update not performed\",\"@context\":{\"knora-api\":\"http://api.knora.org/ontology/knora-api/v2#\"}}"
)
}
Get("/admin/unpe") ~> route ~> check {
status should be(StatusCodes.Conflict)
responseAs[String] should be(
"{\"error\":\"dsp.errors.UpdateNotPerformedException: update not performed\"}"
)
}
}
"return correct status code and response for 'AuthenticationException'" in {
Get("/v2/ae") ~> route ~> check {
status should be(StatusCodes.InternalServerError)
responseAs[String] should be(
"{\"knora-api:error\":\"dsp.errors.AuthenticationException: authentication exception\",\"@context\":{\"knora-api\":\"http://api.knora.org/ontology/knora-api/v2#\"}}"
)
}
Get("/admin/ae") ~> route ~> check {
status should be(StatusCodes.InternalServerError)
responseAs[String] should be(
"{\"error\":\"dsp.errors.AuthenticationException: authentication exception\"}"
)
}
}
}
}