Skip to content

Commit

Permalink
resolve deferred values at the right time in yoml (ie as late as poss…
Browse files Browse the repository at this point in the history
…ible)
  • Loading branch information
ahgittin committed Oct 26, 2016
1 parent ac9ea6b commit d49dc02
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,24 @@
import org.apache.brooklyn.api.typereg.BrooklynTypeRegistry.RegisteredTypeKind;
import org.apache.brooklyn.api.typereg.RegisteredType;
import org.apache.brooklyn.camp.brooklyn.AbstractYamlTest;
import org.apache.brooklyn.camp.yoml.types.YomlInitializers;
import org.apache.brooklyn.core.config.ConfigKeys;
import org.apache.brooklyn.core.entity.EntityAsserts;
import org.apache.brooklyn.core.entity.EntityInternal;
import org.apache.brooklyn.core.sensor.Sensors;
import org.apache.brooklyn.core.typereg.BasicBrooklynTypeRegistry;
import org.apache.brooklyn.test.Asserts;
import org.apache.brooklyn.util.guava.Maybe;
import org.apache.brooklyn.util.time.Duration;
import org.apache.brooklyn.util.time.Time;
import org.apache.brooklyn.util.yoml.annotations.YomlAllFieldsTopLevel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.google.api.client.repackaged.com.google.common.base.Joiner;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;

public class BrooklynDslInYomlStringPlanTest extends AbstractYamlTest {
Expand Down Expand Up @@ -73,14 +81,43 @@ public void testYomlParserRespectsDsl() throws Exception {
"- type: org.apache.brooklyn.core.test.entity.TestEntity",
" brooklyn.config:",
" test.obj:",
// with this, the yoml is resolved at retrieval time
" $brooklyn:object-yoml: item-w-dsl");

Entity app = createStartWaitAndLogApplication(yaml);
Entity entity = Iterables.getOnlyElement( app.getChildren() );

entity.sensors().set(Sensors.newStringSensor("test.sensor"), "bob");
Maybe<Object> raw = ((EntityInternal)entity).config().getRaw(ConfigKeys.newConfigKey(Object.class, "test.obj"));
Asserts.assertPresent(raw);
Asserts.assertInstanceOf(raw.get(), Supplier.class);
Object obj = entity.config().get(ConfigKeys.newConfigKey(Object.class, "test.obj"));
Assert.assertEquals(((ItemA)obj).name, "bob");
}

@Test
public void testYomlDefersDslEvaluationForConfig() throws Exception {
add(SAMPLE_TYPE_BASE);
add(SAMPLE_TYPE_TEST);
YomlInitializers.install(mgmt());

String yaml = Joiner.on("\n").join(
"services:",
"- type: org.apache.brooklyn.core.test.entity.TestEntity",
" brooklyn.initializers:",
" a-sensor:",
" type: static-sensor",
" value: '$brooklyn:self().attributeWhenReady(\"test.sensor\")'",
" period: 100ms");

Entity app = createStartWaitAndLogApplication(yaml);
Entity entity = Iterables.getOnlyElement( app.getChildren() );

entity.sensors().set(Sensors.newStringSensor("test.sensor"), "bob");
// EntityAsserts.assertAttributeEqualsEventually(entity, attribute, expected);
System.out.println(entity.getAttribute(Sensors.newStringSensor("a-sensor")));
Time.sleep(Duration.ONE_SECOND);
System.out.println(entity.getAttribute(Sensors.newStringSensor("a-sensor")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ protected boolean setKeyValueForJavaObjectOnRead(String key, Object value, Strin

Object v2;
try {
v2 = converter.read( ((YomlContextForRead)context).subpath("/"+key, value, optionalType) );
if (isDeferredValue(value)) v2 = value;
else v2 = converter.read( ((YomlContextForRead)context).subpath("/"+key, value, optionalType) );
} catch (Exception e) {
// for config we try with the optional type, but don't insist
Exceptions.propagateIfFatal(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ protected boolean setKeyValueForJavaObjectOnRead(String key, Object value, Strin
String fieldType = getFieldTypeName(ff, optionalTypeConstraint);
Object v2 = converter.read( ((YomlContextForRead)context).subpath("/"+key, value, fieldType) );

if (isDeferredValue(v2)) {
Maybe<?> coerced = config.getCoercer().tryCoerce(v2, ff.getType());
if (coerced.isAbsent()) {
// couldn't coerce or resolve, and this is needed for fields of course
throw new YomlException("Cannot interpret or coerce '"+v2+"' as "+fieldType+" for field "+ff.getName(),
Maybe.getException(coerced));
}
v2 = coerced.get();
}

ff.setAccessible(true);
ff.set(getJavaObject(), v2);
return true;
Expand Down

0 comments on commit d49dc02

Please sign in to comment.