diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicQuery.java new file mode 100644 index 000000000..f27b7f141 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicQuery.java @@ -0,0 +1,65 @@ +/* + * Copyright 2010-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb.builder; + +import com.mongodb.DBObject; +import com.mongodb.util.JSON; + +public class BasicQuery implements Query { + + private DBObject queryObject = null; + + private DBObject fieldsObject = null; + + private int limit; + + public BasicQuery(String query) { + super(); + this.queryObject = (DBObject) JSON.parse(query); + } + + public BasicQuery(DBObject queryObject) { + super(); + this.queryObject = queryObject; + } + + public BasicQuery(String query, String fields) { + this.queryObject = (DBObject) JSON.parse(query); + this.fieldsObject = (DBObject) JSON.parse(fields); + } + + public BasicQuery(DBObject queryObject, DBObject fieldsObject) { + this.queryObject = queryObject; + this.fieldsObject = fieldsObject; + } + + public DBObject getQueryObject() { + return queryObject; + } + + public DBObject getFieldsObject() { + return fieldsObject; + } + + public int getLimit() { + return this.limit; + } + + public void setLimit(int limit) { + this.limit = limit; + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicUpdate.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicUpdate.java new file mode 100644 index 000000000..d5767c6b8 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/BasicUpdate.java @@ -0,0 +1,57 @@ +/* + * Copyright 2010-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb.builder; + +import com.mongodb.DBObject; +import com.mongodb.util.JSON; + +public class BasicUpdate implements Update { + + private DBObject updateObject = null; + + private DBObject sortObject; + + public BasicUpdate(String updateString) { + super(); + this.updateObject = (DBObject) JSON.parse(updateString); + } + + public BasicUpdate(DBObject updateObject) { + super(); + this.updateObject = updateObject; + } + + public BasicUpdate(String updateString, String sortString) { + super(); + this.updateObject = (DBObject) JSON.parse(updateString); + this.sortObject = (DBObject) JSON.parse(sortString); + } + + public BasicUpdate(DBObject updateObject, DBObject sortObject) { + super(); + this.updateObject = updateObject; + this.sortObject = sortObject; + } + + public DBObject getUpdateObject() { + return updateObject; + } + + public DBObject getSortObject() { + return this.sortObject; + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Criteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Criteria.java new file mode 100644 index 000000000..90720858f --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Criteria.java @@ -0,0 +1,24 @@ +/* + * Copyright 2010-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb.builder; + +import com.mongodb.DBObject; + +public interface Criteria { + + DBObject getCriteriaObject(String key); + +} \ No newline at end of file diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/CriteriaSpec.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/CriteriaSpec.java new file mode 100644 index 000000000..e122f2400 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/CriteriaSpec.java @@ -0,0 +1,161 @@ +/* + * Copyright 2010-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb.builder; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + +import org.springframework.data.document.InvalidDocumentStoreApiUsageException; + +import com.mongodb.BasicDBObject; +import com.mongodb.DBObject; + +public class CriteriaSpec implements Criteria { + + private QuerySpec qb = null; + + private LinkedHashMap criteria = new LinkedHashMap(); + + private Object isValue = null; + + + public CriteriaSpec(QuerySpec qb) { + super(); + this.qb = qb; + } + + + public CriteriaSpec and(String key) { + return qb.find(key); + } + + + public CriteriaSpec is(Object o) { + if (isValue != null) { + throw new InvalidDocumentStoreApiUsageException("Multiple 'is' values declared."); + } + this.isValue = o; + return this; + } + + public CriteriaSpec lt(Object o) { + criteria.put("$lt", o); + return this; + } + + public CriteriaSpec lte(Object o) { + criteria.put("$lte", o); + return this; + } + + public CriteriaSpec gt(Object o) { + criteria.put("$gt", o); + return this; + } + + public CriteriaSpec gte(Object o) { + criteria.put("$gte", o); + return this; + } + + public CriteriaSpec in(Object... o) { + criteria.put("$in", o); + return this; + } + + public CriteriaSpec nin(Object... o) { + criteria.put("$min", o); + return this; + } + + public CriteriaSpec mod(Number value, Number remainder) { + List l = new ArrayList(); + l.add(value); + l.add(remainder); + criteria.put("$mod", l); + return this; + } + + public CriteriaSpec all(Object o) { + criteria.put("$is", o); + return this; + } + + public CriteriaSpec size(Object o) { + criteria.put("$is", o); + return this; + } + + public CriteriaSpec exists(boolean b) { + return this; + } + + public CriteriaSpec type(int t) { + return this; + } + + public CriteriaSpec not() { + criteria.put("$not", null); + return this; + } + + public CriteriaSpec regExp(String re) { + return this; + } + + public void or(List queries) { + criteria.put("$or", queries); + } + + public Query build() { + return qb.build(); + } + + /* (non-Javadoc) + * @see org.springframework.datastore.document.mongodb.query.Criteria#getCriteriaObject(java.lang.String) + */ + public DBObject getCriteriaObject(String key) { + DBObject dbo = new BasicDBObject(); + boolean not = false; + for (String k : criteria.keySet()) { + if (not) { + DBObject notDbo = new BasicDBObject(); + notDbo.put(k, criteria.get(k)); + dbo.put("$not", notDbo); + not = false; + } + else { + if ("$not".equals(k)) { + not = true; + } + else { + dbo.put(k, criteria.get(k)); + } + } + } + DBObject queryCriteria = new BasicDBObject(); + if (isValue != null) { + queryCriteria.put(key, isValue); + queryCriteria.putAll(dbo); + } + else { + queryCriteria.put(key, dbo); + } + return queryCriteria; + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/FieldSpec.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/FieldSpec.java new file mode 100644 index 000000000..db0b3572a --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/FieldSpec.java @@ -0,0 +1,61 @@ +/* + * Copyright 2010-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb.builder; + +import java.util.HashMap; +import java.util.Map; + +import com.mongodb.BasicDBObject; +import com.mongodb.DBObject; + + +public class FieldSpec { + + private Map criteria = new HashMap(); + + private Map slices = new HashMap(); + + public FieldSpec include(String key) { + criteria.put(key, Integer.valueOf(1)); + return this; + } + + public FieldSpec exclude(String key) { + criteria.put(key, Integer.valueOf(0)); + return this; + } + + public FieldSpec slice(String key, int size) { + slices.put(key, Integer.valueOf(size)); + return this; + } + + public FieldSpec slice(String key, int offset, int size) { + slices.put(key, new Integer[] {Integer.valueOf(offset), Integer.valueOf(size)}); + return this; + } + + public DBObject getFieldsObject() { + DBObject dbo = new BasicDBObject(); + for (String k : criteria.keySet()) { + dbo.put(k, (criteria.get(k))); + } + for (String k : slices.keySet()) { + dbo.put(k, new BasicDBObject("$slice" ,(slices.get(k)))); + } + return dbo; + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/OrCriteriaSpec.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/OrCriteriaSpec.java new file mode 100644 index 000000000..281c482c4 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/OrCriteriaSpec.java @@ -0,0 +1,46 @@ +/* + * Copyright 2010-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb.builder; + +import org.bson.types.BasicBSONList; + +import com.mongodb.BasicDBObject; +import com.mongodb.DBObject; + +public class OrCriteriaSpec implements Criteria { + + Query[] queries = null; + + public OrCriteriaSpec(Query[] queries) { + super(); + this.queries = queries; + } + + + /* (non-Javadoc) + * @see org.springframework.datastore.document.mongodb.query.Criteria#getCriteriaObject(java.lang.String) + */ + public DBObject getCriteriaObject(String key) { + DBObject dbo = new BasicDBObject(); + BasicBSONList l = new BasicBSONList(); + for (Query q : queries) { + l.add(q.getQueryObject()); + } + dbo.put(key, l); + return dbo; + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Query.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Query.java new file mode 100644 index 000000000..f383f0b10 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Query.java @@ -0,0 +1,28 @@ +/* + * Copyright 2010-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb.builder; + +import com.mongodb.DBObject; + + +public interface Query { + + DBObject getQueryObject(); + + DBObject getFieldsObject(); + + int getLimit(); +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/QuerySpec.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/QuerySpec.java new file mode 100644 index 000000000..07e60e9b0 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/QuerySpec.java @@ -0,0 +1,81 @@ +/* + * Copyright 2010-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb.builder; + +import java.util.LinkedHashMap; + +import com.mongodb.BasicDBObject; +import com.mongodb.DBObject; + +public class QuerySpec implements Query { + + private LinkedHashMap criteria = new LinkedHashMap(); + + private FieldSpec fieldSpec; + + private int limit; + + public CriteriaSpec find(String key) { + CriteriaSpec c = new CriteriaSpec(this); + this.criteria.put(key, c); + return c; + } + + public QuerySpec or(Query... queries) { + this.criteria.put("$or", new OrCriteriaSpec(queries)); + return this; + } + + public FieldSpec fields() { + synchronized (this) { + if (fieldSpec == null) { + this.fieldSpec = new FieldSpec(); + } + } + return this.fieldSpec; + } + + public QuerySpec limit(int limit) { + this.limit = limit; + return this; + } + + public Query build() { + return this; + } + + public DBObject getQueryObject() { + DBObject dbo = new BasicDBObject(); + for (String k : criteria.keySet()) { + Criteria c = criteria.get(k); + DBObject cl = c.getCriteriaObject(k); + dbo.putAll(cl); + } + return dbo; + } + + public DBObject getFieldsObject() { + if (this.fieldSpec == null) { + return null; + } + return fieldSpec.getFieldsObject(); + } + + public int getLimit() { + return this.limit; + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/SortSpec.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/SortSpec.java new file mode 100644 index 000000000..4d49faff7 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/SortSpec.java @@ -0,0 +1,46 @@ +/* + * Copyright 2010-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb.builder; + +import java.util.HashMap; +import java.util.Map; + +import com.mongodb.BasicDBObject; +import com.mongodb.DBObject; + + +public class SortSpec { + + public enum SortOrder { + ASCENDING, DESCENDING + } + + private Map criteria = new HashMap(); + + public SortSpec on(String key, SortOrder order) { + criteria.put(key, order); + return this; + } + + public DBObject getSortObject() { + DBObject dbo = new BasicDBObject(); + for (String k : criteria.keySet()) { + dbo.put(k, (criteria.get(k).equals(SortOrder.ASCENDING) ? 1 : -1)); + } + return dbo; + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Update.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Update.java new file mode 100644 index 000000000..5a3b0e787 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/Update.java @@ -0,0 +1,26 @@ +/* + * Copyright 2010-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb.builder; + +import com.mongodb.DBObject; + + +public interface Update { + + DBObject getUpdateObject(); + + DBObject getSortObject(); +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/UpdateSpec.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/UpdateSpec.java index b81459447..00804bc64 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/UpdateSpec.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/builder/UpdateSpec.java @@ -22,13 +22,15 @@ import java.util.LinkedHashMap; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; -public class UpdateSpec { - +public class UpdateSpec implements Update { + public enum Position { LAST, FIRST } private HashMap criteria = new LinkedHashMap(); + + private SortSpec sortSpec; public UpdateSpec set(String key, Object value) { criteria.put("$set", Collections.singletonMap(key, value)); @@ -84,7 +86,20 @@ public class UpdateSpec { return this; } - public DBObject build() { + public SortSpec sort() { + synchronized (this) { + if (this.sortSpec == null) { + this.sortSpec = new SortSpec(); + } + } + return this.sortSpec; + } + + public Update build() { + return this; + } + + public DBObject getUpdateObject() { DBObject dbo = new BasicDBObject(); for (String k : criteria.keySet()) { dbo.put(k, criteria.get(k)); @@ -92,4 +107,11 @@ public class UpdateSpec { return dbo; } + public DBObject getSortObject() { + if (this.sortSpec == null) { + return null; + } + return this.sortSpec.getSortObject(); + } + } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/QueryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/QueryTests.java new file mode 100644 index 000000000..5012a946a --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/QueryTests.java @@ -0,0 +1,74 @@ +/* + * Copyright 2010-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.document.mongodb.builder; + +import org.junit.Assert; +import org.junit.Test; + +public class QueryTests { + + @Test + public void testSimpleQuery() { + QuerySpec q = new QuerySpec(); + q.find("name").is("Thomas"); + q.find("age").lt(80); + String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$lt\" : 80}}"; + Assert.assertEquals(expected, q.build().getQueryObject().toString()); + } + + @Test + public void testQueryWithNot() { + QuerySpec q = new QuerySpec(); + q.find("name").is("Thomas"); + q.find("age").not().mod(10, 0); + String expected = "{ \"name\" : \"Thomas\" , \"age\" : { \"$not\" : { \"$mod\" : [ 10 , 0]}}}"; + Assert.assertEquals(expected, q.build().getQueryObject().toString()); + } + + @Test + public void testOrQuery() { + QuerySpec q = new QuerySpec();; + q.or( + new QuerySpec().find("name").is("Sven").and("age").lt(50).build(), + new QuerySpec().find("age").lt(50).build(), + new BasicQuery("{'name' : 'Thomas'}") + ); + String expected = "{ \"$or\" : [ { \"name\" : \"Sven\" , \"age\" : { \"$lt\" : 50}} , { \"age\" : { \"$lt\" : 50}} , { \"name\" : \"Thomas\"}]}"; + Assert.assertEquals(expected, q.build().getQueryObject().toString()); + } + + @Test + public void testQueryWithLimit() { + QuerySpec q = new QuerySpec(); + q.find("name").gte("M").lte("T").and("age").not().gt(22); + q.limit(50); + String expected = "{ \"name\" : { \"$gte\" : \"M\" , \"$lte\" : \"T\"} , \"age\" : { \"$not\" : { \"$gt\" : 22}}}"; + Assert.assertEquals(expected, q.build().getQueryObject().toString()); + Assert.assertEquals(50, q.build().getLimit()); + } + + @Test + public void testQueryWithFieldsAndSlice() { + QuerySpec q = new QuerySpec(); + q.find("name").gte("M").lte("T").and("age").not().gt(22); + q.fields().exclude("address").include("name").slice("orders", 10); + + String expected = "{ \"name\" : { \"$gte\" : \"M\" , \"$lte\" : \"T\"} , \"age\" : { \"$not\" : { \"$gt\" : 22}}}"; + Assert.assertEquals(expected, q.build().getQueryObject().toString()); + String expectedFields = "{ \"address\" : 0 , \"name\" : 1 , \"orders\" : { \"$slice\" : 10}}"; + Assert.assertEquals(expectedFields, q.build().getFieldsObject().toString()); + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/UpdateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/UpdateTests.java index 12c5d9934..f7bfb6c3a 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/UpdateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/builder/UpdateTests.java @@ -20,47 +20,48 @@ import java.util.Map; import org.junit.Assert; import org.junit.Test; +import org.springframework.data.document.mongodb.builder.SortSpec.SortOrder; import org.springframework.data.document.mongodb.builder.UpdateSpec; public class UpdateTests { @Test public void testSet() { - UpdateSpec ub = new UpdateSpec() + UpdateSpec u = new UpdateSpec() .set("directory", "/Users/Test/Desktop"); - Assert.assertEquals("{ \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", ub.build().toString()); + Assert.assertEquals("{ \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", u.build().getUpdateObject().toString()); } @Test public void testInc() { - UpdateSpec ub = new UpdateSpec() + UpdateSpec u = new UpdateSpec() .inc("size", 1); - Assert.assertEquals("{ \"$inc\" : { \"size\" : 1}}", ub.build().toString()); + Assert.assertEquals("{ \"$inc\" : { \"size\" : 1}}", u.build().getUpdateObject().toString()); } @Test public void testIncAndSet() { - UpdateSpec ub = new UpdateSpec() + UpdateSpec u = new UpdateSpec() .inc("size", 1) .set("directory", "/Users/Test/Desktop"); Assert.assertEquals("{ \"$inc\" : { \"size\" : 1} , \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", - ub.build().toString()); + u.build().getUpdateObject().toString()); } @Test public void testUnset() { - UpdateSpec ub = new UpdateSpec() + UpdateSpec u = new UpdateSpec() .unset("directory"); - Assert.assertEquals("{ \"$unset\" : { \"directory\" : 1}}", ub.build().toString()); + Assert.assertEquals("{ \"$unset\" : { \"directory\" : 1}}", u.build().getUpdateObject().toString()); } @Test public void testPush() { Map m = new HashMap(); m.put("name", "Sven"); - UpdateSpec ub = new UpdateSpec() + UpdateSpec u = new UpdateSpec() .push("authors", m); - Assert.assertEquals("{ \"$push\" : { \"authors\" : { \"name\" : \"Sven\"}}}", ub.build().toString()); + Assert.assertEquals("{ \"$push\" : { \"authors\" : { \"name\" : \"Sven\"}}}", u.build().getUpdateObject().toString()); } @Test @@ -69,37 +70,37 @@ public class UpdateTests { m1.put("name", "Sven"); Map m2 = new HashMap(); m2.put("name", "Maria"); - UpdateSpec ub = new UpdateSpec() + UpdateSpec u = new UpdateSpec() .pushAll("authors", new Object[] {m1, m2}); - Assert.assertEquals("{ \"$pushAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}", ub.build().toString()); + Assert.assertEquals("{ \"$pushAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}", u.build().getUpdateObject().toString()); } @Test public void testAddToSet() { Map m = new HashMap(); m.put("name", "Sven"); - UpdateSpec ub = new UpdateSpec() + UpdateSpec u = new UpdateSpec() .addToSet("authors", m); - Assert.assertEquals("{ \"$addToSet\" : { \"authors\" : { \"name\" : \"Sven\"}}}", ub.build().toString()); + Assert.assertEquals("{ \"$addToSet\" : { \"authors\" : { \"name\" : \"Sven\"}}}", u.build().getUpdateObject().toString()); } @Test public void testPop() { - UpdateSpec ub = new UpdateSpec() + UpdateSpec u = new UpdateSpec() .pop("authors", UpdateSpec.Position.FIRST); - Assert.assertEquals("{ \"$pop\" : { \"authors\" : -1}}", ub.build().toString()); - ub = new UpdateSpec() + Assert.assertEquals("{ \"$pop\" : { \"authors\" : -1}}", u.build().getUpdateObject().toString()); + u = new UpdateSpec() .pop("authors", UpdateSpec.Position.LAST); - Assert.assertEquals("{ \"$pop\" : { \"authors\" : 1}}", ub.build().toString()); + Assert.assertEquals("{ \"$pop\" : { \"authors\" : 1}}", u.build().getUpdateObject().toString()); } @Test public void testPull() { Map m = new HashMap(); m.put("name", "Sven"); - UpdateSpec ub = new UpdateSpec() + UpdateSpec u = new UpdateSpec() .pull("authors", m); - Assert.assertEquals("{ \"$pull\" : { \"authors\" : { \"name\" : \"Sven\"}}}", ub.build().toString()); + Assert.assertEquals("{ \"$pull\" : { \"authors\" : { \"name\" : \"Sven\"}}}", u.build().getUpdateObject().toString()); } @Test @@ -108,15 +109,33 @@ public class UpdateTests { m1.put("name", "Sven"); Map m2 = new HashMap(); m2.put("name", "Maria"); - UpdateSpec ub = new UpdateSpec() + UpdateSpec u = new UpdateSpec() .pullAll("authors", new Object[] {m1, m2}); - Assert.assertEquals("{ \"$pullAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}", ub.build().toString()); + Assert.assertEquals("{ \"$pullAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}", u.build().getUpdateObject().toString()); } @Test public void testRename() { - UpdateSpec ub = new UpdateSpec() + UpdateSpec u = new UpdateSpec() .rename("directory", "folder"); - Assert.assertEquals("{ \"$rename\" : { \"directory\" : \"folder\"}}", ub.build().toString()); + Assert.assertEquals("{ \"$rename\" : { \"directory\" : \"folder\"}}", u.build().getUpdateObject().toString()); + } + + @Test + public void testWithSortAscending() { + UpdateSpec u = new UpdateSpec(); + u.set("name",52); + u.sort().on("name", SortOrder.ASCENDING); + Assert.assertEquals("{ \"$set\" : { \"name\" : 52}}", u.build().getUpdateObject().toString()); + Assert.assertEquals("{ \"name\" : 1}", u.build().getSortObject().toString()); + } + + @Test + public void testWithSortDescending() { + UpdateSpec u = new UpdateSpec(); + u.set("name",52); + u.sort().on("name", SortOrder.DESCENDING); + Assert.assertEquals("{ \"$set\" : { \"name\" : 52}}", u.build().getUpdateObject().toString()); + Assert.assertEquals("{ \"name\" : -1}", u.build().getSortObject().toString()); } }