From 29bb9bf34c06c91c7de43b68770c8bba79e88728 Mon Sep 17 00:00:00 2001 From: mouad el azaar Date: Fri, 6 Dec 2024 20:24:27 +0100 Subject: [PATCH] add tu for specification --- pom.xml | 4 +- .../service/PegaseSpecificationTest.java | 80 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/rte_france/antares/datamanager_back/service/PegaseSpecificationTest.java diff --git a/pom.xml b/pom.xml index 038c196..4eda159 100644 --- a/pom.xml +++ b/pom.xml @@ -41,7 +41,8 @@ **com/rte_france/antares/datamanager_back/configuration/**, **com/rte_france/antares/datamanager_back/repository/model/**, **com/rte_france/antares/datamanager_back/dto/**, - **com/rte_france/antares/datamanager_back/PegaseBackApplication.java + **com/rte_france/antares/datamanager_back/PegaseBackApplication.java, + **/com/rte_france/antares/datamanager_back/service/impl/SearchCriteria.java @@ -195,6 +196,7 @@ com/rte_france/antares/datamanager_back/repository/model/** com/rte_france/antares/datamanager_back/dto/** com/rte_france/antares/datamanager_back/PegaseBackApplication.java + com/rte_france/antares/datamanager_back/service/impl/SearchCriteria.java diff --git a/src/test/java/com/rte_france/antares/datamanager_back/service/PegaseSpecificationTest.java b/src/test/java/com/rte_france/antares/datamanager_back/service/PegaseSpecificationTest.java new file mode 100644 index 0000000..61905a4 --- /dev/null +++ b/src/test/java/com/rte_france/antares/datamanager_back/service/PegaseSpecificationTest.java @@ -0,0 +1,80 @@ +package com.rte_france.antares.datamanager_back.service; + +import com.rte_france.antares.datamanager_back.service.impl.PegaseSpecification; +import com.rte_france.antares.datamanager_back.service.impl.SearchCriteria; +import jakarta.persistence.criteria.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class PegaseSpecificationTest { + + @Mock + private Root root; + + @Mock + private CriteriaQuery query; + + @Mock + private CriteriaBuilder builder; + + @Mock + private Path path; + + private PegaseSpecification specification; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + when(root.get(anyString())).thenReturn(path); + } + + @Test + void toPredicate_Equal() { + when(root.get("id")).thenReturn(path); + + SearchCriteria criteria = new SearchCriteria("id", ":", 100); + specification = new PegaseSpecification<>(criteria); + + Predicate mockPredicate = mock(Predicate.class); + when(builder.equal(path, 100)).thenReturn(mockPredicate); + + Predicate predicate = specification.toPredicate(root, query, builder); + + assertNotNull(predicate); + verify(builder).equal(path, 100); + } + + @Test + void toPredicate_InvalidOperation() { + when(root.get("name")).thenReturn(path); + + SearchCriteria criteria = new SearchCriteria("name", "invalid", "John"); + specification = new PegaseSpecification<>(criteria); + + Predicate predicate = specification.toPredicate(root, query, builder); + + assertNull(predicate); + } + + + @Test + void inOperation() { + when(root.get("roles")).thenReturn(path); + + SearchCriteria criteria = new SearchCriteria("roles", "in", "admin"); + specification = new PegaseSpecification<>(criteria); + + Predicate mockPredicate = mock(Predicate.class); + when(builder.isMember(eq("admin"), any(Expression.class))).thenReturn(mockPredicate); + + Predicate predicate = specification.toPredicate(root, query, builder); + + assertNotNull(predicate); + verify(builder).isMember(eq("admin"), any(Expression.class)); + } +} \ No newline at end of file