-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArtifactAny.kt
More file actions
239 lines (213 loc) · 9.23 KB
/
ArtifactAny.kt
File metadata and controls
239 lines (213 loc) · 9.23 KB
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
236
237
238
239
package org.openmbee.flexo.mms
import io.kotest.assertions.ktor.client.shouldHaveStatus
import io.kotest.matchers.equals.shouldBeEqual
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldBeEmpty
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldStartWith
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.testing.*
import io.ktor.util.*
import org.openmbee.flexo.mms.util.*
import org.slf4j.LoggerFactory
import java.io.ByteArrayInputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
@OptIn(InternalAPI::class)
open class ArtifactAny : RefAny() {
override val logger = LoggerFactory.getLogger(LockAny::class.java)
val artifactsPath = "$demoRepoPath/artifacts"
init {
"post artifact text/plain" {
testApplication {
httpPost(artifactsPath) {
header("Content-Type", "text/plain")
setBody("foo")
}.apply {
this shouldHaveStatus HttpStatusCode.Created
this.headers[HttpHeaders.Location] shouldStartWith localIri(artifactsPath)
this.contentType() shouldBe ContentType.Text.Plain
}
}
}
// Set a content-type with parameters (like utf-8 on text/plain) and assert that parameters have
// been removed on returned content type
"post artifact text/plain with parameter" {
testApplication {
httpPost(artifactsPath) {
header("Content-Type", "text/plain; charset=utf-8")
setBody("foo")
}.apply {
this shouldHaveStatus HttpStatusCode.Created
this.headers[HttpHeaders.Location] shouldStartWith localIri(artifactsPath)
this.contentType() shouldBe ContentType.Text.Plain
}
}
}
"get all artifacts empty" {
testApplication {
httpGet("$artifactsPath?download") {
}.apply {
this shouldHaveStatus HttpStatusCode.NoContent
this.bodyAsText().shouldBeEmpty()
}
}
}
"get all artifacts two artifacts" {
testApplication {
httpPost(artifactsPath) {
header("Content-Type", "text/plain")
setBody("foo")
}.apply {
val locationFile1 = getURI(this.headers[HttpHeaders.Location].toString())
httpPost(artifactsPath) {
header("Content-Type", "application/octet-stream")
setBody("bar".toByteArray())
}.apply {
val locationFile2 = getURI(this.headers[HttpHeaders.Location].toString())
httpGet("$artifactsPath?download") {}.apply {
this shouldHaveStatus HttpStatusCode.OK
this.contentType() shouldBe ContentType.Application.Zip
val zipBytes = this.content.toByteArray()
val contents = readZipContents(zipBytes)
contents.size shouldBe 2
contents["$locationFile1.txt"] shouldBe "foo"
contents["$locationFile2.bin"] shouldBe "bar"
}
}
}
}
}
// Not used http methods that should fail
"artifact rejects other methods" {
testApplication {
onlyAllowsMethods(artifactsPath, setOf(
HttpMethod.Head,
HttpMethod.Get,
HttpMethod.Post
))
}
}
/*********************************************
/artifacts/{id} route - gets artifacts
*********************************************/
"get an artifact by id - turtle" {
testApplication {
httpPost(artifactsPath) {
header("Content-Type", "text/plain")
setBody("foo".toByteArray())
}.apply {
this shouldHaveStatus HttpStatusCode.Created
this.headers[HttpHeaders.Location] shouldStartWith localIri(artifactsPath)
val uri = getLocation(this.headers[HttpHeaders.Location].toString())
httpGet(uri) {}.apply {
this shouldHaveStatus HttpStatusCode.OK
this.contentType() shouldBe ContentType.Text.Plain
this.bodyAsText() shouldContain "foo"
}
}
}
}
"download an artifact by id - text" {
testApplication {
httpPost(artifactsPath) {
header("Content-Type", "text/plain")
setBody("foo")
}.apply {
this shouldHaveStatus HttpStatusCode.Created
this.headers[HttpHeaders.Location] shouldStartWith localIri(artifactsPath)
val uri = getLocation(this.headers[HttpHeaders.Location].toString())
httpGet("$uri?download") {
}.apply {
this shouldHaveStatus HttpStatusCode.OK
this.contentType() shouldBe ContentType.Text.Plain
this.bodyAsText() shouldContain "foo"
}
}
}
}
"download an artifact by id - binary" {
testApplication {
httpPost(artifactsPath) {
header("Content-Type", "application/octet-stream")
setBody("foo".toByteArray())
}.apply {
this shouldHaveStatus HttpStatusCode.Created
this.headers[HttpHeaders.Location] shouldStartWith localIri(artifactsPath)
val uri = getLocation(this.headers[HttpHeaders.Location].toString())
httpGet("$uri?download") {
}.apply {
this shouldHaveStatus HttpStatusCode.OK
this.contentType() shouldBe ContentType.Application.OctetStream
this.bodyAsText() shouldContain "foo"
}
}
}
}
"get an artifact by id - URI" {
testApplication {
httpPost(artifactsPath) {
header("Content-Type", "text/turtle")
setBody("<http://openmbee.org> <http://openmbee.org> <http://openmbee.org> .")
}.apply {
this shouldHaveStatus HttpStatusCode.Created
this.headers[HttpHeaders.Location] shouldStartWith localIri(artifactsPath)
val uri = getLocation(this.headers[HttpHeaders.Location].toString())
httpGet("$uri?download") {}.apply {
this shouldHaveStatus HttpStatusCode.OK
this.contentType().toString() shouldBeEqual "text/turtle"
this.bodyAsText() shouldBeEqual "<http://openmbee.org> <http://openmbee.org> <http://openmbee.org> ."
}
}
}
}
// Not used http methods that should fail
"artifact/{id} rejects other methods" {
testApplication {
httpPost(artifactsPath) {
header("Content-Type", "text/plain")
setBody("foo")
}.apply {
this shouldHaveStatus HttpStatusCode.Created
this.headers[HttpHeaders.Location] shouldStartWith localIri(artifactsPath)
val uri = getLocation(this.headers[HttpHeaders.Location].toString())
onlyAllowsMethods(
uri, setOf(
HttpMethod.Head,
HttpMethod.Get
)
)
}
}
}
}
// Just gets the artifactsPath/{id} part of the location
fun getLocation(path: String): String{
return path.removePrefix(ROOT_CONTEXT)
}
fun getURI(path: String): String{
return path.removePrefix("${ROOT_CONTEXT}$artifactsPath/")
}
}
/**
* Reads all files in a zip file and returns map of filename to file contents
*/
fun readZipContents(zipBytes: ByteArray): Map<String, String> {
val contents = mutableMapOf<String, String>()
ZipInputStream(ByteArrayInputStream(zipBytes)).use { zipInputStream ->
var entry: ZipEntry? = zipInputStream.nextEntry
while (entry != null) {
if (!entry.isDirectory) {
val fileName = entry.name
// Read the content of the current entry as a String
val fileContent = zipInputStream.bufferedReader(Charsets.UTF_8).readText()
contents[fileName] = fileContent
}
zipInputStream.closeEntry() // Close the current entry
entry = zipInputStream.nextEntry // Move to the next entry
}
}
return contents
}