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

[Not Mandatory] Do some Test #18

Open
Enrico-git opened this issue Apr 30, 2021 · 2 comments
Open

[Not Mandatory] Do some Test #18

Enrico-git opened this issue Apr 30, 2021 · 2 comments
Assignees
Labels
enhancement New feature or request Performance

Comments

@Enrico-git
Copy link
Owner

https://reflectoring.io/spring-boot-data-jpa-test/

wa08-Testing JPA entities with Spring Boot.pdf

@Enrico-git
Copy link
Owner Author

Enrico: AuthService e CustomerService
Giulio: WalletService
Luigi: WalletController
Rosario: AuthController e CustomerController

Test di integrazione e di unità => seguire Giulio's blueprint

@Enrico-git
Copy link
Owner Author

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 = "[email protected]",
        roles = Rolename.CUSTOMER.toString()
    )

    private val customer = Customer(
        name = "Alice",
        surname = "Pleasance Liddell",
        address = "Wonderland, 2",
        email = "[email protected]",
        user = User(
            username = "alice",
            password = "my_salted_pw",
            email = "[email protected]",
            roles = Rolename.CUSTOMER.toString()
        )
    )

    private val customerDTO = CustomerDTO(
        id = 4,
        name = "Alice",
        surname = "Pleasance Liddell",
        address = "Wonderland, 2",
        email = "[email protected]",
        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 = "[email protected]",
            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 = "[email protected]",
            userID = 1,
        )

        val outCustomerDTO  = CustomerDTO(
            id = 4,
            name = "Alice69",
            surname = "Pleasance Liddell69",
            address = "Wonderland, 269",
            email = "[email protected]",
            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 = "[email protected]",
            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 = "[email protected]",
            userID = 1,
        )
        assert(service.addCustomer(customerDTO).id != null)
        assert(service.addCustomer(customerDTO).userID == 1L)
        assert(service.addCustomer(customerDTO).email == "[email protected]")
    }

    @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 = "[email protected]",
            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 = "[email protected]",
            userID = 1,
        )
        assert(service.updateCustomer(updateCustomerDTO, 4).id != null)
        assert(service.updateCustomer(updateCustomerDTO, 4).userID == 1L)
        assert(service.updateCustomer(updateCustomerDTO, 4).email == "[email protected]")
    }

    @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 = "[email protected]",
            userID = 69,
        )
        assertThrows<AccessDeniedException> { service.updateCustomer(fakeCustomerDTO, 69) }
    }
}

Enrico-git added a commit that referenced this issue Jul 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Performance
Projects
None yet
Development

No branches or pull requests

4 participants