Skip to content
Merged
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
162 changes: 52 additions & 110 deletions src/test/kotlin/ui/screens/AuditScreenTest.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package ui.screens

import fake.createAudit
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import io.mockk.verifyOrder
import io.mockk.*
import kotlinx.coroutines.runBlocking
import logic.entities.ActionType
import logic.entities.EntityType
import logic.entities.UserRole
Expand Down Expand Up @@ -63,204 +61,148 @@ class AuditScreenTest {

@Test
fun `showOptionService should display audit logs options when called`() {
// Given && When
auditScreen.showOptionService()

// Then
verify { consoleIO.showWithLine(any()) }
}

@Test
fun `handleFeatureChoice should call getAllAuditLogs when this option is selected`() {
// Given
val getAllAuditLogsOption = "1"
every { consoleIO.read() } returns getAllAuditLogsOption
fun `handleFeatureChoice should call getAllAuditLogs when this option is selected`() = runBlocking {
every { consoleIO.read() } returns "1"
coEvery { getAllAuditLogsUseCase.getAllAuditLogs() } returns allAudit

// When
auditScreen.handleFeatureChoice()

// Then
verify { getAllAuditLogsUseCase.getAllAuditLogs() }
coVerify { getAllAuditLogsUseCase.getAllAuditLogs() }
}

@Test
fun `should view no audit logs found message when getAllAuditLogs returns empty list`() {
// Given
val getAllAuditLogsOption = "1"
every { consoleIO.read() } returns getAllAuditLogsOption
every { getAllAuditLogsUseCase.getAllAuditLogs() } returns emptyList()
fun `should view no audit logs found message when getAllAuditLogs returns empty list`() = runBlocking {
every { consoleIO.read() } returns "1"
coEvery { getAllAuditLogsUseCase.getAllAuditLogs() } returns emptyList()

// When
auditScreen.handleFeatureChoice()

// Then
verify { consoleIO.showWithLine("❌ No Audit Logs Found") }
}

@Test
fun `should view all audit logs when getAllAuditLogs returns non-empty list`() {
// Given
val getAllAuditLogsOption = "1"
every { consoleIO.read() } returns getAllAuditLogsOption
every { getAllAuditLogsUseCase.getAllAuditLogs() } returns allAudit
fun `should view all audit logs when getAllAuditLogs returns non-empty list`() = runBlocking {
every { consoleIO.read() } returns "1"
coEvery { getAllAuditLogsUseCase.getAllAuditLogs() } returns allAudit

// When
auditScreen.handleFeatureChoice()

// Then
verifyOrder {
consoleIO.showWithLine("\n📋 All Audit Logs:\n")
consoleIO.showWithLine(any())
}
}


@Test
fun `handleFeatureChoice should call getAuditLogsByProjectId when this option is selected`() {
// Given
val getAuditLogsByProjectIdOption = "2"
every { consoleIO.read() } returns getAuditLogsByProjectIdOption
fun `handleFeatureChoice should call getAuditLogsByProjectId when this option is selected`() = runBlocking {
every { consoleIO.read() } returns "2"
coEvery { getAuditLogsByProjectIdUseCase.getAuditLogsByProjectId(any()) } returns allAudit

// When
auditScreen.handleFeatureChoice()

// Then
verify { getAuditLogsByProjectIdUseCase.getAuditLogsByProjectId(any()) }
coVerify { getAuditLogsByProjectIdUseCase.getAuditLogsByProjectId(any()) }
}

@Test
fun `should view all project audit logs when getAuditLogsByProjectId returns non-empty list`() {
// Given
val getAuditLogsByProjectIdOption = "2"
fun `should view all project audit logs when getAuditLogsByProjectId returns non-empty list`() = runBlocking {
val projectId = "123"
every { consoleIO.read() } returns getAuditLogsByProjectIdOption andThen projectId
every { getAuditLogsByProjectIdUseCase.getAuditLogsByProjectId(projectId) } returns allAudit
every { consoleIO.read() } returns "2" andThen projectId
coEvery { getAuditLogsByProjectIdUseCase.getAuditLogsByProjectId(projectId) } returns allAudit

// When
auditScreen.handleFeatureChoice()

// Then
verifyOrder {
consoleIO.showWithLine("\n🔍 Audit Logs For Project ID: $projectId\n")
consoleIO.showWithLine(any())
}
}

@Test
fun `should view PROJECT_NOT_FOUND_ERROR message when getAuditLogsByProjectId returns empty list`() {
// Given
val getAuditLogsByProjectIdOption = "2"
fun `should view PROJECT_NOT_FOUND_ERROR message when getAuditLogsByProjectId returns error`() = runBlocking {
val projectId = "123"
val projectNotFoundError = "❌ Error: Project logs not found"
every { consoleIO.read() } returns getAuditLogsByProjectIdOption andThen projectId
every {
val error = "❌ Error: Project logs not found"
every { consoleIO.read() } returns "2" andThen projectId
coEvery {
getAuditLogsByProjectIdUseCase.getAuditLogsByProjectId(projectId)
} throws ProjectNotFoundException(projectNotFoundError)

} throws ProjectNotFoundException(error)

// When
auditScreen.handleFeatureChoice()

// Then
verify { consoleIO.showWithLine("❌ $projectNotFoundError") }
verify { consoleIO.showWithLine("❌ $error") }
}

@Test
fun `should view INVALID_ID_ERROR message when project id is blank`() {
// Given
val getAuditLogsByProjectIdOption = "2"
val projectId = ""
val INVALID_ID_ERROR = "Error: ID shouldn't be blank"
every { consoleIO.read() } returns getAuditLogsByProjectIdOption andThen projectId
every {
getAuditLogsByProjectIdUseCase.getAuditLogsByProjectId(projectId)
} throws InvalidInputException(INVALID_ID_ERROR)

fun `should view INVALID_ID_ERROR message when project id is blank`() = runBlocking {
val error = "Error: ID shouldn't be blank"
every { consoleIO.read() } returns "2" andThen ""
coEvery {
getAuditLogsByProjectIdUseCase.getAuditLogsByProjectId("")
} throws InvalidInputException(error)

// When
auditScreen.handleFeatureChoice()

// Then
verify { consoleIO.showWithLine("❌ $INVALID_ID_ERROR") }
verify { consoleIO.showWithLine("❌ $error") }
}


@Test
fun `handleFeatureChoice should call getAuditLogsByTaskId when this option is selected`() {
// Given
val getAuditLogsByTaskIdOption = "3"
every { consoleIO.read() } returns getAuditLogsByTaskIdOption
fun `handleFeatureChoice should call getAuditLogsByTaskId when this option is selected`() = runBlocking {
every { consoleIO.read() } returns "3"
coEvery { getAuditLogsByTaskIdUseCase.getAuditLogsByTaskId(any()) } returns allAudit

// When
auditScreen.handleFeatureChoice()

// Then
verify { getAuditLogsByTaskIdUseCase.getAuditLogsByTaskId(any()) }
coVerify { getAuditLogsByTaskIdUseCase.getAuditLogsByTaskId(any()) }
}

@Test
fun `should view all task audit logs when getAuditLogsByTaskId returns non-empty list`() {
// Given
val getAuditLogsByTaskIdOption = "3"
fun `should view all task audit logs when getAuditLogsByTaskId returns non-empty list`() = runBlocking {
val taskId = "456"
every { consoleIO.read() } returns getAuditLogsByTaskIdOption andThen taskId
every { getAuditLogsByTaskIdUseCase.getAuditLogsByTaskId(taskId) } returns allAudit
every { consoleIO.read() } returns "3" andThen taskId
coEvery { getAuditLogsByTaskIdUseCase.getAuditLogsByTaskId(taskId) } returns allAudit

// When
auditScreen.handleFeatureChoice()

// Then
verifyOrder {
consoleIO.showWithLine("\n🔍 Audit Logs For Task ID: $taskId\n")
consoleIO.showWithLine(any())
}
}

@Test
fun `should view INVALID_ID_ERROR message when task id is blank`() {
// Given
val getAuditLogsByTaskIdOption = "3"
val taskId = ""
val INVALID_ID_ERROR = "Error: ID shouldn't be blank"
every { consoleIO.read() } returns getAuditLogsByTaskIdOption andThen taskId
every {
getAuditLogsByTaskIdUseCase.getAuditLogsByTaskId(taskId)
} throws InvalidInputException(INVALID_ID_ERROR)


// When
fun `should view INVALID_ID_ERROR message when task id is blank`() = runBlocking {
val error = "Error: ID shouldn't be blank"
every { consoleIO.read() } returns "3" andThen ""
coEvery {
getAuditLogsByTaskIdUseCase.getAuditLogsByTaskId("")
} throws InvalidInputException(error)

auditScreen.handleFeatureChoice()

// Then
verify { consoleIO.showWithLine("❌ $INVALID_ID_ERROR") }
verify { consoleIO.showWithLine("❌ $error") }
}


@Test
fun `should exit from audit log system when 0 is selected`() {
// Given
val exitOption = "0"
every { consoleIO.read() } returns exitOption
fun `should exit from audit log system when 0 is selected`() = runBlocking {
every { consoleIO.read() } returns "0"

// When
auditScreen.handleFeatureChoice()

// Then
verify(exactly = 0) { getAllAuditLogsUseCase.getAllAuditLogs() }
coVerify(exactly = 0) { getAllAuditLogsUseCase.getAllAuditLogs() }
}

@Test
fun `should show error message when enter invalid option`() {
// Given
fun `should show error message when enter invalid option`() = runBlocking {
val invalidOption = "999"
val invalidOptionMessage = "❌ Invalid Option"
every { consoleIO.read() } returns invalidOption

// When
auditScreen.handleFeatureChoice()

// Then
verify { consoleIO.showWithLine(invalidOptionMessage) }
}

}
}
Loading