Skip to content

Commit

Permalink
Renamed to updatePojo and validating that passed pojo is in fact a pojo
Browse files Browse the repository at this point in the history
  • Loading branch information
Marek Paterczyk committed Jan 20, 2017
1 parent 3c657a8 commit aa3931d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
11 changes: 9 additions & 2 deletions core/src/main/java/com/redhat/lightblue/client/Update.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
Expand Down Expand Up @@ -225,9 +226,15 @@ public static Update update(ContainerNode node) {
* @param pojo
* @return
*/
public static Update update(Object pojo) {
public static Update updatePojo(Object pojo) {
Objects.requireNonNull(pojo, "Pojo cannot be null");

ObjectNode set = JsonNodeFactory.instance.objectNode();
set.set("$set", JSON.toJsonNode(pojo));
JsonNode pojoAsJson = JSON.toJsonNode(pojo);
if (!(pojoAsJson instanceof ObjectNode)) {
throw new IllegalArgumentException(pojo+" is not a pojo!");
}
set.set("$set", pojoAsJson);
return new Update(set);
}

Expand Down
31 changes: 29 additions & 2 deletions core/src/test/java/com/redhat/lightblue/client/ExpressionTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.redhat.lightblue.client;

import java.util.Arrays;

import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
Expand Down Expand Up @@ -86,8 +88,33 @@ public int getI() {
}

@Test
public void updatePojoTest() throws Exception {
eq("{'$set':{'foo':'bar','i':13}}", Update.update(new Pojo()));
public void updatePojoValidTest() throws Exception {
eq("{'$set':{'foo':'bar','i':13}}", Update.updatePojo(new Pojo()));
}

@Test(expected=IllegalArgumentException.class)
public void updatePojoInvalidStringTest() throws Exception {
Update.updatePojo("foo");
}

@Test(expected=IllegalArgumentException.class)
public void updatePojoInvalidPrimitiveTest() throws Exception {
Update.updatePojo(3);
}

@Test(expected=IllegalArgumentException.class)
public void updatePojoInvalidArrayTest() throws Exception {
Update.updatePojo(new Pojo[] { new Pojo(), new Pojo()});
}

@Test(expected=IllegalArgumentException.class)
public void updatePojoInvalidListTest() throws Exception {
Update.updatePojo(Arrays.asList(new Pojo[] { new Pojo(), new Pojo()}));
}

@Test(expected=NullPointerException.class)
public void updatePojoInvalidNullTest() throws Exception {
Update.updatePojo(null);
}

@Test
Expand Down

0 comments on commit aa3931d

Please sign in to comment.