package io.nrkv.HypeAPI.task.dao

import org.junit.Before
import org.junit.Test
import io.nrkv.HypeAPI.task.dao.implementation.TaskDAOImpl
import io.nrkv.HypeAPI.task.model.implementation.TaskRequestModel
import java.util.*

/**
 * Created by Ivan Nyrkov on 08/06/17.
 */
class TaskDAOTest {

    lateinit var taskDAO: TaskDAO

    @Before
    fun setup() {
        taskDAO = TaskDAOImpl()
    }

    @Test
    fun taskCRUDTest() {
        // Empty
        assert(taskDAO.getAll().isEmpty())
        assert(taskDAO.getById(UUID.randomUUID().toString()) == null)

        // Update on not-existing record won't create it (upsert)
        assert(taskDAO.update(UUID.randomUUID().toString(), TaskRequestModel(title = "Test")) == null)
        assert(taskDAO.getById(UUID.randomUUID().toString()) == null)

        // Task will be successfully created
        val task1Title = "Test"
        val task1 = taskDAO.create(TaskRequestModel(title = task1Title))
        assert(taskDAO.getAll().isNotEmpty())
        assert(taskDAO.getAll().count() == 1)
        assert(taskDAO.getById(task1.id) != null)
        assert(task1.title == task1Title)

        // Another one task will be created
        val task2Title = "Test2"
        val task2 = taskDAO.create(TaskRequestModel(title = task2Title))
        assert(taskDAO.getAll().count() == 2)
        assert(taskDAO.getById(task2.id) != null)
        assert(task2.title == task2Title)

        // Update task (only specified task must be updated)
        val task1TitleUpdated = "TestUpdated"
        val updatedTask = taskDAO.update(task1.id, TaskRequestModel(title = task1TitleUpdated))!!
        assert(updatedTask == taskDAO.getById(task1.id))
        assert(updatedTask.title == task1TitleUpdated)
        assert(taskDAO.getById(task2.id)!!.title == task2Title)

        // Delete tasks
        taskDAO.delete(task2.id)
        assert(taskDAO.getById(task2.id) == null)
        assert(taskDAO.getAll().count() == 1)
        taskDAO.delete(task1.id)
        assert(taskDAO.getById(task1.id) == null)
        assert(taskDAO.getAll().isEmpty())
    }
}