|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 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 | + * http://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 | + |
| 17 | +package com.mongodb.client.model; |
| 18 | + |
| 19 | +import com.mongodb.OperationFunctionalSpecification; |
| 20 | +import com.mongodb.client.model.geojson.Point; |
| 21 | +import com.mongodb.client.model.geojson.Position; |
| 22 | +import org.bson.BsonArray; |
| 23 | +import org.bson.BsonDocument; |
| 24 | +import org.bson.BsonValue; |
| 25 | +import org.bson.Document; |
| 26 | + |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | +import java.util.stream.Collectors; |
| 30 | + |
| 31 | +import org.bson.codecs.DecoderContext; |
| 32 | +import org.bson.conversions.Bson; |
| 33 | +import org.junit.jupiter.api.AfterEach; |
| 34 | +import org.junit.jupiter.api.BeforeEach; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | + |
| 37 | +import static com.mongodb.ClusterFixture.serverVersionAtLeast; |
| 38 | +import static com.mongodb.MongoClientSettings.getDefaultCodecRegistry; |
| 39 | +import static com.mongodb.client.model.GeoNearOption.distanceMultiplier; |
| 40 | +import static com.mongodb.client.model.GeoNearOption.includeLocs; |
| 41 | +import static com.mongodb.client.model.GeoNearOption.key; |
| 42 | +import static com.mongodb.client.model.GeoNearOption.maxDistance; |
| 43 | +import static com.mongodb.client.model.GeoNearOption.minDistance; |
| 44 | +import static com.mongodb.client.model.GeoNearOption.query; |
| 45 | +import static com.mongodb.client.model.GeoNearOption.spherical; |
| 46 | +import static com.mongodb.client.model.Aggregates.geoNear; |
| 47 | +import static com.mongodb.client.model.Aggregates.unset; |
| 48 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 49 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 50 | + |
| 51 | +public class AggregatesTest extends OperationFunctionalSpecification { |
| 52 | + |
| 53 | + @BeforeEach |
| 54 | + public void beforeEach() { |
| 55 | + super.setupInternal(); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterEach |
| 59 | + public void afterEach() { |
| 60 | + super.cleanupInternal(); |
| 61 | + } |
| 62 | + |
| 63 | + void insertAll(final String insertAll) { |
| 64 | + List<BsonDocument> documents = BsonArray.parse(insertAll).stream().map(BsonValue::asDocument).collect(Collectors.toList()); |
| 65 | + getCollectionHelper().insertDocuments(documents); |
| 66 | + } |
| 67 | + |
| 68 | + private List<Bson> assertPipeline(final String stageAsString, final Bson stage) { |
| 69 | + BsonDocument expectedStage = BsonDocument.parse(stageAsString); |
| 70 | + List<Bson> pipeline = Collections.singletonList(stage); |
| 71 | + assertEquals(expectedStage, pipeline.get(0).toBsonDocument(BsonDocument.class, getDefaultCodecRegistry())); |
| 72 | + return pipeline; |
| 73 | + } |
| 74 | + |
| 75 | + private void assertResults(final List<Bson> pipeline, final String s) { |
| 76 | + List<Document> expectedResults = parseToList(s); |
| 77 | + List<Document> results = getCollectionHelper().aggregate(pipeline); |
| 78 | + assertEquals(expectedResults, results); |
| 79 | + } |
| 80 | + |
| 81 | + private List<Document> parseToList(final String s) { |
| 82 | + return BsonArray.parse(s).stream().map(v -> toDocument(v.asDocument())).collect(Collectors.toList()); |
| 83 | + } |
| 84 | + |
| 85 | + public Document toDocument(final BsonDocument bsonDocument) { |
| 86 | + return getDefaultCodecRegistry().get(Document.class).decode(bsonDocument.asBsonReader(), DecoderContext.builder().build()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testUnset() { |
| 91 | + assumeTrue(serverVersionAtLeast(4, 2)); |
| 92 | + insertAll("[\n" |
| 93 | + + " { _id: 1, title: 'Antelope Antics', author: { last:'An', first: 'Auntie' } },\n" |
| 94 | + + " { _id: 2, title: 'Bees Babble', author: { last:'Bumble', first: 'Bee' } }\n" |
| 95 | + + "]"); |
| 96 | + |
| 97 | + assertPipeline( |
| 98 | + "{ $unset: ['title', 'author.first'] }", |
| 99 | + unset("title", "author.first")); |
| 100 | + |
| 101 | + List<Bson> pipeline = assertPipeline( |
| 102 | + "{ $unset: 'author.first' }", |
| 103 | + unset("author.first")); |
| 104 | + |
| 105 | + assertResults(pipeline, "[\n" |
| 106 | + + " { _id: 1, title: 'Antelope Antics', author: { last:'An' } },\n" |
| 107 | + + " { _id: 2, title: 'Bees Babble', author: { last:'Bumble' } }\n" |
| 108 | + + "]"); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testGeoNear() { |
| 113 | + insertAll("[\n" |
| 114 | + + " {\n" |
| 115 | + + " _id: 1,\n" |
| 116 | + + " name: 'Central Park',\n" |
| 117 | + + " location: { type: 'Point', coordinates: [ -73.97, 40.77 ] },\n" |
| 118 | + + " category: 'Parks'\n" |
| 119 | + + " },\n" |
| 120 | + + " {\n" |
| 121 | + + " _id: 2,\n" |
| 122 | + + " name: 'Sara D. Roosevelt Park',\n" |
| 123 | + + " location: { type: 'Point', coordinates: [ -73.9928, 40.7193 ] },\n" |
| 124 | + + " category: 'Parks'\n" |
| 125 | + + " },\n" |
| 126 | + + " {\n" |
| 127 | + + " _id: 3,\n" |
| 128 | + + " name: 'Polo Grounds',\n" |
| 129 | + + " location: { type: 'Point', coordinates: [ -73.9375, 40.8303 ] },\n" |
| 130 | + + " category: 'Stadiums'\n" |
| 131 | + + " }\n" |
| 132 | + + "]"); |
| 133 | + getCollectionHelper().createIndex(BsonDocument.parse("{ location: '2dsphere' }")); |
| 134 | + |
| 135 | + List<Bson> pipeline = assertPipeline("{\n" |
| 136 | + + " $geoNear: {\n" |
| 137 | + + " near: { type: 'Point', coordinates: [ -73.99279 , 40.719296 ] },\n" |
| 138 | + + " distanceField: 'dist.calculated',\n" |
| 139 | + + " minDistance: 0,\n" |
| 140 | + + " maxDistance: 2,\n" |
| 141 | + + " query: { category: 'Parks' },\n" |
| 142 | + + " includeLocs: 'dist.location',\n" |
| 143 | + + " spherical: true,\n" |
| 144 | + + " key: 'location',\n" |
| 145 | + + " distanceMultiplier: 10.0\n" |
| 146 | + + " }\n" |
| 147 | + + "}", |
| 148 | + geoNear( |
| 149 | + new Point(new Position(-73.99279, 40.719296)), |
| 150 | + "dist.calculated", |
| 151 | + minDistance(0), |
| 152 | + maxDistance(2), |
| 153 | + query(new Document("category", "Parks")), |
| 154 | + includeLocs("dist.location"), |
| 155 | + spherical(), |
| 156 | + key("location"), |
| 157 | + distanceMultiplier(10.0) |
| 158 | + )); |
| 159 | + |
| 160 | + assertResults(pipeline, "" |
| 161 | + + "[{\n" |
| 162 | + + " '_id': 2,\n" |
| 163 | + + " 'name' : 'Sara D. Roosevelt Park',\n" |
| 164 | + + " 'category' : 'Parks',\n" |
| 165 | + + " 'location' : {\n" |
| 166 | + + " 'type' : 'Point',\n" |
| 167 | + + " 'coordinates' : [ -73.9928, 40.7193 ]\n" |
| 168 | + + " },\n" |
| 169 | + + " 'dist' : {\n" |
| 170 | + + " 'calculated' : 9.539931676365992,\n" |
| 171 | + + " 'location' : {\n" |
| 172 | + + " 'type' : 'Point',\n" |
| 173 | + + " 'coordinates' : [ -73.9928, 40.7193 ]\n" |
| 174 | + + " }\n" |
| 175 | + + " }\n" |
| 176 | + + "}]"); |
| 177 | + } |
| 178 | +} |
0 commit comments