Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: purge turtle files by fdkID #164

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,27 @@ open class InformationModelController(
}
}

@DeleteMapping("/{id}")
@PostMapping("/{id}/remove")
fun removeInformationModelById(
@AuthenticationPrincipal jwt: Jwt,
@PathVariable id: String
): ResponseEntity<Void> =
if (endpointPermissions.hasAdminPermission(jwt)) {
informationModelService.removeInformationModel(id)
ResponseEntity(HttpStatus.OK)
} else ResponseEntity(HttpStatus.FORBIDDEN)

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

@PostMapping("/duplicates")
@PostMapping("/remove-duplicates")
fun removeDuplicates(
@AuthenticationPrincipal jwt: Jwt,
@RequestBody duplicates: List<DuplicateIRI>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class TurtleRepository(private val gridFsTemplate: GridFsTemplate) {
gridFsTemplate.store(content.byteInputStream(), filename)
}

fun deleteAllByFilename(filenames: List<String>) {
filenames.forEach { filename ->
gridFsTemplate.delete(Query(Criteria.where("filename").`is`(filename)))
}
}

fun readFileContent(filename: String): String? =
gridFsTemplate.findOne(Query(Criteria.where("filename").`is`(filename)))
?.let { gridFsTemplate.getResource(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,13 @@ class InformationModelService(
}
}

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

turtleService.deleteTurtleFiles(fdkId)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ class TurtleService (private val turtleRepository: TurtleRepository) {
fun findUnionModel(withRecords: Boolean): String? =
findOne(filenameUnion(withRecords))

fun deleteTurtleFiles(fdkId: String) {
turtleRepository.deleteAllByFilename(
listOf(
filenameInformationModel(fdkId, true),
filenameInformationModel(fdkId, false)
)
)
}

private fun filenameCatalog(fdkId: String, withFDKRecords: Boolean): String =
"$CATALOG_ID_PREFIX${if (withFDKRecords) "" else NO_RECORDS_ID_PREFIX}$fdkId"

Expand Down
1 change: 0 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ logging:
level.org.apache.jena: ERROR
server:
port: 8080
error.include-message: always
fuseki:
unionGraphUri: ${FDK_SPARQL_SERVICE_URI:http://fdk-sparql-service:8080}/fuseki/harvested?graph=https://informationmodels.fellesdatakatalog.digdir.no
application:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import no.fdk.fdk_informationmodel_harvester.utils.ApiTestContext
import no.fdk.fdk_informationmodel_harvester.utils.INFO_MODEL_DBO_0
import no.fdk.fdk_informationmodel_harvester.utils.INFO_MODEL_DBO_1
import no.fdk.fdk_informationmodel_harvester.utils.INFO_MODEL_ID_0
import no.fdk.fdk_informationmodel_harvester.utils.INFO_MODEL_ID_1
import no.fdk.fdk_informationmodel_harvester.utils.TestResponseReader
import no.fdk.fdk_informationmodel_harvester.utils.apiGet
import no.fdk.fdk_informationmodel_harvester.utils.authorizedRequest
Expand Down Expand Up @@ -66,45 +67,45 @@ class InformationModelTest : ApiTestContext() {
@Test
fun unauthorizedForNoToken() {
val response = authorizedRequest(
"/informationmodels/$INFO_MODEL_ID_0",
"/informationmodels/$INFO_MODEL_ID_0/remove",
null,
port,
HttpMethod.DELETE
HttpMethod.POST
)
assertEquals(HttpStatus.UNAUTHORIZED.value(), response["status"])
}

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

@Test
fun notFoundWhenIdNotInDB() {
val response = authorizedRequest(
"/informationmodels/123",
"/informationmodels/123/remove",
JwtToken(Access.ROOT).toString(),
port,
HttpMethod.DELETE
HttpMethod.POST
)
assertEquals(HttpStatus.NOT_FOUND.value(), response["status"])
}

@Test
fun okWithSysAdminRole() {
val response = authorizedRequest(
"/informationmodels/$INFO_MODEL_ID_0",
"/informationmodels/$INFO_MODEL_ID_0/remove",
JwtToken(Access.ROOT).toString(),
port,
HttpMethod.DELETE
HttpMethod.POST
)
assertEquals(HttpStatus.NO_CONTENT.value(), response["status"])
assertEquals(HttpStatus.OK.value(), response["status"])
}
}

Expand All @@ -115,7 +116,7 @@ class InformationModelTest : ApiTestContext() {
fun unauthorizedForNoToken() {
val body = listOf(DuplicateIRI(iriToRemove = INFO_MODEL_DBO_0.uri, iriToRetain = INFO_MODEL_DBO_1.uri))
val response = authorizedRequest(
"/informationmodels/duplicates",
"/informationmodels/remove-duplicates",
null,
port,
HttpMethod.POST,
Expand All @@ -128,7 +129,7 @@ class InformationModelTest : ApiTestContext() {
fun forbiddenWithNonSysAdminRole() {
val body = listOf(DuplicateIRI(iriToRemove = INFO_MODEL_DBO_0.uri, iriToRetain = INFO_MODEL_DBO_1.uri))
val response = authorizedRequest(
"/informationmodels/duplicates",
"/informationmodels/remove-duplicates",
JwtToken(Access.ORG_WRITE).toString(),
port,
HttpMethod.POST,
Expand All @@ -142,7 +143,7 @@ class InformationModelTest : ApiTestContext() {
val body = listOf(DuplicateIRI(iriToRemove = "https://123.no", iriToRetain = INFO_MODEL_DBO_1.uri))
val response =
authorizedRequest(
"/informationmodels/duplicates",
"/informationmodels/remove-duplicates",
JwtToken(Access.ROOT).toString(),
port,
HttpMethod.POST,
Expand All @@ -155,7 +156,7 @@ class InformationModelTest : ApiTestContext() {
fun okWithSysAdminRole() {
val body = listOf(DuplicateIRI(iriToRemove = INFO_MODEL_DBO_0.uri, iriToRetain = INFO_MODEL_DBO_1.uri))
val response = authorizedRequest(
"/informationmodels/duplicates",
"/informationmodels/remove-duplicates",
JwtToken(Access.ROOT).toString(),
port,
HttpMethod.POST,
Expand All @@ -165,6 +166,48 @@ class InformationModelTest : ApiTestContext() {
}
}

@Nested
internal inner class PurgeById {

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

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

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

@Test
fun okWithSysAdminRole() {
val response = authorizedRequest(
"/informationmodels/removed",
JwtToken(Access.ROOT).toString(),
port,
HttpMethod.DELETE
)
assertEquals(HttpStatus.NO_CONTENT.value(), response["status"])
}

}

}
Loading