-
Notifications
You must be signed in to change notification settings - Fork 18
/
FusekiTestContainer.scala
82 lines (67 loc) · 3.19 KB
/
FusekiTestContainer.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
/*
* Copyright © 2021 - 2023 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.testcontainers
import org.testcontainers.containers.GenericContainer
import org.testcontainers.utility.DockerImageName
import zio.ZIO
import zio._
import zio.http.URL
import zio.macros.accessible
import zio.nio.file.Files
import zio.nio.file.Path
import java.net.Authenticator
import java.net.PasswordAuthentication
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpRequest.BodyPublishers
import java.net.http.HttpResponse.BodyHandlers
import org.knora.webapi.http.version.BuildInfo
@accessible
trait FusekiTestContainer extends GenericContainer[FusekiTestContainer] {
def baseUrl: URL = {
val urlString = s"http://$getHost:$getFirstMappedPort"
URL.decode(urlString).getOrElse(throw new IllegalStateException(s"Invalid URL $urlString"))
}
def credentials: (String, String) = ("admin", FusekiTestContainer.adminPassword)
private val httpClient: HttpClient = {
val basicAuth: Authenticator = new Authenticator {
val (username, password) = credentials
override def getPasswordAuthentication = new PasswordAuthentication(username, password.toCharArray)
}
HttpClient.newBuilder().authenticator(basicAuth).build()
}
def initializeWithDataset(repositoryName: String): Task[Unit] = for {
_ <- ZIO.logInfo(s"Initializing and creating dataset $repositoryName")
fusekiConfig <- Files
.readAllLines(Path(getClass.getResource("/fuseki-repository-config.ttl.template").getPath))
.map(_.map(line => line.replace("@REPOSITORY@", repositoryName)).mkString("\n"))
request = HttpRequest
.newBuilder()
.uri(baseUrl.path("/$/datasets").toJavaURI)
.POST(BodyPublishers.ofString(fusekiConfig))
.header("Content-Type", "text/turtle; charset=utf-8")
.build()
_ <- ZIO
.attempt(httpClient.send(request, BodyHandlers.ofString()))
.filterOrFail(_.statusCode() == 200)(new IllegalStateException("Could not configure Fuseki"))
} yield ()
}
object FusekiTestContainer {
def apply(dockerImageName: DockerImageName): FusekiTestContainer =
new GenericContainer[FusekiTestContainer](dockerImageName) with FusekiTestContainer
def apply(): FusekiTestContainer =
new GenericContainer[FusekiTestContainer](DockerImageName.parse(BuildInfo.fuseki)) with FusekiTestContainer
val adminPassword = "test"
private val acquire: Task[FusekiTestContainer] = {
val container = FusekiTestContainer()
.withExposedPorts(3030)
.withEnv("ADMIN_PASSWORD", adminPassword)
.withEnv("JVM_ARGS", "-Xmx3G")
ZIO.attemptBlocking(container.start()).as(container).orDie <* ZIO.logInfo(">>> Acquire Fuseki TestContainer <<<")
}
private def release(container: FusekiTestContainer): UIO[Unit] =
ZIO.attemptBlocking(container.stop()).logError.ignore <* ZIO.logInfo(">>> Release Fuseki TestContainer <<<")
val layer: ULayer[FusekiTestContainer] = ZLayer.scoped(ZIO.acquireRelease(acquire)(release)).orDie
}