Skip to content

Commit

Permalink
Merge pull request #179 from Open-MBEE/patch-branch-tests
Browse files Browse the repository at this point in the history
Patch branch tests
  • Loading branch information
dlamoris authored Dec 6, 2024
2 parents 5deb5cc + ce0dfa9 commit 23c390f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 11 deletions.
13 changes: 3 additions & 10 deletions src/main/kotlin/org/openmbee/flexo/mms/GuardedPatch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,13 @@ import org.openmbee.flexo.mms.server.LdpMutateResponse
import org.openmbee.flexo.mms.server.SparqlUpdateRequest


fun quadDataFilter(subjectIri: String): (Quad)->Boolean {
return {
it.subject.isURI && it.subject.uri == subjectIri && !it.predicate.uri.contains(FORBIDDEN_PREDICATES_REGEX)
}
}

fun quadPatternFilter(subjectIri: String): (Quad)->Boolean {
return {
if(it.subject.isVariable) {
throw VariablesNotAllowedInUpdateException("subject")
}
else if(!it.subject.isURI || it.subject.uri != subjectIri) {
throw Http400Exception("All subjects must be exactly <${subjectIri}>. Refusing to evalute ${it.subject}")
throw Http400Exception("All subjects must be exactly <${subjectIri}>. Refusing to evaluate ${it.subject}")
}
else if(it.predicate.isVariable) {
throw VariablesNotAllowedInUpdateException("predicate")
Expand Down Expand Up @@ -61,14 +55,13 @@ suspend fun <TResponseContext: LdpMutateResponse> LdpDcLayer1Context<TResponseCo
var whereString = ""

// prepare quad filters
val dataFilter = quadDataFilter(baseIri)
val patternFilter = quadPatternFilter(baseIri)

// each operation
for(update in sparqlUpdateAst.operations) {
when(update) {
is UpdateDataDelete -> deleteBgpString = asSparqlGroup(update.quads, dataFilter)
is UpdateDataInsert -> insertBgpString = asSparqlGroup(update.quads, dataFilter)
is UpdateDataDelete -> deleteBgpString = asSparqlGroup(update.quads, patternFilter)
is UpdateDataInsert -> insertBgpString = asSparqlGroup(update.quads, patternFilter)
is UpdateDeleteWhere -> {
deleteBgpString = asSparqlGroup(update.quads, patternFilter)
whereString = deleteBgpString
Expand Down
92 changes: 92 additions & 0 deletions src/test/kotlin/org/openmbee/flexo/mms/util/LinkedDataPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,98 @@ class LinkedDataPlatformDirectContainerTests(
}
}
}

"PATCH $resourcePath - SPARQL UPDATE: patch branch with bad delete data" {
val createdBase = resourceCreator() // This creates a tuple
withTest {
httpPatch(resourcePath) {
setSparqlUpdateBody(
withAllTestPrefixes("""
delete data {
<> mms:id <urn:mms:foo> .
}
""".trimIndent())
)
}.apply {
response shouldHaveStatus HttpStatusCode.BadRequest
}
}
}

"PATCH $resourcePath - SPARQL UPDATE: patch branch with bad insert data" {
val createdBase = resourceCreator()
withTest {
httpPatch(resourcePath) {
setSparqlUpdateBody(withAllTestPrefixes("""
insert data {
<> mms:id <urn:mms:foo> .
}
""".trimIndent()))
}.apply {
response shouldHaveStatus HttpStatusCode.BadRequest
}
}
}

"PATCH $resourcePath - SPARQL UPDATE: patch branch with bad delete pattern" {
val createdBase = resourceCreator() // This creates a tuple
withTest {
httpPatch(resourcePath) {
setSparqlUpdateBody(
withAllTestPrefixes("""
delete {
<> mms:id <urn:mms:foo> .
}
where {
?s ?p ?o .
}
""".trimIndent())
)
}.apply {
response shouldHaveStatus HttpStatusCode.BadRequest
}
}
}

"PATCH $resourcePath - SPARQL UPDATE: patch branch with bad insert pattern" {
val createdBase = resourceCreator() // This creates a tuple
withTest {
httpPatch(resourcePath) {
setSparqlUpdateBody(
withAllTestPrefixes("""
insert {
<> mms:id <urn:mms:foo> .
}
where {
?s ?p ?o .
}
""".trimIndent())
)
}.apply {
response shouldHaveStatus HttpStatusCode.BadRequest
}
}
}

"PATCH $resourcePath - SPARQL UPDATE: patch branch with bad delete predicate variable" {
val createdBase = resourceCreator() // This creates a tuple
withTest {
httpPatch(resourcePath) {
setSparqlUpdateBody(
withAllTestPrefixes("""
delete {
<> ?p <urn:mms:foo> .
}
where {
?s ?p ?o .
}
""".trimIndent())
)
}.apply {
response shouldHaveStatus HttpStatusCode.BadRequest
}
}
}
}


Expand Down
3 changes: 2 additions & 1 deletion src/test/kotlin/org/openmbee/flexo/mms/util/Requests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ fun TestApplicationEngine.httpRequest(method: HttpMethod, uri: String, setup: Te
}

response shouldHaveOneOfStatuses setOf(
HttpStatusCode.NotFound,
HttpStatusCode.BadRequest,
HttpStatusCode.Forbidden,
HttpStatusCode.NotFound,
HttpStatusCode.MethodNotAllowed,
HttpStatusCode.NotImplemented,
)
Expand Down

0 comments on commit 23c390f

Please sign in to comment.