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 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
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,7 @@ tasks {
}
systemProperty("spring.profiles.active", "development,test")
}
bootRun {
mainClass.set("com.hibob.AcademyApplicationKt") // Replace with your actual main class
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hibob.KotlinBasics
package com.hibob.academy.basics
//Q1
fun List<Int>.sum():Int{
var result = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Omri.Basics
package com.hibob.academy.basicsomri

//Omri Solution
fun isValidIdentifier(s: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package NullAbilityUpdated.Keren
package com.hibob.academy.nullability

/**
* Modify the main function to print each user's email safely.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package NullAbilityUpdated.Keren
package com.hibob.academy.nullability
/**
* Modify the main function to calculate and print the sum of all non-null integers in the list numbers.
* Use safe calls and/or the Elvis operator where appropriate.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package NullAbilityUpdated.Keren
package com.hibob.academy.nullability
/**
* Write an extension function nullSafeToUpper() for String? that converts the string
* to uppercase if it is not null, or returns "NO TEXT PROVIDED" if it is null.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package NullAbilityUpdated.Keren
package com.hibob.academy.nullability
/**
* Modify the main function to iterate over the employees list and print each employee's city.
* Use safe calls (?.) and the Elvis operator (?:) to ensure that if an employee or their address is null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package NullAbilityUpdated.Keren
package com.hibob.academy.nullability

/**
* Iterate through the list of products.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package NullAbilityUpdated.Keren
package com.hibob.academy.nullability

data class Department(val name: String?, val manager: EmployeeDetails?)
data class EmployeeDetails(val name: String?, val contactInfo: ContactInfo?)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package NullAbilityUpdated.Keren
package com.hibob.academy.nullability

/**
*
Expand Down Expand Up @@ -47,6 +47,6 @@ fun List<Customer?>.printAllCustomersDetails(){
}
}
}
println(Customer(costumerName,Account(accountId,AccountDetails(accountType,accountBalance))))
println(Customer(costumerName, Account(accountId, AccountDetails(accountType,accountBalance))))
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package NullAbilityUpdated.Keren
package com.hibob.academy.nullability
/**
* Instructions:
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package NullAbilityUpdated.Keren
package com.hibob.academy.nullability

data class DepartmentData(val name: String?, val manager: EmployeeData?)
data class EmployeeData(val name: String?, val contactInfo: Contact?)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sequence.omri
package com.hibob.academy.omri
import org.springframework.cglib.core.Predicate
import kotlin.system.exitProcess

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Properties.Tamir
package com.hibob.academy.properties
import java.time.DayOfWeek

//1. create class called Store that initlize by day and list of products
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Basics.Tamir
package com.hibob.academy.tamir
fun main() {
//Q1
printHelloWorld()
Expand Down
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
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lambda.yael
package com.hibob.academy.yael

import java.time.LocalDate

Expand All @@ -14,7 +14,7 @@ fun main() {
}

//2. Initiate the variable movie in line 18 with the function createGoodMovie()
val createGoodMovie :() -> SpidermanNoWayHome = { SpidermanNoWayHome()}
val createGoodMovie :() -> SpidermanNoWayHome = { SpidermanNoWayHome() }

fun printSuccessMessage(success: Boolean) {
if (success) {
Expand Down
106 changes: 106 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,106 @@
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

private val user = User(123,"Ron Azar", "ron.azar@hibob.io" ,"12345", true)
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