Skip to content

Commit 6607ccb

Browse files
committed
Move test objects in the allowed package scope
Fixes tests which expect the DSL evaluated objects to be in the correct package. Adds tests to verify that objects outside of the package scope are inaccessible to DSL.
1 parent e831d75 commit 6607ccb

4 files changed

Lines changed: 148 additions & 69 deletions

File tree

camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/dsl/DslTest.java renamed to camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.apache.brooklyn.camp.brooklyn.dsl;
16+
package org.apache.brooklyn.camp.brooklyn.spi.dsl;
1717

1818
import static com.google.common.base.Preconditions.checkNotNull;
1919
import static org.testng.Assert.assertEquals;
@@ -24,7 +24,6 @@
2424
import java.util.NoSuchElementException;
2525
import java.util.Random;
2626
import java.util.concurrent.Callable;
27-
import java.util.concurrent.ExecutionException;
2827
import java.util.concurrent.Executors;
2928
import java.util.concurrent.TimeUnit;
3029

@@ -33,13 +32,12 @@
3332
import org.apache.brooklyn.api.mgmt.Task;
3433
import org.apache.brooklyn.api.sensor.AttributeSensor;
3534
import org.apache.brooklyn.camp.brooklyn.BrooklynCampConstants;
36-
import org.apache.brooklyn.camp.brooklyn.spi.dsl.BrooklynDslDeferredSupplier;
3735
import org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.BrooklynDslCommon;
3836
import org.apache.brooklyn.config.ConfigKey;
3937
import org.apache.brooklyn.core.config.ConfigKeys;
4038
import org.apache.brooklyn.core.entity.EntityInternal;
41-
import org.apache.brooklyn.core.objs.BasicSpecParameter;
4239
import org.apache.brooklyn.core.mgmt.BrooklynTaskTags;
40+
import org.apache.brooklyn.core.objs.BasicSpecParameter;
4341
import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
4442
import org.apache.brooklyn.core.test.entity.TestApplication;
4543
import org.apache.brooklyn.core.test.entity.TestEntity;

camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/dsl/DslYamlBlockingTest.java renamed to camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslYamlTest.java

Lines changed: 46 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,22 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.apache.brooklyn.camp.brooklyn.dsl;
16+
package org.apache.brooklyn.camp.brooklyn.spi.dsl;
1717

1818
import static org.testng.Assert.assertEquals;
1919

2020
import java.util.concurrent.Callable;
21+
import java.util.concurrent.ExecutionException;
2122

2223
import org.apache.brooklyn.api.entity.Entity;
2324
import org.apache.brooklyn.api.mgmt.Task;
2425
import org.apache.brooklyn.api.sensor.AttributeSensor;
2526
import org.apache.brooklyn.camp.brooklyn.AbstractYamlTest;
26-
import org.apache.brooklyn.camp.brooklyn.spi.dsl.DslCallable;
27+
import org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslTestObjects.DslTestCallable;
28+
import org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslTestObjects.DslTestSupplierWrapper;
29+
import org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslTestObjects.TestDslSupplier;
30+
import org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslTestObjects.TestDslSupplierValue;
31+
import org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.custom.UserSuppliedPackageType;
2732
import org.apache.brooklyn.config.ConfigKey;
2833
import org.apache.brooklyn.core.config.ConfigKeys;
2934
import org.apache.brooklyn.core.entity.EntityInternal;
@@ -32,16 +37,14 @@
3237
import org.apache.brooklyn.entity.stock.BasicApplication;
3338
import org.apache.brooklyn.entity.stock.BasicEntity;
3439
import org.apache.brooklyn.test.Asserts;
35-
import org.apache.brooklyn.util.core.task.DeferredSupplier;
36-
import org.apache.brooklyn.util.core.task.ImmediateSupplier;
3740
import org.apache.brooklyn.util.guava.Maybe;
3841
import org.testng.annotations.Test;
3942

4043
import com.google.common.base.Function;
4144
import com.google.common.collect.Iterables;
4245

4346
// Doesn't test executing the DSL from different contexts (i.e. fetching the config from children inheriting it)
44-
public class DslYamlBlockingTest extends AbstractYamlTest {
47+
public class DslYamlTest extends AbstractYamlTest {
4548
private static final ConfigKey<Object> DEST = ConfigKeys.newConfigKey(Object.class, "dest");
4649
private static final ConfigKey<Object> DEST2 = ConfigKeys.newConfigKey(Object.class, "dest2");
4750
private static final ConfigKey<Object> DEST3 = ConfigKeys.newConfigKey(Object.class, "dest3");
@@ -501,15 +504,46 @@ public void testDslFunctionRegexReplacementWithDeferredArg() throws Exception {
501504
assertEquals(replacementFn.apply("Broooklyn"), "Brooklyn");
502505
}
503506

507+
public static class InaccessibleType {
508+
public static void isEvaluated() {}
509+
}
510+
511+
@Test
512+
public void testDeferredDslInaccessibleCall() throws Exception {
513+
final Entity app = createAndStartApplication(
514+
"services:",
515+
"- type: " + BasicApplication.class.getName(),
516+
" brooklyn.config:",
517+
" dest: $brooklyn:config(\"targetValue\").isEvaluated()");
518+
app.config().set(ConfigKeys.newConfigKey(InaccessibleType.class, "targetValue"), new InaccessibleType());
519+
try {
520+
getConfigEventually(app, DEST);
521+
Asserts.shouldHaveFailedPreviously("Outside of allowed package scope");
522+
} catch (ExecutionException e) {
523+
Asserts.expectedFailureContains(e, "(outside allowed package scope)");
524+
}
525+
}
526+
527+
@Test
528+
public void testDeferredDslUserSuppliedPackage() throws Exception {
529+
final Entity app = createAndStartApplication(
530+
"services:",
531+
"- type: " + BasicApplication.class.getName(),
532+
" brooklyn.config:",
533+
" dest: $brooklyn:config(\"targetValue\").isEvaluated()");
534+
app.config().set(ConfigKeys.newConfigKey(UserSuppliedPackageType.class, "targetValue"), new UserSuppliedPackageType());
535+
assertEquals(getConfigEventually(app, DEST), Boolean.TRUE);
536+
}
537+
504538
@Test
505539
public void testDeferredDslChainingOnConfig() throws Exception {
506540
final Entity app = createAndStartApplication(
507541
"services:",
508542
"- type: " + BasicApplication.class.getName(),
509543
" brooklyn.config:",
510-
" targetEntity: $brooklyn:self()",
511-
" dest: $brooklyn:config(\"targetEntity\").getId()");
512-
assertEquals(getConfigEventually(app, DEST), app.getId());
544+
" dest: $brooklyn:config(\"targetValue\").isSupplierEvaluated()");
545+
app.config().set(ConfigKeys.newConfigKey(TestDslSupplierValue.class, "targetValue"), new TestDslSupplierValue());
546+
assertEquals(getConfigEventually(app, DEST), Boolean.TRUE);
513547
}
514548

515549
@Test
@@ -535,10 +569,10 @@ public void testDeferredDslChainingOnSensor() throws Exception {
535569
"services:",
536570
"- type: " + BasicApplication.class.getName(),
537571
" brooklyn.config:",
538-
" dest: $brooklyn:attributeWhenReady(\"targetEntity\").getId()");
539-
AttributeSensor<Entity> targetEntitySensor = Sensors.newSensor(Entity.class, "targetEntity");
540-
app.sensors().set(targetEntitySensor, app);
541-
assertEquals(getConfigEventually(app, DEST), app.getId());
572+
" dest: $brooklyn:attributeWhenReady(\"targetValue\").isSupplierEvaluated()");
573+
AttributeSensor<TestDslSupplierValue> targetValueSensor = Sensors.newSensor(TestDslSupplierValue.class, "targetValue");
574+
app.sensors().set(targetValueSensor, new TestDslSupplierValue());
575+
assertEquals(getConfigEventually(app, DEST), Boolean.TRUE);
542576
}
543577

544578
@Test(groups="WIP")
@@ -568,42 +602,6 @@ public void testDeferredDslChainingOnNullConfig() throws Exception {
568602
}
569603
}
570604

571-
public static class DslTestSupplierWrapper {
572-
private Object supplier;
573-
574-
public DslTestSupplierWrapper(Object supplier) {
575-
this.supplier = supplier;
576-
}
577-
578-
public Object getSupplier() {
579-
return supplier;
580-
}
581-
}
582-
583-
public static class TestDslSupplierValue {
584-
public boolean isSupplierEvaluated() {
585-
return true;
586-
}
587-
}
588-
589-
public static class TestDslSupplier implements DeferredSupplier<Object>, ImmediateSupplier<Object> {
590-
private Object value;
591-
592-
public TestDslSupplier(Object value) {
593-
this.value = value;
594-
}
595-
596-
@Override
597-
public Object get() {
598-
return getImmediately().get();
599-
}
600-
601-
@Override
602-
public Maybe<Object> getImmediately() {
603-
return Maybe.of(value);
604-
}
605-
}
606-
607605
@Test
608606
public void testDeferredDslChainingWithCustomSupplier() throws Exception {
609607
final Entity app = createAndStartApplication(
@@ -616,23 +614,6 @@ public void testDeferredDslChainingWithCustomSupplier() throws Exception {
616614
assertEquals(getConfigEventually(app, DEST), Boolean.TRUE);
617615
}
618616

619-
public static class DslTestCallable implements DslCallable, DeferredSupplier<TestDslSupplier>, ImmediateSupplier<TestDslSupplier> {
620-
621-
@Override
622-
public Maybe<TestDslSupplier> getImmediately() {
623-
throw new IllegalStateException("Not to be called");
624-
}
625-
626-
@Override
627-
public TestDslSupplier get() {
628-
throw new IllegalStateException("Not to be called");
629-
}
630-
631-
public boolean isSupplierCallable() {
632-
return true;
633-
}
634-
}
635-
636617
@Test
637618
public void testDeferredDslChainingWithCustomCallable() throws Exception {
638619
final Entity app = createAndStartApplication(
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2016 The Apache Software Foundation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.brooklyn.camp.brooklyn.spi.dsl.methods;
17+
18+
import org.apache.brooklyn.camp.brooklyn.spi.dsl.DslCallable;
19+
import org.apache.brooklyn.util.core.task.DeferredSupplier;
20+
import org.apache.brooklyn.util.core.task.ImmediateSupplier;
21+
import org.apache.brooklyn.util.guava.Maybe;
22+
23+
public class DslTestObjects {
24+
25+
public static class DslTestSupplierWrapper {
26+
private Object supplier;
27+
28+
public DslTestSupplierWrapper(Object supplier) {
29+
this.supplier = supplier;
30+
}
31+
32+
public Object getSupplier() {
33+
return supplier;
34+
}
35+
}
36+
37+
public static class TestDslSupplierValue {
38+
public boolean isSupplierEvaluated() {
39+
return true;
40+
}
41+
}
42+
43+
public static class TestDslSupplier implements DeferredSupplier<Object>, ImmediateSupplier<Object> {
44+
private Object value;
45+
46+
public TestDslSupplier(Object value) {
47+
this.value = value;
48+
}
49+
50+
@Override
51+
public Object get() {
52+
return getImmediately().get();
53+
}
54+
55+
@Override
56+
public Maybe<Object> getImmediately() {
57+
return Maybe.of(value);
58+
}
59+
}
60+
61+
public static class DslTestCallable implements DslCallable, DeferredSupplier<TestDslSupplier>, ImmediateSupplier<TestDslSupplier> {
62+
63+
@Override
64+
public Maybe<TestDslSupplier> getImmediately() {
65+
throw new IllegalStateException("Not to be called");
66+
}
67+
68+
@Override
69+
public TestDslSupplier get() {
70+
throw new IllegalStateException("Not to be called");
71+
}
72+
73+
public boolean isSupplierCallable() {
74+
return true;
75+
}
76+
}
77+
78+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2016 The Apache Software Foundation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.custom;
17+
18+
public class UserSuppliedPackageType {
19+
public boolean isEvaluated() {
20+
return true;
21+
}
22+
}

0 commit comments

Comments
 (0)