From b44da57f394858b28f7f4bd47276c3620568bcdf Mon Sep 17 00:00:00 2001 From: aalicic Date: Tue, 17 May 2022 10:46:04 +0200 Subject: [PATCH] VIC-660: CRUD Agencies --- services/useradminservice.yaml | 23 +++++++++++++++++++ .../api/admin/service/UserAdminService.java | 10 +++++++- .../admin/service/UserAdminServiceTest.java | 22 ++++++++++++++---- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/services/useradminservice.yaml b/services/useradminservice.yaml index 67842d86..164442f0 100644 --- a/services/useradminservice.yaml +++ b/services/useradminservice.yaml @@ -85,6 +85,11 @@ paths: required: true schema: type: integer + - name: sort + in: query + description: The sort parameter containing field and order the response should be sorted + schema: + $ref: '#/components/schemas/Sort' responses: 200: description: OK - successfull operation @@ -668,6 +673,24 @@ components: absent: type: boolean + Sort: + type: object + properties: + field: + type: string + example: 'firstName|lastName|username|email' + enum: + - 'firstName' + - 'lastName' + - 'username' + - 'email' + order: + type: string + example: 'ASC|DESC' + enum: + - 'ASC' + - 'DESC' + ConsultantAgencyAdminResultDTO: type: object properties: diff --git a/src/main/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminService.java b/src/main/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminService.java index 4212decd..6bcea033 100644 --- a/src/main/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminService.java +++ b/src/main/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminService.java @@ -8,11 +8,14 @@ import de.caritas.cob.agencyservice.useradminservice.generated.web.model.AgencyTypeDTO.AgencyTypeEnum; import de.caritas.cob.agencyservice.useradminservice.generated.web.model.ConsultantAdminResponseDTO; import de.caritas.cob.agencyservice.useradminservice.generated.web.model.ConsultantFilter; +import de.caritas.cob.agencyservice.useradminservice.generated.web.model.Sort.FieldEnum; +import de.caritas.cob.agencyservice.useradminservice.generated.web.model.Sort.OrderEnum; import java.util.List; import lombok.NonNull; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Service; +import de.caritas.cob.agencyservice.useradminservice.generated.web.model.Sort; @Service @RequiredArgsConstructor @@ -56,8 +59,13 @@ public List getConsultantsOfAgency(Long agencyId, in int perPage) { addDefaultHeaders(this.adminUserControllerApi.getApiClient()); ConsultantFilter consultantFilter = new ConsultantFilter().agencyId(agencyId); + + Sort sortBy = new Sort(); + sortBy.setField(FieldEnum.LASTNAME); + sortBy.setOrder(OrderEnum.ASC); + return this.adminUserControllerApi - .getConsultants(currentPage, perPage, consultantFilter) + .getConsultants(currentPage, perPage, consultantFilter, sortBy) .getEmbedded(); } } diff --git a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminServiceTest.java b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminServiceTest.java index da903c60..6773164c 100644 --- a/src/test/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminServiceTest.java +++ b/src/test/java/de/caritas/cob/agencyservice/api/admin/service/UserAdminServiceTest.java @@ -1,6 +1,7 @@ package de.caritas.cob.agencyservice.api.admin.service; import static de.caritas.cob.agencyservice.useradminservice.generated.web.model.AgencyTypeDTO.AgencyTypeEnum.TEAM_AGENCY; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.times; @@ -15,6 +16,9 @@ import de.caritas.cob.agencyservice.useradminservice.generated.web.model.AgencyTypeDTO; import de.caritas.cob.agencyservice.useradminservice.generated.web.model.ConsultantFilter; import de.caritas.cob.agencyservice.useradminservice.generated.web.model.ConsultantSearchResultDTO; +import de.caritas.cob.agencyservice.useradminservice.generated.web.model.Sort; +import de.caritas.cob.agencyservice.useradminservice.generated.web.model.Sort.FieldEnum; +import de.caritas.cob.agencyservice.useradminservice.generated.web.model.Sort.OrderEnum; import java.util.List; import org.jeasy.random.EasyRandom; import org.junit.Before; @@ -25,7 +29,6 @@ import org.mockito.junit.MockitoJUnitRunner; import org.springframework.http.HttpHeaders; import org.springframework.test.util.ReflectionTestUtils; -import static org.junit.jupiter.api.Assertions.assertEquals; @RunWith(MockitoJUnitRunner.class) public class UserAdminServiceTest { @@ -70,13 +73,13 @@ public void getConsultantsOfAgency_Should_callServicesCorrectly() { Long agencyId = 1L; int currentPage = 1; int perPage = 1; - when(this.adminUserControllerApi.getConsultants(any(), any(), any())) + when(this.adminUserControllerApi.getConsultants(any(), any(), any(), any())) .thenReturn(new EasyRandom().nextObject(ConsultantSearchResultDTO.class)); this.userAdminService.getConsultantsOfAgency(agencyId, currentPage, perPage); verify(this.adminUserControllerApi, times(1)) .getConsultants(eq(currentPage), eq(perPage), - eq(new ConsultantFilter().agencyId(agencyId))); + eq(new ConsultantFilter().agencyId(agencyId)), any()); verify(this.apiClient, times(this.httpHeaders.size())).addDefaultHeader(any(), any()); } @@ -86,12 +89,21 @@ public void addTenantHeader_WhenMultitenacy_Enabled() { ApiClient apiClient = new ApiClient(); TenantHeaderSupplier tenantHeaderSupplier = new TenantHeaderSupplier(); ReflectionTestUtils.setField(tenantHeaderSupplier, "multitenancy", true); - ReflectionTestUtils.setField(this.userAdminService, "tenantHeaderSupplier", tenantHeaderSupplier); + ReflectionTestUtils + .setField(this.userAdminService, "tenantHeaderSupplier", tenantHeaderSupplier); this.userAdminService.addDefaultHeaders(apiClient); - HttpHeaders httpHeaders = (HttpHeaders) ReflectionTestUtils.getField(apiClient, "defaultHeaders"); + HttpHeaders httpHeaders = (HttpHeaders) ReflectionTestUtils + .getField(apiClient, "defaultHeaders"); List tenantId = httpHeaders.get("tenantId"); assertEquals(tenantId.get(0), TenantContext.getCurrentTenant().toString()); TenantContext.clear(); } + + private Sort getSort() { + Sort sortBy = new Sort(); + sortBy.setField(FieldEnum.LASTNAME); + sortBy.setOrder(OrderEnum.ASC); + return sortBy; + } }