/
CustomerServiceTest.kt
164 lines (147 loc) · 6.04 KB
/
CustomerServiceTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package me.dio.credit.application.system.service
import io.mockk.every
import io.mockk.impl.annotations.InjectMockKs
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import io.mockk.just
import io.mockk.runs
import io.mockk.verify
import me.dio.credit.application.system.entity.Address
import me.dio.credit.application.system.entity.Customer
import me.dio.credit.application.system.exception.BusinessException
import me.dio.credit.application.system.repository.CustomerRepository
import me.dio.credit.application.system.service.impl.CustomerService
import org.assertj.core.api.Assertions
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.fail
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.api.fail
import org.springframework.test.context.ActiveProfiles
import java.math.BigDecimal
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.*
//@ActiveProfiles("test")
@ExtendWith(MockKExtension::class)
class CustomerServiceTest {
@MockK
lateinit var customerRepository: CustomerRepository
@InjectMockKs
lateinit var customerService: CustomerService
@Test
fun `should the date of the first installment must be a maximum of 3 months after the current day`() {
//given
val fakeCustomer: Customer = buildCustomer()
every { customerRepository.save(any()) } returns fakeCustomer
//when
val actual: Customer = customerService.save(fakeCustomer)
//then ChatGPT
// Get the current date and time
val currentDate = LocalDateTime.now()
// Calculate the date for the first installment (maximum 3 months from now)
val firstInstallmentDate = currentDate.plusMonths(3)
// Print the result
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
println("Current Date and Time: ${currentDate.format(formatter)}")
println("First Installment Date: ${firstInstallmentDate.format(formatter)}")
// Use AssertJ to make assertions
assertThat(firstInstallmentDate).isAfterOrEqualTo(currentDate)
.isBeforeOrEqualTo(currentDate.plusMonths(3))
Assertions.assertThat(actual).isNotNull
Assertions.assertThat(actual).isSameAs(fakeCustomer)
verify(exactly = 1) { customerRepository.save(fakeCustomer) }
}
@Test
fun `should the maximum installments allowed will be 48`() {
//given
val fakeCustomer: Customer = buildCustomer()
every { customerRepository.save(any()) } returns fakeCustomer
//when
val actual: Customer = customerService.save(fakeCustomer)
// actual.installments = 48
//then
Assertions.assertThat(actual.installments).isSameAs(fakeCustomer.installments)
Assertions.assertThat(actual).isNotNull
Assertions.assertThat(actual).isSameAs(fakeCustomer)
verify(exactly = 1) { customerRepository.save(fakeCustomer) }
}
@Test
fun `should create customer`() {
//given
val fakeCustomer: Customer = buildCustomer()
every { customerRepository.save(any()) } returns fakeCustomer
//when
val actual: Customer = customerService.save(fakeCustomer)
//then
Assertions.assertThat(actual).isNotNull
Assertions.assertThat(actual).isSameAs(fakeCustomer)
verify(exactly = 1) { customerRepository.save(fakeCustomer) }
}
@Test
fun `should find customer by id`() {
//given
val fakeId: Long = Random().nextLong()
val fakeCustomer: Customer = buildCustomer(id = fakeId)
every { customerRepository.findById(fakeId) } returns Optional.of(fakeCustomer)
//when
val actual: Customer = customerService.findById(fakeId)
//then
Assertions.assertThat(actual).isNotNull
Assertions.assertThat(actual).isExactlyInstanceOf(Customer::class.java)
Assertions.assertThat(actual).isSameAs(fakeCustomer)
verify(exactly = 1) { customerRepository.findById(fakeId) }
}
@Test
fun `should not find customer by invalid id and throw BusinessException`() {
//given
val fakeId: Long = Random().nextLong()
every { customerRepository.findById(fakeId) } returns Optional.empty()
//when
//then
Assertions.assertThatExceptionOfType(BusinessException::class.java)
.isThrownBy { customerService.findById(fakeId) }
.withMessage("Id $fakeId not found")
verify(exactly = 1) { customerRepository.findById(fakeId) }
}
@Test
fun `should delete customer by id`() {
//given
val fakeId: Long = Random().nextLong()
val fakeCustomer: Customer = buildCustomer(id = fakeId)
every { customerRepository.findById(fakeId) } returns Optional.of(fakeCustomer)
every { customerRepository.delete(fakeCustomer) } just runs
//when
customerService.delete(fakeId)
//then
verify(exactly = 1) { customerRepository.findById(fakeId) }
verify(exactly = 1) { customerRepository.delete(fakeCustomer) }
}
private fun buildCustomer(
currentDay: LocalDateTime = LocalDateTime.now(),
installments: Int = 48,
firstName: String = "Cami",
lastName: String = "Cavalcante",
cpf: String = "28475934625",
email: String = "camila@gmail.com",
password: String = "12345",
zipCode: String = "12345",
street: String = "Rua da Cami",
income: BigDecimal = BigDecimal.valueOf(1000.0),
id: Long = 1L
) = Customer(
currentDay = currentDay,
installments = installments,
firstName = firstName,
lastName = lastName,
cpf = cpf,
email = email,
password = password,
address = Address(
zipCode = zipCode,
street = street,
),
income = income,
id = id
)
}