From 9884511adceade735d8926a9205465b93d5c6a23 Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Sat, 6 Jan 2024 11:43:30 +0100 Subject: [PATCH] fix: error on consultant agency search --- ...nsultantAdminFilterTenantAwareService.java | 7 +- .../service/TenantHibernateInterceptor.java | 5 +- .../userservice/api/tenant/TenantAspect.java | 4 +- .../userservice/api/tenant/TenantContext.java | 6 ++ ...tantAdminFilterTenantAwareServiceTest.java | 97 +++++++++++++++++++ 5 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 src/test/java/de/caritas/cob/userservice/api/admin/service/consultant/ConsultantAdminFilterTenantAwareServiceTest.java diff --git a/src/main/java/de/caritas/cob/userservice/api/admin/service/consultant/ConsultantAdminFilterTenantAwareService.java b/src/main/java/de/caritas/cob/userservice/api/admin/service/consultant/ConsultantAdminFilterTenantAwareService.java index 3b67e6a66..fac62f711 100644 --- a/src/main/java/de/caritas/cob/userservice/api/admin/service/consultant/ConsultantAdminFilterTenantAwareService.java +++ b/src/main/java/de/caritas/cob/userservice/api/admin/service/consultant/ConsultantAdminFilterTenantAwareService.java @@ -27,6 +27,7 @@ public ConsultantAdminFilterTenantAwareService( super(entityManagerFactory); } + @Override protected FullTextQuery buildFilteredQuery( ConsultantFilter consultantFilter, FullTextEntityManager fullTextEntityManager) { @@ -53,8 +54,10 @@ protected FullTextQuery buildFilteredQuery( .onConsultantFilter(consultantFilter) .buildQuery(); - Query resultQuery = queryBuilder.bool().must(tenantQuery).must(query).createQuery(); - + Query resultQuery = + TenantContext.isTechnicalOrSuperAdminContext() + ? query + : queryBuilder.bool().must(tenantQuery).must(query).createQuery(); return fullTextEntityManager.createFullTextQuery(resultQuery, Consultant.class); } } diff --git a/src/main/java/de/caritas/cob/userservice/api/service/TenantHibernateInterceptor.java b/src/main/java/de/caritas/cob/userservice/api/service/TenantHibernateInterceptor.java index 619cbeb53..7aec50bd2 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/TenantHibernateInterceptor.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/TenantHibernateInterceptor.java @@ -1,7 +1,5 @@ package de.caritas.cob.userservice.api.service; -import static de.caritas.cob.userservice.api.tenant.TenantResolverService.TECHNICAL_TENANT_ID; - import de.caritas.cob.userservice.api.model.TenantAware; import de.caritas.cob.userservice.api.tenant.TenantContext; import java.util.Iterator; @@ -22,8 +20,7 @@ public void preFlush(Iterator entities) { entity = entities.next(); if (entity instanceof TenantAware) { var tenantAware = (TenantAware) entity; - if (tenantAware.getTenantId() == null - && !TECHNICAL_TENANT_ID.equals(TenantContext.getCurrentTenant())) { + if (tenantAware.getTenantId() == null && !TenantContext.isTechnicalOrSuperAdminContext()) { ((TenantAware) entity).setTenantId(TenantContext.getCurrentTenant()); } } diff --git a/src/main/java/de/caritas/cob/userservice/api/tenant/TenantAspect.java b/src/main/java/de/caritas/cob/userservice/api/tenant/TenantAspect.java index 9cef34d35..91a5d57e3 100644 --- a/src/main/java/de/caritas/cob/userservice/api/tenant/TenantAspect.java +++ b/src/main/java/de/caritas/cob/userservice/api/tenant/TenantAspect.java @@ -1,7 +1,5 @@ package de.caritas.cob.userservice.api.tenant; -import static de.caritas.cob.userservice.api.tenant.TenantResolverService.TECHNICAL_TENANT_ID; - import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import lombok.NonNull; @@ -23,7 +21,7 @@ public class TenantAspect { @Before("execution(* de.caritas.cob.userservice.api.port..*(..)))") public void beforeQueryAspect() { - if (TECHNICAL_TENANT_ID.equals(TenantContext.getCurrentTenant())) { + if (TenantContext.isTechnicalOrSuperAdminContext()) { return; } diff --git a/src/main/java/de/caritas/cob/userservice/api/tenant/TenantContext.java b/src/main/java/de/caritas/cob/userservice/api/tenant/TenantContext.java index 15f9aeeab..123f30227 100644 --- a/src/main/java/de/caritas/cob/userservice/api/tenant/TenantContext.java +++ b/src/main/java/de/caritas/cob/userservice/api/tenant/TenantContext.java @@ -3,6 +3,8 @@ /** Holds the tenant_id variable for ongoing thread assigned for HTTP request. */ public class TenantContext { + public static final Long TECHNICAL_TENANT_ID = 0L; + private TenantContext() {} private static final ThreadLocal CURRENT_TENANT_DATA = new ThreadLocal<>(); @@ -42,4 +44,8 @@ public static void clear() { public static boolean contextIsSet() { return getCurrentTenant() != null; } + + public static boolean isTechnicalOrSuperAdminContext() { + return TECHNICAL_TENANT_ID.equals(getCurrentTenant()); + } } diff --git a/src/test/java/de/caritas/cob/userservice/api/admin/service/consultant/ConsultantAdminFilterTenantAwareServiceTest.java b/src/test/java/de/caritas/cob/userservice/api/admin/service/consultant/ConsultantAdminFilterTenantAwareServiceTest.java new file mode 100644 index 000000000..983e9aac6 --- /dev/null +++ b/src/test/java/de/caritas/cob/userservice/api/admin/service/consultant/ConsultantAdminFilterTenantAwareServiceTest.java @@ -0,0 +1,97 @@ +package de.caritas.cob.userservice.api.admin.service.consultant; + +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import de.caritas.cob.userservice.api.adapters.web.dto.ConsultantFilter; +import de.caritas.cob.userservice.api.model.Consultant; +import de.caritas.cob.userservice.api.tenant.TenantContext; +import javax.persistence.EntityManagerFactory; +import org.apache.lucene.search.Query; +import org.hibernate.search.jpa.FullTextEntityManager; +import org.hibernate.search.query.dsl.BooleanJunction; +import org.hibernate.search.query.dsl.MustJunction; +import org.hibernate.search.query.dsl.QueryBuilder; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Answers; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class ConsultantAdminFilterTenantAwareServiceTest { + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + FullTextEntityManager fullTextEntityManager; + + @Mock EntityManagerFactory entityManagerFactory; + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + QueryBuilder queryBuilder; + + @Mock BooleanJunction booleanJunction; + + @Mock MustJunction mustJunction; + + @Mock MustJunction secondMustJunction; + + @InjectMocks ConsultantAdminFilterTenantAwareService consultantAdminFilterTenantAwareService; + + @Test + void + buildFilteredQuery_Should_CreateFilteredQuery_WithoutTenantFilter_If_TenantContextIsSuperAdmin() { + // given + TenantContext.setCurrentTenant(TenantContext.TECHNICAL_TENANT_ID); + when(fullTextEntityManager + .getSearchFactory() + .buildQueryBuilder() + .forEntity(Consultant.class) + .get()) + .thenReturn(queryBuilder); + when(queryBuilder.bool()).thenReturn(booleanJunction); + when(booleanJunction.must(Mockito.any(Query.class))).thenReturn(mustJunction); + + // when + consultantAdminFilterTenantAwareService.buildFilteredQuery( + new ConsultantFilter().agencyId(59L), fullTextEntityManager); + + // then + ArgumentCaptor captor = ArgumentCaptor.forClass(Query.class); + verify(fullTextEntityManager) + .createFullTextQuery(captor.capture(), Mockito.eq(Consultant.class)); + verify(booleanJunction).must(Mockito.any(Query.class)); + verify(mustJunction, Mockito.never()).must(Mockito.any(Query.class)); + TenantContext.clear(); + } + + @Test + void + buildFilteredQuery_Should_CreateFilteredQuery_WithTenantFilter_When_TenantContextIsNotSuperAdmin() { + // given + TenantContext.setCurrentTenant(1L); + when(fullTextEntityManager + .getSearchFactory() + .buildQueryBuilder() + .forEntity(Consultant.class) + .get()) + .thenReturn(queryBuilder); + when(queryBuilder.bool()).thenReturn(booleanJunction); + when(booleanJunction.must(Mockito.any(Query.class))).thenReturn(mustJunction); + when(mustJunction.must(null)).thenReturn(secondMustJunction); + + // when + consultantAdminFilterTenantAwareService.buildFilteredQuery( + new ConsultantFilter().agencyId(59L), fullTextEntityManager); + + // then + ArgumentCaptor captor = ArgumentCaptor.forClass(Query.class); + verify(fullTextEntityManager) + .createFullTextQuery(captor.capture(), Mockito.eq(Consultant.class)); + verify(booleanJunction, Mockito.times(2)).must(Mockito.any(Query.class)); + verify(mustJunction).must(Mockito.any()); + TenantContext.clear(); + } +}