From 6ea65f6519cff7c16e979f56bea29d1fe80a4061 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Fri, 19 Mar 2021 22:29:00 -0400 Subject: [PATCH] =?UTF-8?q?Les=20groupes=20n'ont=20plus=20de=20liste=20d'e?= =?UTF-8?q?mploy=C3=A9s=20car=20la=20r=C3=A9cup=C3=A9ration=20d'un=20group?= =?UTF-8?q?e=20depuis=20la=20base=20de=20donn=C3=A9e=20causait=20une=20bou?= =?UTF-8?q?cle=20infinie.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/AccountModel.kt | 13 +- .../rest/AccountControllers.kt | 18 +-- .../service/AccountService.kt | 50 +------- .../service/AccountsServiceTest.kt | 119 +----------------- 4 files changed, 17 insertions(+), 183 deletions(-) diff --git a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/model/AccountModel.kt b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/model/AccountModel.kt index b83dce7..b4d56f8 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/model/AccountModel.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/model/AccountModel.kt @@ -15,6 +15,7 @@ import javax.persistence.* import javax.validation.constraints.NotBlank import javax.validation.constraints.NotNull import javax.validation.constraints.Size +import kotlin.jvm.Transient private const val EMPLOYEE_ID_NULL_MESSAGE = "Un numéro d'employé est requis" @@ -136,14 +137,7 @@ data class EmployeeGroup( @Column(name = "permission") @Fetch(FetchMode.SUBSELECT) val permissions: MutableSet = mutableSetOf(), - - @OneToMany(mappedBy = "group") - @field:JsonIgnore - val employees: MutableSet = mutableSetOf() -) : NamedModel { - @JsonProperty("employeeCount") - fun getEmployeeCount() = employees.size - 1 // -1 removes the default employee -} +) : NamedModel open class EmployeeGroupSaveDto( @field:NotBlank(message = GROUP_NAME_NULL_MESSAGE) @@ -327,9 +321,8 @@ fun employeeGroup( id: Long? = null, name: String = "name", permissions: MutableSet = mutableSetOf(), - employees: MutableSet = mutableSetOf(), op: EmployeeGroup.() -> Unit = {} -) = EmployeeGroup(id, name, permissions, employees).apply(op) +) = EmployeeGroup(id, name, permissions).apply(op) fun employeeGroupSaveDto( name: String = "name", diff --git a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/rest/AccountControllers.kt b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/rest/AccountControllers.kt index a405685..46d73b8 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/rest/AccountControllers.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/rest/AccountControllers.kt @@ -107,18 +107,20 @@ class GroupsController(groupService: EmployeeGroupServiceImpl) : @PutMapping("{groupId}/{employeeId}") @ResponseStatus(HttpStatus.NO_CONTENT) fun addEmployeeToGroup(@PathVariable groupId: Long, @PathVariable employeeId: Long): ResponseEntity { - service.addEmployeeToGroup(groupId, employeeId) - return ResponseEntity - .noContent() - .build() +// service.addEmployeeToGroup(groupId, employeeId) +// return ResponseEntity +// .noContent() +// .build() + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build() // TODO Go to employee controller } @DeleteMapping("{groupId}/{employeeId}") @ResponseStatus(HttpStatus.NO_CONTENT) fun removeEmployeeFromGroup(@PathVariable groupId: Long, @PathVariable employeeId: Long): ResponseEntity { - service.removeEmployeeFromGroup(groupId, employeeId) - return ResponseEntity - .noContent() - .build() +// service.removeEmployeeFromGroup(groupId, employeeId) +// return ResponseEntity +// .noContent() +// .build() + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build() // TODO Go to employee controller } } 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 7b6c130..ef888c7 100644 --- a/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountService.kt +++ b/src/main/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountService.kt @@ -66,23 +66,6 @@ interface EmployeeGroupService : /** Sets the default group cookie for the given HTTP [response]. */ fun setResponseDefaultGroup(groupId: Long, response: HttpServletResponse) - - /** Adds the employee with the given [employeeId] to the group with the given [groupId]. */ - fun addEmployeeToGroup(groupId: Long, employeeId: Long) - - /** - * Adds a given [employee] to a given [group]. - * - * If the [employee] is already in the [group], nothing will be done. - * If the [employee] is already in a group, it will be removed from it. - */ - fun addEmployeeToGroup(group: EmployeeGroup, employee: Employee) - - /** Removes the employee with the given [employeeId] from the group with the given [groupId]. */ - fun removeEmployeeFromGroup(groupId: Long, employeeId: Long) - - /** Removes a given [employee] from the given [group]. */ - fun removeEmployeeFromGroup(group: EmployeeGroup, employee: Employee) } interface EmployeeUserDetailsService : UserDetailsService { @@ -240,7 +223,7 @@ class EmployeeGroupServiceImpl( EmployeeGroupService { override fun existsByName(name: String): Boolean = repository.existsByName(name) override fun getEmployeesForGroup(id: Long): Collection = - getById(id).employees + employeeService.getByGroup(getById(id)) @Transactional override fun save(entity: EmployeeGroup): EmployeeGroup { @@ -255,8 +238,7 @@ class EmployeeGroupServiceImpl( EmployeeGroup( entity.id, if (name.isNotBlank()) entity.name else persistedGroup.name, - if (permissions.isNotEmpty()) entity.permissions else persistedGroup.permissions, - persistedGroup.employees + if (permissions.isNotEmpty()) entity.permissions else persistedGroup.permissions ) }) } @@ -286,34 +268,6 @@ class EmployeeGroupServiceImpl( "$defaultGroupCookieName=${defaultGroupUser.id}; Max-Age=${defaultGroupCookieMaxAge}; Path=/api; HttpOnly; Secure; SameSite=strict" ) } - - override fun addEmployeeToGroup(groupId: Long, employeeId: Long) { - addEmployeeToGroup(getById(groupId), employeeService.getById(employeeId)) - } - - @Transactional - override fun addEmployeeToGroup(group: EmployeeGroup, employee: Employee) { - if (employee.group == group) return - if (employee.group != null) removeEmployeeFromGroup(employee.group!!, employee) - - group.employees.add(employee) - employee.group = group - update(group) - employeeService.update(employee) - } - - override fun removeEmployeeFromGroup(groupId: Long, employeeId: Long) = - removeEmployeeFromGroup(getById(groupId), employeeService.getById(employeeId)) - - @Transactional - override fun removeEmployeeFromGroup(group: EmployeeGroup, employee: Employee) { - if (employee.group == null || employee.group != group) return - - group.employees.removeIf { it.id == employee.id } - employee.group = null - update(group) - employeeService.update(employee) - } } @Service 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 69cd19a..6fc7ece 100644 --- a/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountsServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/trial/colorrecipesexplorer/service/AccountsServiceTest.kt @@ -222,9 +222,10 @@ class EmployeeGroupServiceTest : @Test fun `getEmployeesForGroup() returns all employees in the given group`() { - val group = employeeGroup(id = 1L, employees = mutableSetOf(groupEmployee)) + val group = employeeGroup(id = 1L) doReturn(group).whenever(service).getById(group.id!!) + whenever(employeeService.getByGroup(group)).doReturn(listOf(groupEmployee)) val found = service.getEmployeesForGroup(group.id!!) @@ -286,122 +287,6 @@ class EmployeeGroupServiceTest : assertTrue(found.secure) } - // addEmployeeToGroup() - - @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) - - service.addEmployeeToGroup(entity.id!!, groupEmployeeId) - - verify(service).addEmployeeToGroup(entity, groupEmployee) - } - - @Test - fun `addEmployeeToGroup() calls update() and employeeService_update() with the updated entities`() { - val group = employeeGroup() - val employee = employee() - - 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.any { it.id == employee.id }) - assertEquals(group, employee.group) - } - - @Test - fun `addEmployeeToGroup() 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 `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) - - 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.any { it.id == employee.id }) - 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(any())).doReturn(employee) - doReturn(group).whenever(service).update(group) - - service.removeEmployeeFromGroup(group, employee) - - verify(service).update(group) - verify(employeeService).update(argThat { this.group == null }) - - 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) - } - // save() @Test