Untitled

mail@pastecode.io avatar
unknown
kotlin
3 years ago
2.1 kB
3
Indexable
Never
package com.bftcom.mdm.server.services.tasks

import com.bftcom.mdm.BaseMdmTests
import com.bftcom.mdm.commons.service.TaskStateEnum
import org.junit.Test
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import kotlin.test.assertEquals
import kotlin.test.assertTrue


open class TaskServiceInitFromQueue : BaseMdmTests() {

    @Autowired
    lateinit var chunkPublisher: ChunkPublisher

    @Autowired(required = false)
    var chunkExecutor: ChunkExecutor? = null

    @Autowired
    lateinit var taskService: TaskServiceImpl

    private val logger = LoggerFactory.getLogger(this::class.java)

    @Test
    fun testInitFromQueue() {
        if (chunkExecutor != null) {
            logger.info("Chunk executor is not turned off so the method won't run")
            return
        }

        taskService.registerTaskType("test", "xxx", "xxx")
        chunkPublisher.putChunkToQueue(
            Chunk(
                "111",
                TaskType("test", "xxx", "xxx"),
                1,
                TaskStateEnum.started,
                listOf()
            )
        )
        chunkPublisher.putChunkToQueue(
            Chunk(
                "113",
                TaskType("test", "xxx", "xxx"),
                3,
                TaskStateEnum.started,
                listOf(),
                3
            )
        )
        chunkPublisher.putChunkToQueue(
            Chunk(
                "113",
                TaskType("test", "xxx", "xxx"),
                2,
                TaskStateEnum.started,
                listOf(),
                3
            )
        )
        taskService.readTaskStateFromQueue()
        assertTrue(taskService.tasks.any { it.value.id == "111" && it.value.totalChunksCount == -1 && it.value.chunks.size == 1 })
        assertTrue(taskService.tasks.any { it.value.id == "113" && it.value.totalChunksCount == 3 && it.value.chunks.size == 2 })
        assertEquals(2, taskService.tasks.size)
        logger.info("Test executed: tasks' states are recreated successfully")
    }
}