Open Enrico-git opened 3 years ago
Enrico: AuthService e CustomerService Giulio: WalletService Luigi: WalletController Rosario: AuthController e CustomerController
Test di integrazione e di unità => seguire Giulio's blueprint
CustomerServiceUnitTests.kt
package it.polito.ecommerce.unit
//TODO EA
import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
import io.mockk.mockkObject
import it.polito.ecommerce.common.Rolename
import it.polito.ecommerce.domain.Customer
import it.polito.ecommerce.domain.User
import it.polito.ecommerce.dto.CustomerDTO
import it.polito.ecommerce.dto.toDTO
import it.polito.ecommerce.repositories.CustomerRepository
import it.polito.ecommerce.repositories.UserRepository
import it.polito.ecommerce.security.JwtAuthenticationTokenFilter
import it.polito.ecommerce.security.MethodSecurityConfig
import it.polito.ecommerce.services.CustomerServiceImpl
import javassist.NotFoundException
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
import org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.FilterType
import org.springframework.security.config.annotation.web.WebSecurityConfigurer
import java.util.*
@WebMvcTest(
CustomerServiceImpl::class,
excludeFilters = [ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE, value =
[WebSecurityConfigurer::class, MethodSecurityConfig::class, JwtAuthenticationTokenFilter::class]
)],
excludeAutoConfiguration = [SecurityAutoConfiguration::class, SecurityFilterAutoConfiguration::class]
)
class CustomerServiceUnitTests(@Autowired private val customerServiceImpl: CustomerServiceImpl) {
@MockkBean
private lateinit var customerRepository: CustomerRepository
@MockkBean
private lateinit var userRepository: UserRepository
private val user = User(
username = "alice",
password = "my_salted_pw",
email = "alice_inwonderland@mail.com",
roles = Rolename.CUSTOMER.toString()
)
private val customer = Customer(
name = "Alice",
surname = "Pleasance Liddell",
address = "Wonderland, 2",
email = "alice_inwonderland@mail.com",
user = User(
username = "alice",
password = "my_salted_pw",
email = "alice_inwonderland@mail.com",
roles = Rolename.CUSTOMER.toString()
)
)
private val customerDTO = CustomerDTO(
id = 4,
name = "Alice",
surname = "Pleasance Liddell",
address = "Wonderland, 2",
email = "alice_inwonderland@mail.com",
userID = 1,
)
@Test
fun `Assert get customer successfully retrieves existing customer`() {
mockkObject(customer)
every { customerRepository.findById(1L) } returns Optional.of(customer)
every { customer.getId() } returns 4L
every { customer.user.getId() } returns 1L
val customerFetched = customerServiceImpl.getCustomer(1L)
assert(customerFetched == customer.toDTO())
}
@Test
fun `Assert get customer throws not found exception if customer not exists`() {
every { customerRepository.findById(69L) } returns Optional.empty()
assertThrows<NotFoundException> { customerServiceImpl.getCustomer(69L) }
}
@Test
fun `Assert add customer successfully`() {
mockkObject(customer)
every { customer.getId() } returns 4L
every { customer.user.getId() } returns 1L
every { userRepository.findById(1L) } returns Optional.of(user)
every { customerRepository.save(any()) } returns customer
assert(customerServiceImpl.addCustomer(customerDTO) == customer.toDTO())
}
@Test
fun `Assert add customer throws not found exception if user not exists`() {
every { userRepository.findById(69L) } returns Optional.empty()
val fakeCustomerDTO = CustomerDTO(
id = 4,
name = "Alice",
surname = "Pleasance Liddell",
address = "Wonderland, 2",
email = "alice_inwonderland@mail.com",
userID = 69,
)
assertThrows<IllegalArgumentException> { customerServiceImpl.addCustomer(fakeCustomerDTO) }
}
@Test
fun `Assert update customer successfully`() {
mockkObject(customer)
every { customer.getId() } returns 4L
every { customer.user.getId() } returns 1L
every { customerRepository.findById(4L) } returns Optional.of(customer)
every { customerRepository.save(any()) } returns customer
val updateCustomerDTO = CustomerDTO(
id = 4,
name = "Alice69",
surname = "Pleasance Liddell69",
address = "Wonderland, 269",
email = "alice_inwonderland69@mail.com",
userID = 1,
)
val outCustomerDTO = CustomerDTO(
id = 4,
name = "Alice69",
surname = "Pleasance Liddell69",
address = "Wonderland, 269",
email = "alice_inwonderland69@mail.com",
userID = 1,
)
assert(customerServiceImpl.updateCustomer(updateCustomerDTO, 4L) == outCustomerDTO)
}
@Test
fun `Assert update customer throws not found exception if customer not exists`() {
every { customerRepository.findById(69L) } returns Optional.empty()
val fakeCustomerDTO = CustomerDTO(
id = 4,
name = "Alice",
surname = "Pleasance Liddell",
address = "Wonderland, 2",
email = "alice_inwonderland@mail.com",
userID = 69,
)
assertThrows<IllegalArgumentException> { customerServiceImpl.updateCustomer(fakeCustomerDTO, 69L) }
}
}
CustomerServiceIntTests.kt
package it.polito.ecommerce.integration
//TODO EA
import it.polito.ecommerce.dto.CustomerDTO
import it.polito.ecommerce.repositories.CustomerRepository
import it.polito.ecommerce.repositories.UserRepository
import it.polito.ecommerce.services.CustomerService
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.FilterType
import org.springframework.security.access.AccessDeniedException
import org.springframework.security.test.context.support.WithUserDetails
import org.springframework.stereotype.Component
import org.springframework.stereotype.Repository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
@Transactional
@DataJpaTest(
includeFilters = [ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = [Repository::class, Component::class, Service::class]
)]
)
class CustomerServiceIntTests @Autowired constructor(
private val service: CustomerService,
private val customerRepository: CustomerRepository,
private val userRepository: UserRepository
){
@Test
@WithUserDetails(value = "alice_in_wonderland")
fun `Assert get customer successfully`() {
assert(service.getCustomer(4L).id == 4L)
}
@Test
@WithUserDetails(value = "alice_in_wonderland")
fun `Assert get customer throws access denied`() {
assertThrows<AccessDeniedException> { service.getCustomer(69L).id }
}
@Test
@WithUserDetails(value = "alice_in_wonderland")
fun `Assert add customer successfully`() {
val customerDTO = CustomerDTO(
id = null,
name = "Alice",
surname = "Pleasance Liddell",
address = "Wonderland, 2",
email = "alice_inwonderland@mail.com",
userID = 1,
)
assert(service.addCustomer(customerDTO).id != null)
assert(service.addCustomer(customerDTO).userID == 1L)
assert(service.addCustomer(customerDTO).email == "alice_inwonderland@mail.com")
}
@Test
@WithUserDetails(value = "alice_in_wonderland")
fun `Assert add customer is forbidden if user is not correct`() {
val fakeCustomerDTO = CustomerDTO(
id = null,
name = "Alice",
surname = "Pleasance Liddell",
address = "Wonderland, 2",
email = "alice_inwonderland@mail.com",
userID = 69,
)
assertThrows<AccessDeniedException> { service.addCustomer(fakeCustomerDTO) }
}
@Test
@WithUserDetails(value = "alice_in_wonderland")
fun `Assert update customer successfully`() {
val updateCustomerDTO = CustomerDTO(
id = 4,
name = "Alice69",
surname = "Pleasance Liddell69",
address = "Wonderland, 269",
email = "alice_inwonderland69@mail.com",
userID = 1,
)
assert(service.updateCustomer(updateCustomerDTO, 4).id != null)
assert(service.updateCustomer(updateCustomerDTO, 4).userID == 1L)
assert(service.updateCustomer(updateCustomerDTO, 4).email == "alice_inwonderland69@mail.com")
}
@Test
@WithUserDetails(value = "alice_in_wonderland")
fun `Assert update customer is forbidden if user is not correct`() {
val fakeCustomerDTO = CustomerDTO(
id = null,
name = "Alice69",
surname = "Pleasance Liddell69",
address = "Wonderland, 269",
email = "alice_inwonderland69@mail.com",
userID = 69,
)
assertThrows<AccessDeniedException> { service.updateCustomer(fakeCustomerDTO, 69) }
}
}
https://reflectoring.io/spring-boot-data-jpa-test/
wa08-Testing JPA entities with Spring Boot.pdf