Skip to content

Commit

Permalink
test(backend): test /get-original-metadata locked during new submis…
Browse files Browse the repository at this point in the history
…sions

This is a follow-up to #2846
  • Loading branch information
fengelniederhammer committed Sep 20, 2024
1 parent 266b7bf commit 9deb1f6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ open class SubmissionController(
)
@ApiResponse(
responseCode = "423",
description = "Locked. The metadata is currently being processed.",
description = "Locked. New sequence entries are currently being uploaded.",
)
@GetMapping("/get-original-metadata", produces = [MediaType.APPLICATION_JSON_VALUE])
fun getOriginalMetadata(
Expand All @@ -375,7 +375,7 @@ open class SubmissionController(
headers.add(HttpHeaders.CONTENT_ENCODING, compression.compressionName)
}

val stillProcessing = submissionDatabaseService.checkIfStillProcessingSubmittedData()
val stillProcessing = submitModel.checkIfStillProcessingSubmittedData()
if (stillProcessing) {
return ResponseEntity.status(HttpStatus.LOCKED).build()
}
Expand Down
12 changes: 12 additions & 0 deletions backend/src/main/kotlin/org/loculus/backend/model/SubmitModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import org.loculus.backend.controller.UnprocessableEntityException
import org.loculus.backend.service.datauseterms.DataUseTermsPreconditionValidator
import org.loculus.backend.service.groupmanagement.GroupManagementPreconditionValidator
import org.loculus.backend.service.submission.CompressionAlgorithm
import org.loculus.backend.service.submission.MetadataUploadAuxTable
import org.loculus.backend.service.submission.SequenceUploadAuxTable
import org.loculus.backend.service.submission.UploadDatabaseService
import org.loculus.backend.utils.FastaReader
import org.loculus.backend.utils.metadataEntryStreamAsSequence
import org.loculus.backend.utils.revisionEntryStreamAsSequence
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.multipart.MultipartFile
import java.io.BufferedInputStream
import java.io.File
Expand Down Expand Up @@ -313,4 +316,13 @@ class SubmitModel(
throw UnprocessableEntityException(metadataNotPresentErrorText + sequenceNotPresentErrorText)
}
}

@Transactional(readOnly = true)
fun checkIfStillProcessingSubmittedData(): Boolean {
val metadataInAuxTable: Boolean =
MetadataUploadAuxTable.select(MetadataUploadAuxTable.submissionIdColumn).count() > 0
val sequencesInAuxTable: Boolean =
SequenceUploadAuxTable.select(SequenceUploadAuxTable.sequenceSubmissionIdColumn).count() > 0
return metadataInAuxTable || sequencesInAuxTable
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -950,14 +950,6 @@ open class SubmissionDatabaseService(
)
}

fun checkIfStillProcessingSubmittedData(): Boolean {
val metadataInAuxTable: Boolean =
MetadataUploadAuxTable.select(MetadataUploadAuxTable.submissionIdColumn).count() > 0
val sequencesInAuxTable: Boolean =
SequenceUploadAuxTable.select(SequenceUploadAuxTable.sequenceSubmissionIdColumn).count() > 0
return metadataInAuxTable || sequencesInAuxTable
}

fun streamOriginalMetadata(
authenticatedUser: AuthenticatedUser,
organism: Organism,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.jetbrains.exposed.sql.VarCharColumnType
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.batchInsert
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.statements.StatementType
import org.jetbrains.exposed.sql.transactions.transaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package org.loculus.backend.controller.submission

import com.fasterxml.jackson.module.kotlin.readValue
import com.github.luben.zstd.ZstdInputStream
import io.mockk.every
import io.mockk.mockk
import kotlinx.datetime.LocalDateTime
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.hasSize
import org.hamcrest.Matchers.not
import org.junit.jupiter.api.Test
import org.loculus.backend.api.AccessionVersionOriginalMetadata
import org.loculus.backend.api.Organism
import org.loculus.backend.api.Status
import org.loculus.backend.auth.AuthenticatedUser
import org.loculus.backend.controller.DEFAULT_ORGANISM
import org.loculus.backend.controller.EndpointTest
import org.loculus.backend.controller.OTHER_ORGANISM
Expand All @@ -18,6 +23,8 @@ import org.loculus.backend.controller.groupmanagement.GroupManagementControllerC
import org.loculus.backend.controller.groupmanagement.andGetGroupId
import org.loculus.backend.controller.jacksonObjectMapper
import org.loculus.backend.controller.submission.SubmitFiles.DefaultFiles
import org.loculus.backend.service.submission.UploadDatabaseService
import org.loculus.backend.utils.MetadataEntry
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
Expand All @@ -33,6 +40,7 @@ class GetOriginalMetadataEndpointTest(
@Autowired val convenienceClient: SubmissionConvenienceClient,
@Autowired val submissionControllerClient: SubmissionControllerClient,
@Autowired val groupManagementClient: GroupManagementControllerClient,
@Autowired val uploadDatabaseService: UploadDatabaseService,
) {
@Test
fun `GIVEN invalid authorization token THEN returns 401 Unauthorized`() {
Expand Down Expand Up @@ -148,4 +156,28 @@ class GetOriginalMetadataEndpointTest(
val responseAccessionVersions = responseBody.map { it.displayAccessionVersion() }.toSet()
assertThat(responseAccessionVersions, `is`(expectedAccessionVersions))
}

@Test
fun `GIVEN there are sequences currently being uploaded THEN returns locked`() {
val uploadId = "upload id"
val mockUser = mockk<AuthenticatedUser>()
every { mockUser.username }.returns("username")

uploadDatabaseService.batchInsertMetadataInAuxTable(
uploadId = uploadId,
authenticatedUser = mockUser,
groupId = 1,
submittedOrganism = Organism("organism"),
uploadedMetadataBatch = listOf(MetadataEntry("submission id", mapOf("key" to "value"))),
uploadedAt = LocalDateTime(2024, 1, 1, 1, 1, 1),
)

submissionControllerClient.getOriginalMetadata()
.andExpect(status().isLocked)

uploadDatabaseService.deleteUploadData(uploadId)

submissionControllerClient.getOriginalMetadata()
.andExpect(status().isOk)
}
}

0 comments on commit 9deb1f6

Please sign in to comment.