Skip to content

Commit

Permalink
feat: purge turtle files by fdkID
Browse files Browse the repository at this point in the history
  • Loading branch information
NilsOveTen committed Aug 22, 2024
1 parent 41c245b commit 53650e9
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ open class ConceptsController(
ResponseEntity(HttpStatus.OK)
} else ResponseEntity(HttpStatus.FORBIDDEN)

@DeleteMapping("/{id}")
fun purgeConceptById(
@AuthenticationPrincipal jwt: Jwt,
@PathVariable id: String
): ResponseEntity<Void> =
if (endpointPermissions.hasAdminPermission(jwt)) {
conceptService.purgeByFdkId(id)
ResponseEntity(HttpStatus.NO_CONTENT)
} else ResponseEntity(HttpStatus.FORBIDDEN)

@PostMapping("/remove-duplicates")
fun removeDuplicates(
@AuthenticationPrincipal jwt: Jwt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,13 @@ class ConceptService(
}
}

// Purges everything associated with a removed fdkID
fun purgeByFdkId(fdkId: String) {
conceptMetaRepository.findAllByFdkId(fdkId)
.also { concepts -> if (concepts.any { !it.removed }) throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Unable to purge files, concept with id $fdkId has not been removed") }
.run { conceptMetaRepository.deleteAll(this) }

turtleService.deleteTurtleFiles(fdkId)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ class TurtleService(private val turtleRepository: TurtleRepository) {
?.turtle
?.let { ungzip(it) }

fun deleteTurtleFiles(fdkId: String) {
turtleRepository.findAllById(
listOf(
conceptTurtleID(fdkId, true),
conceptTurtleID(fdkId, false)
)
).run { turtleRepository.deleteAll(this) }
}

}

private fun collectionTurtleID(fdkId: String, withFDKRecords: Boolean): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,54 @@ class ConceptTest : ApiTestContext() {
}
}

@Nested
internal inner class PurgeById {

@Test
fun unauthorizedForNoToken() {
val response = authorizedRequest(port, "/concepts/removed", null, HttpMethod.DELETE)
assertEquals(HttpStatus.UNAUTHORIZED.value(), response["status"])
}

@Test
fun forbiddenWithNonSysAdminRole() {
val response = authorizedRequest(
port,
"/concepts/removed",
JwtToken(Access.ORG_WRITE).toString(),
HttpMethod.DELETE
)
assertEquals(HttpStatus.FORBIDDEN.value(), response["status"])
}

@Test
fun badRequestWhenNotAlreadyRemoved() {
val response = authorizedRequest(
port,
"/concepts/$CONCEPT_1_ID",
JwtToken(Access.ROOT).toString(),
HttpMethod.DELETE
)
assertEquals(HttpStatus.BAD_REQUEST.value(), response["status"])
}

@Test
fun purgingStopsDeepLinking() {
val pre = apiGet(port, "/concepts/removed", "text/turtle")
assertEquals(HttpStatus.OK.value(), pre["status"])

val response = authorizedRequest(
port,
"/concepts/removed",
JwtToken(Access.ROOT).toString(),
HttpMethod.DELETE
)
assertEquals(HttpStatus.NO_CONTENT.value(), response["status"])

val post = apiGet(port, "/concepts/removed", "text/turtle")
assertEquals(HttpStatus.NOT_FOUND.value(), post["status"])
}

}

}
21 changes: 19 additions & 2 deletions src/test/kotlin/no/fdk/fdk_concept_harvester/utils/DatabaseData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ val CONCEPT_1 = ConceptMeta(
issued = 1609852539831, modified = 1609852539831
)

val REMOVED_CONCEPT = ConceptMeta(
uri = "https://example.com/begrep/removed", fdkId = "removed",
isPartOf = "http://localhost:5050/collections/9b8f1c42-1161-33b1-9d43-a733ee94ddfc",
removed = true, issued = 1609852539831, modified = 1609852539831
)

val CONCEPT_2 = ConceptMeta(
uri = "https://example.com/begrep/2", fdkId = "no-collection",
isPartOf = null,
Expand All @@ -53,6 +59,16 @@ val CONCEPT_TURTLE_1_NO_META = TurtleDBO(
turtle = gzip(responseReader.readFile("no_meta_concept_1.ttl"))
)

val REMOVED_CONCEPT_TURTLE_META = TurtleDBO(
id = "concept-removed",
turtle = gzip(responseReader.readFile("concept_1.ttl"))
)

val REMOVED_CONCEPT_TURTLE_NO_META = TurtleDBO(
id = "concept-no-records-removed",
turtle = gzip(responseReader.readFile("no_meta_concept_1.ttl"))
)

val CONCEPT_TURTLE_2_META = TurtleDBO(
id = "concept-no-collection",
turtle = gzip(responseReader.readFile("concept_2.ttl"))
Expand Down Expand Up @@ -93,12 +109,12 @@ fun turleDBPopulation(): List<Document> =
COLLECTION_UNION_TURTLE, HARVESTED_TURTLE, CONCEPT_TURTLE_0_META,
CONCEPT_TURTLE_0_NO_META, CONCEPT_TURTLE_1_META, CONCEPT_TURTLE_1_NO_META, COLLECTION_TURTLE_META,
COLLECTION_TURTLE_NO_META, CONCEPT_TURTLE_2_META, CONCEPT_TURTLE_2_NO_META,
COLLECTION_UNION_TURTLE_NO_RECORDS
COLLECTION_UNION_TURTLE_NO_RECORDS, REMOVED_CONCEPT_TURTLE_META, REMOVED_CONCEPT_TURTLE_NO_META
)
.map { it.mapDBO() }

fun conceptDBPopulation(): List<Document> =
listOf(CONCEPT_0, CONCEPT_1, CONCEPT_2)
listOf(CONCEPT_0, CONCEPT_1, CONCEPT_2, REMOVED_CONCEPT)
.map { it.mapDBO() }

fun collectionDBPopulation(): List<Document> =
Expand All @@ -118,6 +134,7 @@ private fun ConceptMeta.mapDBO(): Document =
.append("_id", uri)
.append("fdkId", fdkId)
.append("isPartOf", isPartOf)
.append("removed", removed)
.append("issued", issued)
.append("modified", modified)

Expand Down

0 comments on commit 53650e9

Please sign in to comment.