Skip to content

Commit

Permalink
fix: error on consultant agency search
Browse files Browse the repository at this point in the history
  • Loading branch information
tkuzynow committed Jan 6, 2024
1 parent d23e08d commit 9884511
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public ConsultantAdminFilterTenantAwareService(
super(entityManagerFactory);
}

@Override
protected FullTextQuery buildFilteredQuery(
ConsultantFilter consultantFilter, FullTextEntityManager fullTextEntityManager) {

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TenantData> CURRENT_TENANT_DATA = new ThreadLocal<>();
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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> 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<Query> 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<Query> 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();
}
}

0 comments on commit 9884511

Please sign in to comment.