use of com.icodici.universa.contract.jsapi.permissions.JSApiChangeNumberPermission in project universa by UniversaBlockchain.
the class ScriptEngineTest method testChangeNumberPermission.
@Test
public void testChangeNumberPermission() throws Exception {
KeyAddress k0 = TestKeys.publicKey(0).getShortAddress();
String p0 = TestKeys.publicKey(0).packToBase64String();
String p1 = TestKeys.publicKey(1).packToBase64String();
Contract contract = new Contract(TestKeys.privateKey(0));
String js = "";
js += "print('testChangeNumberPermission');";
js += "var simpleRole = jsApi.getRoleBuilder().createSimpleRole('owner', '" + k0.toString() + "');";
js += "var changeNumberPermission = jsApi.getPermissionBuilder().createChangeNumberPermission(simpleRole, " + "{field_name: 'testval', min_value: 44, max_value: 55, min_step: 1, max_step: 2}" + ");";
js += "jsApi.getCurrentContract().addPermission(changeNumberPermission);";
js += "var isPermitted0 = jsApi.getCurrentContract().isPermitted('decrement_permission', jsApi.base64toPublicKey('" + p0 + "'));";
js += "var isPermitted1 = jsApi.getCurrentContract().isPermitted('decrement_permission', jsApi.base64toPublicKey('" + p1 + "'));";
js += "print('isPermitted0: ' + isPermitted0);";
js += "print('isPermitted1: ' + isPermitted1);";
js += "result = [changeNumberPermission, isPermitted0, isPermitted1];";
contract.getDefinition().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters());
contract.seal();
ScriptObjectMirror res = (ScriptObjectMirror) contract.execJS(js.getBytes());
ChangeNumberPermission changeNumberPermission = (ChangeNumberPermission) ((JSApiChangeNumberPermission) res.get("0")).extractPermission(new JSApiAccessor());
ChangeNumberPermission sample = new ChangeNumberPermission(new SimpleRole("test"), Binder.of("field_name", "testval", "min_value", 44, "max_value", 55, "min_step", 1, "max_step", 2));
Field field = ChangeNumberPermission.class.getDeclaredField("fieldName");
field.setAccessible(true);
assertEquals(field.get(sample), field.get(changeNumberPermission));
field = ChangeNumberPermission.class.getDeclaredField("minValue");
field.setAccessible(true);
assertEquals(field.get(sample), field.get(changeNumberPermission));
field = ChangeNumberPermission.class.getDeclaredField("maxValue");
field.setAccessible(true);
assertEquals(field.get(sample), field.get(changeNumberPermission));
field = ChangeNumberPermission.class.getDeclaredField("minStep");
field.setAccessible(true);
assertEquals(field.get(sample), field.get(changeNumberPermission));
field = ChangeNumberPermission.class.getDeclaredField("maxStep");
field.setAccessible(true);
assertEquals(field.get(sample), field.get(changeNumberPermission));
assertTrue((boolean) res.get("1"));
assertFalse((boolean) res.get("2"));
}
Aggregations