/
ITKnoraLiveSpec.scala
214 lines (191 loc) · 7.01 KB
/
ITKnoraLiveSpec.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
/*
* Copyright © 2021 - 2022 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
import akka.http.scaladsl.client.RequestBuilding
import akka.http.scaladsl.model._
import akka.testkit.TestKitBase
import com.typesafe.scalalogging.LazyLogging
import com.typesafe.scalalogging.Logger
import org.scalatest.BeforeAndAfterAll
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import spray.json._
import zio._
import zio.logging.backend.SLF4J
import java.util.concurrent.TimeUnit
import scala.concurrent.Await
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.duration.FiniteDuration
import org.knora.webapi.config.AppConfig
import org.knora.webapi.core.AppRouter
import org.knora.webapi.core.AppServer
import org.knora.webapi.core.TestStartupUtils
import org.knora.webapi.messages.store.sipimessages._
import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.messages.store.triplestoremessages.TriplestoreJsonProtocol
import org.knora.webapi.messages.util.rdf.JsonLDDocument
import org.knora.webapi.messages.util.rdf.JsonLDUtil
import org.knora.webapi.testservices.FileToUpload
import org.knora.webapi.testservices.TestClientService
import org.knora.webapi.util.LogAspect
/**
* This class can be used in End-to-End testing. It starts the DSP stack and
* provides access to configuration and logging.
*/
abstract class ITKnoraLiveSpec
extends AnyWordSpec
with TestKitBase
with TestStartupUtils
with Matchers
with BeforeAndAfterAll
with RequestBuilding
with TriplestoreJsonProtocol
with LazyLogging {
/**
* The `Environment` that we require to exist at startup.
* Can be overriden in specs that need other implementations.
*/
type Environment = core.LayersTest.DefaultTestEnvironmentWithSipi
/**
* The effect layers from which the App is built.
* Can be overriden in specs that need other implementations.
*/
lazy val effectLayers = core.LayersTest.defaultLayersTestWithSipi
/**
* `Bootstrap` will ensure that everything is instantiated when the Runtime is created
* and cleaned up when the Runtime is shutdown.
*/
private val bootstrap: ZLayer[
Any,
Any,
Environment
] = ZLayer.empty ++ Runtime.removeDefaultLoggers ++ SLF4J.slf4j ++ effectLayers
// create a configured runtime
val runtime = Unsafe.unsafe { implicit u =>
Runtime.unsafe
.fromLayer(bootstrap)
}
// An effect for getting stuff out, so that we can pass them
// to some legacy code
val routerAndConfig = for {
router <- ZIO.service[core.AppRouter]
config <- ZIO.service[AppConfig]
} yield (router, config)
/**
* Create router and config by unsafe running them.
*/
val (router: AppRouter, config: AppConfig) =
Unsafe.unsafe { implicit u =>
runtime.unsafe
.run(
routerAndConfig
)
.getOrThrowFiberFailure()
}
implicit lazy val system: akka.actor.ActorSystem = router.system
implicit lazy val executionContext: ExecutionContext = system.dispatcher
lazy val rdfDataObjects = List.empty[RdfDataObject]
val log: Logger = Logger(this.getClass())
val appActor = router.ref
val appConfig = config
// needed by some tests
val baseApiUrl = config.knoraApi.internalKnoraApiBaseUrl
val baseInternalSipiUrl = config.sipi.internalBaseUrl
final override def beforeAll(): Unit =
/* Here we start our app and initialize the repository before each suit runs */
Unsafe.unsafe { implicit u =>
runtime.unsafe
.run(
for {
_ <- AppServer.testWithSipi
_ <- prepareRepository(rdfDataObjects) @@ LogAspect.logSpan("prepare-repo")
} yield ()
)
.getOrThrow()
}
final override def afterAll(): Unit =
/* Stop ZIO runtime and release resources (e.g., running docker containers) */
Unsafe.unsafe { implicit u =>
runtime.unsafe.shutdown()
}
protected def getResponseStringOrThrow(request: HttpRequest): String =
Unsafe.unsafe { implicit u =>
runtime.unsafe
.run(
for {
testClient <- ZIO.service[TestClientService]
result <- testClient.getResponseString(request)
} yield result
)
.getOrThrowFiberFailure()
}
protected def checkResponseOK(request: HttpRequest): Unit =
Unsafe.unsafe { implicit u =>
runtime.unsafe
.run(
for {
testClient <- ZIO.service[TestClientService]
result <- testClient.checkResponseOK(request)
} yield result
)
.getOrThrowFiberFailure()
}
protected def getResponseJson(request: HttpRequest): JsObject =
Unsafe.unsafe { implicit u =>
runtime.unsafe
.run(
for {
testClient <- ZIO.service[TestClientService]
result <- testClient.getResponseJson(request)
} yield result
)
.getOrThrowFiberFailure()
}
protected def singleAwaitingRequest(request: HttpRequest, duration: zio.Duration = 15.seconds): HttpResponse =
Unsafe.unsafe { implicit u =>
runtime.unsafe
.run(
for {
testClient <- ZIO.service[TestClientService]
result <- testClient.singleAwaitingRequest(request, duration)
} yield result
)
.getOrThrowFiberFailure()
}
protected def getResponseJsonLD(request: HttpRequest): JsonLDDocument =
Unsafe.unsafe { implicit u =>
runtime.unsafe
.run(
for {
testClient <- ZIO.service[TestClientService]
result <- testClient.getResponseJsonLD(request)
} yield result
)
.getOrThrowFiberFailure()
}
protected def uploadToSipi(loginToken: String, filesToUpload: Seq[FileToUpload]): SipiUploadResponse =
Unsafe.unsafe { implicit u =>
runtime.unsafe
.run(
for {
testClient <- ZIO.service[TestClientService]
result <- testClient.uploadToSipi(loginToken, filesToUpload)
} yield result
)
.getOrThrow()
}
protected def responseToString(httpResponse: HttpResponse): String = {
val responseBodyFuture: Future[String] =
httpResponse.entity.toStrict(FiniteDuration(10L, TimeUnit.SECONDS)).map(_.data.decodeString("UTF-8"))
Await.result(responseBodyFuture, FiniteDuration(10L, TimeUnit.SECONDS))
}
protected def responseToJsonLDDocument(httpResponse: HttpResponse): JsonLDDocument = {
val responseBodyFuture: Future[String] =
httpResponse.entity.toStrict(FiniteDuration(10L, TimeUnit.SECONDS)).map(_.data.decodeString("UTF-8"))
val responseBodyStr = Await.result(responseBodyFuture, FiniteDuration(10L, TimeUnit.SECONDS))
JsonLDUtil.parseJsonLD(responseBodyStr)
}
}