private Promise<ResourceResponse, ResourceException> evaluate( final Request request, final Script script) throws ScriptException { Object result = script.eval(); ResourcePath resourcePath = request.getResourcePathObject(); if (null == result) { return new NotFoundException("script returned null").asPromise(); } JsonValue resultJson = (result instanceof JsonValue) ? (JsonValue) result : new JsonValue(result); // If the resultJson isn't able to provide an ID, then we default to the resourcePath. String id = resultJson.get(ResourceResponse.FIELD_CONTENT_ID).defaultTo("").asString(); if (id.isEmpty() && resourcePath.size() > 0) { id = resourcePath.leaf(); } return newResourceResponse(id, null, resultJson).asPromise(); }
@SuppressWarnings("rawtypes") @Test(priority = 3) public void testCreateNew() throws Exception { config.put("property1", "value1"); config.put("property2", "value2"); configObjectService.create(rname, id, json(config), false).getOrThrow(); ConfigObjectService.ParsedId parsedId = configObjectService.getParsedId(rname, id); Configuration config = configObjectService.findExistingConfiguration(parsedId); assertNotNull(config); assertNotNull(config.getProperties()); Dictionary properties = config.getProperties(); EnhancedConfig enhancedConfig = new JSONEnhancedConfig(); JsonValue value = enhancedConfig.getConfiguration(properties, rname.toString(), false); assertTrue(value.keys().contains("property1")); Assert.assertEquals(value.get("property1").asString(), "value1"); }
@SuppressWarnings("rawtypes") @Test(priority = 6) public void testUpdate() throws Exception { config.put("property1", "newvalue1"); config.put("property2", "newvalue2"); when(enhancedConfig.getConfiguration(any(Dictionary.class), any(String.class), eq(false))) .thenReturn(json(config)); configObjectService.update(rname, id, json(config)).getOrThrow(); ConfigObjectService.ParsedId parsedId = configObjectService.getParsedId(rname, id); Configuration config = configObjectService.findExistingConfiguration(parsedId); assertNotNull(config); assertNotNull(config.getProperties()); Dictionary properties = config.getProperties(); JSONEnhancedConfig enhancedConfig = new JSONEnhancedConfig(); JsonValue value = enhancedConfig.getConfiguration(properties, rname.toString(), false); assertTrue(value.keys().contains("property1")); Assert.assertEquals(value.get("property1").asString(), "newvalue1"); }