Skip to content

Commit

Permalink
Pivotal #ID 176479720: Mongo Metadata Query (#328)
Browse files Browse the repository at this point in the history
* added project and basic submission query support
  • Loading branch information
Juan-EBI authored Jan 27, 2021
1 parent 224839a commit 6acafa1
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 55 deletions.
4 changes: 4 additions & 0 deletions commons/commons-test/src/main/kotlin/ebi/ac/uk/db/Versions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ebi.ac.uk.db

const val MONGO_VERSION = "mongo:4.0.10"
const val MYSQL_VERSION = "mysql:5.7.33"
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import org.springframework.data.mongodb.repository.Query

interface SubmissionMongoRepository : MongoRepository<DocSubmission, String> {

fun findByAccNo(accNo: String): DocSubmission?

fun existsByAccNo(accNo: String): Boolean

fun getByAccNo(accNo: String): DocSubmission
Expand All @@ -15,6 +17,10 @@ interface SubmissionMongoRepository : MongoRepository<DocSubmission, String> {

fun getByAccNoAndVersionGreaterThan(accNo: String, version: Int): List<DocSubmission>

fun findFirstByAccNoOrderByVersionDesc(accNo: String): DocSubmission?

fun getFirstByAccNo(accNo: String): DocSubmission

@Query(value = "{ 'groupId' : ?0 }", fields = "{ '_id': 0, 'user.\$id':1 }")
fun findAllUserIdByGroupId(accNo: String): List<String?>?
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ class MongoDbReposConfig {
): SubmissionRequestDocDataRepository = SubmissionRequestDocDataRepository(submissionRequestRepository)

@Bean
internal fun submissionMetaQueryService(): SubmissionMongoMetaQueryService = SubmissionMongoMetaQueryService()
internal fun submissionMongoMetaQueryService(
submissionDocDataRepository: SubmissionDocDataRepository
): SubmissionMongoMetaQueryService = SubmissionMongoMetaQueryService(submissionDocDataRepository)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ac.uk.ebi.biostd.persistence.doc.model

import ac.uk.ebi.biostd.persistence.common.model.BasicSubmission
import ebi.ac.uk.model.SubmissionMethod
import ebi.ac.uk.model.constants.ProcessingStatus
import java.time.ZoneOffset

fun DocSubmission.asBasicSubmission(): BasicSubmission {
return BasicSubmission(
accNo = accNo,
version = version,
secretKey = secretKey,
title = title,
relPath = relPath,
released = released,
creationTime = creationTime.atOffset(ZoneOffset.UTC),
modificationTime = modificationTime.atOffset(ZoneOffset.UTC),
releaseTime = releaseTime?.atOffset(ZoneOffset.UTC),
status = status.toProcessingStatus(),
method = method.toSubmissionMethod(),
owner = owner)
}

private fun DocProcessingStatus.toProcessingStatus(): ProcessingStatus =
when (this) {
DocProcessingStatus.PROCESSED -> ProcessingStatus.PROCESSED
DocProcessingStatus.PROCESSING -> ProcessingStatus.PROCESSING
DocProcessingStatus.REQUESTED -> ProcessingStatus.REQUESTED
}

private fun DocSubmissionMethod.toSubmissionMethod(): SubmissionMethod =
when (this) {
DocSubmissionMethod.FILE -> SubmissionMethod.FILE
DocSubmissionMethod.PAGE_TAB -> SubmissionMethod.PAGE_TAB
DocSubmissionMethod.UNKNOWN -> SubmissionMethod.UNKNOWN
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,34 @@ package ac.uk.ebi.biostd.persistence.doc.service
import ac.uk.ebi.biostd.persistence.common.model.BasicProject
import ac.uk.ebi.biostd.persistence.common.model.BasicSubmission
import ac.uk.ebi.biostd.persistence.common.service.SubmissionMetaQueryService
import ac.uk.ebi.biostd.persistence.doc.db.data.SubmissionDocDataRepository
import ac.uk.ebi.biostd.persistence.doc.model.DocSubmission
import ac.uk.ebi.biostd.persistence.doc.model.asBasicSubmission
import ac.uk.ebi.biostd.persistence.exception.ProjectNotFoundException
import ac.uk.ebi.biostd.persistence.exception.ProjectWithoutPatternException
import ebi.ac.uk.model.constants.SubFields.ACC_NO_TEMPLATE
import java.time.ZoneOffset

class SubmissionMongoMetaQueryService(
private val submissionDocDataRepository: SubmissionDocDataRepository
) : SubmissionMetaQueryService {

class SubmissionMongoMetaQueryService : SubmissionMetaQueryService {
override fun getBasicProject(accNo: String): BasicProject {
TODO("Not yet implemented")
}
val projectDb: DocSubmission? = submissionDocDataRepository.findByAccNo(accNo)
require(projectDb != null) { throw ProjectNotFoundException(accNo) }

val projectPattern = projectDb.attributes.firstOrNull { it.name == ACC_NO_TEMPLATE.value }?.value
?: throw ProjectWithoutPatternException(accNo)

override fun findLatestBasicByAccNo(accNo: String): BasicSubmission? {
return null
return BasicProject(projectDb.accNo, projectPattern, projectDb.releaseTime?.atOffset(ZoneOffset.UTC))
}

override fun findLatestBasicByAccNo(accNo: String): BasicSubmission? =
submissionDocDataRepository.findFirstByAccNoOrderByVersionDesc(accNo)?.asBasicSubmission()

override fun getAccessTags(accNo: String): List<String> {
return emptyList()
}

override fun existByAccNo(accNo: String): Boolean {
return false
}
override fun existByAccNo(accNo: String): Boolean = submissionDocDataRepository.existsByAccNo(accNo)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package ac.uk.ebi.biostd.persistence.doc.db.data

import ac.uk.ebi.biostd.persistence.doc.db.repositories.SubmissionMongoRepository
import ac.uk.ebi.biostd.persistence.doc.integration.MongoDbReposConfig
import ac.uk.ebi.biostd.persistence.doc.model.DocProcessingStatus
import ac.uk.ebi.biostd.persistence.doc.model.DocProcessingStatus.PROCESSED
import ac.uk.ebi.biostd.persistence.doc.model.DocProcessingStatus.PROCESSING
import ac.uk.ebi.biostd.persistence.doc.model.DocSection
import ac.uk.ebi.biostd.persistence.doc.model.DocSubmission
import ac.uk.ebi.biostd.persistence.doc.model.DocSubmissionMethod
import ac.uk.ebi.biostd.persistence.doc.test.doc.testDocSubmission
import ebi.ac.uk.db.MONGO_VERSION
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
Expand All @@ -21,7 +19,6 @@ import org.testcontainers.containers.MongoDBContainer
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.junit.jupiter.Testcontainers
import org.testcontainers.utility.DockerImageName
import java.time.Instant

@ExtendWith(SpringExtension::class)
@Testcontainers
Expand All @@ -38,48 +35,66 @@ internal class SubmissionDocDataRepositoryTest {
inner class UpdateStatus {
@Test
fun `successful status update`() {
submissionMongoRepository.save(testDocSubmission("accNo1", 1, PROCESSING))
submissionMongoRepository.save(testDocSubmission.copy(accNo = "accNo10", version = 1, status = PROCESSING))

testInstance.updateStatus(PROCESSED, "accNo1", 1)
testInstance.updateStatus(PROCESSED, "accNo10", 1)

assertThat(submissionMongoRepository.getByAccNo("accNo1").status).isEqualTo(PROCESSED)
assertThat(submissionMongoRepository.getByAccNo(accNo = "accNo10").status).isEqualTo(PROCESSED)
}

@Test
fun `status should not be updated when version does not match`() {
submissionMongoRepository.save(testDocSubmission("accNo2", 1, PROCESSING))
submissionMongoRepository.save(testDocSubmission.copy(accNo = "accNo20", version = 1, status = PROCESSING))

testInstance.updateStatus(PROCESSED, "accNo2", 3)
testInstance.updateStatus(PROCESSED, "accNo20", 3)

assertThat(submissionMongoRepository.getByAccNo("accNo2").status).isEqualTo(PROCESSING)
assertThat(submissionMongoRepository.getByAccNo(accNo = "accNo20").status).isEqualTo(PROCESSING)
}
}

@Test
fun `successful status update`() {
submissionMongoRepository.save(testDocSubmission.copy(accNo = "accNo1", version = 1, status = PROCESSING))

testInstance.updateStatus(PROCESSED, "accNo1", 1)

assertThat(submissionMongoRepository.getByAccNo("accNo1").status).isEqualTo(PROCESSED)
}

@Test
fun `status update when version does not match so do not update`() {
submissionMongoRepository.save(testDocSubmission.copy(accNo = "accNo2", version = 1, status = PROCESSING))

testInstance.updateStatus(PROCESSED, "accNo2", 3)

assertThat(submissionMongoRepository.getByAccNo(accNo = "accNo2").status).isEqualTo(PROCESSING)
}

@Test
fun getCurrentVersion() {
submissionMongoRepository.save(testDocSubmission("accNo3", -1, PROCESSED))
submissionMongoRepository.save(testDocSubmission("accNo3", 2, PROCESSED))
submissionMongoRepository.save(testDocSubmission.copy(accNo = "accNo3", version = -1, status = PROCESSED))
submissionMongoRepository.save(testDocSubmission.copy(accNo = "accNo3", version = 2, status = PROCESSED))

assertThat(testInstance.getCurrentVersion("accNo3")).isEqualTo(2)
}

@Test
fun expireActiveProcessedVersions() {
submissionMongoRepository.save(testDocSubmission("accNo4", -1, PROCESSED))
submissionMongoRepository.save(testDocSubmission("accNo4", 2, PROCESSED))
submissionMongoRepository.save(testDocSubmission("accNo4", 3, PROCESSING))
submissionMongoRepository.save(testDocSubmission.copy(accNo = "accNo4", version = -1, status = PROCESSED))
submissionMongoRepository.save(testDocSubmission.copy(accNo = "accNo4", version = 2, status = PROCESSED))
submissionMongoRepository.save(testDocSubmission.copy(accNo = "accNo4", version = 3, status = PROCESSING))

testInstance.expireActiveProcessedVersions("accNo4")

assertThat(submissionMongoRepository.getByAccNoAndVersion("accNo4", -1)).isNotNull
assertThat(submissionMongoRepository.getByAccNoAndVersion("accNo4", -2)).isNotNull
assertThat(submissionMongoRepository.getByAccNoAndVersion("accNo4", version = -1)).isNotNull
assertThat(submissionMongoRepository.getByAccNoAndVersion("accNo4", version = -2)).isNotNull
assertThat(submissionMongoRepository.getByAccNoAndVersion("accNo4", 3)).isNotNull
}

companion object {

@Container
val mongoContainer: MongoDBContainer = MongoDBContainer(DockerImageName.parse("mongo:4.0.10"))
val mongoContainer: MongoDBContainer = MongoDBContainer(DockerImageName.parse(MONGO_VERSION))

@JvmStatic
@DynamicPropertySource
Expand All @@ -89,29 +104,4 @@ internal class SubmissionDocDataRepositoryTest {
register.add("app.persistence.enableMongo") { "true" }
}
}

private fun testDocSubmission(accNo: String, version: Int, status: DocProcessingStatus): DocSubmission {
return DocSubmission(
id = "",
accNo = accNo,
version = version,
owner = "",
submitter = "",
title = "",
method = DocSubmissionMethod.PAGE_TAB,
relPath = "",
rootPath = "",
released = true,
secretKey = "",
status = status,
releaseTime = Instant.ofEpochSecond(1),
modificationTime = Instant.ofEpochSecond(1),
creationTime = Instant.ofEpochSecond(1),
section = DocSection(type = "Study"),
attributes = listOf(),
tags = listOf(),
projects = listOf(),
stats = listOf()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ac.uk.ebi.biostd.persistence.doc.service

import ac.uk.ebi.biostd.persistence.doc.db.repositories.SubmissionMongoRepository
import ac.uk.ebi.biostd.persistence.doc.integration.MongoDbReposConfig
import ac.uk.ebi.biostd.persistence.doc.model.DocAttribute
import ac.uk.ebi.biostd.persistence.doc.model.DocProcessingStatus.PROCESSED
import ac.uk.ebi.biostd.persistence.doc.test.doc.testDocSubmission
import ebi.ac.uk.db.MONGO_VERSION
import ebi.ac.uk.model.constants.SubFields
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.testcontainers.containers.MongoDBContainer
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.junit.jupiter.Testcontainers
import org.testcontainers.utility.DockerImageName

@ExtendWith(SpringExtension::class)
@Testcontainers
@SpringBootTest(classes = [MongoDbReposConfig::class])
internal class SubmissionMongoMetaQueryServiceTest {

@Autowired
lateinit var testInstance: SubmissionMongoMetaQueryService

@Autowired
lateinit var submissionMongoRepository: SubmissionMongoRepository

@Test
fun getBasicProject() {
submissionMongoRepository.save(testDocSubmission.copy(
accNo = "accNo1",
version = 1,
status = PROCESSED,
attributes = listOf(DocAttribute(SubFields.ACC_NO_TEMPLATE.value, "template"))
))

val result = testInstance.getBasicProject("accNo1")

assertThat(result.accNo).isEqualTo("accNo1")
}

@Test
fun findLatestBasicByAccNo() {
submissionMongoRepository.save(testDocSubmission.copy(accNo = "accNo2", version = 1, status = PROCESSED))
submissionMongoRepository.save(testDocSubmission.copy(accNo = "accNo2", version = -2, status = PROCESSED))
submissionMongoRepository.save(testDocSubmission.copy(accNo = "accNo2", version = 4, status = PROCESSED))

val lastVersion = testInstance.findLatestBasicByAccNo("accNo2")

assertThat(lastVersion).isNotNull()
assertThat(lastVersion!!.version).isEqualTo(4)
}

@Test
fun `exists by AccNo when exists`() {
submissionMongoRepository.save(testDocSubmission.copy(accNo = "accNo3", version = 1, status = PROCESSED))

assertThat(submissionMongoRepository.existsByAccNo("accNo3")).isTrue()
}

@Test
fun `exist by AccNo when don't exists`() {
submissionMongoRepository.save(testDocSubmission.copy(accNo = "accNo4", version = 1, status = PROCESSED))

assertThat(submissionMongoRepository.existsByAccNo("accNo5")).isFalse()
}

companion object {

@Container
val mongoContainer: MongoDBContainer = MongoDBContainer(DockerImageName.parse(MONGO_VERSION))

@JvmStatic
@DynamicPropertySource
fun propertySource(register: DynamicPropertyRegistry) {
register.add("spring.data.mongodb.uri") { mongoContainer.getReplicaSetUrl("testDb") }
register.add("spring.data.mongodb.database") { "testDb" }
register.add("app.persistence.enableMongo") { "true" }
}
}
}
Loading

0 comments on commit 6acafa1

Please sign in to comment.