Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All unit tests using Mock #21

Merged
merged 5 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
60 changes: 60 additions & 0 deletions src/main/kotlin/com/hibob/academy/unitests/keren/userService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.hibob.bootcamp.unittests

data class User(val id: Long, val name: String, val email: String, val password: String, val isEmailVerified: Boolean = false)

interface UserDao {
fun findById(userId: Long): User?
fun save(user: User): Boolean
fun update(user: User): Boolean
}

interface NotificationService {
fun sendEmail(email: String, message: String): Boolean
}

interface EmailVerificationService {
fun sendVerificationEmail(email: String): Boolean
fun verifyEmail(email: String, token: String): Boolean
}

class UserService(
private val userDao: UserDao,
private val notificationService: NotificationService,
private val emailVerificationService: EmailVerificationService
) {
fun registerUser(user: User): Boolean {
if (userDao.findById(user.id) != null) {
throw IllegalArgumentException("User already exists")
}

val isSaved = userDao.save(user.copy(isEmailVerified = false))
if (!isSaved) {
throw IllegalStateException("User registration failed")
}

val isVerificationEmailSent = emailVerificationService.sendVerificationEmail(user.email)
if (!isVerificationEmailSent) {
throw IllegalStateException("Failed to send verification email")
}

return true
}

fun verifyUserEmail(userId: Long, token: String): Boolean {
val user = userDao.findById(userId) ?: throw IllegalArgumentException("User not found")

val isEmailVerified = emailVerificationService.verifyEmail(user.email, token)
if (!isEmailVerified) {
throw IllegalArgumentException("Email verification failed")
}

val updatedUser = user.copy(isEmailVerified = true)
val isUpdated = userDao.update(updatedUser)

if (isUpdated) {
notificationService.sendEmail(user.email, "Welcome ${user.name}!")
}

return isUpdated
}
}
110 changes: 110 additions & 0 deletions src/test/kotlin/com/hibob/academy/unitests/keren/UserServiceTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.hibob.academy.unitests.keren

import com.hibob.bootcamp.unittests.*
import org.hibernate.validator.internal.util.Contracts.assertTrue
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.mockito.kotlin.*

class UserServiceTest{
val token = "Waffle"
val id: Long= 123
val name: String = "Ron Azar"
val email: String = "ron.azar@hibob.io"
val password: String ="12345"
val isEmailValid = true

private val user = User(id,name, email ,password, isEmailValid)
RonAzar marked this conversation as resolved.
Show resolved Hide resolved
private val userDao= mock<UserDao>{}
private val notificationService= mock<NotificationService>{}
private val emailVerificationService= mock<EmailVerificationService>{}
private val userService=UserService(userDao,notificationService,emailVerificationService)

@Test
fun `Test register user findById -- User already exists`(){
whenever( userDao.findById(user.id)).thenReturn(user)
val errorMessage = assertThrows<IllegalArgumentException>{
userService.registerUser(user)
}
assertEquals("User already exists",errorMessage.message)

// Verifying that save and sendVerificationEmail are NOT called because the user exists
verify(userDao, never()).save(any())
verify(emailVerificationService, never()).sendVerificationEmail(any())
}

@Test
fun `Test register user findByEmail -- User registration failed`(){
whenever( userDao.findById(user.id)).thenReturn(null)
whenever(userDao.save(user.copy(isEmailVerified = false))).thenReturn(false)
val errorMessage = assertThrows<IllegalStateException>{
userService.registerUser(user)
}
assertEquals("User registration failed",errorMessage.message)

verify(emailVerificationService, never()).sendVerificationEmail(any())
}

@Test
fun `Test register user findByEmail -- Failed to send verification email`(){
whenever( userDao.findById(user.id)).thenReturn(null)
whenever(userDao.save(user.copy(isEmailVerified = false))).thenReturn(true)
whenever(emailVerificationService.sendVerificationEmail(user.email)).thenReturn(false)
val errorMessage = assertThrows<IllegalStateException>{
userService.registerUser(user)
}
assertEquals("Failed to send verification email",errorMessage.message)
}

@Test
fun `Test register user findByEmail -- Successfully registered user`(){
whenever( userDao.findById(user.id)).thenReturn(null)
whenever(userDao.save(user.copy(isEmailVerified = false))).thenReturn(true)
whenever(emailVerificationService.sendVerificationEmail(user.email)).thenReturn(true)
assertTrue(userService.registerUser(user),"User not added")
}

@Test
fun `verify User Email -- User not found`(){
whenever( userDao.findById(id)).thenReturn(null)
val errorMessage = assertThrows<IllegalArgumentException>{
userService.verifyUserEmail(id,token)
}
assertEquals("User not found",errorMessage.message)
verify(emailVerificationService, never()).verifyEmail(any(), any())
verify(userDao, never()).save(any())
verify(userDao, never()).update(any())
}

@Test
fun `Test verify User Email -- Email verification failed`(){
whenever( userDao.findById(id)).thenReturn(user)
whenever(emailVerificationService.verifyEmail(user.email, token)).thenReturn(false)
val errorMessage = assertThrows<IllegalArgumentException>{
userService.verifyUserEmail(id,token)
}
assertEquals("Email verification failed",errorMessage.message)
verify(userDao, never()).save(any())
verify(userDao, never()).update(any())
}

@Test
fun `Test verify User Email -- isUpdated-true- send email`(){
whenever( userDao.findById(id)).thenReturn(user)
whenever(emailVerificationService.verifyEmail(user.email, token)).thenReturn(true)
val updatedUser = user.copy()
whenever(userDao.update(updatedUser)).thenReturn(true)
assertTrue(userService.verifyUserEmail(id,token),"User updated")
}

@Test
fun `Test verify User Email -- isUpdated-false- dont send email`(){
whenever( userDao.findById(id)).thenReturn(user)
whenever(emailVerificationService.verifyEmail(user.email, token)).thenReturn(true)
val updatedUser = user.copy()
whenever(userDao.update(updatedUser)).thenReturn(false)
assertFalse(userService.verifyUserEmail(id,token),"User updated")
}
}
Loading