-
Notifications
You must be signed in to change notification settings - Fork 18
/
E2EZSpec.scala
83 lines (67 loc) · 2.96 KB
/
E2EZSpec.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
/*
* 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
import zio._
import zio.http._
import zio.json._
import zio.test._
import org.knora.webapi.core.AppServer
import org.knora.webapi.core.LayersTest
import org.knora.webapi.core.TestStartupUtils
import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
abstract class E2EZSpec extends ZIOSpecDefault with TestStartupUtils {
private lazy val testLayers = util.Logger.text() >>> core.LayersTest.integrationTestsWithFusekiTestcontainers()
def rdfDataObjects: List[RdfDataObject] = List.empty[RdfDataObject]
type env = LayersTest.DefaultTestEnvironmentWithoutSipi with Client with Scope
private def prepare: ZIO[AppServer.AppServerEnvironment, Throwable, AppServer] = for {
appServer <- AppServer.init()
_ <- appServer.start(requiresAdditionalRepositoryChecks = false, requiresIIIFService = false).orDie
_ <- prepareRepository(rdfDataObjects)
} yield appServer
def withResettedTriplestore =
TestAspect.before(prepareRepository(rdfDataObjects))
def e2eSpec: Spec[env, Any]
override def spec = (
e2eSpec
@@ TestAspect.beforeAll(prepare)
@@ TestAspect.sequential
).provideShared(testLayers, Client.default, Scope.default)
def sendGetRequestString(url: String): ZIO[env, String, String] =
for {
client <- ZIO.service[Client]
urlStr = s"http://localhost:3333$url"
urlFull <- ZIO.fromEither(URL.decode(urlStr)).mapError(_.getMessage)
response <- client.url(urlFull).get("/").mapError(_.getMessage)
data <- response.body.asString.mapError(_.getMessage)
} yield data
def sendGetRequest[B](url: String)(implicit dec: JsonDecoder[B]): ZIO[env, String, B] =
for {
response <- sendGetRequestString(url)
result <- ZIO.fromEither(response.fromJson[B])
} yield result
def sendPostRequestString(url: String, data: String): ZIO[env, String, String] =
for {
client <- ZIO.service[Client]
urlStr = s"http://localhost:3333$url"
urlFull <- ZIO.fromEither(URL.decode(urlStr)).mapError(_.getMessage)
body = Body.fromString(data)
header = Header.ContentType(MediaType.application.json)
response <- client.url(urlFull).addHeader(header).post("")(body).mapError(_.getMessage)
data <- response.body.asString.mapError(_.getMessage)
} yield data
def getToken(email: String, password: String): ZIO[env, String, String] =
for {
response <-
sendPostRequestString(
"/v2/authentication",
s"""|{
| "email": "$email",
| "password": "$password"
|}""".stripMargin,
)
result <- ZIO.fromEither(response.fromJson[Map[String, String]])
token <- ZIO.fromOption(result.get("token")).orElseFail("No token in response")
} yield token
}