From 04e18d93bede5677f06b3f6d15ec3215d79e8a73 Mon Sep 17 00:00:00 2001 From: William Nolin Date: Mon, 8 Mar 2021 09:43:12 -0500 Subject: [PATCH] Master --- .../service/AccountService.kt | 13 +- .../service/AbstractServiceTest.kt | 407 ++++++------ .../service/AccountsServiceTest.kt | 581 +++++++++--------- .../service/CompanyServiceTest.kt | 26 +- .../service/MaterialServiceTest.kt | 226 ++++--- .../service/MaterialTypeServiceTest.kt | 230 ++++--- .../service/MixMaterialServiceTest.kt | 23 +- .../service/MixServiceTest.kt | 100 ++- .../service/MixTypeServiceTest.kt | 106 ++-- .../service/RecipeServiceTest.kt | 343 +++++------ .../service/RecipeStepServiceTest.kt | 37 +- 11 files changed, 1021 insertions(+), 1071 deletions(-) diff --git a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountService.kt b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountService.kt index 366bc19..8200393 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountService.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountService.kt @@ -58,10 +58,7 @@ interface EmployeeService : ExternalModelService { - /** Checks if a group with the given [name] exists. */ - fun existsByName(name: String): Boolean - + ExternalNamedModelService { /** Gets all the employees of the group with the given [id]. */ fun getEmployeesForGroup(id: Long): Collection @@ -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( + AbstractExternalNamedModelService( employeeGroupRepository ), EmployeeGroupService { @@ -248,7 +245,7 @@ class EmployeeGroupServiceImpl( @Transactional override fun save(entity: EmployeeGroup): EmployeeGroup { - return super.save(entity).apply { + return super.save(entity).apply { employeeService.saveDefaultGroupEmployee(this) } } diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AbstractServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AbstractServiceTest.kt index f1c4e10..097aaec 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AbstractServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AbstractServiceTest.kt @@ -38,155 +38,146 @@ abstract class AbstractServiceTest, R : JpaRepository 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, R : JpaRepository> : AbstractServiceTest() { - @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 { 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 { 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 { service.update(entity) } - assertTrue(exception.value is Long) - assertEquals(entity.id, exception.value as Long) - } + val exception = assertThrows { 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 { 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 { 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() { 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 { 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 { 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 { 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 { service.update(entity) } - assertEquals(entity.name, exception.value) - } + val exception = assertThrows { 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 { 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 { 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 { 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, 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, U : EntityDto, S : ExternalNamedModelService, R : NamedJpaRepository> : @@ -329,17 +316,17 @@ abstract class AbstractExternalNamedModelServiceTest> saveDtoTest(entity: E, entitySaveDto: N, service: ExternalService) { diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountsServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountsServiceTest.kt index 1fc7d60..da872a2 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountsServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountsServiceTest.kt @@ -19,7 +19,8 @@ import javax.servlet.http.Cookie import javax.servlet.http.HttpServletRequest import kotlin.test.* -class EmployeeServiceTest : AbstractExternalModelServiceTest() { +class EmployeeServiceTest : + AbstractExternalModelServiceTest() { private val passwordEncoder = BCryptPasswordEncoder() override val entity: Employee = employee(passwordEncoder, id = 0L) @@ -33,7 +34,16 @@ class EmployeeServiceTest : AbstractExternalModelServiceTest { 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 { 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 { + 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 { + 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 { 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 { 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 { 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 { + service.update( + entity, + true, + ignoreSystemUsers = true + ) } + assertTrue(exception.value is String) + assertEquals("${entity.firstName} ${entity.lastName}", exception.value as String) } } -class EmployeeGroupServiceTest : AbstractExternalModelServiceTest() { +class EmployeeGroupServiceTest : + AbstractExternalNamedModelServiceTest() { 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 = 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 = 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 { 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 { 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()) + // 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()) + + 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 { 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 { 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) } } diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/CompanyServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/CompanyServiceTest.kt index bd2a7d1..450336a 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/CompanyServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/CompanyServiceTest.kt @@ -9,7 +9,8 @@ import org.junit.jupiter.api.Nested import kotlin.test.assertFalse import kotlin.test.assertTrue -class CompanyServiceTest : AbstractExternalNamedModelServiceTest() { +class CompanyServiceTest : + AbstractExternalNamedModelServiceTest() { 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 { 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 { service.update(material) } - assertEquals(material.name, exception.value) - } + val exception = assertThrows { 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.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 { 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.contains(material: Material): Boolean = + any { it.id == material.id } } diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialTypeServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialTypeServiceTest.kt index 5eadefa..04e85a3 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialTypeServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MaterialTypeServiceTest.kt @@ -13,7 +13,8 @@ import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue -class MaterialTypeServiceTest : AbstractExternalNamedModelServiceTest() { +class MaterialTypeServiceTest : + AbstractExternalNamedModelServiceTest() { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { service.delete(entity) } } } diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixMaterialServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixMaterialServiceTest.kt index 1ccac5d..698a0b5 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixMaterialServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/MixMaterialServiceTest.kt @@ -22,22 +22,21 @@ class MixMaterialServiceTest : AbstractModelServiceTest { 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 { service.save(entity) } - assertEquals(entity.name, exception.value) - } + val exception = assertThrows { 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 { 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) } } diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeServiceTest.kt index d54f982..6886983 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeServiceTest.kt @@ -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()) + @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()) - - 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()) + service.updatePublicData(publicDataDto) - val found = service.removeMix(mix) + verify(mixService).updateLocation(mix, location) + } - verify(service).update(any()) + // 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()) + + val found = service.addMix(recipe, mix) + + verify(service).update(any()) + + 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()) + + val found = service.removeMix(mix) + + verify(service).update(any()) + + 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 { 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 { 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) } } diff --git a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeStepServiceTest.kt b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeStepServiceTest.kt index beb6bed..4109030 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeStepServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/RecipeStepServiceTest.kt @@ -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) } }