|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jpa.repository.query; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import jakarta.persistence.EntityManager; |
| 22 | +import jakarta.persistence.EntityManagerFactory; |
| 23 | +import jakarta.persistence.metamodel.Metamodel; |
| 24 | + |
| 25 | +import java.lang.reflect.Method; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.mockito.Mock; |
| 30 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 31 | +import org.mockito.quality.Strictness; |
| 32 | + |
| 33 | +import org.springframework.core.annotation.AnnotatedElementUtils; |
| 34 | +import org.springframework.data.domain.Sort; |
| 35 | +import org.springframework.data.jpa.provider.QueryExtractor; |
| 36 | +import org.springframework.data.jpa.repository.Query; |
| 37 | +import org.springframework.data.jpa.repository.QueryRewriter; |
| 38 | +import org.springframework.data.projection.SpelAwareProxyProjectionFactory; |
| 39 | +import org.springframework.data.repository.Repository; |
| 40 | +import org.springframework.data.repository.core.RepositoryMetadata; |
| 41 | +import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; |
| 42 | +import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider; |
| 43 | +import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 44 | +import org.springframework.util.ReflectionUtils; |
| 45 | + |
| 46 | +/** |
| 47 | + * Unit tests for {@link NativeJpaQuery}. |
| 48 | + * |
| 49 | + * @author Mark Paluch |
| 50 | + */ |
| 51 | +@MockitoSettings(strictness = Strictness.LENIENT) |
| 52 | +class NativeJpaQueryUnitTests { |
| 53 | + |
| 54 | + @Mock EntityManager em; |
| 55 | + @Mock EntityManagerFactory emf; |
| 56 | + @Mock Metamodel metamodel; |
| 57 | + |
| 58 | + @BeforeEach |
| 59 | + void setUp() { |
| 60 | + |
| 61 | + when(em.getMetamodel()).thenReturn(metamodel); |
| 62 | + when(em.getEntityManagerFactory()).thenReturn(emf); |
| 63 | + when(em.getDelegate()).thenReturn(em); |
| 64 | + } |
| 65 | + |
| 66 | + @Test // GH-3546 |
| 67 | + void shouldApplySorting() { |
| 68 | + |
| 69 | + NativeJpaQuery query = getQuery(TestRepo.class, "find", Sort.class); |
| 70 | + String sql = query.getSortedQueryString(Sort.by("foo", "bar")); |
| 71 | + |
| 72 | + assertThat(sql).isEqualTo("SELECT e FROM Employee e order by e.foo asc, e.bar asc"); |
| 73 | + } |
| 74 | + |
| 75 | + private NativeJpaQuery getQuery(Class<?> repository, String method, Class<?>... args) { |
| 76 | + Method respositoryMethod = ReflectionUtils.findMethod(repository, method, args); |
| 77 | + RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(repository); |
| 78 | + SpelAwareProxyProjectionFactory projectionFactory = mock(SpelAwareProxyProjectionFactory.class); |
| 79 | + QueryExtractor queryExtractor = mock(QueryExtractor.class); |
| 80 | + JpaQueryMethod queryMethod = new JpaQueryMethod(respositoryMethod, repositoryMetadata, projectionFactory, |
| 81 | + queryExtractor); |
| 82 | + |
| 83 | + Query annotation = AnnotatedElementUtils.getMergedAnnotation(respositoryMethod, Query.class); |
| 84 | + |
| 85 | + NativeJpaQuery query = new NativeJpaQuery(queryMethod, em, annotation.value(), annotation.countQuery(), |
| 86 | + QueryRewriter.IdentityQueryRewriter.INSTANCE, QueryMethodEvaluationContextProvider.DEFAULT, |
| 87 | + new SpelExpressionParser()); |
| 88 | + return query; |
| 89 | + } |
| 90 | + |
| 91 | + interface TestRepo extends Repository<Object, Object> { |
| 92 | + |
| 93 | + @Query("SELECT e FROM Employee e") |
| 94 | + Object find(Sort sort); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments