Skip to content

Commit

Permalink
Prepare implementation of $nearSphere and $geoWithin query operators #…
Browse files Browse the repository at this point in the history
  • Loading branch information
bwaldvogel committed Apr 19, 2020
1 parent c2a9e1e commit b14dc50
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import de.bwaldvogel.mongo.exception.FailedToParseException;
import de.bwaldvogel.mongo.exception.MongoServerError;
import de.bwaldvogel.mongo.exception.MongoServerException;
import de.bwaldvogel.mongo.exception.MongoServerNotYetImplementedException;

public class DefaultQueryMatcher implements QueryMatcher {

Expand Down Expand Up @@ -507,6 +508,10 @@ private boolean checkExpressionMatch(Object value, Object expressionValue, Strin
return matchTypes(value, expressionValue);
case ELEM_MATCH:
return checkMatchesElemValues(expressionValue, value);
case NEAR_SPHERE:
return checkNearSphere(expressionValue, value);
case GEO_WITHIN:
return checkGeoWithin(expressionValue, value);
default:
throw new IllegalArgumentException("unhandled query operator: " + queryOperator);
}
Expand Down Expand Up @@ -551,4 +556,15 @@ private boolean comparableTypes(Object value, Object expressionValue) {

return value.getClass().equals(expressionValue.getClass());
}

private boolean checkNearSphere(Object expressionValue, Object value) {
log.debug("Expression value: {}, value: {}", expressionValue, value);
throw new MongoServerNotYetImplementedException(132, QueryOperator.NEAR_SPHERE.getValue());
}

private boolean checkGeoWithin(Object expressionValue, Object value) {
log.debug("Expression value: {}, value: {}", expressionValue, value);
throw new MongoServerNotYetImplementedException(132, QueryOperator.GEO_WITHIN.getValue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public enum QueryOperator {
ALL("$all"),
ELEM_MATCH("$elemMatch"),
TYPE("$type"),
NEAR_SPHERE("$nearSphere"),
GEO_WITHIN("$geoWithin"),
;

private final String value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.bwaldvogel.mongo.exception;

public class MongoServerNotYetImplementedException extends MongoServerException {

private static final long serialVersionUID = 1L;

private static final String ISSUES_URL = "https://github.com/bwaldvogel/mongo-java-server/issues/";

public MongoServerNotYetImplementedException(int gitHubIssueNumber, String prefix) {
super(prefix + " is not yet implemented. See " + ISSUES_URL + gitHubIssueNumber);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import de.bwaldvogel.mongo.bson.ObjectId;
import de.bwaldvogel.mongo.exception.BadValueException;
import de.bwaldvogel.mongo.exception.MongoServerError;
import de.bwaldvogel.mongo.exception.MongoServerNotYetImplementedException;

class DefaultQueryMatcherTest {

Expand Down Expand Up @@ -943,4 +944,41 @@ void testMatchesJavaScript() throws Exception {
assertThat(matcher.matches(document, new Document("data", new BsonJavaScript("code 2")))).isFalse();
}

// https://github.com/bwaldvogel/mongo-java-server/issues/132
@Test
void testNearSphere() throws Exception {
assertThatExceptionOfType(MongoServerNotYetImplementedException.class)
.isThrownBy(() -> matcher.matches(json("location: [0, 0]"), json("location: " +
"{" +
" $nearSphere: {" +
" $geometry: {" +
" type : 'Point'," +
" coordinates : [ -73.9667, 40.78 ]" +
" },\n" +
" $minDistance: 1000,\n" +
" $maxDistance: 5000\n" +
" }" +
"}")))
.withMessage("$nearSphere is not yet implemented. See https://github.com/bwaldvogel/mongo-java-server/issues/132");
}

// https://github.com/bwaldvogel/mongo-java-server/issues/132
@Test
void testGeoWithin() throws Exception {
assertThatExceptionOfType(MongoServerNotYetImplementedException.class)
.isThrownBy(() -> matcher.matches(json("location: [0, 0]"), json("location: {" +
" $geoWithin: {" +
" $geometry: {" +
" type: 'Polygon'," +
" coordinates: [[[0, 0], [3, 6], [6, 1], [0, 0]]]," +
" crs: {" +
" type: 'name'," +
" properties: { name: 'urn:x-mongodb:crs:strictwinding:EPSG:4326' }" +
" }" +
" }" +
" }" +
"}")))
.withMessage("$geoWithin is not yet implemented. See https://github.com/bwaldvogel/mongo-java-server/issues/132");
}

}

0 comments on commit b14dc50

Please sign in to comment.