added QuerySpec and associated support classes; refactored UpdateSpec to implement Update interface

This commit is contained in:
Thomas Risberg
2011-02-02 12:51:01 -05:00
parent 0b2a3fae1d
commit b362d559cb
13 changed files with 737 additions and 27 deletions

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}

View File

@@ -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<String, Object> criteria = new LinkedHashMap<String, Object>();
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<Object> l = new ArrayList<Object>();
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<Query> 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;
}
}

View File

@@ -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<String, Integer> criteria = new HashMap<String, Integer>();
private Map<String, Object> slices = new HashMap<String, Object>();
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;
}
}

View File

@@ -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;
}
}

View File

@@ -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();
}

View File

@@ -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<String, Criteria> criteria = new LinkedHashMap<String, Criteria>();
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;
}
}

View File

@@ -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<String, SortOrder> criteria = new HashMap<String, SortOrder>();
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;
}
}

View File

@@ -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();
}

View File

@@ -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<String, Object> criteria = new LinkedHashMap<String, Object>();
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();
}
}

View File

@@ -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());
}
}

View File

@@ -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<String, Object> m = new HashMap<String, Object>();
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<String, Object> m2 = new HashMap<String, Object>();
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<String, Object> m = new HashMap<String, Object>();
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<String, Object> m = new HashMap<String, Object>();
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<String, Object> m2 = new HashMap<String, Object>();
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());
}
}