brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [1/6] incubator-brooklyn git commit: Added LoopOverGroupMembersTestCase. Removed some targeting functionality from BaseTest and added it to a new Interface called TargetabeTestComponent Made TestCase and ParallelTestCase TargetabeTestComponent Infrastruc
Date Wed, 13 Jan 2016 18:06:00 GMT
Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 58337c4e4 -> e2392c3a0


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/74a22487/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCaseTest.java
----------------------------------------------------------------------
diff --git a/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCaseTest.java
b/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCaseTest.java
new file mode 100644
index 0000000..39c85c5
--- /dev/null
+++ b/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/LoopOverGroupMembersTestCaseTest.java
@@ -0,0 +1,286 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.brooklyn.test.framework;
+
+import static org.apache.brooklyn.core.entity.trait.Startable.SERVICE_UP;
+import static org.apache.brooklyn.test.Asserts.fail;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.entity.EntitySpec;
+import org.apache.brooklyn.api.entity.Group;
+import org.apache.brooklyn.api.location.LocationSpec;
+import org.apache.brooklyn.api.mgmt.ManagementContext;
+import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.entity.Entities;
+import org.apache.brooklyn.core.sensor.AttributeSensorAndConfigKey;
+import org.apache.brooklyn.core.test.entity.TestApplication;
+import org.apache.brooklyn.entity.group.DynamicGroup;
+import org.apache.brooklyn.entity.software.base.EmptySoftwareProcess;
+import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation;
+import org.apache.brooklyn.util.collections.MutableSet;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.text.Identifiers;
+
+/**
+ * @author Graeme Miller on 27/10/2015.
+ */
+public class LoopOverGroupMembersTestCaseTest {
+
+    private TestApplication app;
+    private Group testGroup;
+    private ManagementContext managementContext;
+    private LocalhostMachineProvisioningLocation loc;
+    private String testId;
+    private final String SENSOR_VAL = "Hello World!";
+
+    private static final AttributeSensorAndConfigKey<String, String> STRING_SENSOR
= ConfigKeys.newSensorAndConfigKey(String.class, "string-sensor", "String Sensor");
+
+    @BeforeMethod
+    public void setup() {
+        testId = Identifiers.makeRandomId(8);
+        app = TestApplication.Factory.newManagedInstanceForTests();
+        managementContext = app.getManagementContext();
+
+        loc = managementContext.getLocationManager()
+                .createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class)
+                        .configure("name", testId));
+
+        testGroup = app.createAndManageChild(EntitySpec.create(DynamicGroup.class));
+    }
+
+    @AfterMethod(alwaysRun = true)
+    public void tearDown() throws Exception {
+        if (app != null) Entities.destroyAll(app.getManagementContext());
+    }
+
+    @Test
+    public void testOneChildWhichPasses() {
+        EmptySoftwareProcess emptySoftwareProcess = addEmptySoftwareProcessToGroup();
+        EntitySpec<TestSensor> testSpec = createPassingTestSensorSpec();
+
+        LoopOverGroupMembersTestCase loopOverGroupMembersTestCase = app.createAndManageChild(EntitySpec.create(LoopOverGroupMembersTestCase.class));
+        loopOverGroupMembersTestCase.config().set(LoopOverGroupMembersTestCase.TEST_SPEC,
testSpec);
+        loopOverGroupMembersTestCase.config().set(LoopOverGroupMembersTestCase.TARGET_ENTITY,
testGroup);
+
+        app.start(ImmutableList.of(app.newSimulatedLocation()));
+
+        assertThat(loopOverGroupMembersTestCase.getChildren().size()).isEqualTo(1);
+        assertThat(loopOverGroupMembersTestCase.sensors().get(SERVICE_UP)).isTrue();
+
+        Entity loopChildEntity = loopOverGroupMembersTestCase.getChildren().iterator().next();
+        assertThat(loopChildEntity).isInstanceOf(TestSensor.class);
+        assertThat(loopChildEntity.sensors().get(SERVICE_UP)).isTrue();
+        assertThat(loopChildEntity.config().get(LoopOverGroupMembersTestCase.TARGET_ENTITY)).isEqualTo(emptySoftwareProcess);
+    }
+
+    @Test
+    public void testMultipleChildrenWhichPass() {
+        Set<EmptySoftwareProcess> emptySoftwareProcesses = addMultipleEmptySoftwareProcessesToGroup(4);
+        EntitySpec<TestSensor> testSpec = createPassingTestSensorSpec();
+
+        LoopOverGroupMembersTestCase loopOverGroupMembersTestCase = app.createAndManageChild(EntitySpec.create(LoopOverGroupMembersTestCase.class));
+        loopOverGroupMembersTestCase.config().set(LoopOverGroupMembersTestCase.TEST_SPEC,
testSpec);
+        loopOverGroupMembersTestCase.config().set(LoopOverGroupMembersTestCase.TARGET_ENTITY,
testGroup);
+
+        app.start(ImmutableList.of(app.newSimulatedLocation()));
+
+        assertThat(loopOverGroupMembersTestCase.getChildren().size()).isEqualTo(4);
+        assertThat(loopOverGroupMembersTestCase.sensors().get(SERVICE_UP)).isTrue();
+
+        for (Entity loopChildEntity : loopOverGroupMembersTestCase.getChildren()) {
+            assertThat(loopChildEntity).isInstanceOf(TestSensor.class);
+            assertThat(loopChildEntity.sensors().get(SERVICE_UP)).isTrue();
+            assertThat(emptySoftwareProcesses.contains(loopChildEntity.config().get(LoopOverGroupMembersTestCase.TARGET_ENTITY))).isTrue();
+            emptySoftwareProcesses.remove(loopChildEntity.config().get(LoopOverGroupMembersTestCase.TARGET_ENTITY));
+        }
+    }
+
+    @Test
+    public void testMultipleChildrenWhichAllFail() {
+        Set<EmptySoftwareProcess> emptySoftwareProcesses = addMultipleEmptySoftwareProcessesToGroup(4);
+        EntitySpec<TestSensor> testSpec = createFailingTestSensorSpec();
+
+        LoopOverGroupMembersTestCase loopOverGroupMembersTestCase = app.createAndManageChild(EntitySpec.create(LoopOverGroupMembersTestCase.class));
+        loopOverGroupMembersTestCase.config().set(LoopOverGroupMembersTestCase.TEST_SPEC,
testSpec);
+        loopOverGroupMembersTestCase.config().set(LoopOverGroupMembersTestCase.TARGET_ENTITY,
testGroup);
+
+        app.start(ImmutableList.of(app.newSimulatedLocation()));
+
+        assertThat(loopOverGroupMembersTestCase.getChildren().size()).isEqualTo(4);
+        assertThat(loopOverGroupMembersTestCase.sensors().get(SERVICE_UP)).isFalse();
+
+        for (Entity loopChildEntity : loopOverGroupMembersTestCase.getChildren()) {
+            assertThat(loopChildEntity).isInstanceOf(TestSensor.class);
+            assertThat(loopChildEntity.sensors().get(SERVICE_UP)).isFalse();
+            assertThat(emptySoftwareProcesses.contains(loopChildEntity.config().get(LoopOverGroupMembersTestCase.TARGET_ENTITY))).isTrue();
+            emptySoftwareProcesses.remove(loopChildEntity.config().get(LoopOverGroupMembersTestCase.TARGET_ENTITY));
+        }
+    }
+
+    @Test
+    public void testMultipleChildrenOneOfWhichFails() {
+        Set<EmptySoftwareProcess> emptySoftwareProcesses = addMultipleEmptySoftwareProcessesToGroup(3);
+        EntitySpec<TestSensor> testSpec = createPassingTestSensorSpec();
+
+        EmptySoftwareProcess failingProcess = testGroup.addMemberChild(EntitySpec.create(EmptySoftwareProcess.class));
+        failingProcess.sensors().set(STRING_SENSOR, "THIS STRING WILL CAUSE SENSOR TEST TO
FAIL");
+
+        LoopOverGroupMembersTestCase loopOverGroupMembersTestCase = app.createAndManageChild(EntitySpec.create(LoopOverGroupMembersTestCase.class));
+        loopOverGroupMembersTestCase.config().set(LoopOverGroupMembersTestCase.TEST_SPEC,
testSpec);
+        loopOverGroupMembersTestCase.config().set(LoopOverGroupMembersTestCase.TARGET_ENTITY,
testGroup);
+
+        app.start(ImmutableList.of(app.newSimulatedLocation()));
+
+        assertThat(loopOverGroupMembersTestCase.getChildren().size()).isEqualTo(4);
+        assertThat(loopOverGroupMembersTestCase.sensors().get(SERVICE_UP)).isFalse();
+
+        for (Entity loopChildEntity : loopOverGroupMembersTestCase.getChildren()) {
+            assertThat(loopChildEntity).isInstanceOf(TestSensor.class);
+
+            Entity targetedEntity = loopChildEntity.config().get(LoopOverGroupMembersTestCase.TARGET_ENTITY);
+
+            if (targetedEntity.equals(failingProcess)) {
+                assertThat(loopChildEntity.sensors().get(SERVICE_UP)).isFalse();
+            } else if (emptySoftwareProcesses.contains(targetedEntity)) {
+                assertThat(loopChildEntity.sensors().get(SERVICE_UP)).isTrue();
+                emptySoftwareProcesses.remove(targetedEntity);
+            } else {
+                fail("Targeted entity not recognized");
+            }
+        }
+    }
+
+    @Test
+    public void testOneChildWhichFails() {
+        EmptySoftwareProcess emptySoftwareProcess = addEmptySoftwareProcessToGroup();
+        EntitySpec<TestSensor> testSpec = createFailingTestSensorSpec();
+
+        LoopOverGroupMembersTestCase loopOverGroupMembersTestCase = app.createAndManageChild(EntitySpec.create(LoopOverGroupMembersTestCase.class));
+        loopOverGroupMembersTestCase.config().set(LoopOverGroupMembersTestCase.TEST_SPEC,
testSpec);
+        loopOverGroupMembersTestCase.config().set(LoopOverGroupMembersTestCase.TARGET_ENTITY,
testGroup);
+
+        app.start(ImmutableList.of(app.newSimulatedLocation()));
+
+        assertThat(loopOverGroupMembersTestCase.getChildren().size()).isEqualTo(1);
+        assertThat(loopOverGroupMembersTestCase.sensors().get(SERVICE_UP)).isFalse();
+
+        Entity loopChildEntity = loopOverGroupMembersTestCase.getChildren().iterator().next();
+        assertThat(loopChildEntity).isInstanceOf(TestSensor.class);
+        assertThat(loopChildEntity.sensors().get(SERVICE_UP)).isFalse();
+        assertThat(loopChildEntity.config().get(LoopOverGroupMembersTestCase.TARGET_ENTITY)).isEqualTo(emptySoftwareProcess);
+    }
+
+    //negative
+    // without test spec
+    // without target + taget id
+    // not a group
+
+    @Test
+    public void testNoTarget() {
+        EmptySoftwareProcess emptySoftwareProcess = addEmptySoftwareProcessToGroup();
+        EntitySpec<TestSensor> testSpec = createFailingTestSensorSpec();
+
+        LoopOverGroupMembersTestCase loopOverGroupMembersTestCase = app.createAndManageChild(EntitySpec.create(LoopOverGroupMembersTestCase.class));
+        loopOverGroupMembersTestCase.config().set(LoopOverGroupMembersTestCase.TEST_SPEC,
testSpec);
+
+        app.start(ImmutableList.of(app.newSimulatedLocation()));
+
+        assertThat(loopOverGroupMembersTestCase.getChildren().size()).isEqualTo(0);
+        assertThat(loopOverGroupMembersTestCase.sensors().get(SERVICE_UP)).isFalse();
+    }
+
+    @Test
+    public void testNotTargetingGroup() {
+        EmptySoftwareProcess emptySoftwareProcess = addEmptySoftwareProcessToGroup();
+        EntitySpec<TestSensor> testSpec = createFailingTestSensorSpec();
+
+        LoopOverGroupMembersTestCase loopOverGroupMembersTestCase = app.createAndManageChild(EntitySpec.create(LoopOverGroupMembersTestCase.class));
+        loopOverGroupMembersTestCase.config().set(LoopOverGroupMembersTestCase.TEST_SPEC,
testSpec);
+        loopOverGroupMembersTestCase.config().set(LoopOverGroupMembersTestCase.TARGET_ENTITY,
app);
+
+        app.start(ImmutableList.of(app.newSimulatedLocation()));
+
+        assertThat(loopOverGroupMembersTestCase.getChildren().size()).isEqualTo(0);
+        assertThat(loopOverGroupMembersTestCase.sensors().get(SERVICE_UP)).isFalse();
+    }
+
+    @Test
+    public void testNoSpec() {
+        EmptySoftwareProcess emptySoftwareProcess = addEmptySoftwareProcessToGroup();
+        EntitySpec<TestSensor> testSpec = createFailingTestSensorSpec();
+
+        LoopOverGroupMembersTestCase loopOverGroupMembersTestCase = app.createAndManageChild(EntitySpec.create(LoopOverGroupMembersTestCase.class));
+        loopOverGroupMembersTestCase.config().set(LoopOverGroupMembersTestCase.TARGET_ENTITY,
testGroup);
+
+        app.start(ImmutableList.of(app.newSimulatedLocation()));
+
+        assertThat(loopOverGroupMembersTestCase.getChildren().size()).isEqualTo(0);
+        assertThat(loopOverGroupMembersTestCase.sensors().get(SERVICE_UP)).isFalse();
+    }
+
+    //UTILITY METHODS
+    private EntitySpec<TestSensor> createFailingTestSensorSpec() {
+        List<Map<String, Object>> assertions = ImmutableList.<Map<String,
Object>>of(
+                ImmutableMap.<String, Object>of(TestFrameworkAssertions.EQUAL_TO, "THIS
IS THE WRONG STRING")
+        );
+
+        return EntitySpec.create(TestSensor.class)
+                .configure(TestSensor.SENSOR_NAME, STRING_SENSOR.getName())
+                .configure(TestSensor.ASSERTIONS, assertions);
+    }
+
+    private EntitySpec<TestSensor> createPassingTestSensorSpec() {
+        List<Map<String, Object>> assertions = ImmutableList.<Map<String,
Object>>of(
+                ImmutableMap.<String, Object>of(TestFrameworkAssertions.EQUAL_TO, SENSOR_VAL)
+        );
+
+        return EntitySpec.create(TestSensor.class)
+                .configure(TestSensor.SENSOR_NAME, STRING_SENSOR.getName())
+                .configure(TestSensor.ASSERTIONS, assertions);
+    }
+
+    private Set<EmptySoftwareProcess> addMultipleEmptySoftwareProcessesToGroup(int
number) {
+        MutableSet<EmptySoftwareProcess> softwareProcesses = MutableSet.<EmptySoftwareProcess>of();
+        for (int i = 0; i < number; i++) {
+            softwareProcesses.add(addEmptySoftwareProcessToGroup());
+        }
+
+        return softwareProcesses;
+    }
+
+    private EmptySoftwareProcess addEmptySoftwareProcessToGroup() {
+        EmptySoftwareProcess emptySoftwareProcess = testGroup.addMemberChild(EntitySpec.create(EmptySoftwareProcess.class));
+        emptySoftwareProcess.sensors().set(STRING_SENSOR, SENSOR_VAL);
+        return emptySoftwareProcess;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/74a22487/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestInfrastructure.java
----------------------------------------------------------------------
diff --git a/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestInfrastructure.java
b/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestInfrastructure.java
new file mode 100644
index 0000000..abcd679
--- /dev/null
+++ b/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestInfrastructure.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.brooklyn.test.framework.entity;
+
+import org.apache.brooklyn.api.entity.ImplementedBy;
+import org.apache.brooklyn.entity.stock.BasicApplication;
+
+/**
+ * Created by graememiller on 17/12/2015.
+ */
+
+@ImplementedBy(TestInfrastructureImpl.class)
+public interface TestInfrastructure extends BasicApplication {
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/74a22487/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestInfrastructureImpl.java
----------------------------------------------------------------------
diff --git a/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestInfrastructureImpl.java
b/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestInfrastructureImpl.java
new file mode 100644
index 0000000..6ec0638
--- /dev/null
+++ b/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestInfrastructureImpl.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.brooklyn.test.framework.entity;
+
+import java.util.Collection;
+
+import com.google.common.reflect.TypeToken;
+
+import org.apache.brooklyn.api.location.Location;
+import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.sensor.AttributeSensorAndConfigKey;
+import org.apache.brooklyn.entity.stock.BasicApplicationImpl;
+
+/**
+ * Created by graememiller on 17/12/2015.
+ */
+public class TestInfrastructureImpl extends BasicApplicationImpl implements TestInfrastructure
{
+
+    private final AttributeSensorAndConfigKey<Location, Location> DEPLOYMENT_LOCATION
= ConfigKeys.newSensorAndConfigKey(
+            new TypeToken<Location>() {
+            },
+            "deploymentLocationSensor", "The location to deploy to");
+
+    @Override
+    public void postStart(Collection<? extends Location> locations) {
+        super.postStart(locations);
+        sensors().set(DEPLOYMENT_LOCATION, config().get(DEPLOYMENT_LOCATION));
+    }
+}


Mime
View raw message