|
| 1 | +/* |
| 2 | + * Copyright 2025 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.mongodb.repository.aot; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import example.aot.User; |
| 21 | +import example.aot.UserRepository; |
| 22 | + |
| 23 | +import java.lang.reflect.Method; |
| 24 | + |
| 25 | +import javax.lang.model.element.Modifier; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.springframework.data.geo.Box; |
| 29 | +import org.springframework.data.geo.Circle; |
| 30 | +import org.springframework.data.geo.Distance; |
| 31 | +import org.springframework.data.geo.GeoResults; |
| 32 | +import org.springframework.data.geo.Point; |
| 33 | +import org.springframework.data.geo.Polygon; |
| 34 | +import org.springframework.data.mongodb.core.MongoOperations; |
| 35 | +import org.springframework.data.mongodb.core.geo.Sphere; |
| 36 | +import org.springframework.data.mongodb.repository.ReadPreference; |
| 37 | +import org.springframework.data.repository.Repository; |
| 38 | +import org.springframework.data.repository.aot.generate.AotQueryMethodGenerationContext; |
| 39 | +import org.springframework.data.repository.aot.generate.AotRepositoryFragmentMetadata; |
| 40 | +import org.springframework.data.repository.aot.generate.MethodContributor; |
| 41 | +import org.springframework.data.repository.core.RepositoryInformation; |
| 42 | +import org.springframework.data.repository.query.QueryMethod; |
| 43 | +import org.springframework.javapoet.ClassName; |
| 44 | +import org.springframework.javapoet.FieldSpec; |
| 45 | +import org.springframework.javapoet.MethodSpec; |
| 46 | + |
| 47 | +/** |
| 48 | + * @author Christoph Strobl |
| 49 | + */ |
| 50 | +public class QueryMethodContributionUnitTests { |
| 51 | + |
| 52 | + @Test |
| 53 | + void rendersQueryForNearUsingPoint() throws NoSuchMethodException { |
| 54 | + |
| 55 | + MethodSpec methodSpec = codeOf(UserRepository.class, "findByLocationCoordinatesNear", Point.class); |
| 56 | + |
| 57 | + assertThat(methodSpec.toString()) // |
| 58 | + .contains("{'location.coordinates':{'$near':?0}}") // |
| 59 | + .contains("Object[]{ location }") // |
| 60 | + .contains("return finder.matching(filterQuery).all()"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void rendersQueryForWithinUsingCircle() throws NoSuchMethodException { |
| 65 | + |
| 66 | + MethodSpec methodSpec = codeOf(UserRepository.class, "findByLocationCoordinatesWithin", Circle.class); |
| 67 | + |
| 68 | + assertThat(methodSpec.toString()) // |
| 69 | + .contains("{'location.coordinates':{'$geoWithin':{'$center':?0}}") // |
| 70 | + .contains( |
| 71 | + "List.of(circle.getCenter().getX(), circle.getCenter().getY()), circle.getRadius().getNormalizedValue())") // |
| 72 | + .contains("return finder.matching(filterQuery).all()"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void rendersQueryForWithinUsingSphere() throws NoSuchMethodException { |
| 77 | + |
| 78 | + MethodSpec methodSpec = codeOf(UserRepository.class, "findByLocationCoordinatesWithin", Sphere.class); |
| 79 | + |
| 80 | + assertThat(methodSpec.toString()) // |
| 81 | + .contains("{'location.coordinates':{'$geoWithin':{'$centerSphere':?0}}") // |
| 82 | + .contains( |
| 83 | + "List.of(circle.getCenter().getX(), circle.getCenter().getY()), circle.getRadius().getNormalizedValue())") // |
| 84 | + .contains("return finder.matching(filterQuery).all()"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void rendersQueryForWithinUsingBox() throws NoSuchMethodException { |
| 89 | + |
| 90 | + MethodSpec methodSpec = codeOf(UserRepository.class, "findByLocationCoordinatesWithin", Box.class); |
| 91 | + |
| 92 | + assertThat(methodSpec.toString()) // |
| 93 | + .contains("{'location.coordinates':{'$geoWithin':{'$box':?0}}") // |
| 94 | + .contains("List.of(box.getFirst().getX(), box.getFirst().getY())") // |
| 95 | + .contains("List.of(box.getSecond().getX(), box.getSecond().getY())") // |
| 96 | + .contains("return finder.matching(filterQuery).all()"); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void rendersQueryForWithinUsingPolygon() throws NoSuchMethodException { |
| 101 | + |
| 102 | + MethodSpec methodSpec = codeOf(UserRepository.class, "findByLocationCoordinatesWithin", Polygon.class); |
| 103 | + |
| 104 | + assertThat(methodSpec.toString()) // |
| 105 | + .contains("{'location.coordinates':{'$geoWithin':{'$polygon':?0}}") // |
| 106 | + .contains("polygon.getPoints().stream().map(_p ->") // |
| 107 | + .contains("List.of(_p.getX(), _p.getY())") // |
| 108 | + .contains("return finder.matching(filterQuery).all()"); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void rendersNearQueryForGeoResults() throws NoSuchMethodException { |
| 113 | + |
| 114 | + MethodSpec methodSpec = codeOf(UserRepoWithMeta.class, "findByLocationCoordinatesNear", Point.class, |
| 115 | + Distance.class); |
| 116 | + |
| 117 | + assertThat(methodSpec.toString()) // |
| 118 | + .contains("NearQuery.near(point)") // |
| 119 | + .contains("nearQuery.maxDistance(maxDistance).in(maxDistance.getMetric())") // |
| 120 | + .contains(".withReadPreference(com.mongodb.ReadPreference.valueOf(\"NEAREST\")") // |
| 121 | + .contains(".near(nearQuery).all()"); |
| 122 | + } |
| 123 | + |
| 124 | + private static MethodSpec codeOf(Class<?> repository, String methodName, Class<?>... args) |
| 125 | + throws NoSuchMethodException { |
| 126 | + |
| 127 | + Method method = repository.getMethod(methodName, args); |
| 128 | + |
| 129 | + TestMongoAotRepositoryContext repoContext = new TestMongoAotRepositoryContext(repository, null); |
| 130 | + MongoRepositoryContributor contributor = new MongoRepositoryContributor(repoContext); |
| 131 | + MethodContributor<? extends QueryMethod> methodContributor = contributor.contributeQueryMethod(method); |
| 132 | + |
| 133 | + AotRepositoryFragmentMetadata metadata = new AotRepositoryFragmentMetadata(ClassName.get(UserRepository.class)); |
| 134 | + metadata.addField( |
| 135 | + FieldSpec.builder(MongoOperations.class, "mongoOperations", Modifier.PRIVATE, Modifier.FINAL).build()); |
| 136 | + |
| 137 | + TestQueryMethodGenerationContext methodContext = new TestQueryMethodGenerationContext( |
| 138 | + repoContext.getRepositoryInformation(), method, methodContributor.getQueryMethod(), metadata); |
| 139 | + return methodContributor.contribute(methodContext); |
| 140 | + } |
| 141 | + |
| 142 | + static class TestQueryMethodGenerationContext extends AotQueryMethodGenerationContext { |
| 143 | + |
| 144 | + protected TestQueryMethodGenerationContext(RepositoryInformation repositoryInformation, Method method, |
| 145 | + QueryMethod queryMethod, AotRepositoryFragmentMetadata targetTypeMetadata) { |
| 146 | + super(repositoryInformation, method, queryMethod, targetTypeMetadata); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + interface UserRepoWithMeta extends Repository<User, String> { |
| 151 | + |
| 152 | + @ReadPreference("NEAREST") |
| 153 | + GeoResults<User> findByLocationCoordinatesNear(Point point, Distance maxDistance); |
| 154 | + |
| 155 | + } |
| 156 | +} |
0 commit comments