Skip to content

Commit

Permalink
add preferredAnnotationName to client; add integrationtest to show re…
Browse files Browse the repository at this point in the history
…using annotation names is possible once the original was deleted.
  • Loading branch information
brambg committed Aug 9, 2023
1 parent dd79821 commit db16668
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,25 +231,35 @@ class AnnoRepoClient @JvmOverloads constructor(
* @return
*/
fun createAnnotation(
containerName: String, annotation: Any,
): Either<RequestError, CreateAnnotationResult> = doPost(
request = webTarget.path(W3C).path(containerName).request(),
entity = Entity.json(annotation),
responseHandlers = mapOf(Response.Status.CREATED to { response ->
val location = response.location()!!
val annotationName = extractAnnotationName(location.toString())
val eTag = response.eTag() ?: ""
Either.Right(
CreateAnnotationResult(
response = response,
location = location,
containerName = containerName,
annotationName = annotationName,
eTag = eTag
containerName: String,
annotation: Any,
preferredAnnotationName: String? = null,
): Either<RequestError, CreateAnnotationResult> {
val target = webTarget.path(W3C).path(containerName)
val request = if (preferredAnnotationName != null) {
target.request().header("slug", preferredAnnotationName)
} else {
target.request()
}
return doPost(
request = request,
entity = Entity.json(annotation),
responseHandlers = mapOf(Response.Status.CREATED to { response ->
val location = response.location()!!
val annotationName = extractAnnotationName(location.toString())
val eTag = response.eTag() ?: ""
Either.Right(
CreateAnnotationResult(
response = response,
location = location,
containerName = containerName,
annotationName = annotationName,
eTag = eTag
)
)
)
})
)
})
)
}

/**
* Read annotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package nl.knaw.huc.annorepo.integration

import java.net.URI
import jakarta.ws.rs.core.EntityTag
import kotlinx.cli.ArgParser
import kotlinx.cli.ArgType
import kotlinx.cli.required
import arrow.core.Either
import arrow.core.getOrElse
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.ObjectWriter
import com.github.ajalt.mordant.rendering.TextColors.*
import com.github.ajalt.mordant.table.table
import com.github.ajalt.mordant.terminal.Terminal
import kotlinx.cli.ArgParser
import kotlinx.cli.ArgType
import kotlinx.cli.required
import nl.knaw.huc.annorepo.api.IndexType
import nl.knaw.huc.annorepo.api.UserEntry
import nl.knaw.huc.annorepo.client.ARResult
Expand Down Expand Up @@ -196,7 +196,7 @@ class IntegrationTest {
val annotation: Map<String, Any> = mapOf("body" to mapOf("id" to "urn:example:blahblahblah"))
t.printStep("Adding annotation with body.id field: ")
t.printJson(annotation)
val car = createAnnotation(containerName, annotation).getOrElse { throw Exception() }
val car = createAnnotation(containerName, annotation, null).getOrElse { throw Exception() }
// t.println(green(car.toString()))

val fc1 = getFieldInfo(containerName).getOrElse { throw Exception() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void testCreateAnnotation() {
.withBody("http://example.org/annotation1")
.withTarget("http://example.org/target")
.build();
Boolean success = client.createAnnotation(containerName, annotation).fold(
Boolean success = client.createAnnotation(containerName, annotation, null).fold(
(RequestError error) -> {
handleError(error);
return false;
Expand Down
65 changes: 54 additions & 11 deletions integration-test/src/test/kotlin/IntegratedClientKotlinTester.kt
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class IntegratedClientKotlinTester {
.withTarget("http://example.org/target").build()

// Create
client.createAnnotation(containerName, annotation).fold(
client.createAnnotation(containerName, annotation, null).fold(
{ error: RequestError ->
handleError(error)
false
Expand Down Expand Up @@ -253,6 +253,7 @@ class IntegratedClientKotlinTester {
})
assertThat(success).isTrue
}

}

@Nested
Expand Down Expand Up @@ -406,13 +407,15 @@ class IntegratedClientKotlinTester {
@Test
fun testListIndexes() {
val containerName = "republic"
val success = client.listIndexes(containerName).fold({ error: RequestError ->
handleError(error)
false
}, { (_, indexes): ListIndexesResult ->
doSomethingWith(indexes)
true
})
val success = client.listIndexes(containerName).fold(
{ error: RequestError ->
handleError(error)
false
},
{ (_, indexes): ListIndexesResult ->
doSomethingWith(indexes)
true
})
assertThat(success).isTrue
}

Expand All @@ -423,8 +426,10 @@ class IntegratedClientKotlinTester {
@Test
fun testFieldInfo() {
val containerName = "republic"
client.getFieldInfo(containerName).fold({ error: RequestError -> handleError(error) },
{ (_, fieldInfo): AnnotationFieldInfoResult -> doSomethingWith(fieldInfo) })
client.getFieldInfo(containerName).fold(
{ error: RequestError -> handleError(error) },
{ (_, fieldInfo): AnnotationFieldInfoResult -> doSomethingWith(fieldInfo) }
)
}

@Test
Expand All @@ -435,7 +440,8 @@ class IntegratedClientKotlinTester {
{ error: RequestError -> handleError(error) },
{ (_, distinctValues): ARResult.DistinctAnnotationFieldValuesResult ->
doSomethingWith(distinctValues)
})
}
)
}
}

Expand Down Expand Up @@ -578,6 +584,43 @@ class IntegratedClientKotlinTester {
fail(it.message)
}
}

@Test
fun `you should be able to reuse an annotation name after deleting the existing annotation with that name`() {
either {
val createResult = client.createContainer("test-container").bind()
val containerName = createResult.containerName
val containerETag = createResult.eTag

val annotation1 = WebAnnotation.Builder().withBody("http://example.org/annotation1")
.withTarget("http://example.org/target").build()
val preferredAnnotationName = "my-annotation"
val createAnnotationResult =
client.createAnnotation(containerName, annotation1, preferredAnnotationName).bind()
val annotationETag = createAnnotationResult.eTag
val annotationName = createAnnotationResult.annotationName
assertThat(annotationName).isEqualTo(preferredAnnotationName)

client.deleteAnnotation(containerName, annotationName, annotationETag).bind()

val annotation2 = WebAnnotation.Builder().withBody("http://example.org/annotation2")
.withTarget("http://example.org/target2").build()
val createAnnotationResult2 =
client.createAnnotation(containerName, annotation2, preferredAnnotationName).bind()
val annotationName2 = createAnnotationResult2.annotationName
assertThat(annotationName2).isEqualTo(preferredAnnotationName)
log.info("this link will be valid for 10 seconds:")
println(createAnnotationResult2.location)
Thread.sleep(10_000)

client.deleteContainer(containerName, containerETag, force = true).bind()
}.mapLeft<Void> {
log.error("error=$it")
fail(it.message)
}

}

}

companion object {
Expand Down

0 comments on commit db16668

Please sign in to comment.