Les classes imbriquées dans les tests ont été retirés

This commit is contained in:
FyloZ 2021-03-07 21:28:52 -05:00
parent 42e33f83ef
commit 10a3f0db08
11 changed files with 1021 additions and 1071 deletions

View File

@ -58,10 +58,7 @@ interface EmployeeService : ExternalModelService<Employee, EmployeeSaveDto, Empl
}
interface EmployeeGroupService :
ExternalModelService<EmployeeGroup, EmployeeGroupSaveDto, EmployeeGroupUpdateDto, EmployeeGroupRepository> {
/** Checks if a group with the given [name] exists. */
fun existsByName(name: String): Boolean
ExternalNamedModelService<EmployeeGroup, EmployeeGroupSaveDto, EmployeeGroupUpdateDto, EmployeeGroupRepository> {
/** Gets all the employees of the group with the given [id]. */
fun getEmployeesForGroup(id: Long): Collection<Employee>
@ -235,10 +232,10 @@ const val defaultGroupCookieMaxAge = 10 * 365 * 24 * 60 * 60 // 10 ans
@Service
class EmployeeGroupServiceImpl(
val employeeGroupRepository: EmployeeGroupRepository,
val employeeService: EmployeeService
val employeeService: EmployeeService,
employeeGroupRepository: EmployeeGroupRepository
) :
AbstractExternalModelService<EmployeeGroup, EmployeeGroupSaveDto, EmployeeGroupUpdateDto, EmployeeGroupRepository>(
AbstractExternalNamedModelService<EmployeeGroup, EmployeeGroupSaveDto, EmployeeGroupUpdateDto, EmployeeGroupRepository>(
employeeGroupRepository
),
EmployeeGroupService {
@ -248,7 +245,7 @@ class EmployeeGroupServiceImpl(
@Transactional
override fun save(entity: EmployeeGroup): EmployeeGroup {
return super<AbstractExternalModelService>.save(entity).apply {
return super<AbstractExternalNamedModelService>.save(entity).apply {
employeeService.saveDefaultGroupEmployee(this)
}
}

View File

@ -38,155 +38,146 @@ abstract class AbstractServiceTest<E, S : Service<E, *>, R : JpaRepository<E, *>
reset(repository, service)
}
@Nested
inner class GetAll {
@Test
fun `returns all available entities`() {
whenever(repository.findAll()).doReturn(entityList)
// getAll()
val found = service.getAll()
@Test
open fun `getAll() returns all available entities`() {
whenever(repository.findAll()).doReturn(entityList)
assertEquals(entityList, found)
}
val found = service.getAll()
@Test
fun `returns empty list when there is no entities`() {
whenever(repository.findAll()).doReturn(listOf())
val found = service.getAll()
assertTrue { found.isEmpty() }
}
assertEquals(entityList, found)
}
@Nested
inner class Save {
@Test
fun `calls and returns save() in the repository`() {
whenever(repository.save(entity)).doReturn(entity)
@Test
open fun `getAll() returns empty list when there is no entities`() {
whenever(repository.findAll()).doReturn(listOf())
val found = service.save(entity)
val found = service.getAll()
verify(repository).save(entity)
assertEquals(entity, found)
}
assertTrue { found.isEmpty() }
}
@Nested
inner class Update {
@Test
fun `calls and returns save() in the repository`() {
whenever(repository.save(entity)).doReturn(entity)
// save()
val found = service.update(entity)
@Test
open fun `save() saves in the repository and returns the saved value`() {
whenever(repository.save(entity)).doReturn(entity)
verify(repository).save(entity)
assertEquals(entity, found)
}
val found = service.save(entity)
verify(repository).save(entity)
assertEquals(entity, found)
}
// update()
@Nested
inner class Delete {
@Test
fun `calls delete() in the repository`() {
service.delete(entity)
@Test
open fun `update() saves in the repository and returns the updated value`() {
whenever(repository.save(entity)).doReturn(entity)
verify(repository).delete(entity)
}
val found = service.update(entity)
verify(repository).save(entity)
assertEquals(entity, found)
}
// delete()
@Test
open fun `delete() deletes in the repository`() {
service.delete(entity)
verify(repository).delete(entity)
}
}
abstract class AbstractModelServiceTest<E : Model, S : ModelService<E, *>, R : JpaRepository<E, Long>> :
AbstractServiceTest<E, S, R>() {
@Nested
inner class ExistsById {
@Test
fun `returns true when an entity with the given id exists in the repository`() {
whenever(repository.existsById(entity.id!!)).doReturn(true)
val found = service.existsById(entity.id!!)
// existsById()
assertTrue(found)
}
@Test
open fun `existsById() returns true when an entity with the given id exists in the repository`() {
whenever(repository.existsById(entity.id!!)).doReturn(true)
@Test
fun `returns false when no entity with the given id exists in the repository`() {
whenever(repository.existsById(entity.id!!)).doReturn(false)
val found = service.existsById(entity.id!!)
val found = service.existsById(entity.id!!)
assertFalse(found)
}
assertTrue(found)
}
@Nested
inner class GetById {
@Test
fun `returns the entity with the given id from the repository`() {
whenever(repository.findById(entity.id!!)).doReturn(Optional.of(entity))
@Test
open fun `existsById() returns false when no entity with the given id exists in the repository`() {
whenever(repository.existsById(entity.id!!)).doReturn(false)
val found = service.getById(entity.id!!)
val found = service.existsById(entity.id!!)
assertEquals(entity, found)
}
@Test
fun `throws EntityNotFoundRestException when no entity with the given id exists in the repository`() {
whenever(repository.findById(entity.id!!)).doReturn(Optional.empty())
val exception = assertThrows<EntityNotFoundRestException> { service.getById(entity.id!!) }
assertTrue(exception.value is Long)
assertEquals(entity.id, exception.value as Long)
}
assertFalse(found)
}
@Nested
inner class SaveModel {
@Test
fun `throws EntityAlreadyExistsRestException when an entity with the given id exists in the repository`() {
doReturn(true).whenever(repository).existsById(entity.id!!)
// getById()
val exception = assertThrows<EntityAlreadyExistsRestException> { service.save(entity) }
assertTrue(exception.value is Long)
assertEquals(entity.id, exception.value as Long)
}
@Test
open fun `getById() returns the entity with the given id from the repository`() {
whenever(repository.findById(entity.id!!)).doReturn(Optional.of(entity))
val found = service.getById(entity.id!!)
assertEquals(entity, found)
}
@Nested
inner class Update {
@Test
fun `calls and returns save() in the repository`() {
whenever(repository.save(entity)).doReturn(entity)
doReturn(true).whenever(service).existsById(entity.id!!)
doReturn(entity).whenever(service).getById(entity.id!!)
@Test
open fun `getById() throws EntityNotFoundRestException when no entity with the given id exists in the repository`() {
whenever(repository.findById(entity.id!!)).doReturn(Optional.empty())
val found = service.update(entity)
verify(repository).save(entity)
assertEquals(entity, found)
}
@Test
fun `throws EntityNotFoundRestException when no entity with the given id exists in the repository`() {
doReturn(false).whenever(service).existsById(entity.id!!)
val exception = assertThrows<EntityNotFoundRestException> { service.update(entity) }
assertTrue(exception.value is Long)
assertEquals(entity.id, exception.value as Long)
}
val exception = assertThrows<EntityNotFoundRestException> { service.getById(entity.id!!) }
assertTrue(exception.value is Long)
assertEquals(entity.id, exception.value as Long)
}
@Nested
inner class DeleteById {
@Test
fun `calls deleteById() in the repository with the given id`() {
doReturn(entity).whenever(service).getById(entity.id!!)
// save()
service.deleteById(entity.id!!)
@Test
open fun `save() throws EntityAlreadyExistsRestException when an entity with the given id exists in the repository`() {
doReturn(true).whenever(repository).existsById(entity.id!!)
verify(repository).delete(entity)
}
val exception = assertThrows<EntityAlreadyExistsRestException> { service.save(entity) }
assertTrue(exception.value is Long)
assertEquals(entity.id, exception.value as Long)
}
// update()
@Test
override fun `update() saves in the repository and returns the updated value`() {
whenever(repository.save(entity)).doReturn(entity)
doReturn(true).whenever(service).existsById(entity.id!!)
doReturn(entity).whenever(service).getById(entity.id!!)
val found = service.update(entity)
verify(repository).save(entity)
assertEquals(entity, found)
}
@Test
open fun `update() throws EntityNotFoundRestException when no entity with the given id exists in the repository`() {
doReturn(false).whenever(service).existsById(entity.id!!)
val exception = assertThrows<EntityNotFoundRestException> { service.update(entity) }
assertTrue(exception.value is Long)
assertEquals(entity.id, exception.value as Long)
}
// deleteById()
@Test
open fun `deleteById() deletes the entity with the given id in the repository`() {
doReturn(entity).whenever(service).getById(entity.id!!)
service.deleteById(entity.id!!)
verify(repository).delete(entity)
}
}
@ -194,101 +185,97 @@ abstract class AbstractNamedModelServiceTest<E : NamedModel, S : NamedModelServi
AbstractModelServiceTest<E, S, R>() {
protected abstract val entityWithEntityName: E
@Nested
inner class ExistsByName {
@Test
fun `returns true when an entity with the given name exists`() {
whenever(repository.existsByName(entity.name)).doReturn(true)
// existsByName()
val found = service.existsByName(entity.name)
@Test
open fun `existsByName() returns true when an entity with the given name exists`() {
whenever(repository.existsByName(entity.name)).doReturn(true)
assertTrue(found)
}
val found = service.existsByName(entity.name)
@Test
fun `returns false when no entity with the given name exists`() {
whenever(repository.existsByName(entity.name)).doReturn(false)
val found = service.existsByName(entity.name)
assertFalse(found)
}
assertTrue(found)
}
@Nested
inner class GetByName {
@Test
fun `returns the entity with the given name`() {
whenever(repository.findByName(entity.name)).doReturn(entity)
@Test
open fun `existsByName() returns false when no entity with the given name exists`() {
whenever(repository.existsByName(entity.name)).doReturn(false)
val found = service.getByName(entity.name)
val found = service.existsByName(entity.name)
assertEquals(entity, found)
}
@Test
fun `throws EntityNotFoundRestException when no entity with the given name exists`() {
whenever(repository.findByName(entity.name)).doReturn(null)
val exception = assertThrows<EntityNotFoundRestException> { service.getByName(entity.name) }
assertEquals(entity.name, exception.value)
}
assertFalse(found)
}
@Nested
inner class SaveNamedModel {
@Test
fun `throws EntityAlreadyExistsRestException when an entity with the given name exists`() {
doReturn(true).whenever(service).existsByName(entity.name)
// getByName()
val exception = assertThrows<EntityAlreadyExistsRestException> { service.save(entity) }
assertEquals(entity.name, exception.value)
}
@Test
open fun `getByName() returns the entity with the given name`() {
whenever(repository.findByName(entity.name)).doReturn(entity)
val found = service.getByName(entity.name)
assertEquals(entity, found)
}
@Nested
inner class Update {
@Test
fun `calls and returns save() in the repository`() {
whenever(repository.save(entity)).doReturn(entity)
whenever(repository.findByName(entity.name)).doReturn(null)
doReturn(true).whenever(service).existsById(entity.id!!)
doReturn(entity).whenever(service).getById(entity.id!!)
@Test
open fun `getByName() throws EntityNotFoundRestException when no entity with the given name exists`() {
whenever(repository.findByName(entity.name)).doReturn(null)
val found = service.update(entity)
verify(repository).save(entity)
assertEquals(entity, found)
}
@Test
fun `throws EntityNotFoundRestException when no entity with the given id exists in the repository`() {
whenever(repository.findByName(entity.name)).doReturn(null)
doReturn(false).whenever(service).existsById(entity.id!!)
val exception = assertThrows<EntityNotFoundRestException> { service.update(entity) }
assertTrue(exception.value is Long)
assertEquals(entity.id, exception.value as Long)
}
@Test
fun `throws EntityAlreadyExistsRestException when an entity with the updated name exists`() {
whenever(repository.findByName(entity.name)).doReturn(entityWithEntityName)
doReturn(entity).whenever(service).getById(entity.id!!)
val exception = assertThrows<EntityAlreadyExistsRestException> { service.update(entity) }
assertEquals(entity.name, exception.value)
}
val exception = assertThrows<EntityNotFoundRestException> { service.getByName(entity.name) }
assertEquals(entity.name, exception.value)
}
@Nested
inner class DeleteByName {
@Test
fun `calls deleteByName() in the repository with the given name`() {
service.deleteByName(entity.name)
// save()
verify(repository).deleteByName(entity.name)
}
@Test
open fun `save() throws EntityAlreadyExistsRestException when an entity with the given name exists`() {
doReturn(true).whenever(service).existsByName(entity.name)
val exception = assertThrows<EntityAlreadyExistsRestException> { service.save(entity) }
assertEquals(entity.name, exception.value)
}
// update()
@Test
override fun `update() saves in the repository and returns the updated value`() {
whenever(repository.save(entity)).doReturn(entity)
whenever(repository.findByName(entity.name)).doReturn(null)
doReturn(true).whenever(service).existsById(entity.id!!)
doReturn(entity).whenever(service).getById(entity.id!!)
val found = service.update(entity)
verify(repository).save(entity)
assertEquals(entity, found)
}
@Test
override fun `update() throws EntityNotFoundRestException when no entity with the given id exists in the repository`() {
whenever(repository.findByName(entity.name)).doReturn(null)
doReturn(false).whenever(service).existsById(entity.id!!)
val exception = assertThrows<EntityNotFoundRestException> { service.update(entity) }
assertTrue(exception.value is Long)
assertEquals(entity.id, exception.value as Long)
}
@Test
open fun `update() throws EntityAlreadyExistsRestException when an entity with the updated name exists`() {
whenever(repository.findByName(entity.name)).doReturn(entityWithEntityName)
doReturn(entity).whenever(service).getById(entity.id!!)
val exception = assertThrows<EntityAlreadyExistsRestException> { service.update(entity) }
assertEquals(entity.name, exception.value)
}
// deleteByName()
@Test
open fun `deleteByName() deletes the entity with the given name in the repository`() {
service.deleteByName(entity.name)
verify(repository).deleteByName(entity.name)
}
}
@ -305,17 +292,17 @@ abstract class AbstractExternalModelServiceTest<E : Model, N : EntityDto<E>, U :
super.afterEach()
}
@Nested
inner class SaveDto {
@Test
fun `calls and returns save() with the created entity`() = saveDtoTest(entity, entitySaveDto, service)
}
// save()
@Nested
inner class UpdateDto {
@Test
fun `calls and returns update() with the created entity`() = updateDtoTest(entity, entityUpdateDto, service)
}
@Test
open fun `save(dto) calls and returns save() with the created entity`() =
saveDtoTest(entity, entitySaveDto, service)
// update()
@Test
open fun `update(dto) calls and returns update() with the created entity`() =
updateDtoTest(entity, entityUpdateDto, service)
}
abstract class AbstractExternalNamedModelServiceTest<E : NamedModel, N : EntityDto<E>, U : EntityDto<E>, S : ExternalNamedModelService<E, N, U, *>, R : NamedJpaRepository<E>> :
@ -329,17 +316,17 @@ abstract class AbstractExternalNamedModelServiceTest<E : NamedModel, N : EntityD
super.afterEach()
}
@Nested
inner class SaveDto {
@Test
fun `calls and returns save() with the created entity`() = saveDtoTest(entity, entitySaveDto, service)
}
// save()
@Nested
inner class UpdateDto {
@Test
fun `calls and returns update() with the created entity`() = updateDtoTest(entity, entityUpdateDto, service)
}
@Test
open fun `save(dto) calls and returns save() with the created entity`() =
saveDtoTest(entity, entitySaveDto, service)
// update()
@Test
open fun `update(dto) calls and returns update() with the created entity`() =
updateDtoTest(entity, entityUpdateDto, service)
}
fun <E, N : EntityDto<E>> saveDtoTest(entity: E, entitySaveDto: N, service: ExternalService<E, N, *, *>) {

View File

@ -19,7 +19,8 @@ import javax.servlet.http.Cookie
import javax.servlet.http.HttpServletRequest
import kotlin.test.*
class EmployeeServiceTest : AbstractExternalModelServiceTest<Employee, EmployeeSaveDto, EmployeeUpdateDto, EmployeeService, EmployeeRepository>() {
class EmployeeServiceTest :
AbstractExternalModelServiceTest<Employee, EmployeeSaveDto, EmployeeUpdateDto, EmployeeService, EmployeeRepository>() {
private val passwordEncoder = BCryptPasswordEncoder()
override val entity: Employee = employee(passwordEncoder, id = 0L)
@ -33,7 +34,16 @@ class EmployeeServiceTest : AbstractExternalModelServiceTest<Employee, EmployeeS
override val repository: EmployeeRepository = mock()
override val service: EmployeeService = spy(EmployeeServiceImpl(repository, passwordEncoder))
private val entitySaveDtoEmployee = Employee(entitySaveDto.id, entitySaveDto.firstName, entitySaveDto.lastName, passwordEncoder.encode(entitySaveDto.password), isDefaultGroupUser = false, isSystemUser = false, group = null, permissions = entitySaveDto.permissions)
private val entitySaveDtoEmployee = Employee(
entitySaveDto.id,
entitySaveDto.firstName,
entitySaveDto.lastName,
passwordEncoder.encode(entitySaveDto.password),
isDefaultGroupUser = false,
isSystemUser = false,
group = null,
permissions = entitySaveDto.permissions
)
@AfterEach
override fun afterEach() {
@ -41,141 +51,154 @@ class EmployeeServiceTest : AbstractExternalModelServiceTest<Employee, EmployeeS
super.afterEach()
}
@Nested
inner class ExistsByFirstNameAndLastName {
@Test
fun `returns true when an employee with the given first name and last name exists`() {
whenever(repository.existsByFirstNameAndLastName(entity.firstName, entity.lastName)).doReturn(true)
// existsByFirstNameAndLastName()
val found = service.existsByFirstNameAndLastName(entity.firstName, entity.lastName)
@Test
fun `existsByFirstNameAndLastName() returns true when an employee with the given first name and last name exists`() {
whenever(repository.existsByFirstNameAndLastName(entity.firstName, entity.lastName)).doReturn(true)
assertTrue(found)
}
val found = service.existsByFirstNameAndLastName(entity.firstName, entity.lastName)
@Test
fun `returns false when no employee with the given first name and last name exists`() {
whenever(repository.existsByFirstNameAndLastName(entity.firstName, entity.lastName)).doReturn(false)
val found = service.existsByFirstNameAndLastName(entity.firstName, entity.lastName)
assertFalse(found)
}
assertTrue(found)
}
@Nested
inner class GetById {
@Test
fun `throws EntityNotFoundRestException when the corresponding employee is a default group user`() {
whenever(repository.findById(entityDefaultGroupUser.id)).doReturn(Optional.of(entityDefaultGroupUser))
@Test
fun `existsByFirstNameAndLastName() returns false when no employee with the given first name and last name exists`() {
whenever(repository.existsByFirstNameAndLastName(entity.firstName, entity.lastName)).doReturn(false)
val exception = assertThrows<EntityNotFoundRestException> { service.getById(entityDefaultGroupUser.id, ignoreDefaultGroupUsers = true, ignoreSystemUsers = false) }
assertTrue(exception.value is Long)
assertEquals(entityDefaultGroupUser.id, exception.value as Long)
}
val found = service.existsByFirstNameAndLastName(entity.firstName, entity.lastName)
@Test
fun `throws EntityNotFoundRestException when the corresponding employee is a system user`() {
whenever(repository.findById(entitySystemUser.id)).doReturn(Optional.of(entitySystemUser))
val exception = assertThrows<EntityNotFoundRestException> { service.getById(entitySystemUser.id, ignoreDefaultGroupUsers = false, ignoreSystemUsers = true) }
assertTrue(exception.value is Long)
assertEquals(entitySystemUser.id, exception.value as Long)
}
assertFalse(found)
}
@Nested
inner class GetByGroup {
@Test
fun `returns all the employees with the given group from the repository`() {
whenever(repository.findAllByGroup(group)).doReturn(entityList)
// getById()
val found = service.getByGroup(group)
@Test
fun `getById() throws EntityNotFoundRestException when the corresponding employee is a default group user`() {
whenever(repository.findById(entityDefaultGroupUser.id)).doReturn(Optional.of(entityDefaultGroupUser))
assertTrue(found.containsAll(entityList))
assertTrue(entityList.containsAll(found))
}
@Test
fun `returns an empty list when there is no employee with the given group in the repository`() {
whenever(repository.findAllByGroup(group)).doReturn(listOf())
val found = service.getByGroup(group)
assertTrue(found.isEmpty())
val exception = assertThrows<EntityNotFoundRestException> {
service.getById(
entityDefaultGroupUser.id,
ignoreDefaultGroupUsers = true,
ignoreSystemUsers = false
)
}
assertTrue(exception.value is Long)
assertEquals(entityDefaultGroupUser.id, exception.value as Long)
}
@Nested
inner class GetDefaultGroupUser {
@Test
fun `returns the default employee of the given group from the repository`() {
whenever(repository.findByIsDefaultGroupUserIsTrueAndGroupIs(group)).doReturn(entityDefaultGroupUser)
@Test
fun `getById() throws EntityNotFoundRestException when the corresponding employee is a system user`() {
whenever(repository.findById(entitySystemUser.id)).doReturn(Optional.of(entitySystemUser))
val found = service.getDefaultGroupEmployee(group)
assertEquals(entityDefaultGroupUser, found)
val exception = assertThrows<EntityNotFoundRestException> {
service.getById(
entitySystemUser.id,
ignoreDefaultGroupUsers = false,
ignoreSystemUsers = true
)
}
assertTrue(exception.value is Long)
assertEquals(entitySystemUser.id, exception.value as Long)
}
@Nested
inner class Save {
@Test
fun `calls save() in the repository`() {
whenever(repository.save(entity)).doReturn(entity)
doReturn(false).whenever(repository).existsByFirstNameAndLastName(entity.firstName, entity.lastName)
// getByGroup()
val found = service.save(entity)
@Test
fun `getByGroup() returns all the employees with the given group from the repository`() {
whenever(repository.findAllByGroup(group)).doReturn(entityList)
verify(repository).save(entity)
assertEquals(entity, found)
}
val found = service.getByGroup(group)
@Test
fun `throws EntityAlreadyExistsException when firstName and lastName exists`() {
doReturn(true).whenever(repository).existsByFirstNameAndLastName(entity.firstName, entity.lastName)
val exception = assertThrows<EntityAlreadyExistsRestException> { service.save(entity) }
assertEquals("${entity.firstName} ${entity.lastName}", exception.value)
}
assertTrue(found.containsAll(entityList))
assertTrue(entityList.containsAll(found))
}
@Nested
inner class SaveDto {
@Test
fun `calls save() with the created employee`() {
whenever(entitySaveDto.toEntity()).doReturn(entitySaveDtoEmployee)
doReturn(entitySaveDtoEmployee).whenever(service).save(entitySaveDtoEmployee)
@Test
fun `getByGroup() returns an empty list when there is no employee with the given group in the repository`() {
whenever(repository.findAllByGroup(group)).doReturn(listOf())
val found = service.save(entitySaveDto)
val found = service.getByGroup(group)
verify(service).save(entitySaveDtoEmployee)
assertEquals(entitySaveDtoEmployee, found)
}
assertTrue(found.isEmpty())
}
@Nested
inner class Update {
@Test
fun `throws EntityAlreadyExistsRestException when a different employee with the given first name and last name exists`() {
whenever(repository.findByFirstNameAndLastName(entity.firstName, entity.lastName)).doReturn(entityDefaultGroupUser)
doReturn(entity).whenever(service).getById(eq(entity.id), any(), any())
// getDefaultGroupUser()
val exception = assertThrows<EntityAlreadyExistsRestException> { service.update(entity, true, ignoreSystemUsers = true) }
assertTrue(exception.value is String)
assertEquals("${entity.firstName} ${entity.lastName}", exception.value as String)
@Test
fun `getDefaultGroupUser() returns the default employee of the given group from the repository`() {
whenever(repository.findByIsDefaultGroupUserIsTrueAndGroupIs(group)).doReturn(entityDefaultGroupUser)
val found = service.getDefaultGroupEmployee(group)
assertEquals(entityDefaultGroupUser, found)
}
// save()
override fun `save() saves in the repository and returns the saved value`() {
whenever(repository.save(entity)).doReturn(entity)
doReturn(false).whenever(repository).existsByFirstNameAndLastName(entity.firstName, entity.lastName)
val found = service.save(entity)
verify(repository).save(entity)
assertEquals(entity, found)
}
@Test
fun `save() throws EntityAlreadyExistsException when firstName and lastName exists`() {
doReturn(true).whenever(repository).existsByFirstNameAndLastName(entity.firstName, entity.lastName)
val exception = assertThrows<EntityAlreadyExistsRestException> { service.save(entity) }
assertEquals("${entity.firstName} ${entity.lastName}", exception.value)
}
@Test
fun `save(dto) calls and returns save() with the created employee`() {
whenever(entitySaveDto.toEntity()).doReturn(entitySaveDtoEmployee)
doReturn(entitySaveDtoEmployee).whenever(service).save(entitySaveDtoEmployee)
val found = service.save(entitySaveDto)
verify(service).save(entitySaveDtoEmployee)
assertEquals(entitySaveDtoEmployee, found)
}
// update()
@Test
fun `update() throws EntityAlreadyExistsRestException when a different employee with the given first name and last name exists`() {
whenever(repository.findByFirstNameAndLastName(entity.firstName, entity.lastName)).doReturn(
entityDefaultGroupUser
)
doReturn(entity).whenever(service).getById(eq(entity.id), any(), any())
val exception = assertThrows<EntityAlreadyExistsRestException> {
service.update(
entity,
true,
ignoreSystemUsers = true
)
}
assertTrue(exception.value is String)
assertEquals("${entity.firstName} ${entity.lastName}", exception.value as String)
}
}
class EmployeeGroupServiceTest : AbstractExternalModelServiceTest<EmployeeGroup, EmployeeGroupSaveDto, EmployeeGroupUpdateDto, EmployeeGroupServiceImpl, EmployeeGroupRepository>() {
class EmployeeGroupServiceTest :
AbstractExternalNamedModelServiceTest<EmployeeGroup, EmployeeGroupSaveDto, EmployeeGroupUpdateDto, EmployeeGroupService, EmployeeGroupRepository>() {
private val employeeService: EmployeeService = mock()
override val repository: EmployeeGroupRepository = mock()
override val service: EmployeeGroupServiceImpl = spy(EmployeeGroupServiceImpl(repository, employeeService))
override val service: EmployeeGroupServiceImpl = spy(EmployeeGroupServiceImpl(employeeService, repository))
override val entity: EmployeeGroup = employeeGroup(id = 0L, name = "group")
override val anotherEntity: EmployeeGroup = employeeGroup(id = 1L, name = "another group")
override val entitySaveDto: EmployeeGroupSaveDto = spy(employeeGroupSaveDto(name = "group"))
override val entityUpdateDto: EmployeeGroupUpdateDto = spy(employeeGroupUpdateDto(id = 0L, name = "groupL"))
override val entityWithEntityName: EmployeeGroup = employeeGroup(id = 2L, name = entity.name)
private val groupEmployeeId = 1000000L + entity.id!!
private val groupEmployee = employee(BCryptPasswordEncoder(), id = groupEmployeeId, group = entity)
@ -186,213 +209,188 @@ class EmployeeGroupServiceTest : AbstractExternalModelServiceTest<EmployeeGroup,
super.afterEach()
}
@Nested
inner class ExistsByName {
@Test
fun `returns true when a group with the given name exists in the repository`() {
whenever(repository.existsByName(entity.name)).doReturn(true)
// getEmployeesForGroup()
val found = service.existsByName(entity.name)
@Test
fun `getEmployeesForGroup() returns all employees in the given group`() {
val group = employeeGroup(id = 1L, employees = mutableSetOf(groupEmployee))
assertTrue(found)
}
doReturn(group).whenever(service).getById(group.id!!)
@Test
fun `returns false when no group with the given name exists in the repository`() {
whenever(repository.existsByName(entity.name)).doReturn(false)
val found = service.getEmployeesForGroup(group.id!!)
val found = service.existsByName(entity.name)
assertFalse(found)
}
assertTrue(found.contains(groupEmployee))
assertTrue(found.size == 1)
}
@Nested
inner class GetEmployeesForGroup {
@Test
fun `returns all employees in the given group`() {
val group = employeeGroup(id = 1L, employees = mutableSetOf(groupEmployee))
@Test
fun `getEmployeesForGroup() returns empty collection when the given group contains any employee`() {
doReturn(entity).whenever(service).getById(entity.id!!)
doReturn(group).whenever(service).getById(group.id!!)
val found = service.getEmployeesForGroup(entity.id!!)
val found = service.getEmployeesForGroup(group.id!!)
assertTrue(found.contains(groupEmployee))
assertTrue(found.size == 1)
}
@Test
fun `returns empty collection when the given group contains any employee`() {
doReturn(entity).whenever(service).getById(entity.id!!)
val found = service.getEmployeesForGroup(entity.id!!)
assertTrue(found.isEmpty())
}
assertTrue(found.isEmpty())
}
@Nested
inner class GetRequestDefaultGroup {
@Test
fun `returns the group contained in the cookie of the HTTP request`() {
val cookies: Array<Cookie> = arrayOf(Cookie(defaultGroupCookieName, groupEmployeeId.toString()))
val request: HttpServletRequest = mock()
// getRequestDefaultGroup()
whenever(request.cookies).doReturn(cookies)
whenever(employeeService.getById(eq(groupEmployeeId), any(), any())).doReturn(groupEmployee)
@Test
fun `getRequestDefaultGroup() returns the group contained in the cookie of the HTTP request`() {
val cookies: Array<Cookie> = arrayOf(Cookie(defaultGroupCookieName, groupEmployeeId.toString()))
val request: HttpServletRequest = mock()
val found = service.getRequestDefaultGroup(request)
whenever(request.cookies).doReturn(cookies)
whenever(employeeService.getById(eq(groupEmployeeId), any(), any())).doReturn(groupEmployee)
assertEquals(entity, found)
}
val found = service.getRequestDefaultGroup(request)
@Test
fun `throws EntityNotFoundRestException when the HTTP request does not contains a cookie for the default group`() {
val request: HttpServletRequest = mock()
whenever(request.cookies).doReturn(arrayOf())
val exception = assertThrows<EntityNotFoundRestException> { service.getRequestDefaultGroup(request) }
assertEquals("defaultGroup", exception.value)
}
assertEquals(entity, found)
}
@Nested
inner class SetResponseDefaultGroup {
@Test
fun `the default group cookie has been added to the given HTTP response with the given group id`() {
val response = MockHttpServletResponse()
@Test
fun `getRequestDefaultGroup() throws EntityNotFoundRestException when the HTTP request does not contains a cookie for the default group`() {
val request: HttpServletRequest = mock()
whenever(employeeService.getDefaultGroupEmployee(entity)).doReturn(groupEmployee)
doReturn(entity).whenever(service).getById(entity.id!!)
whenever(request.cookies).doReturn(arrayOf())
service.setResponseDefaultGroup(entity.id!!, response)
val found = response.getCookie(defaultGroupCookieName)
assertNotNull(found)
assertEquals(defaultGroupCookieName, found.name)
assertEquals(groupEmployeeId.toString(), found.value)
assertEquals(defaultGroupCookieMaxAge, found.maxAge)
assertTrue(found.isHttpOnly)
assertTrue(found.secure)
}
val exception = assertThrows<EntityNotFoundRestException> { service.getRequestDefaultGroup(request) }
assertEquals("defaultGroup", exception.value)
}
@Nested
inner class AddEmployeeToGroup {
@Test
fun `calls addEmployeeToGroup() with the group of the given groupId and the employee of the given employeeId`() {
whenever(employeeService.getById(groupEmployeeId)).doReturn(groupEmployee)
doReturn(entity).whenever(service).getById(entity.id!!)
// setResponseDefaultGroup()
service.addEmployeeToGroup(entity.id!!, groupEmployeeId)
@Test
fun `setResponseDefaultGroup() the default group cookie has been added to the given HTTP response with the given group id`() {
val response = MockHttpServletResponse()
verify(service).addEmployeeToGroup(entity, groupEmployee)
}
whenever(employeeService.getDefaultGroupEmployee(entity)).doReturn(groupEmployee)
doReturn(entity).whenever(service).getById(entity.id!!)
@Test
fun `calls update() and employeeService_update() with the updated entities`() {
val group = employeeGroup()
val employee = employee()
service.setResponseDefaultGroup(entity.id!!, response)
val found = response.getCookie(defaultGroupCookieName)
whenever(employeeService.update(employee)).doReturn(employee)
doReturn(group).whenever(service).update(group)
service.addEmployeeToGroup(group, employee)
verify(service).update(group)
verify(employeeService).update(employee)
assertTrue(group.employees.contains(employee))
assertEquals(group, employee.group)
}
@Test
fun `do nothing when the given employee is already in the given group`() {
val group = employeeGroup()
val employee = employee(group = group)
service.addEmployeeToGroup(group, employee)
verify(service, times(0)).update(group)
verify(employeeService, times(0)).update(employee)
}
@Test
fun `remove previous group from the given employee and add it the the given group`() {
val group = employeeGroup(id = 0L)
val previousGroup = employeeGroup(id = 1L)
val employee = employee(group = previousGroup)
whenever(employeeService.update(employee)).doReturn(employee)
doReturn(group).whenever(service).update(group)
doReturn(group).whenever(service).update(previousGroup)
service.addEmployeeToGroup(group, employee)
verify(service).removeEmployeeFromGroup(previousGroup, employee)
verify(service).update(group)
verify(employeeService, times(2)).update(employee)
assertTrue(group.employees.contains(employee))
assertEquals(group, employee.group)
}
assertNotNull(found)
assertEquals(defaultGroupCookieName, found.name)
assertEquals(groupEmployeeId.toString(), found.value)
assertEquals(defaultGroupCookieMaxAge, found.maxAge)
assertTrue(found.isHttpOnly)
assertTrue(found.secure)
}
@Nested
inner class RemoveEmployeeFromGroup {
@Test
fun `calls removeEmployeeFromGroup() with the group of the given group id and the employee of the given employee id`() {
whenever(employeeService.getById(groupEmployeeId)).doReturn(groupEmployee)
doReturn(entity).whenever(service).getById(entity.id!!)
doAnswer { it.arguments[0] }.whenever(service).update(any<EmployeeGroup>())
// addEmployeeToGroup()
service.removeEmployeeFromGroup(entity.id!!, groupEmployeeId)
@Test
fun `addEmployeeToGroup() calls addEmployeeToGroup() with the group of the given groupId and the employee of the given employeeId`() {
whenever(employeeService.getById(groupEmployeeId)).doReturn(groupEmployee)
doReturn(entity).whenever(service).getById(entity.id!!)
doAnswer { }.whenever(service).addEmployeeToGroup(entity, groupEmployee)
verify(service).removeEmployeeFromGroup(entity, groupEmployee)
}
service.addEmployeeToGroup(entity.id!!, groupEmployeeId)
@Test
fun `calls update() and employeeService_update() with the updated entities`() {
val employee = employee()
val group = employeeGroup(employees = mutableSetOf(employee))
employee.group = group
verify(service).addEmployeeToGroup(entity, groupEmployee)
}
whenever(employeeService.update(employee)).doReturn(employee)
doReturn(group).whenever(service).update(group)
@Test
fun `addEmployeeToGroup() calls update() and employeeService_update() with the updated entities`() {
val group = employeeGroup()
val employee = employee()
service.removeEmployeeFromGroup(group, employee)
whenever(employeeService.update(employee)).doReturn(employee)
doReturn(group).whenever(service).update(group)
verify(service).update(group)
verify(employeeService).update(employee)
service.addEmployeeToGroup(group, employee)
assertFalse(group.employees.contains(employee))
assertNull(employee.group)
}
verify(service).update(group)
verify(employeeService).update(employee)
@Test
fun `do nothing when the given employee is not in the given group`() {
val employee = employee()
val group = employeeGroup(id = 0L)
val anotherGroup = employeeGroup(id = 1L, employees = mutableSetOf(employee))
employee.group = anotherGroup
assertTrue(group.employees.contains(employee))
assertEquals(group, employee.group)
}
service.removeEmployeeFromGroup(group, employee)
@Test
fun `addEmployeeToGroup() do nothing when the given employee is already in the given group`() {
val group = employeeGroup()
val employee = employee(group = group)
verify(service, times(0)).update(anotherGroup)
verify(employeeService, times(0)).update(employee)
}
service.addEmployeeToGroup(group, employee)
@Test
fun `do nothing when the given employee is not in a group`() {
val employee = employee()
val group = employeeGroup()
verify(service, times(0)).update(group)
verify(employeeService, times(0)).update(employee)
}
service.removeEmployeeFromGroup(group, employee)
@Test
fun `addEmployeeToGroup() remove previous group from the given employee and add it the the given group`() {
val group = employeeGroup(id = 0L)
val previousGroup = employeeGroup(id = 1L)
val employee = employee(group = previousGroup)
verify(service, times(0)).update(group)
verify(employeeService, times(0)).update(employee)
}
whenever(employeeService.update(employee)).doReturn(employee)
doReturn(group).whenever(service).update(group)
doReturn(group).whenever(service).update(previousGroup)
service.addEmployeeToGroup(group, employee)
verify(service).removeEmployeeFromGroup(previousGroup, employee)
verify(service).update(group)
verify(employeeService, times(2)).update(employee)
assertTrue(group.employees.contains(employee))
assertEquals(group, employee.group)
}
// removeEmployeeFromGroup()
@Test
fun `removeEmployeeFromGroup() calls removeEmployeeFromGroup() with the group of the given group id and the employee of the given employee id`() {
whenever(employeeService.getById(groupEmployeeId)).doReturn(groupEmployee)
doReturn(entity).whenever(service).getById(entity.id!!)
doAnswer { it.arguments[0] }.whenever(service).update(any<EmployeeGroup>())
service.removeEmployeeFromGroup(entity.id!!, groupEmployeeId)
verify(service).removeEmployeeFromGroup(entity, groupEmployee)
}
@Test
fun `removeEmployeeFromGroup() calls update() and employeeService_update() with the updated entities`() {
val employee = employee()
val group = employeeGroup(employees = mutableSetOf(employee))
employee.group = group
whenever(employeeService.update(employee)).doReturn(employee)
doReturn(group).whenever(service).update(group)
service.removeEmployeeFromGroup(group, employee)
verify(service).update(group)
verify(employeeService).update(employee)
assertFalse(group.employees.contains(employee))
assertNull(employee.group)
}
@Test
fun `removeEmployeeFromGroup() do nothing when the given employee is not in the given group`() {
val employee = employee()
val group = employeeGroup(id = 0L)
val anotherGroup = employeeGroup(id = 1L, employees = mutableSetOf(employee))
employee.group = anotherGroup
service.removeEmployeeFromGroup(group, employee)
verify(service, times(0)).update(anotherGroup)
verify(employeeService, times(0)).update(employee)
}
@Test
fun `removeEmployeeFromGroup() do nothing when the given employee is not in a group`() {
val employee = employee()
val group = employeeGroup()
service.removeEmployeeFromGroup(group, employee)
verify(service, times(0)).update(group)
verify(employeeService, times(0)).update(employee)
}
}
@ -407,36 +405,39 @@ class EmployeeUserDetailsServiceTest {
reset(employeeService, service)
}
@Nested
inner class LoadUserByUsername {
@Test
fun `calls loadUserByEmployeeId() with the given username as an id`() {
whenever(employeeService.getById(eq(employee.id), any(), any())).doReturn(employee)
doReturn(User(employee.id.toString(), employee.password, listOf())).whenever(service).loadUserByEmployeeId(employee.id)
// loadUserByUsername()
service.loadUserByUsername(employee.id.toString())
@Test
fun `loadUserByUsername() calls loadUserByEmployeeId() with the given username as an id`() {
whenever(employeeService.getById(eq(employee.id), any(), any())).doReturn(employee)
doReturn(User(employee.id.toString(), employee.password, listOf())).whenever(service)
.loadUserByEmployeeId(employee.id)
verify(service).loadUserByEmployeeId(eq(employee.id), any())
}
service.loadUserByUsername(employee.id.toString())
@Test
fun `throws UsernameNotFoundException when no employee with the given id exists`() {
whenever(employeeService.getById(eq(employee.id), any(), any())).doThrow(EntityNotFoundRestException(employee.id))
assertThrows<UsernameNotFoundException> { service.loadUserByUsername(employee.id.toString()) }
}
verify(service).loadUserByEmployeeId(eq(employee.id), any())
}
@Nested
inner class LoadUserByEmployeeId {
@Test
fun `returns an User corresponding to the employee with the given id`() {
whenever(employeeService.getById(eq(employee.id), any(), any())).doReturn(employee)
@Test
fun `loadUserByUsername() throws UsernameNotFoundException when no employee with the given id exists`() {
whenever(employeeService.getById(eq(employee.id), any(), any())).doThrow(
EntityNotFoundRestException(
employee.id
)
)
val found = service.loadUserByEmployeeId(employee.id)
assertThrows<UsernameNotFoundException> { service.loadUserByUsername(employee.id.toString()) }
}
assertEquals(employee.id, found.username.toLong())
assertEquals(employee.password, found.password)
}
// loadUserByEmployeeId
@Test
fun `loadUserByEmployeeId() returns an User corresponding to the employee with the given id`() {
whenever(employeeService.getById(eq(employee.id), any(), any())).doReturn(employee)
val found = service.loadUserByEmployeeId(employee.id)
assertEquals(employee.id, found.username.toLong())
assertEquals(employee.password, found.password)
}
}

View File

@ -9,7 +9,8 @@ import org.junit.jupiter.api.Nested
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class CompanyServiceTest : AbstractExternalNamedModelServiceTest<Company, CompanySaveDto, CompanyUpdateDto, CompanyService, CompanyRepository>() {
class CompanyServiceTest :
AbstractExternalNamedModelServiceTest<Company, CompanySaveDto, CompanyUpdateDto, CompanyService, CompanyRepository>() {
private val recipeService: RecipeJavaService = mock()
override val repository: CompanyRepository = mock()
override val service: CompanyService = spy(CompanyServiceImpl(repository, recipeService))
@ -26,22 +27,21 @@ class CompanyServiceTest : AbstractExternalNamedModelServiceTest<Company, Compan
super.afterEach()
}
@Nested
inner class IsLinkedToRecipes {
fun `returns true when a given company is linked to one or more recipes`() {
whenever(recipeService.existsByCompany(entity)).doReturn(true)
// isLinkedToRecipes
val found = service.isLinkedToRecipes(entity)
fun `isLinkedToRecipes() returns true when a given company is linked to one or more recipes`() {
whenever(recipeService.existsByCompany(entity)).doReturn(true)
assertTrue(found)
}
val found = service.isLinkedToRecipes(entity)
fun `returns false when a given company is not linked to any recipe`() {
whenever(recipeService.existsByCompany(entity)).doReturn(false)
assertTrue(found)
}
val found = service.isLinkedToRecipes(entity)
fun `isLinkedToRecipes() returns false when a given company is not linked to any recipe`() {
whenever(recipeService.existsByCompany(entity)).doReturn(false)
assertFalse(found)
}
val found = service.isLinkedToRecipes(entity)
assertFalse(found)
}
}

View File

@ -29,164 +29,150 @@ class MaterialServiceTest :
override val entitySaveDto: MaterialSaveDto = spy(materialSaveDto())
override val entityUpdateDto: MaterialUpdateDto = spy(materialUpdateDto(id = 0L))
private val materialType = materialType()
@AfterEach
override fun afterEach() {
reset(simdutService)
super.afterEach()
}
@Nested
inner class ExistsByMaterialType {
private val materialType = materialType()
// existsByMaterialType
@Test
fun `returns true when a material with the given material type exists in the repository`() {
whenever(repository.existsByMaterialType(materialType)).doReturn(true)
@Test
fun `existsByMaterialType() returns true when a material with the given material type exists in the repository`() {
whenever(repository.existsByMaterialType(materialType)).doReturn(true)
val found = service.existsByMaterialType(materialType)
val found = service.existsByMaterialType(materialType)
assertTrue(found)
}
@Test
fun `returns false when no material with the given material type exists in the repository`() {
whenever(repository.existsByMaterialType(materialType)).doReturn(false)
val found = service.existsByMaterialType(materialType)
assertFalse(found)
}
assertTrue(found)
}
@Nested
inner class HasSimdut {
@Test
fun `returns false when simdutService_exists() returns false`() {
whenever(simdutService.exists(entity)).doReturn(false)
doReturn(entity).whenever(service).getById(entity.id!!)
@Test
fun `existsByMaterialType() returns false when no material with the given material type exists in the repository`() {
whenever(repository.existsByMaterialType(materialType)).doReturn(false)
val found = service.hasSimdut(entity.id!!)
val found = service.existsByMaterialType(materialType)
assertFalse(found)
}
@Test
fun `returns true when simdutService_exists() returns true`() {
whenever(simdutService.exists(entity)).doReturn(true)
doReturn(entity).whenever(service).getById(entity.id!!)
val found = service.hasSimdut(entity.id!!)
assertTrue(found)
}
assertFalse(found)
}
@Nested
inner class GetAllNotMixType {
@Test
fun `returns a list containing every material that are not a mix type`() {
val mixTypeMaterial = material(name = "mix type material", isMixType = true)
val materialList = listOf(entity, mixTypeMaterial)
// hasSimdut()
doReturn(materialList).whenever(service).getAll()
@Test
fun `hasSimdut() returns false when simdutService_exists() returns false`() {
whenever(simdutService.exists(entity)).doReturn(false)
doReturn(entity).whenever(service).getById(entity.id!!)
val found = service.getAllNotMixType()
val found = service.hasSimdut(entity.id!!)
assertTrue(found.contains(entity))
assertFalse(found.contains(mixTypeMaterial))
}
assertFalse(found)
}
@Nested
inner class Save {
@Test
fun `throws EntityAlreadyExistsRestException when a material with the given name exists in the repository`() {
doReturn(true).whenever(service).existsByName(entity.name)
@Test
fun `hasSimdut() returns true when simdutService_exists() returns true`() {
whenever(simdutService.exists(entity)).doReturn(true)
doReturn(entity).whenever(service).getById(entity.id!!)
val exception = assertThrows<EntityAlreadyExistsRestException> { service.save(entity) }
assertEquals(entity.name, exception.value)
}
val found = service.hasSimdut(entity.id!!)
assertTrue(found)
}
@Nested
inner class SaveDto {
@Test
fun `calls simdutService_write() with the saved entity`() {
val mockMultipartFile = spy(MockMultipartFile("simdut", byteArrayOf()))
val materialSaveDto = spy(materialSaveDto(simdutFile = mockMultipartFile))
// getAllNotMixType()
doReturn(false).whenever(mockMultipartFile).isEmpty
doReturn(entity).whenever(materialSaveDto).toEntity()
doReturn(entity).whenever(service).save(entity)
@Test
fun `getAllNotMixType() returns a list containing every material that are not a mix type`() {
val mixTypeMaterial = material(name = "mix type material", isMixType = true)
val materialList = listOf(entity, mixTypeMaterial)
service.save(materialSaveDto)
doReturn(materialList).whenever(service).getAll()
verify(simdutService).write(entity, mockMultipartFile)
}
val found = service.getAllNotMixType()
assertTrue(found.contains(entity))
assertFalse(found.contains(mixTypeMaterial))
}
@Nested
inner class Update {
@Test
fun `throws EntityAlreadyExistsRestException when another material with the updated name exists in the repository`() {
val material = material(id = 0L, name = "name")
val anotherMaterial = material(id = 1L, name = "name")
// save()
whenever(repository.findByName(material.name)).doReturn(anotherMaterial)
doReturn(entity).whenever(service).getById(material.id!!)
@Test
fun `save() throws EntityAlreadyExistsRestException when a material with the given name exists in the repository`() {
doReturn(true).whenever(service).existsByName(entity.name)
val exception = assertThrows<EntityAlreadyExistsRestException> { service.update(material) }
assertEquals(material.name, exception.value)
}
val exception = assertThrows<EntityAlreadyExistsRestException> { service.save(entity) }
assertEquals(entity.name, exception.value)
}
/** Helper function to replace collections.in because the id is not considered in the equals function of Material while Thymeleaf is supported. */
private infix fun Collection<Material>.contains(material: Material): Boolean =
any { it.id == material.id }
@Test
fun `save(dto) calls simdutService_write() with the saved entity`() {
val mockMultipartFile = spy(MockMultipartFile("simdut", byteArrayOf()))
val materialSaveDto = spy(materialSaveDto(simdutFile = mockMultipartFile))
@Nested
inner class GetAllForMixCreation {
@Test
fun `returns all normal materials and all mix type materials for the given recipe`() {
val normalMaterial = material(id = 0L, isMixType = false)
val mixTypeMaterial = material(id = 1L, isMixType = true)
val anotherMixTypeMaterial = material(id = 2L, isMixType = true)
val materials = listOf(normalMaterial, mixTypeMaterial, anotherMixTypeMaterial)
val recipe =
recipe(id = 0L, mixes = mutableListOf(mix(mixType = mixType(id = 0L, material = mixTypeMaterial))))
doReturn(false).whenever(mockMultipartFile).isEmpty
doReturn(entity).whenever(materialSaveDto).toEntity()
doReturn(entity).whenever(service).save(entity)
whenever(recipeService.getById(recipe.id!!)).doReturn(recipe)
doReturn(materials).whenever(service).getAll()
service.save(materialSaveDto)
val found = service.getAllForMixCreation(recipe.id!!)
assertTrue(found contains normalMaterial)
assertTrue(found contains mixTypeMaterial)
assertFalse(found contains anotherMixTypeMaterial)
}
verify(simdutService).write(entity, mockMultipartFile)
}
@Nested
inner class GetAllForMixUpdate {
@Test
fun `returns all normal materials and all mix type materials for the recipe of the given mix without the mix type of the said mix`() {
val normalMaterial = material(id = 0L, isMixType = false)
val mixTypeMaterial = material(id = 1L, isMixType = true)
val anotherMixTypeMaterial = material(id = 2L, isMixType = true)
val materials = listOf(normalMaterial, mixTypeMaterial, anotherMixTypeMaterial)
val recipe = recipe(id = 0L, mixes = mutableListOf(mix(mixType = mixType(material = mixTypeMaterial))))
val mix = mix(id = 1L, recipe = recipe, mixType = mixType(material = anotherMixTypeMaterial))
recipe.mixes.add(mix)
// update()
whenever(mixService.getById(mix.id!!)).doReturn(mix)
doReturn(materials).whenever(service).getAll()
@Test
fun `update() throws EntityAlreadyExistsRestException when another material with the updated name exists in the repository`() {
val material = material(id = 0L, name = "name")
val anotherMaterial = material(id = 1L, name = "name")
val found = service.getAllForMixUpdate(mix.id!!)
whenever(repository.findByName(material.name)).doReturn(anotherMaterial)
doReturn(entity).whenever(service).getById(material.id!!)
assertTrue(found contains normalMaterial)
assertTrue(found contains mixTypeMaterial)
assertFalse(found contains anotherMixTypeMaterial)
}
val exception = assertThrows<EntityAlreadyExistsRestException> { service.update(material) }
assertEquals(material.name, exception.value)
}
// getAllForMixCreation()
@Test
fun `getAllForMixCreation() returns all normal materials and all mix type materials for the given recipe`() {
val normalMaterial = material(id = 0L, isMixType = false)
val mixTypeMaterial = material(id = 1L, isMixType = true)
val anotherMixTypeMaterial = material(id = 2L, isMixType = true)
val materials = listOf(normalMaterial, mixTypeMaterial, anotherMixTypeMaterial)
val recipe =
recipe(id = 0L, mixes = mutableListOf(mix(mixType = mixType(id = 0L, material = mixTypeMaterial))))
whenever(recipeService.getById(recipe.id!!)).doReturn(recipe)
doReturn(materials).whenever(service).getAll()
val found = service.getAllForMixCreation(recipe.id!!)
assertTrue(found contains normalMaterial)
assertTrue(found contains mixTypeMaterial)
assertFalse(found contains anotherMixTypeMaterial)
}
// getAllForMixUpdate()
@Test
fun `getAllForMixUpdate() returns all normal materials and all mix type materials for the recipe of the given mix without the mix type of the said mix`() {
val normalMaterial = material(id = 0L, isMixType = false)
val mixTypeMaterial = material(id = 1L, isMixType = true)
val anotherMixTypeMaterial = material(id = 2L, isMixType = true)
val materials = listOf(normalMaterial, mixTypeMaterial, anotherMixTypeMaterial)
val recipe = recipe(id = 0L, mixes = mutableListOf(mix(mixType = mixType(material = mixTypeMaterial))))
val mix = mix(id = 1L, recipe = recipe, mixType = mixType(material = anotherMixTypeMaterial))
recipe.mixes.add(mix)
whenever(mixService.getById(mix.id!!)).doReturn(mix)
doReturn(materials).whenever(service).getAll()
val found = service.getAllForMixUpdate(mix.id!!)
assertTrue(found contains normalMaterial)
assertTrue(found contains mixTypeMaterial)
assertFalse(found contains anotherMixTypeMaterial)
}
// @Nested
@ -204,4 +190,8 @@ class MaterialServiceTest :
// verify(simdutService).update(mockSimdutFile, entity)
// }
// }
/** Helper function to replace collections.in because the id is not considered in the equals function of Material while Thymeleaf is supported. */
private infix fun Collection<Material>.contains(material: Material): Boolean =
any { it.id == material.id }
}

View File

@ -13,7 +13,8 @@ import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class MaterialTypeServiceTest : AbstractExternalNamedModelServiceTest<MaterialType, MaterialTypeSaveDto, MaterialTypeUpdateDto, MaterialTypeService, MaterialTypeRepository>() {
class MaterialTypeServiceTest :
AbstractExternalNamedModelServiceTest<MaterialType, MaterialTypeSaveDto, MaterialTypeUpdateDto, MaterialTypeService, MaterialTypeRepository>() {
override val repository: MaterialTypeRepository = mock()
private val materialService: MaterialService = mock()
override val service: MaterialTypeService = spy(MaterialTypeServiceImpl(repository, materialService))
@ -23,7 +24,8 @@ class MaterialTypeServiceTest : AbstractExternalNamedModelServiceTest<MaterialTy
private val systemType = materialType(id = 3L, name = "systype", prefix = "SYS", systemType = true)
private val anotherSystemType = materialType(id = 4L, name = "another systype", prefix = "ASY", systemType = true)
override val entitySaveDto: MaterialTypeSaveDto = spy(materialTypeSaveDto(name = "material type", prefix = "MAT"))
override val entityUpdateDto: MaterialTypeUpdateDto = spy(materialTypeUpdateDto(id = 0L, name = "material type", prefix = "MAT"))
override val entityUpdateDto: MaterialTypeUpdateDto =
spy(materialTypeUpdateDto(id = 0L, name = "material type", prefix = "MAT"))
@AfterEach
override fun afterEach() {
@ -31,151 +33,141 @@ class MaterialTypeServiceTest : AbstractExternalNamedModelServiceTest<MaterialTy
super.afterEach()
}
@Nested
inner class ExistsByPrefix {
@Test
fun `returns true when a material type with the given prefix exists`() {
whenever(repository.existsByPrefix(entity.prefix)).doReturn(true)
// existsByPrefix()
val found = service.existsByPrefix(entity.prefix)
@Test
fun `existsByPrefix() returns true when a material type with the given prefix exists`() {
whenever(repository.existsByPrefix(entity.prefix)).doReturn(true)
assertTrue(found)
}
val found = service.existsByPrefix(entity.prefix)
@Test
fun `returns false when no material type with the given prefix exists`() {
whenever(repository.existsByPrefix(entity.prefix)).doReturn(false)
val found = service.existsByPrefix(entity.prefix)
assertFalse(found)
}
assertTrue(found)
}
@Nested
inner class IsUsedByMaterial {
@Test
fun `returns true when materialService_existsByMaterialType() returns true`() {
whenever(materialService.existsByMaterialType(entity)).doReturn(true)
@Test
fun `existsByPrefix() returns false when no material type with the given prefix exists`() {
whenever(repository.existsByPrefix(entity.prefix)).doReturn(false)
val found = service.isUsedByMaterial(entity)
val found = service.existsByPrefix(entity.prefix)
assertTrue(found)
}
@Test
fun `returns false when materialService_existsByMaterialType() returns false`() {
whenever(materialService.existsByMaterialType(entity)).doReturn(false)
val found = service.isUsedByMaterial(entity)
assertFalse(found)
}
assertFalse(found)
}
@Nested
inner class GetAllSystemTypes {
@Test
fun `returns all system types`() {
whenever(repository.findAllBySystemTypeIs(true)).doReturn(listOf(systemType, anotherSystemType))
// isUsedByMaterial()
val found = service.getAllSystemTypes()
@Test
fun `isUsedByMaterial() returns true when materialService_existsByMaterialType() returns true`() {
whenever(materialService.existsByMaterialType(entity)).doReturn(true)
assertTrue(found.contains(systemType))
assertTrue(found.contains(anotherSystemType))
}
val found = service.isUsedByMaterial(entity)
assertTrue(found)
}
@Nested
inner class GetAllNonSystemTypes {
@Test
fun `returns all non system types`() {
whenever(repository.findAllBySystemTypeIs(false)).doReturn(listOf(entity, anotherEntity))
@Test
fun `isUsedByMaterial() returns false when materialService_existsByMaterialType() returns false`() {
whenever(materialService.existsByMaterialType(entity)).doReturn(false)
val found = service.getAllNonSystemType()
val found = service.isUsedByMaterial(entity)
assertTrue(found.contains(entity))
assertTrue(found.contains(anotherEntity))
}
assertFalse(found)
}
@Nested
inner class SaveMaterialType {
@Test
fun `throws EntityAlreadyExistsRestException when a material type with the given prefix already exists`() {
doReturn(true).whenever(service).existsByPrefix(entity.prefix)
// getAllSystemTypes()
val exception = assertThrows<EntityAlreadyExistsRestException> { service.save(entity) }
assertEquals(entity.prefix, exception.value)
}
@Test
fun `getAllSystemTypes() returns all system types`() {
whenever(repository.findAllBySystemTypeIs(true)).doReturn(listOf(systemType, anotherSystemType))
val found = service.getAllSystemTypes()
assertTrue(found.contains(systemType))
assertTrue(found.contains(anotherSystemType))
}
@Nested
inner class Update {
@Test
fun `calls and returns save() in the repository`() {
whenever(repository.save(entity)).doReturn(entity)
whenever(repository.findByName(entity.name)).doReturn(null)
whenever(repository.findByPrefix(entity.prefix)).doReturn(null)
doReturn(true).whenever(service).existsById(entity.id!!)
doReturn(entity).whenever(service).getById(entity.id!!)
// getAllNonSystemTypes()
val found = service.update(entity)
@Test
fun `getAllNonSystemTypes() returns all non system types`() {
whenever(repository.findAllBySystemTypeIs(false)).doReturn(listOf(entity, anotherEntity))
verify(repository).save(entity)
assertEquals(entity, found)
}
val found = service.getAllNonSystemType()
@Test
fun `throws EntityNotFoundRestException when no entity with the given id exists in the repository`() {
whenever(repository.findByName(entity.name)).doReturn(null)
whenever(repository.findByPrefix(entity.prefix)).doReturn(null)
doReturn(false).whenever(service).existsById(entity.id!!)
doReturn(null).whenever(service).getById(entity.id!!)
val exception = assertThrows<EntityNotFoundRestException> { service.update(entity) }
assertTrue(exception.value is Long)
assertEquals(entity.id, exception.value as Long)
}
@Test
fun `throws EntityAlreadyExistsRestException when an entity with the updated name exists`() {
whenever(repository.findByName(entity.name)).doReturn(entityWithEntityName)
whenever(repository.findByPrefix(entity.prefix)).doReturn(null)
doReturn(true).whenever(service).existsById(entity.id!!)
doReturn(entity).whenever(service).getById(entity.id!!)
val exception = assertThrows<EntityAlreadyExistsRestException> { service.update(entity) }
assertEquals(entity.name, exception.value)
}
@Test
fun `throws EntityAlreadyExistsRestException when an entity with the updated prefix exists`() {
val anotherMaterialType = materialType(prefix = entity.prefix)
whenever(repository.findByPrefix(entity.prefix)).doReturn(anotherMaterialType)
doReturn(entity).whenever(service).getById(entity.id!!)
val exception = assertThrows<EntityAlreadyExistsRestException> { service.update(entity) }
assertEquals(entity.prefix, exception.value)
}
assertTrue(found.contains(entity))
assertTrue(found.contains(anotherEntity))
}
@Nested
inner class Delete {
@Test
fun `calls delete() in the repository`() {
doReturn(false).whenever(service).isUsedByMaterial(entity)
// saveMaterialType()
service.delete(entity)
@Test
fun `saveMaterialType() throws EntityAlreadyExistsRestException when a material type with the given prefix already exists`() {
doReturn(true).whenever(service).existsByPrefix(entity.prefix)
verify(repository).delete(entity)
}
val exception = assertThrows<EntityAlreadyExistsRestException> { service.save(entity) }
assertEquals(entity.prefix, exception.value)
}
@Test
fun `throws CannotDeleteUsedMaterialTypeRestException when the material type is in use`() {
doReturn(true).whenever(service).isUsedByMaterial(entity)
// update()
assertThrows<CannotDeleteUsedMaterialTypeRestException> { service.delete(entity) }
}
override fun `update() saves in the repository and returns the updated value`() {
whenever(repository.save(entity)).doReturn(entity)
whenever(repository.findByName(entity.name)).doReturn(null)
whenever(repository.findByPrefix(entity.prefix)).doReturn(null)
doReturn(true).whenever(service).existsById(entity.id!!)
doReturn(entity).whenever(service).getById(entity.id!!)
val found = service.update(entity)
verify(repository).save(entity)
assertEquals(entity, found)
}
override fun `update() throws EntityNotFoundRestException when no entity with the given id exists in the repository`() {
whenever(repository.findByName(entity.name)).doReturn(null)
whenever(repository.findByPrefix(entity.prefix)).doReturn(null)
doReturn(false).whenever(service).existsById(entity.id!!)
doReturn(null).whenever(service).getById(entity.id!!)
val exception = assertThrows<EntityNotFoundRestException> { service.update(entity) }
assertTrue(exception.value is Long)
assertEquals(entity.id, exception.value as Long)
}
override fun `update() throws EntityAlreadyExistsRestException when an entity with the updated name exists`() {
whenever(repository.findByName(entity.name)).doReturn(entityWithEntityName)
whenever(repository.findByPrefix(entity.prefix)).doReturn(null)
doReturn(true).whenever(service).existsById(entity.id!!)
doReturn(entity).whenever(service).getById(entity.id!!)
val exception = assertThrows<EntityAlreadyExistsRestException> { service.update(entity) }
assertEquals(entity.name, exception.value)
}
@Test
fun `update() throws EntityAlreadyExistsRestException when an entity with the updated prefix exists`() {
val anotherMaterialType = materialType(prefix = entity.prefix)
whenever(repository.findByPrefix(entity.prefix)).doReturn(anotherMaterialType)
doReturn(entity).whenever(service).getById(entity.id!!)
val exception = assertThrows<EntityAlreadyExistsRestException> { service.update(entity) }
assertEquals(entity.prefix, exception.value)
}
// delete()
@Test
fun `delete() calls delete() in the repository`() {
doReturn(false).whenever(service).isUsedByMaterial(entity)
service.delete(entity)
verify(repository).delete(entity)
}
@Test
fun `delete() throws CannotDeleteUsedMaterialTypeRestException when the material type is in use`() {
doReturn(true).whenever(service).isUsedByMaterial(entity)
assertThrows<CannotDeleteUsedMaterialTypeRestException> { service.delete(entity) }
}
}

View File

@ -22,22 +22,21 @@ class MixMaterialServiceTest : AbstractModelServiceTest<MixMaterial, MixMaterial
override val entity: MixMaterial = mixMaterial(id = 0L, material = material)
override val anotherEntity: MixMaterial = mixMaterial(id = 1L, material = material)
@Nested
inner class ExistsByMaterial {
fun `returns true when a mix material with the given material exists`() {
whenever(repository.existsByMaterial(material)).doReturn(true)
// existsByMaterial()
val found = service.existsByMaterial(material)
fun `existsByMaterial() returns true when a mix material with the given material exists`() {
whenever(repository.existsByMaterial(material)).doReturn(true)
assertTrue(found)
}
val found = service.existsByMaterial(material)
fun `returns false when no mix material with the given material exists`() {
whenever(repository.existsByMaterial(material)).doReturn(false)
assertTrue(found)
}
val found = service.existsByMaterial(material)
fun `existsByMaterial() returns false when no mix material with the given material exists`() {
whenever(repository.existsByMaterial(material)).doReturn(false)
assertFalse(found)
}
val found = service.existsByMaterial(material)
assertFalse(found)
}
}

View File

@ -21,70 +21,68 @@ class MixServiceTest : AbstractExternalModelServiceTest<Mix, MixSaveDto, MixUpda
override val entitySaveDto: MixSaveDto = spy(mixSaveDto(mixMaterials = mapOf(1L to 1000f)))
override val entityUpdateDto: MixUpdateDto = spy(mixUpdateDto(id = entity.id!!))
@Nested
inner class GetAllByMixType {
@Test
fun `returns all mixes with the given mix type`() {
val mixType = mixType(id = 0L)
// getAllByMixType()
whenever(repository.findAllByMixType(mixType)).doReturn(entityList)
@Test
fun `getAllByMixType() returns all mixes with the given mix type`() {
val mixType = mixType(id = 0L)
val found = service.getAllByMixType(mixType)
whenever(repository.findAllByMixType(mixType)).doReturn(entityList)
assertEquals(entityList, found)
}
val found = service.getAllByMixType(mixType)
assertEquals(entityList, found)
}
@Nested
inner class SaveDto {
@Test
fun `calls and returns save() with the created entity`() {
val recipe = recipe(id = entitySaveDto.recipeId)
val materialType = materialType(id = entitySaveDto.materialTypeId)
val material = material(
name = entitySaveDto.name,
inventoryQuantity = Float.MIN_VALUE,
isMixType = true,
materialType = materialType
)
val mixType = mixType(name = entitySaveDto.name, material = material)
val mix = mix(recipe = recipe, mixType = mixType)
val mixWithId = mix(id = 0L, recipe = recipe, mixType = mixType)
val mixMaterials = listOf(mixMaterial(mix = mixWithId, material = material(id = 1L), quantity = 1000f))
val mixWithMaterials = mix.apply { this.mixMaterials.addAll(mixMaterials) }
// save()
whenever(recipeService.getById(recipe.id!!)).doReturn(recipe)
whenever(materialTypeService.getById(materialType.id!!)).doReturn(materialType)
whenever(mixMaterialService.createFromMap(mixWithId, entitySaveDto.mixMaterials!!)).doReturn(mixMaterials)
whenever(mixTypeService.createForNameAndMaterialType(mixType.name, mixType.material.materialType!!)).doReturn(mixType)
doReturn(true).whenever(service).existsById(mixWithId.id!!)
doReturn(mixWithId).whenever(service).save(mix)
doReturn(mixWithMaterials).whenever(service).update(mixWithMaterials)
override fun `save(dto) calls and returns save() with the created entity`() {
val recipe = recipe(id = entitySaveDto.recipeId)
val materialType = materialType(id = entitySaveDto.materialTypeId)
val material = material(
name = entitySaveDto.name,
inventoryQuantity = Float.MIN_VALUE,
isMixType = true,
materialType = materialType
)
val mixType = mixType(name = entitySaveDto.name, material = material)
val mix = mix(recipe = recipe, mixType = mixType)
val mixWithId = mix(id = 0L, recipe = recipe, mixType = mixType)
val mixMaterials = listOf(mixMaterial(mix = mixWithId, material = material(id = 1L), quantity = 1000f))
val mixWithMaterials = mix.apply { this.mixMaterials.addAll(mixMaterials) }
val found = service.save(entitySaveDto)
whenever(recipeService.getById(recipe.id!!)).doReturn(recipe)
whenever(materialTypeService.getById(materialType.id!!)).doReturn(materialType)
whenever(mixMaterialService.createFromMap(mixWithId, entitySaveDto.mixMaterials!!)).doReturn(mixMaterials)
whenever(mixTypeService.createForNameAndMaterialType(mixType.name, mixType.material.materialType!!)).doReturn(
mixType
)
doReturn(true).whenever(service).existsById(mixWithId.id!!)
doReturn(mixWithId).whenever(service).save(mix)
doReturn(mixWithMaterials).whenever(service).update(mixWithMaterials)
verify(service).save(mix)
verify(service).update(mixWithMaterials)
verify(recipeService).addMix(recipe, mix)
val found = service.save(entitySaveDto)
// Verify if this method is called instead of the MixType's constructor, which does not check if the name is already taken by a material.
verify(mixTypeService).createForNameAndMaterialType(mixType.name, mixType.material.materialType!!)
verify(service).save(mix)
verify(service).update(mixWithMaterials)
verify(recipeService).addMix(recipe, mix)
assertEquals(mixWithMaterials, found)
}
// Verify if this method is called instead of the MixType's constructor, which does not check if the name is already taken by a material.
verify(mixTypeService).createForNameAndMaterialType(mixType.name, mixType.material.materialType!!)
assertEquals(mixWithMaterials, found)
}
@Nested
inner class UpdateLocation {
@Test
fun `calls update() with the given mix with the given location`() {
val newLocation = "new location"
val expected = entity.apply { location = newLocation }
doReturn(expected).whenever(service).update(expected)
// updateLocation()
service.updateLocation(entity, newLocation)
@Test
fun `updateLocation() calls update() with the given mix with the given location`() {
val newLocation = "new location"
val expected = entity.apply { location = newLocation }
doReturn(expected).whenever(service).update(expected)
verify(service).update(expected)
}
service.updateLocation(entity, newLocation)
verify(service).update(expected)
}
}

View File

@ -29,76 +29,72 @@ class MixTypeServiceTest : AbstractNamedModelServiceTest<MixType, MixTypeService
super.afterEach()
}
@Nested
inner class GetByMaterial {
@Test
fun `returns the mix type with the given material`() {
whenever(repository.findByMaterial(material)).doReturn(entity)
// getByMaterial()
val found = service.getByMaterial(material)
@Test
fun `getByMaterial() returns the mix type with the given material`() {
whenever(repository.findByMaterial(material)).doReturn(entity)
assertEquals(entity, found)
}
val found = service.getByMaterial(material)
@Test
fun `throws EntityNotFoundRestException when no mix type with the given material exists`() {
whenever(repository.findByMaterial(material)).doReturn(null)
val exception = assertThrows<EntityNotFoundRestException> { service.getByMaterial(material) }
assertEquals(material.name, exception.value)
}
assertEquals(entity, found)
}
@Nested
inner class SaveMixType {
@Test
fun `throws EntityAlreadyExistsRestException when a material with the name of the new mix type exists`() {
whenever(materialService.existsByName(entity.name)).doReturn(true)
@Test
fun `getByMaterial() throws EntityNotFoundRestException when no mix type with the given material exists`() {
whenever(repository.findByMaterial(material)).doReturn(null)
val exception = assertThrows<EntityAlreadyExistsRestException> { service.save(entity) }
assertEquals(entity.name, exception.value)
}
val exception = assertThrows<EntityNotFoundRestException> { service.getByMaterial(material) }
assertEquals(material.name, exception.value)
}
@Nested
inner class CreateForNameAndMaterialType {
@Test
fun `creates a save a valid mix type with the given name and material type`() {
val name = entity.name
val materialType = materialType()
// save()
doAnswer { it.arguments[0] }.whenever(service).save(any())
@Test
fun `save() throws EntityAlreadyExistsRestException when a material with the name of the new mix type exists`() {
whenever(materialService.existsByName(entity.name)).doReturn(true)
val found = service.createForNameAndMaterialType(name, materialType)
verify(service).save(any())
assertEquals(name, found.name)
assertEquals(name, found.material.name)
assertEquals(materialType, found.material.materialType)
assertTrue(found.material.isMixType)
}
val exception = assertThrows<EntityAlreadyExistsRestException> { service.save(entity) }
assertEquals(entity.name, exception.value)
}
@Nested
inner class UpdateForNameAndMaterialType {
@Test
fun `updates the given mix type with the given name and material type`() {
val mixType = mixType(id = 1L, material = material(isMixType = true))
val name = entity.name
val materialType = materialType()
// createForNameAndMaterialType()
doAnswer { it.arguments[0] }.whenever(service).update(any())
@Test
fun `createForNameAndMaterialType() creates a save a valid mix type with the given name and material type`() {
val name = entity.name
val materialType = materialType()
val found = service.updateForNameAndMaterialType(mixType, name, materialType)
doAnswer { it.arguments[0] }.whenever(service).save(any())
verify(service).update(any())
val found = service.createForNameAndMaterialType(name, materialType)
assertEquals(mixType.id, found.id)
assertEquals(name, found.name)
assertEquals(name, found.material.name)
assertEquals(materialType, found.material.materialType)
assertTrue(found.material.isMixType)
}
verify(service).save(any())
assertEquals(name, found.name)
assertEquals(name, found.material.name)
assertEquals(materialType, found.material.materialType)
assertTrue(found.material.isMixType)
}
// updateForNameAndMaterialType()
@Test
fun `updateForNameAndMaterialType() updates the given mix type with the given name and material type`() {
val mixType = mixType(id = 1L, material = material(isMixType = true))
val name = entity.name
val materialType = materialType()
doAnswer { it.arguments[0] }.whenever(service).update(any())
val found = service.updateForNameAndMaterialType(mixType, name, materialType)
verify(service).update(any())
assertEquals(mixType.id, found.id)
assertEquals(name, found.name)
assertEquals(name, found.material.name)
assertEquals(materialType, found.material.materialType)
assertTrue(found.material.isMixType)
}
}

View File

@ -29,113 +29,107 @@ class RecipeServiceTest :
override val entitySaveDto: RecipeSaveDto = spy(recipeSaveDto(name = entity.name, companyId = entity.company.id!!))
override val entityUpdateDto: RecipeUpdateDto = spy(recipeUpdateDto(id = entity.id!!, name = entity.name))
@Nested
inner class ExistsByCompany {
@Test
fun `returns true when at least one recipe exists for the given company`() {
whenever(repository.existsByCompany(company)).doReturn(true)
// existsByCompany()
val found = service.existsByCompany(company)
@Test
fun `existsByCompany() returns true when at least one recipe exists for the given company`() {
whenever(repository.existsByCompany(company)).doReturn(true)
assertTrue(found)
}
val found = service.existsByCompany(company)
@Test
fun `returns false when no recipe exists for the given company`() {
whenever(repository.existsByCompany(company)).doReturn(false)
val found = service.existsByCompany(company)
assertFalse(found)
}
assertTrue(found)
}
@Nested
inner class GetAllByCompany {
@Test
fun `returns the recipes with the given company`() {
val companies = listOf(entity, anotherEntity)
whenever(repository.findAllByCompany(company)).doReturn(companies)
@Test
fun `existsByCompany() returns false when no recipe exists for the given company`() {
whenever(repository.existsByCompany(company)).doReturn(false)
val found = service.getAllByCompany(company)
val found = service.existsByCompany(company)
assertEquals(companies, found)
}
assertFalse(found)
}
@Nested
inner class SaveDto {
@Test
fun `calls and returns save() with the created entity`() {
whenever(companyService.getById(company.id!!)).doReturn(company)
saveDtoTest(entity, entitySaveDto, service)
}
// getAllByCompany()
@Test
fun `getAllByCompany() returns the recipes with the given company`() {
val companies = listOf(entity, anotherEntity)
whenever(repository.findAllByCompany(company)).doReturn(companies)
val found = service.getAllByCompany(company)
assertEquals(companies, found)
}
@Nested
inner class UpdatePublicDate {
@Test
fun `calls update with the updated note`() {
val publicDataDto = recipePublicDataDto(id = entity.id!!, note = "newNote", mixesLocation = null)
val expected = entity.apply { note = publicDataDto.note!! }
doReturn(entity).whenever(service).getById(entity.id!!)
doReturn(expected).whenever(service).update(expected)
// save()
service.updatePublicData(publicDataDto)
verify(service).update(expected)
}
@Test
fun `calls mixService_updateLocation() with every mix and locations`() {
val mix = mix(id = 0L)
val mixRecipe = entity.apply { mixes.add(mix) }
val location = "location"
val mixLocation = mapOf(mix.id!! to location)
val publicDataDto = recipePublicDataDto(id = mixRecipe.id!!, note = null, mixesLocation = mixLocation)
doReturn(mixRecipe).whenever(service).getById(mixRecipe.id!!)
service.updatePublicData(publicDataDto)
verify(mixService).updateLocation(mix, location)
}
@Test
override fun `save(dto) calls and returns save() with the created entity`() {
whenever(companyService.getById(company.id!!)).doReturn(company)
saveDtoTest(entity, entitySaveDto, service)
}
@Nested
inner class AddMix {
@Test
fun `adds the given mix to the given recipe and updates it`() {
val mix = mix(id = 0L)
val recipe = recipe(id = 0L, mixes = mutableListOf())
// updatePublicData()
doAnswer { it.arguments[0] }.whenever(service).update(any<Recipe>())
@Test
fun `updatePublicData() calls update with the updated note`() {
val publicDataDto = recipePublicDataDto(id = entity.id!!, note = "newNote", mixesLocation = null)
val expected = entity.apply { note = publicDataDto.note!! }
doReturn(entity).whenever(service).getById(entity.id!!)
doReturn(expected).whenever(service).update(expected)
val found = service.addMix(recipe, mix)
service.updatePublicData(publicDataDto)
verify(service).update(any<Recipe>())
assertEquals(recipe.id, found.id)
assertTrue(found.mixes.contains(mix))
}
verify(service).update(expected)
}
@Nested
inner class RemoveMix {
@Test
fun `removes the given mix from its recipe and updates it`() {
val recipe = recipe(id = 0L, mixes = mutableListOf())
val mix = mix(id = 0L, recipe = recipe)
recipe.mixes.add(mix)
@Test
fun `updatePublicData() calls mixService_updateLocation() with every mix and locations`() {
val mix = mix(id = 0L)
val mixRecipe = entity.apply { mixes.add(mix) }
val location = "location"
val mixLocation = mapOf(mix.id!! to location)
val publicDataDto = recipePublicDataDto(id = mixRecipe.id!!, note = null, mixesLocation = mixLocation)
doReturn(mixRecipe).whenever(service).getById(mixRecipe.id!!)
doAnswer { it.arguments[0] }.whenever(service).update(any<Recipe>())
service.updatePublicData(publicDataDto)
val found = service.removeMix(mix)
verify(mixService).updateLocation(mix, location)
}
verify(service).update(any<Recipe>())
// addMix()
assertEquals(recipe.id, found.id)
assertFalse(found.mixes.contains(mix))
}
@Test
fun `addMix() adds the given mix to the given recipe and updates it`() {
val mix = mix(id = 0L)
val recipe = recipe(id = 0L, mixes = mutableListOf())
doAnswer { it.arguments[0] }.whenever(service).update(any<Recipe>())
val found = service.addMix(recipe, mix)
verify(service).update(any<Recipe>())
assertEquals(recipe.id, found.id)
assertTrue(found.mixes.contains(mix))
}
// removeMix()
@Test
fun `removeMix() removes the given mix from its recipe and updates it`() {
val recipe = recipe(id = 0L, mixes = mutableListOf())
val mix = mix(id = 0L, recipe = recipe)
recipe.mixes.add(mix)
doAnswer { it.arguments[0] }.whenever(service).update(any<Recipe>())
val found = service.removeMix(mix)
verify(service).update(any<Recipe>())
assertEquals(recipe.id, found.id)
assertFalse(found.mixes.contains(mix))
}
}
@ -158,121 +152,116 @@ class RecipeImageServiceTest {
reset(recipeService, fileService, service, recipeDirectory)
}
@Nested
inner class GetByIdForRecipe {
@Test
fun `returns data for the given recipe and image id red by the file service`() {
whenever(fileService.getPath(imagePath)).doReturn(imagePath)
whenever(fileService.readAsBytes(imagePath)).doReturn(imageData)
// getByIdForRecipe()
val found = service.getByIdForRecipe(imageId, recipeId)
@Test
fun `getByIdForRecipe() returns data for the given recipe and image id red by the file service`() {
whenever(fileService.getPath(imagePath)).doReturn(imagePath)
whenever(fileService.readAsBytes(imagePath)).doReturn(imageData)
assertEquals(imageData, found)
}
val found = service.getByIdForRecipe(imageId, recipeId)
@Test
fun `throws EntityNotFoundRestException when no image with the given recipe and image id exists`() {
doReturn(imagePath).whenever(service).getPath(imageId, recipeId)
whenever(fileService.readAsBytes(imagePath)).doThrow(NoSuchFileException(imagePath))
val exception =
assertThrows<EntityNotFoundRestException> { service.getByIdForRecipe(imageId, recipeId) }
assertEquals("$recipeId/$imageId", exception.value)
}
assertEquals(imageData, found)
}
@Nested
inner class GetAllIdsForRecipe {
@Test
fun `returns a list containing all image's identifier of the images of the given recipe`() {
val expectedFiles = imagesIds.map { File(it.toString()) }.toTypedArray()
@Test
fun `getByIdForRecipe() throws EntityNotFoundRestException when no image with the given recipe and image id exists`() {
doReturn(imagePath).whenever(service).getPath(imageId, recipeId)
whenever(fileService.readAsBytes(imagePath)).doThrow(NoSuchFileException(imagePath))
whenever(recipeService.getById(recipeId)).doReturn(recipe)
whenever(recipeDirectory.exists()).doReturn(true)
whenever(recipeDirectory.isDirectory).doReturn(true)
whenever(recipeDirectory.listFiles()).doReturn(expectedFiles)
doReturn(recipeDirectory).whenever(service).getRecipeDirectory(recipeId)
val found = service.getAllIdsForRecipe(recipeId)
assertEquals(imagesIds, found)
}
@Test
fun `returns an empty list when the given recipe's directory does not exists`() {
whenever(recipeService.getById(recipeId)).doReturn(recipe)
whenever(recipeDirectory.exists()).doReturn(false)
whenever(recipeDirectory.isDirectory).doReturn(true)
doReturn(recipeDirectory).whenever(service).getRecipeDirectory(recipeId)
val found = service.getAllIdsForRecipe(recipeId)
assertTrue(found.isEmpty())
}
@Test
fun `returns an empty list when the given recipe's directory is not a directory`() {
whenever(recipeService.getById(recipeId)).doReturn(recipe)
whenever(recipeDirectory.exists()).doReturn(true)
whenever(recipeDirectory.isDirectory).doReturn(false)
doReturn(recipeDirectory).whenever(service).getRecipeDirectory(recipeId)
val found = service.getAllIdsForRecipe(recipeId)
assertTrue(found.isEmpty())
}
val exception =
assertThrows<EntityNotFoundRestException> { service.getByIdForRecipe(imageId, recipeId) }
assertEquals("$recipeId/$imageId", exception.value)
}
@Nested
inner class Save {
@Test
fun `writes the given image to the file service with the expected path`() {
val expectedNextAvailableId = imagesIds.maxOrNull()!! + 1
val imagePath = "$RECIPE_IMAGES_DIRECTORY/$recipeId/$expectedNextAvailableId"
doReturn(imagesIds).whenever(service).getAllIdsForRecipe(recipeId)
doReturn(imagePath).whenever(service).getPath(expectedNextAvailableId, recipeId)
// getAllIdsForRecipe()
service.save(image, recipeId)
@Test
fun `getAllIdsForRecipe() returns a list containing all image's identifier of the images of the given recipe`() {
val expectedFiles = imagesIds.map { File(it.toString()) }.toTypedArray()
verify(fileService).write(image, imagePath)
}
whenever(recipeService.getById(recipeId)).doReturn(recipe)
whenever(recipeDirectory.exists()).doReturn(true)
whenever(recipeDirectory.isDirectory).doReturn(true)
whenever(recipeDirectory.listFiles()).doReturn(expectedFiles)
doReturn(recipeDirectory).whenever(service).getRecipeDirectory(recipeId)
val found = service.getAllIdsForRecipe(recipeId)
assertEquals(imagesIds, found)
}
@Nested
inner class Delete {
@Test
fun `deletes the image with the given recipe and image id from the file service`() {
doReturn(imagePath).whenever(service).getPath(imageId, recipeId)
@Test
fun `getAllIdsForRecipe() returns an empty list when the given recipe's directory does not exists`() {
whenever(recipeService.getById(recipeId)).doReturn(recipe)
whenever(recipeDirectory.exists()).doReturn(false)
whenever(recipeDirectory.isDirectory).doReturn(true)
doReturn(recipeDirectory).whenever(service).getRecipeDirectory(recipeId)
service.delete(imageId, recipeId)
val found = service.getAllIdsForRecipe(recipeId)
verify(fileService).delete(imagePath)
}
assertTrue(found.isEmpty())
}
@Nested
inner class GetRecipeDirectory {
@Test
fun `returns a file with the expected path`() {
val recipeDirectoryPath = "$RECIPE_IMAGES_DIRECTORY/$recipeId"
whenever(fileService.getPath(recipeDirectoryPath)).doReturn(recipeDirectoryPath)
@Test
fun `getAllIdsForRecipe() returns an empty list when the given recipe's directory is not a directory`() {
whenever(recipeService.getById(recipeId)).doReturn(recipe)
whenever(recipeDirectory.exists()).doReturn(true)
whenever(recipeDirectory.isDirectory).doReturn(false)
doReturn(recipeDirectory).whenever(service).getRecipeDirectory(recipeId)
val found = service.getRecipeDirectory(recipeId)
val found = service.getAllIdsForRecipe(recipeId)
assertEquals(recipeDirectoryPath, found.path)
}
assertTrue(found.isEmpty())
}
@Nested
inner class GetPath {
@Test
fun `returns the expected path`() {
whenever(fileService.getPath(any())).doAnswer { it.arguments[0] as String }
// save()
val found = service.getPath(imageId, recipeId)
@Test
fun `save() writes the given image to the file service with the expected path`() {
val expectedNextAvailableId = imagesIds.maxOrNull()!! + 1
val imagePath = "$RECIPE_IMAGES_DIRECTORY/$recipeId/$expectedNextAvailableId"
assertEquals(imagePath, found)
}
doReturn(imagesIds).whenever(service).getAllIdsForRecipe(recipeId)
doReturn(imagePath).whenever(service).getPath(expectedNextAvailableId, recipeId)
service.save(image, recipeId)
verify(fileService).write(image, imagePath)
}
// delete()
@Test
fun `delete() deletes the image with the given recipe and image id from the file service`() {
doReturn(imagePath).whenever(service).getPath(imageId, recipeId)
service.delete(imageId, recipeId)
verify(fileService).delete(imagePath)
}
// getRecipeDirectory()
@Test
fun `getRecipeDirectory() returns a file with the expected path`() {
val recipeDirectoryPath = "$RECIPE_IMAGES_DIRECTORY/$recipeId"
whenever(fileService.getPath(recipeDirectoryPath)).doReturn(recipeDirectoryPath)
val found = service.getRecipeDirectory(recipeId)
assertEquals(recipeDirectoryPath, found.path)
}
// getPath()
@Test
fun `getPath() returns the expected path`() {
whenever(fileService.getPath(any())).doAnswer { it.arguments[0] as String }
val found = service.getPath(imageId, recipeId)
assertEquals(imagePath, found)
}
}

View File

@ -5,6 +5,7 @@ import com.nhaarman.mockitokotlin2.spy
import dev.fyloz.trial.colorrecipesexplorer.model.*
import dev.fyloz.trial.colorrecipesexplorer.repository.RecipeStepRepository
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class RecipeStepServiceTest :
@ -15,29 +16,29 @@ class RecipeStepServiceTest :
override val entity: RecipeStep = recipeStep(id = 0L, recipe = recipe(id = 0L), message = "message")
override val anotherEntity: RecipeStep = recipeStep(id = 1L, recipe = recipe(id = 1L), message = "another message")
@Nested
inner class CreateForRecipe {
fun `returns a correct RecipeStep`() {
val step = recipeStep(null, entity.recipe, entity.message)
// createForRecipe()
val found = service.createForRecipe(entity.recipe!!, entity.message)
@Test
fun `createForRecipe() returns a correct RecipeStep`() {
val step = recipeStep(null, entity.recipe, entity.message)
assertEquals(step, found)
}
val found = service.createForRecipe(entity.recipe!!, entity.message)
assertEquals(step, found)
}
@Nested
inner class CreateAllForRecipe {
fun `returns all correct RecipeSteps`() {
val steps = listOf(
recipeStep(null, entity.recipe, entity.message),
recipeStep(null, entity.recipe, anotherEntity.message)
)
val messages = steps.map { it.message }
// createAllForRecipe()
val found = service.createAllForRecipe(entity.recipe!!, messages)
@Test
fun `createAllForRecipe() returns all correct RecipeSteps`() {
val steps = listOf(
recipeStep(null, entity.recipe, entity.message),
recipeStep(null, entity.recipe, anotherEntity.message)
)
val messages = steps.map { it.message }
assertEquals(steps, found)
}
val found = service.createAllForRecipe(entity.recipe!!, messages)
assertEquals(steps, found)
}
}