|
40 | 40 | import static com.github.mhewedy.expressions.model.Employee.Lang;
|
41 | 41 | import static com.github.mhewedy.expressions.model.Status.ACTIVE;
|
42 | 42 | import static com.github.mhewedy.expressions.model.Status.NOT_ACTIVE;
|
43 |
| -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 43 | +import static org.assertj.core.api.Assertions.assertThat; |
| 44 | +import static org.assertj.core.api.Assertions.assertThatList; |
44 | 45 | import static org.assertj.core.api.AssertionsForClassTypes.fail;
|
45 | 46 |
|
46 | 47 | @Slf4j
|
@@ -758,6 +759,127 @@ public void testLeftJoinAlternateSyntax() throws JsonProcessingException {
|
758 | 759 | // ...from employee e join department d on d.id = e.department_id left join city c on c.id = d.city_id where e.first_name = ? or c.name = ?
|
759 | 760 | }
|
760 | 761 |
|
| 762 | + @Test |
| 763 | + public void testSearchWithMultipleNestedJoins() throws Exception { |
| 764 | + String json = """ |
| 765 | + { |
| 766 | + "$and": [ |
| 767 | + { |
| 768 | + "department.city.name": "cairo" |
| 769 | + }, |
| 770 | + { |
| 771 | + "tasks.status": "ACTIVE" |
| 772 | + } |
| 773 | + ] |
| 774 | + } |
| 775 | + """; |
| 776 | + Expressions expressions = new ObjectMapper().readValue(json, Expressions.class); |
| 777 | + List<Employee> employees = employeeRepository.findAll(expressions); |
| 778 | + |
| 779 | + assertThat(employees).isNotNull(); |
| 780 | + assertThatList(employees).hasSize(3); // All employees in Cairo with active tasks |
| 781 | + assertThatList(employees).allSatisfy(emp -> { |
| 782 | + assertThat(emp.department).isNotNull(); |
| 783 | + assertThat(emp.department.city).isNotNull(); |
| 784 | + assertThat(emp.department.city.name).isEqualTo("cairo"); |
| 785 | + assertThat(emp.tasks).isNotNull(); |
| 786 | + assertThat(emp.tasks.stream().anyMatch(task -> task.status == ACTIVE)).isTrue(); |
| 787 | + }); |
| 788 | + } |
| 789 | + |
| 790 | + @Test |
| 791 | + public void testSearchWithComplexNestedConditions() throws Exception { |
| 792 | + String json = """ |
| 793 | + { |
| 794 | + "$or": [ |
| 795 | + { |
| 796 | + "department.name": "hr", |
| 797 | + "tasks.status": "ACTIVE" |
| 798 | + }, |
| 799 | + { |
| 800 | + "department.name": "sw dev", |
| 801 | + "age": { "$gt": 25 } |
| 802 | + } |
| 803 | + ] |
| 804 | + } |
| 805 | + """; |
| 806 | + Expressions expressions = new ObjectMapper().readValue(json, Expressions.class); |
| 807 | + List<Employee> employees = employeeRepository.findAll(expressions); |
| 808 | + |
| 809 | + assertThat(employees).isNotNull(); |
| 810 | + assertThatList(employees).hasSize(4); // HR employees with active tasks + SW dev employees over 25 |
| 811 | + assertThatList(employees).allSatisfy(emp -> { |
| 812 | + if (emp.department != null && emp.department.name.equals("hr")) { |
| 813 | + assertThat(emp.tasks).isNotNull(); |
| 814 | + assertThat(emp.tasks.stream().anyMatch(task -> task.status == ACTIVE)).isTrue(); |
| 815 | + } else if (emp.department != null && emp.department.name.equals("sw dev")) { |
| 816 | + assertThat(emp.age).isGreaterThan(25); |
| 817 | + } |
| 818 | + }); |
| 819 | + } |
| 820 | + |
| 821 | + @Test |
| 822 | + public void testSearchWithMultipleSortOrders() throws Exception { |
| 823 | + String json = """ |
| 824 | + { |
| 825 | + "department.name": "hr" |
| 826 | + } |
| 827 | + """; |
| 828 | + Expressions expressions = new ObjectMapper().readValue(json, Expressions.class); |
| 829 | + Sort sort = Sort.by( |
| 830 | + Sort.Order.asc("firstName"), |
| 831 | + Sort.Order.desc("age") |
| 832 | + ); |
| 833 | + List<Employee> employees = employeeRepository.findAll(expressions, sort); |
| 834 | + |
| 835 | + assertThat(employees).isNotNull(); |
| 836 | + assertThatList(employees).hasSize(3); // All HR employees |
| 837 | + assertThatList(employees).allSatisfy(emp -> { |
| 838 | + assertThat(emp.department).isNotNull(); |
| 839 | + assertThat(emp.department.name).isEqualTo("hr"); |
| 840 | + }); |
| 841 | + |
| 842 | + // Verify sorting |
| 843 | + for (int i = 1; i < employees.size(); i++) { |
| 844 | + Employee prev = employees.get(i-1); |
| 845 | + Employee curr = employees.get(i); |
| 846 | + assertThat(prev.firstName.compareTo(curr.firstName)).isLessThanOrEqualTo(0); |
| 847 | + if (prev.firstName.equals(curr.firstName)) { |
| 848 | + assertThat(prev.age).isGreaterThanOrEqualTo(curr.age); |
| 849 | + } |
| 850 | + } |
| 851 | + } |
| 852 | + |
| 853 | + @Test |
| 854 | + public void testSearchWithPaginationAndComplexConditions() throws Exception { |
| 855 | + String json = """ |
| 856 | + { |
| 857 | + "$or": [ |
| 858 | + { |
| 859 | + "department.name": "hr" |
| 860 | + }, |
| 861 | + { |
| 862 | + "department.name": "sw dev" |
| 863 | + } |
| 864 | + ], |
| 865 | + "age": { "$gte": 30 } |
| 866 | + } |
| 867 | + """; |
| 868 | + Expressions expressions = new ObjectMapper().readValue(json, Expressions.class); |
| 869 | + Page<Employee> page = employeeRepository.findAll(expressions, PageRequest.of(0, 2, Sort.by("firstName"))); |
| 870 | + |
| 871 | + assertThat(page).isNotNull(); |
| 872 | + assertThat(page.getTotalElements()).isEqualTo(3); // Total matching employees |
| 873 | + assertThat(page.getSize()).isEqualTo(2); // Page size |
| 874 | + assertThat(page.getContent()).isNotNull(); |
| 875 | + assertThatList(page.getContent()).hasSize(2); // Current page content |
| 876 | + assertThatList(page.getContent()).allSatisfy(emp -> { |
| 877 | + assertThat(emp.department).isNotNull(); |
| 878 | + assertThat(emp.department.name).isIn("hr", "sw dev"); |
| 879 | + assertThat(emp.age).isGreaterThanOrEqualTo(30); |
| 880 | + }); |
| 881 | + } |
| 882 | + |
761 | 883 | @SneakyThrows
|
762 | 884 | private String loadResourceJsonFile(String name) {
|
763 | 885 | File file = ResourceUtils.getFile("classpath:" + name + ".json");
|
|
0 commit comments