completed the set of modifier operations for UpdateBuilder
This commit is contained in:
@@ -23,6 +23,10 @@ import com.mongodb.DBObject;
|
||||
|
||||
public class UpdateBuilder {
|
||||
|
||||
public enum Position {
|
||||
LAST, FIRST
|
||||
}
|
||||
|
||||
private LinkedHashMap<String, Object> criteria = new LinkedHashMap<String, Object>();
|
||||
|
||||
public UpdateBuilder set(String key, Object value) {
|
||||
@@ -40,6 +44,45 @@ public class UpdateBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder push(String key, Object value) {
|
||||
criteria.put("$push", Collections.singletonMap(key, value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder pushAll(String key, Object[] values) {
|
||||
DBObject keyValue = new BasicDBObject();
|
||||
keyValue.put(key, values);
|
||||
criteria.put("$pushAll", keyValue);
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder addToSet(String key, Object value) {
|
||||
criteria.put("$addToSet", Collections.singletonMap(key, value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder pop(String key, Position pos) {
|
||||
criteria.put("$pop", Collections.singletonMap(key, (pos == Position.FIRST ? -1 : 1)));
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder pull(String key, Object value) {
|
||||
criteria.put("$pull", Collections.singletonMap(key, value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder pullAll(String key, Object[] values) {
|
||||
DBObject keyValue = new BasicDBObject();
|
||||
keyValue.put(key, values);
|
||||
criteria.put("$pullAll", keyValue);
|
||||
return this;
|
||||
}
|
||||
|
||||
public UpdateBuilder rename(String oldName, String newName) {
|
||||
criteria.put("$rename", Collections.singletonMap(oldName, newName));
|
||||
return this;
|
||||
}
|
||||
|
||||
public DBObject build() {
|
||||
DBObject dbo = new BasicDBObject();
|
||||
for (String k : criteria.keySet()) {
|
||||
|
||||
@@ -15,27 +15,30 @@
|
||||
*/
|
||||
package org.springframework.data.document.mongodb;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UpdateBuilderTests {
|
||||
|
||||
@Test
|
||||
public void testSetUpdate() {
|
||||
public void testSet() {
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
.set("directory", "/Users/Test/Desktop");
|
||||
Assert.assertEquals("{ \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", ub.build().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncUpdate() {
|
||||
public void testInc() {
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
.inc("size", 1);
|
||||
Assert.assertEquals("{ \"$inc\" : { \"size\" : 1}}", ub.build().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncAndSetUpdate() {
|
||||
public void testIncAndSet() {
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
.inc("size", 1)
|
||||
.set("directory", "/Users/Test/Desktop");
|
||||
@@ -50,4 +53,69 @@ public class UpdateBuilderTests {
|
||||
Assert.assertEquals("{ \"$unset\" : { \"directory\" : 1}}", ub.build().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPush() {
|
||||
Map<String, Object> m = new HashMap<String, Object>();
|
||||
m.put("name", "Sven");
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
.push("authors", m);
|
||||
Assert.assertEquals("{ \"$push\" : { \"authors\" : { \"name\" : \"Sven\"}}}", ub.build().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPushAll() {
|
||||
Map<String, Object> m1 = new HashMap<String, Object>();
|
||||
m1.put("name", "Sven");
|
||||
Map<String, Object> m2 = new HashMap<String, Object>();
|
||||
m2.put("name", "Maria");
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
.pushAll("authors", new Object[] {m1, m2});
|
||||
Assert.assertEquals("{ \"$pushAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}", ub.build().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddToSet() {
|
||||
Map<String, Object> m = new HashMap<String, Object>();
|
||||
m.put("name", "Sven");
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
.addToSet("authors", m);
|
||||
Assert.assertEquals("{ \"$addToSet\" : { \"authors\" : { \"name\" : \"Sven\"}}}", ub.build().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPop() {
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
.pop("authors", UpdateBuilder.Position.FIRST);
|
||||
Assert.assertEquals("{ \"$pop\" : { \"authors\" : -1}}", ub.build().toString());
|
||||
ub = new UpdateBuilder()
|
||||
.pop("authors", UpdateBuilder.Position.LAST);
|
||||
Assert.assertEquals("{ \"$pop\" : { \"authors\" : 1}}", ub.build().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPull() {
|
||||
Map<String, Object> m = new HashMap<String, Object>();
|
||||
m.put("name", "Sven");
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
.pull("authors", m);
|
||||
Assert.assertEquals("{ \"$pull\" : { \"authors\" : { \"name\" : \"Sven\"}}}", ub.build().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPullAll() {
|
||||
Map<String, Object> m1 = new HashMap<String, Object>();
|
||||
m1.put("name", "Sven");
|
||||
Map<String, Object> m2 = new HashMap<String, Object>();
|
||||
m2.put("name", "Maria");
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
.pullAll("authors", new Object[] {m1, m2});
|
||||
Assert.assertEquals("{ \"$pullAll\" : { \"authors\" : [ { \"name\" : \"Sven\"} , { \"name\" : \"Maria\"}]}}", ub.build().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRename() {
|
||||
UpdateBuilder ub = new UpdateBuilder()
|
||||
.rename("directory", "folder");
|
||||
Assert.assertEquals("{ \"$rename\" : { \"directory\" : \"folder\"}}", ub.build().toString());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user