feat: company on emails and audit search
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
a6996841ac
commit
118c57c0ea
6 files changed
+147
-4
No files matched your search
+67
@@ -0,0 +1,67 @@
|
||||
package com.solvway.services.email.repo;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.solvway.services.email.model.Email;
|
||||
import com.solvway.services.email.util.paging.PageableRest;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
class EmailCompanyScopingTest {
|
||||
|
||||
private final EntityManager em = mock(EntityManager.class, RETURNS_DEEP_STUBS);
|
||||
private final CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
private final EmailRepositoryImpl repo = new EmailRepositoryImpl();
|
||||
|
||||
EmailCompanyScopingTest() {
|
||||
ReflectionTestUtils.setField(repo, "em", em);
|
||||
jakarta.persistence.TypedQuery query = mock(jakarta.persistence.TypedQuery.class, org.mockito.Mockito.RETURNS_SELF);
|
||||
org.mockito.Mockito.doReturn(java.util.List.of()).when(query).getResultList();
|
||||
org.mockito.Mockito.doReturn(0L).when(query).getSingleResult();
|
||||
org.mockito.Mockito.doReturn(query).when(em).createQuery(any(jakarta.persistence.criteria.CriteriaQuery.class));
|
||||
}
|
||||
|
||||
private PageableRest pageable() {
|
||||
PageableRest p = new PageableRest();
|
||||
p.setNumber(0);
|
||||
p.setSize(10);
|
||||
PageableRest.SortRest sr = new PageableRest.SortRest();
|
||||
sr.setDirection("DESC");
|
||||
sr.setProperty("id");
|
||||
p.setSorts(new java.util.ArrayList<>(java.util.List.of(sr)));
|
||||
return p;
|
||||
}
|
||||
|
||||
@Test
|
||||
void emailKeepsCompanyFromPayload() throws Exception {
|
||||
Email e = new ObjectMapper().readValue("{\"subject\":\"s\",\"companyId\":2}", Email.class);
|
||||
assertThat(e.getCompanyId()).isEqualTo(2L);
|
||||
assertThat(new ObjectMapper().readValue("{\"subject\":\"s\"}", Email.class).getCompanyId()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchFiltersByCompanyWhenGiven() {
|
||||
HashMap<String, String> property = new HashMap<>();
|
||||
property.put("companyId", "2");
|
||||
repo.findByPropertyAndPageable(property, pageable());
|
||||
// once for the page query, once for the count query
|
||||
verify(cb, org.mockito.Mockito.times(2)).equal(any(), org.mockito.ArgumentMatchers.eq(2L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchDoesNotFilterByCompanyWhenAbsent() {
|
||||
repo.findByPropertyAndPageable(new HashMap<>(), pageable());
|
||||
verify(cb, never()).equal(any(), any(Object.class));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user