Skip to content

Commit

Permalink
fixes #542
Browse files Browse the repository at this point in the history
  • Loading branch information
eliantor committed Nov 17, 2014
1 parent 7d0f209 commit ee246e9
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 21 deletions.
7 changes: 5 additions & 2 deletions app/com/baasbox/commands/ScriptsResource.java
Expand Up @@ -26,6 +26,7 @@
import com.baasbox.service.scripting.base.JsonCallback;
import com.baasbox.service.scripting.js.Json;
import com.baasbox.service.events.EventsService;
import com.baasbox.util.JSONFormats;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.IntNode;
import com.fasterxml.jackson.databind.node.NullNode;
Expand Down Expand Up @@ -118,7 +119,7 @@ private static JsonNode storageCommand(JsonNode command, JsonCallback callback)
if (args==null){
args = NullNode.getInstance();
}
if (!args.isObject()||!args.isNull()){
if (!args.isObject() && !args.isNull()){
throw new CommandExecutionException(command,"Stored values should be objects or null");
}
try {
Expand Down Expand Up @@ -148,7 +149,9 @@ private static JsonNode storageCommand(JsonNode command, JsonCallback callback)
} else {
String s = result.toJSON();
try {
JsonNode jsonNode = Json.mapper().readTree(s);
ObjectNode jsonNode =(ObjectNode) Json.mapper().readTree(s);
jsonNode.remove("@version");
jsonNode.remove("@type");
return jsonNode;
} catch (IOException e) {
throw new CommandExecutionException(command,"error converting result",e);
Expand Down
22 changes: 16 additions & 6 deletions app/com/baasbox/service/scripting/ScriptingService.java
Expand Up @@ -70,10 +70,16 @@ public static ODocument resetStore(String name,JsonNode data) throws ScriptExcep
ScriptsDao dao = ScriptsDao.getInstance();
ODocument script = dao.getByName(name);
if (script == null) throw new ScriptException("Script not found");
ODocument emdedded = new ODocument().fromJSON(data.toString());
script.field(ScriptsDao.LOCAL_STORAGE,emdedded);
ODocument embedded;
if (data==null||data.isNull()){
embedded = null;
script.removeField(ScriptsDao.LOCAL_STORAGE);
} else {
embedded = new ODocument().fromJSON(data.toString());
script.field(ScriptsDao.LOCAL_STORAGE,embedded);
}
dao.save(script);
return emdedded;
return embedded;
}

public static ODocument getStore(String name) throws ScriptException {
Expand Down Expand Up @@ -101,9 +107,13 @@ private static ODocument updateStorageLocked(String name,boolean before,JsonCall
if (current.isMissingNode()) throw new ScriptEvalException("Error reading local storage as json");

JsonNode updated = updaterFn.call(current);

ODocument result = new ODocument().fromJSON(updated.toString());
script.field(ScriptsDao.LOCAL_STORAGE, result);
ODocument result;
if (updated ==null||updated.isNull()){
script.removeField(ScriptsDao.LOCAL_STORAGE);
} else {
result = new ODocument().fromJSON(updated.toString());
script.field(ScriptsDao.LOCAL_STORAGE, result);
}
dao.save(script);
ODocument field = retScript.field(ScriptsDao.LOCAL_STORAGE);
return field;
Expand Down
5 changes: 3 additions & 2 deletions app/com/baasbox/util/JSONFormats.java
Expand Up @@ -36,8 +36,9 @@ public enum Formats{
LINK("fetchPlan:*:0 out.data:1 in.data:1,version,class,attribSameRow,alwaysFetchEmbedded,indent:0"),
FILE("fetchPlan:resized:-2 audit:0 _links:0 _allow:0 _allowread:0 _allowwrite:0 _allowUpdate:0 _allowDelete:0 file:-2 metadata:1 text_content:-2,version,attribSameRow,indent:0"),
ROLES("indent:0,fetchPlan:rules:-2 inheritedRole:-2"),
DOCUMENT_PUBLIC("fetchPlan:_audit:-2 _links:-2 _allow:0 _allowread:0 _allowwrite:0 _allowUpdate:0 _allowDelete:0,rid,version,class,attribSameRow,alwaysFetchEmbedded,indent:0"),
;
DOCUMENT_PUBLIC("fetchPlan:_audit:-2 _links:-2 _allow:0 _allowread:0 _allowwrite:0 _allowUpdate:0 _allowDelete:0,rid,version,class,attribSameRow,alwaysFetchEmbedded,indent:0")
//,STORE("fetchPlan:")
;



Expand Down
75 changes: 64 additions & 11 deletions test/ScriptStorageTest.java
@@ -1,17 +1,15 @@
import com.baasbox.db.DbHelper;
import com.baasbox.service.scripting.js.Json;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;
import core.TestConfig;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.junit.BeforeClass;
import org.junit.Test;
import play.mvc.Result;
import play.test.FakeRequest;

import java.time.Instant;
import java.time.Year;
import java.util.Date;
import java.util.UUID;

import static play.test.Helpers.*;
import static org.junit.Assert.*;
Expand All @@ -20,19 +18,21 @@
* Created by eto on 08/10/14.
*/
public class ScriptStorageTest {
private static final String SCRIPT = "test.storage_"+ ScriptTestHelpers.randomScriptName();
private static final String SCRIPT_STORAGE = "test.storage_"+ ScriptTestHelpers.randomScriptName();
private static final String SERIALIZE = "test.serialize_"+ScriptTestHelpers.randomScriptName();
private static final String CALLWS = "test.callws_"+ScriptTestHelpers.randomScriptName();
private static final String SCRIPT_STORAGE_GET_SET = "test.storage_set_get_"+ScriptTestHelpers.randomScriptName();


@BeforeClass
public static void init(){
running(fakeApplication(),()-> {
try {
DbHelper.open("1234567890", "admin", "admin");
ScriptTestHelpers.createScript(SCRIPT, "scripts/local_storage_test.js");
ScriptTestHelpers.createScript(SCRIPT_STORAGE, "scripts/local_storage_test.js");
ScriptTestHelpers.createScript(SERIALIZE,"scripts/serialize_test.js");
ScriptTestHelpers.createScript(CALLWS,"scripts/call_external.js");
ScriptTestHelpers.createScript(SCRIPT_STORAGE_GET_SET,"scripts/local_storage_set_get_test.js");
} catch (Exception e) {
fail(ExceptionUtils.getStackTrace(e));
} finally {
Expand Down Expand Up @@ -82,13 +82,14 @@ public void serializationTest(){
}
});
}

@Test
public void testCanStoreValues(){
public void testCanSwapValues(){
running(fakeApplication(),()->{
try {
FakeRequest req = new FakeRequest(POST,"/plugin/"+SCRIPT);
FakeRequest req = new FakeRequest(POST,"/plugin/"+ SCRIPT_STORAGE);
req = req.withHeader(TestConfig.KEY_APPCODE,TestConfig.VALUE_APPCODE)
.withHeader(TestConfig.KEY_AUTH,TestConfig.AUTH_ADMIN_ENC)
.withHeader(TestConfig.KEY_AUTH,TestConfig.AUTH_ADMIN_ENC)
.withHeader("Content-Type","application/json")
.withJsonBody(Json.mapper().createObjectNode());

Expand All @@ -98,7 +99,7 @@ public void testCanStoreValues(){

assertEquals(0,resp.path("data").path("val").asInt());

req = new FakeRequest(POST,"/plugin/"+SCRIPT);
req = new FakeRequest(POST,"/plugin/"+ SCRIPT_STORAGE);
req = req.withHeader(TestConfig.KEY_APPCODE,TestConfig.VALUE_APPCODE)
.withHeader(TestConfig.KEY_AUTH,TestConfig.AUTH_ADMIN_ENC)
.withHeader("Content-Type","application/json")
Expand All @@ -110,7 +111,7 @@ public void testCanStoreValues(){

assertEquals(1,resp.path("data").path("val").asInt());

req = new FakeRequest(GET,"/plugin/"+SCRIPT);
req = new FakeRequest(GET,"/plugin/"+ SCRIPT_STORAGE);
req = req.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE)
.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);

Expand All @@ -122,7 +123,7 @@ public void testCanStoreValues(){

assertEquals(1,resp.path("data").path("val").asInt());

req = new FakeRequest(GET,"/admin/plugin/"+SCRIPT);
req = new FakeRequest(GET,"/admin/plugin/"+ SCRIPT_STORAGE);
req = req.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE)
.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);

Expand All @@ -138,4 +139,56 @@ public void testCanStoreValues(){
});
}

@Test
public void testCanStoreValues(){
running(fakeApplication(),()->{
try {
FakeRequest req = new FakeRequest(POST,"/plugin/"+ SCRIPT_STORAGE_GET_SET);
req = req.withHeader(TestConfig.KEY_APPCODE,TestConfig.VALUE_APPCODE)
.withHeader(TestConfig.KEY_AUTH,TestConfig.AUTH_ADMIN_ENC)
.withHeader("Content-Type","application/json")
.withJsonBody(Json.mapper().createObjectNode().put("store","store"));

Result res = routeAndCall(req);
String s = contentAsString(res);
JsonNode resp =Json.mapper().readTree(s);

assertEquals(Json.mapper().createObjectNode().put("store","store"),resp.path("data").path("storage"));

req = new FakeRequest(GET,"/plugin/"+SCRIPT_STORAGE_GET_SET);
req.withHeader(TestConfig.KEY_APPCODE,TestConfig.VALUE_APPCODE)
.withHeader(TestConfig.KEY_AUTH,TestConfig.AUTH_ADMIN_ENC);

res = routeAndCall(req);
s = contentAsString(res);
resp = Json.mapper().readTree(s);

assertEquals(Json.mapper().createObjectNode().put("store","store"),resp.path("data").path("storage"));

req = new FakeRequest(POST,"/plugin/"+ SCRIPT_STORAGE_GET_SET);
req = req.withHeader(TestConfig.KEY_APPCODE,TestConfig.VALUE_APPCODE)
.withHeader(TestConfig.KEY_AUTH,TestConfig.AUTH_ADMIN_ENC)
.withHeader("Content-Type","application/json")
.withJsonBody(Json.mapper().createObjectNode());
res = routeAndCall(req);
s = contentAsString(res);
resp =Json.mapper().readTree(s);

assertEquals(NullNode.getInstance(),resp.path("data").path("storage"));

req = new FakeRequest(GET,"/plugin/"+SCRIPT_STORAGE_GET_SET);
req.withHeader(TestConfig.KEY_APPCODE,TestConfig.VALUE_APPCODE)
.withHeader(TestConfig.KEY_AUTH,TestConfig.AUTH_ADMIN_ENC);

res = routeAndCall(req);
s = contentAsString(res);
resp = Json.mapper().readTree(s);

assertEquals(NullNode.getInstance(),resp.path("data").path("storage"));
}catch (Exception e){
fail(ExceptionUtils.getStackTrace(e));
}
});
}

}
15 changes: 15 additions & 0 deletions test/resources/scripts/local_storage_set_get_test.js
@@ -0,0 +1,15 @@
/**
* Created by eto on 08/10/14.
*/

http().post(function (e){
var b = e.body;
b= b.store?b:null;
var st =storage.set(b);


return {status: 200,content: {storage: st}};
}).get(function (e){
var st =storage.get();
return {status: 200,content: {storage: st}};
});

0 comments on commit ee246e9

Please sign in to comment.