Return-Path: X-Original-To: apmail-brooklyn-commits-archive@minotaur.apache.org Delivered-To: apmail-brooklyn-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 35CEB10C43 for ; Tue, 8 Mar 2016 09:11:15 +0000 (UTC) Received: (qmail 84390 invoked by uid 500); 8 Mar 2016 09:11:15 -0000 Delivered-To: apmail-brooklyn-commits-archive@brooklyn.apache.org Received: (qmail 84335 invoked by uid 500); 8 Mar 2016 09:11:15 -0000 Mailing-List: contact commits-help@brooklyn.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.apache.org Delivered-To: mailing list commits@brooklyn.apache.org Received: (qmail 84314 invoked by uid 99); 8 Mar 2016 09:11:14 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 08 Mar 2016 09:11:14 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id C1DD5DFA26; Tue, 8 Mar 2016 09:11:14 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: aledsage@apache.org To: commits@brooklyn.apache.org Date: Tue, 08 Mar 2016 09:11:14 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [1/2] brooklyn-dist git commit: mvn archetype: change to be OSGi java entity Repository: brooklyn-dist Updated Branches: refs/heads/master f1561b631 -> b38cdc72c mvn archetype: change to be OSGi java entity Previously, the generated project included example Java apps and example main class, with an assembly to produce a stand-alone distort. This deletes all of those, replacing it with a simple example Java blueprint. It also includes java-based tests (including for testing the YAML, and using the YAML test framework). Project: http://git-wip-us.apache.org/repos/asf/brooklyn-dist/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-dist/commit/4545ac2a Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-dist/tree/4545ac2a Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-dist/diff/4545ac2a Branch: refs/heads/master Commit: 4545ac2a97bd7f89db6dcd4190463fd5c4500db9 Parents: b14ee79 Author: Aled Sage Authored: Wed Mar 2 12:11:07 2016 +0000 Committer: Aled Sage Committed: Tue Mar 8 09:08:31 2016 +0000 ---------------------------------------------------------------------- archetypes/quickstart/pom.xml | 8 +- .../quickstart/src/brooklyn-sample/README.md | 74 +++++----- .../quickstart/src/brooklyn-sample/pom.xml | 85 ++++++++---- .../src/main/assembly/assembly.xml | 88 ------------ .../src/main/assembly/files/README.txt | 96 ------------- .../src/main/assembly/files/conf/logback.xml | 11 -- .../src/main/assembly/scripts/start.sh | 40 ------ .../java/com/acme/sample/brooklyn/MySample.java | 38 +++++ .../com/acme/sample/brooklyn/MySampleImpl.java | 74 ++++++++++ .../com/acme/sample/brooklyn/SampleMain.java | 81 ----------- .../app/ClusterWebServerDatabaseSample.java | 137 ------------------- .../sample/app/SingleWebServerSample.java | 32 ----- .../src/main/resources/logback-custom.xml | 10 -- .../main/resources/visitors-creation-script.sql | 35 ----- .../com/acme/sample/brooklyn/MySampleTest.java | 36 +++++ .../acme/sample/brooklyn/MySampleYamlTest.java | 73 ++++++++++ .../app/SampleLocalhostIntegrationTest.java | 81 ----------- .../brooklyn/sample/app/SampleUnitTest.java | 69 ---------- .../src/test/resources/sample-test.yaml | 43 ++++++ .../src/test/resources/sample.bom | 12 ++ 20 files changed, 378 insertions(+), 745 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/pom.xml ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/pom.xml b/archetypes/quickstart/pom.xml index f47c395..c83a3db 100644 --- a/archetypes/quickstart/pom.xml +++ b/archetypes/quickstart/pom.xml @@ -139,7 +139,9 @@ ${basedir}/src/brooklyn-sample/ ${basedir} src/main/resources/archetype-resources/ - **/* + + **/* + src/main/java/** src/test/java/** @@ -217,9 +219,9 @@ **/src/main/resources/logback-custom.xml **/src/brooklyn-sample/README.md **/src/brooklyn-sample/pom.xml - **/src/brooklyn-sample/src/main/assembly/files/conf/* **/src/brooklyn-sample/src/main/java/**/*.java - **/src/brooklyn-sample/src/main/resources/logback-custom.xml + **/src/brooklyn-sample/src/test/resources/**/*.bom + **/src/brooklyn-sample/src/test/resources/**/*.yaml **/src/brooklyn-sample/src/test/java/**/*.java http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/README.md ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/README.md b/archetypes/quickstart/src/brooklyn-sample/README.md index c431a7d..9737cd0 100644 --- a/archetypes/quickstart/src/brooklyn-sample/README.md +++ b/archetypes/quickstart/src/brooklyn-sample/README.md @@ -1,31 +1,27 @@ brooklyn-sample === -This is a Sample Brooklyn project, showing how to define an application -which Brooklyn will deploy and manage. +This is a Sample Brooklyn project, showing how to write a Java-based entity and +build it as an OSGi bundle that can be used in a Brooklyn catalog. This sample project is intended to be customized to suit your purposes: but -search for all lines containing the word "sample" to make sure all the +search for all lines containing the word "sample" or "acme" to make sure all the references to this being a sample are removed! -To build an assembly, simply run: +To easily find the bits you should customize, do a: + + grep -ri sample src/ *.* - mvn clean assembly:assembly -This creates a tarball with a full standalone application which can be installed in any *nix machine at: - target/brooklyn-sample-0.1.0-SNAPSHOT-dist.tar.gz +### Building -It also installs an unpacked version which you can run locally: - - cd target/brooklyn-sample-0.1.0-SNAPSHOT-dist/brooklyn-sample-0.1.0-SNAPSHOT - ./start.sh server - -For more information see the README (or `./start.sh help`) in that directory. -On OS X and Linux, this application will deploy to localhost *if* you have key-based -password-less (and passphrase-less) ssh enabled. +To build an assembly, simply run: + + mvn clean install -To configure cloud and fixed-IP locations, see the README file in the built application directly. -For more information you can run `./start.sh help`) in that directory. +This creates an OSGi bundle at: + + target/brooklyn-sample-0.1.0-SNAPSHOT.jar ### Opening in an IDE @@ -34,23 +30,35 @@ To open this project in an IDE, you will need maven support enabled (e.g. with the relevant plugin). You should then be able to develop it and run it as usual. For more information on IDE support, visit: - https://brooklyn.incubator.apache.org/v/latest/dev/env/ide/ + https://brooklyn.apache.org/v/latest/dev/env/ide/ -### Customizing the Assembly +### Brooklyn Catalog -The artifacts (directory and tar.gz by default) which get built into -`target/` can be changed. Simply edit the relevant files under -`src/main/assembly`. +The new blueprint can be added to the Brooklyn catalog using the `sample.bom` file. +located in `src/test/resources`. You will first have to copy the target jar to a suitable location, +and update the URL in `sample.bom` pointing at that jar. -You will likely wish to customize the `SampleMain` class as well as -the `Sample*App` classes provided. That is the intention! -You will also likely want to update the `start.sh` script and -the `README.*` files. +The command below will use the REST api to add this to the catalog of a running Brooklyn instance: -To easily find the bits you should customize, do a: + curl -u admin:pa55w0rd http://127.0.0.1:8081/v1/catalog --data-binary @src/test/resources/sample.bom - grep -ri sample src/ *.* +The YAML blueprint below shows an example usage of this blueprint: + + name: my sample + services: + - type: com.acme.sample.brooklyn.MySampleInCatalog:1.0 + + +### Testing Entities + +This project comes with unit tests that demonstrate how to test entities, both within Java and +also using YAML-based blueprints. + +A strongly recommended way is to write a YAML test blueprint using the test framework, and making +this available to anyone who will use your entity. This will allow users to easily run the test +blueprint in their own environment (simply by deploying it to their own Brooklyn server) to confirm +that the entity is working as expected. An example is shown in `src/test/resources/sample-test.yaml`. ### More About Apache Brooklyn @@ -59,15 +67,13 @@ Apache Brooklyn is a code library and framework for managing applications in a cloud-first dev-ops-y way. It has been used to create this sample project which shows how to define an application and entities for Brooklyn. -This project can be extended for more complex topologies and more -interesting applications, and to develop the policies to scale or tune the -deployment depending on what the application needs. - For more information consider: -* Visiting the Apache Brooklyn home page at https://brooklyn.incubator.apache.org +* Visiting the Apache Brooklyn home page at https://brooklyn.apache.org * Finding us on IRC #brooklyncentral or email (click "Community" at the site above) * Forking the project at http://github.com/apache/brooklyn/ -A sample Brooklyn project should specify its license. +### License + +Please add your desired license to this project template. http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/pom.xml ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/pom.xml b/archetypes/quickstart/src/brooklyn-sample/pom.xml index 44f5f93..6076b84 100644 --- a/archetypes/quickstart/src/brooklyn-sample/pom.xml +++ b/archetypes/quickstart/src/brooklyn-sample/pom.xml @@ -13,16 +13,15 @@ com.acme.sample brooklyn-sample 0.1.0-SNAPSHOT - jar - Sample Brooklyn Project com.acme.sample:brooklyn-sample v0.1.0-SNAPSHOT + Sample Apache Brooklyn Project com.acme.sample:brooklyn-sample v0.1.0-SNAPSHOT - Sample optional description goes here. + Sample Apache Brooklyn Project for Java entity development, and OSGi bundle generation. - - com.acme.sample.brooklyn.SampleMain - - apache.snapshots @@ -58,19 +53,34 @@ false + + true + - + org.apache.brooklyn brooklyn-all ${brooklyn.version} + + org.apache.brooklyn + brooklyn-logback-xml + ${brooklyn.version} + true + provided + + + org.apache.brooklyn brooklyn-test-support @@ -79,24 +89,43 @@ - + org.apache.brooklyn - brooklyn-logback-xml + brooklyn-utils-test-support ${brooklyn.version} + test + + + + + org.apache.brooklyn + brooklyn-core + ${brooklyn.version} + tests + test - - - - - maven-assembly-plugin - - - src/main/assembly/assembly.xml - - - - - + + + org.apache.brooklyn + brooklyn-camp + ${brooklyn.version} + test + + + org.apache.brooklyn + brooklyn-camp + ${brooklyn.version} + tests + test + + + org.apache.brooklyn + brooklyn-test-framework + ${brooklyn.version} + test + + + http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/assembly.xml ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/assembly.xml b/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/assembly.xml deleted file mode 100644 index 2875a8a..0000000 --- a/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/assembly.xml +++ /dev/null @@ -1,88 +0,0 @@ - - - - dist - - - dir - tar.gz - - - - - - - false - lib - false - - - - - - - src/main/assembly/scripts/start.sh - - 0755 - true - - - - - src/main/assembly/scripts - - 0755 - - * - - - start.sh - - - - - src/main/assembly/files - - - ** - - - - - target - - - *.jar - - - *-tests.jar - - - - http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/files/README.txt ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/files/README.txt b/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/files/README.txt deleted file mode 100644 index 8ba14f1..0000000 --- a/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/files/README.txt +++ /dev/null @@ -1,96 +0,0 @@ -brooklyn-sample -=== - -This is a sample application which is launched and managed by Brooklyn. -This README file is at the root of the built assembly, which can be uploaded -and run most anywhere. This file provides end-user instructions. - -To use this, configure any cloud credentials then run ./start.sh in this -directory. You can then access the management context in your browser, -typically on http://localhost:8081 , and through that console deploy the -applications contained in this archive. - - -### Cloud Credentials - -To run, you'll need to specify credentials for your preferred cloud. This -can be done in `~/.brooklyn/brooklyn.properties`. - -A recommended starting point is the file at: - - https://brooklyn.incubator.apache.org/v/latest/use/guide/quickstart/brooklyn.properties - -As a quick-start, you can specify: - - brooklyn.location.jclouds.aws-ec2.identity=AKXXXXXXXXXXXXXXXXXX - brooklyn.location.jclouds.aws-ec2.credential=secret01xxxxxxxxxxxxxxxxxxxxxxxxxxx - - brooklyn.location.named.My_Amazon_US_west=jclouds:aws-ec2:us-west-1 - brooklyn.location.named.My_Amazon_EU=jclouds:aws-ec2:eu-west-1 - -Alternatively these can be set as shell environment parameters or JVM system properties. - -Many other clouds are supported also, as well as pre-existing machines -("bring your own nodes"), custom endpoints for private clouds, and specifying -custom keys and passphrases. For more information see: - - https://brooklyn.incubator.apache.org/v/latest/use/guide/defining-applications/common-usage#locations - - -### Run - -Usage: - - ./start.sh launch [--WHAT] \ - [--port 8081+] \ - [--location location] - -The optional port argument specifies the port where the Brooklyn console -will be running, such as localhost:8081. (The console is only bound to -localhost, unless you set up users and security, as described at -https://brooklyn.incubator.apache.org/v/latest/use/guide/management/ .) - -In the console, you can access the catalog and deploy applications to -configured locations. - -In this sample application, `--cluster` and `--single` are supported in place of `--WHAT`, -corresponding to sample blueprints in the project. This can be left blank to have nothing -launched; note that the blueprints can be launched at runtime via the GUI or API. - -Use `./start.sh help` or `./start.sh help launch` for more detailed help. - -The location might be `localhost` (the default), `aws-ec2:us-east-1`, -`openstack:endpoint`, `softlayer`, or based on names, such as -`named:My_Amazon_US_west`, so long as it is configured as above. -(Localhost does not need any configuration in `~/brooklyn/brooklyn.properties`, -provided you have password-less ssh access enabled to localhost, using a -private key with no passphrase.) - - -### More About Brooklyn - -Brooklyn is a code library and framework for managing applications in a -cloud-first dev-ops-y way. It has been used to create this sample project -which shows how to define an application and entities for Brooklyn. - -This project can be extended for more complex topologies and more -interesting applications, and to develop the policies to scale or tune the -deployment depending on what the application needs. - -For more information consider: - -* Visiting the open-source Brooklyn home page at http://brooklyncentral.github.com -* Forking the Brooklyn project at http://github.com/brooklyncentral/brooklyn -* Emailing brooklyn-users@googlegroups.com - -For commercial enquiries -- including bespoke development and paid support -- -contact Cloudsoft, the supporters of Brooklyn, at: - -* www.CloudsoftCorp.com -* info@cloudsoftcorp.com - -Brooklyn is (c) 2014 Cloudsoft Corporation and released as open source under -the Apache License v2.0. - -A sample Brooklyn project should specify its license here. - http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/files/conf/logback.xml ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/files/conf/logback.xml b/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/files/conf/logback.xml deleted file mode 100644 index 7b7a972..0000000 --- a/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/files/conf/logback.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/scripts/start.sh ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/scripts/start.sh b/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/scripts/start.sh deleted file mode 100755 index 2879b6d..0000000 --- a/archetypes/quickstart/src/brooklyn-sample/src/main/assembly/scripts/start.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash -# -# 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. -# - -if [ ! -z "$JAVA_HOME" ] ; then - JAVA=$JAVA_HOME/bin/java -else - JAVA=`which java` -fi - -if [ ! -x "$JAVA" ] ; then - echo Cannot find java. Set JAVA_HOME or add java to path. - exit 1 -fi - -if [[ ! `ls ${project.artifactId}-*.jar 2> /dev/null` ]] ; then - echo Command must be run from the directory where the JAR is installed. - exit 4 -fi - -$JAVA -Xms256m -Xmx1024m -XX:MaxPermSize=1024m \ - -classpath "conf/:patch/*:*:lib/*" \ - ${project.entry} \ - "$@" http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/MySample.java ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/MySample.java b/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/MySample.java new file mode 100644 index 0000000..af13e32 --- /dev/null +++ b/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/MySample.java @@ -0,0 +1,38 @@ +package com.acme.sample.brooklyn; + +import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.api.entity.ImplementedBy; +import org.apache.brooklyn.api.sensor.AttributeSensor; +import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.core.annotation.Effector; +import org.apache.brooklyn.core.annotation.EffectorParam; +import org.apache.brooklyn.core.config.ConfigKeys; +import org.apache.brooklyn.core.entity.trait.Startable; +import org.apache.brooklyn.core.sensor.Sensors; + +/** + * A simple entity, which demonstrates some of the things that can be done in Java when + * implementing an entity. + * + * Note the {@link ImplementedBy} annotation, which points at the class to be instantiated + * for this entity type. + */ +@ImplementedBy(MySampleImpl.class) +public interface MySample extends Entity, Startable { + + ConfigKey MY_CONF = ConfigKeys.newStringConfigKey( + "myConf", + "My example config key", + "defaultval"); + + AttributeSensor LAST_CALL = Sensors.newStringSensor( + "lastCall", + "Example sensor, showing the last effector call"); + + /** + * Declares an effector. The annotation marks this as an effector, so the effector will + * be automatically wired up to the method's implementation. + */ + @Effector(description="My example effector") + void myEffector(@EffectorParam(name="arg1") String arg1); +} http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/MySampleImpl.java ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/MySampleImpl.java b/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/MySampleImpl.java new file mode 100644 index 0000000..6d28efa --- /dev/null +++ b/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/MySampleImpl.java @@ -0,0 +1,74 @@ +package com.acme.sample.brooklyn; + +import java.util.Collection; + +import org.apache.brooklyn.api.location.Location; +import org.apache.brooklyn.core.entity.AbstractEntity; +import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; +import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic; +import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic.ServiceNotUpLogic; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The entity's implementation. It is normal to extend one of the pre-existing superclasses + * such as the most basic {@link AbstractEntity} or (for a software process being installed) + * a {@link org.apache.brooklyn.entity.software.base.SoftwareProcessImpl}. However, for the + * latter it is strongly encouraged to instead consider using YAML-based blueprints. + * + * The service.isUp and service.state sensors are populated by using the {@link ServiceNotUpLogic} + * and {@link ServiceStateLogic} utilities. A simpler (less powerful) way is to directly set + * the sensors {@link org.apache.brooklyn.core.entity.Attributes#SERVICE_UP} and + * {@link org.apache.brooklyn.core.entity.Attributes#SERVICE_STATE_ACTUAL}. The advantage + * of the {@link ServiceNotUpLogic} is that multiple indicators can be used, all of which + * must be satisfied for the service to be considered healthy. For example, it might be required + * that a web-app's URL is reachable and also that the app-server's management API reports + * healthy. + */ +public class MySampleImpl extends AbstractEntity implements MySample { + + private static final Logger LOG = LoggerFactory.getLogger(MySampleImpl.class); + + @Override + public void init() { + super.init(); + LOG.info("MySampleImpl.init() with config {}", config().get(MY_CONF)); + ServiceNotUpLogic.updateNotUpIndicator(this, "started", "not started"); + } + + @Override + protected void initEnrichers() { + super.initEnrichers(); + } + + @Override + public void start(Collection locs) { + LOG.info("MySampleImpl.start({})", locs); + sensors().set(LAST_CALL, "start"); + ServiceNotUpLogic.clearNotUpIndicator(this, "started"); + ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING); + } + + @Override + public void stop() { + LOG.info("MySampleImpl.stop()"); + sensors().set(LAST_CALL, "stop"); + ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPED); + ServiceNotUpLogic.updateNotUpIndicator(this, "started", "stopped"); + } + + @Override + public void restart() { + LOG.info("MySampleImpl.restart()"); + sensors().set(LAST_CALL, "restart"); + ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPED); + ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING); + } + + @Override + public void myEffector(String arg1) { + LOG.info("MySampleImpl.myEffector({})", arg1); + sensors().set(LAST_CALL, "myEffector("+arg1+")"); + } + +} http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/SampleMain.java ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/SampleMain.java b/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/SampleMain.java deleted file mode 100644 index 253f657..0000000 --- a/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/SampleMain.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.acme.sample.brooklyn; - -import java.util.Arrays; - -import io.airlift.command.Command; -import io.airlift.command.Option; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.apache.brooklyn.api.catalog.BrooklynCatalog; -import org.apache.brooklyn.cli.Main; - -import com.google.common.base.Objects.ToStringHelper; - -import com.acme.sample.brooklyn.sample.app.*; - -/** - * This class provides a static main entry point for launching a custom Brooklyn-based app. - *

- * It inherits the standard Brooklyn CLI options from {@link Main}, - * plus adds a few more shortcuts for favourite blueprints to the {@link LaunchCommand}. - */ -public class SampleMain extends Main { - - private static final Logger log = LoggerFactory.getLogger(SampleMain.class); - - public static final String DEFAULT_LOCATION = "localhost"; - - public static void main(String... args) { - log.debug("CLI invoked with args "+Arrays.asList(args)); - new SampleMain().execCli(args); - } - - @Override - protected String cliScriptName() { - return "start.sh"; - } - - @Override - protected Class cliLaunchCommand() { - return LaunchCommand.class; - } - - @Command(name = "launch", description = "Starts a server, and optionally an application. " - + "Use e.g. --single or --cluster to launch one-node and clustered variants of the sample web application.") - public static class LaunchCommand extends Main.LaunchCommand { - - // add these options to the LaunchCommand as shortcuts for our favourite applications - - @Option(name = { "--single" }, description = "Launch a single web-server instance") - public boolean single; - - @Option(name = { "--cluster" }, description = "Launch a web-server cluster") - public boolean cluster; - - @Override - public Void call() throws Exception { - // process our CLI arguments - if (single) setAppToLaunch( SingleWebServerSample.class.getCanonicalName() ); - if (cluster) setAppToLaunch( ClusterWebServerDatabaseSample.class.getCanonicalName() ); - - // now process the standard launch arguments - return super.call(); - } - - @Override - protected void populateCatalog(BrooklynCatalog catalog) { - super.populateCatalog(catalog); - catalog.addItem(SingleWebServerSample.class); - catalog.addItem(ClusterWebServerDatabaseSample.class); - } - - @Override - public ToStringHelper string() { - return super.string() - .add("single", single) - .add("cluster", cluster); - } - } -} http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/sample/app/ClusterWebServerDatabaseSample.java ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/sample/app/ClusterWebServerDatabaseSample.java b/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/sample/app/ClusterWebServerDatabaseSample.java deleted file mode 100644 index 11d977f..0000000 --- a/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/sample/app/ClusterWebServerDatabaseSample.java +++ /dev/null @@ -1,137 +0,0 @@ -package com.acme.sample.brooklyn.sample.app; - -import java.util.concurrent.TimeUnit; - -import org.apache.brooklyn.api.catalog.Catalog; -import org.apache.brooklyn.api.catalog.CatalogConfig; -import org.apache.brooklyn.api.entity.EntitySpec; -import org.apache.brooklyn.api.sensor.AttributeSensor; -import org.apache.brooklyn.config.ConfigKey; -import org.apache.brooklyn.core.config.ConfigKeys; -import org.apache.brooklyn.core.entity.AbstractApplication; -import org.apache.brooklyn.core.entity.Entities; -import org.apache.brooklyn.core.location.PortRanges; -import org.apache.brooklyn.core.sensor.Sensors; -import org.apache.brooklyn.enricher.stock.SensorPropagatingEnricher; -import org.apache.brooklyn.enricher.stock.SensorTransformingEnricher; -import org.apache.brooklyn.entity.database.DatastoreMixins.DatastoreCommon; -import org.apache.brooklyn.entity.database.mysql.MySqlNode; -import org.apache.brooklyn.entity.group.DynamicCluster; -import org.apache.brooklyn.entity.java.JavaEntityMethods; -import org.apache.brooklyn.entity.webapp.ControlledDynamicWebAppCluster; -import org.apache.brooklyn.entity.webapp.DynamicWebAppCluster; -import org.apache.brooklyn.entity.webapp.JavaWebAppService; -import org.apache.brooklyn.entity.webapp.WebAppService; -import org.apache.brooklyn.entity.webapp.WebAppServiceConstants; -import org.apache.brooklyn.policy.autoscaling.AutoScalerPolicy; -import org.apache.brooklyn.policy.enricher.HttpLatencyDetector; -import org.apache.brooklyn.util.maven.MavenArtifact; -import org.apache.brooklyn.util.maven.MavenRetriever; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.base.Functions; - -import static org.apache.brooklyn.core.sensor.DependentConfiguration.attributeWhenReady; -import static org.apache.brooklyn.core.sensor.DependentConfiguration.formatString; - - -/** This sample builds a 3-tier application with an elastic app-server cluster, - * and it sets it up for use in the Brooklyn catalog. - *

- * Note that root access (and xcode etc) may be required to install nginx. - **/ -@Catalog(name="Elastic Java Web + DB", - description="Deploys a WAR to a load-balanced elastic Java AppServer cluster, " + - "with an auto-scaling policy, " + - "wired to a database initialized with the provided SQL; " + - "defaults to a 'Hello World' chatroom app.", - iconUrl="classpath://sample-icon.png") -public class ClusterWebServerDatabaseSample extends AbstractApplication { - - public static final Logger LOG = LoggerFactory.getLogger(ClusterWebServerDatabaseSample.class); - - // ---------- WAR configuration --------------- - - public static final String DEFAULT_WAR_URL = - // can supply any URL -- this loads a stock example from maven central / sonatype - MavenRetriever.localUrl(MavenArtifact.fromCoordinate("io.brooklyn.example:brooklyn-example-hello-world-sql-webapp:war:0.5.0")); - - @CatalogConfig(label="WAR (URL)", priority=2) - public static final ConfigKey WAR_URL = ConfigKeys.newConfigKey( - "app.war", "URL to the application archive which should be deployed", DEFAULT_WAR_URL); - - - // ---------- DB configuration ---------------- - - // this is included in src/main/resources. if in an IDE, ensure your build path is set appropriately. - public static final String DEFAULT_DB_SETUP_SQL_URL = "classpath://visitors-creation-script.sql"; - - @CatalogConfig(label="DB Setup SQL (URL)", priority=1) - public static final ConfigKey DB_SETUP_SQL_URL = ConfigKeys.newConfigKey( - "app.db_sql", "URL to the SQL script to set up the database", - DEFAULT_DB_SETUP_SQL_URL); - - public static final String DB_TABLE = "visitors"; - public static final String DB_USERNAME = "brooklyn"; - public static final String DB_PASSWORD = "br00k11n"; - - - // --------- Custom Sensor -------------------- - - AttributeSensor APPSERVERS_COUNT = Sensors.newIntegerSensor( - "appservers.count", "Number of app servers deployed"); - - - // --------- Initialization ------------------- - - /** Initialize our application. In this case it consists of - * a single DB, with a load-balanced cluster (nginx + multiple JBosses, by default), - * with some sensors and a policy. */ - @Override - public void init() { - DatastoreCommon db = addChild( - EntitySpec.create(MySqlNode.class) - .configure(MySqlNode.CREATION_SCRIPT_URL, Entities.getRequiredUrlConfig(this, DB_SETUP_SQL_URL))); - - ControlledDynamicWebAppCluster web = addChild( - EntitySpec.create(ControlledDynamicWebAppCluster.class) - // set WAR to use, and port to use - .configure(JavaWebAppService.ROOT_WAR, getConfig(WAR_URL)) - .configure(WebAppService.HTTP_PORT, PortRanges.fromString("8080+")) - -// // optionally - use Tomcat instead of JBoss (default: -// .configure(ControlledDynamicWebAppCluster.MEMBER_SPEC, EntitySpec.create(TomcatServer.class)) - - // inject a JVM system property to point to the DB - .configure(JavaEntityMethods.javaSysProp("brooklyn.example.db.url"), - formatString("jdbc:%s%s?user=%s\\&password=%s", - attributeWhenReady(db, DatastoreCommon.DATASTORE_URL), DB_TABLE, DB_USERNAME, DB_PASSWORD)) - - // start with 2 appserver nodes, initially - .configure(DynamicCluster.INITIAL_SIZE, 2) - ); - - // add an enricher which measures latency - web.addEnricher(HttpLatencyDetector.builder(). - url(WebAppServiceConstants.ROOT_URL). - rollup(10, TimeUnit.SECONDS). - build()); - - // add a policy which scales out based on Reqs/Sec - web.getCluster().addPolicy(AutoScalerPolicy.builder(). - metric(DynamicWebAppCluster.REQUESTS_PER_SECOND_IN_WINDOW_PER_NODE). - metricRange(10, 100). - sizeRange(2, 5). - build()); - - // add a few more sensors at the top-level (KPI's at the root of the application) - addEnricher(SensorPropagatingEnricher.newInstanceListeningTo(web, - WebAppServiceConstants.ROOT_URL, - DynamicWebAppCluster.REQUESTS_PER_SECOND_IN_WINDOW, - HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_IN_WINDOW)); - addEnricher(SensorTransformingEnricher.newInstanceTransforming(web, - DynamicWebAppCluster.GROUP_SIZE, Functions.identity(), APPSERVERS_COUNT)); - } - -} http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/sample/app/SingleWebServerSample.java ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/sample/app/SingleWebServerSample.java b/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/sample/app/SingleWebServerSample.java deleted file mode 100644 index 3cc9426..0000000 --- a/archetypes/quickstart/src/brooklyn-sample/src/main/java/com/acme/sample/brooklyn/sample/app/SingleWebServerSample.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.acme.sample.brooklyn.sample.app; - -import org.apache.brooklyn.api.entity.EntitySpec; -import org.apache.brooklyn.core.entity.AbstractApplication; -import org.apache.brooklyn.core.entity.Attributes; -import org.apache.brooklyn.core.location.PortRanges; -import org.apache.brooklyn.entity.webapp.JavaWebAppService; -import org.apache.brooklyn.entity.webapp.jboss.JBoss7Server; -import org.apache.brooklyn.util.maven.MavenArtifact; -import org.apache.brooklyn.util.maven.MavenRetriever; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** This example starts one web app on 8080. */ -public class SingleWebServerSample extends AbstractApplication { - - public static final Logger LOG = LoggerFactory.getLogger(SingleWebServerSample.class); - - public static final String DEFAULT_WAR_URL = - // can supply any URL -- this loads a stock example from maven central / sonatype - MavenRetriever.localUrl(MavenArtifact.fromCoordinate("io.brooklyn.example:brooklyn-example-hello-world-sql-webapp:war:0.5.0")); - - /** Initialize our application. In this case it consists of - * a single JBoss entity, configured to run the WAR above. */ - @Override - public void init() { - addChild(EntitySpec.create(JBoss7Server.class) - .configure(JavaWebAppService.ROOT_WAR, DEFAULT_WAR_URL) - .configure(Attributes.HTTP_PORT, PortRanges.fromString("8080+"))); - } - -} http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/main/resources/logback-custom.xml ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/main/resources/logback-custom.xml b/archetypes/quickstart/src/brooklyn-sample/src/main/resources/logback-custom.xml deleted file mode 100644 index 5a1ec98..0000000 --- a/archetypes/quickstart/src/brooklyn-sample/src/main/resources/logback-custom.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/main/resources/visitors-creation-script.sql ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/main/resources/visitors-creation-script.sql b/archetypes/quickstart/src/brooklyn-sample/src/main/resources/visitors-creation-script.sql deleted file mode 100644 index 0a805c4..0000000 --- a/archetypes/quickstart/src/brooklyn-sample/src/main/resources/visitors-creation-script.sql +++ /dev/null @@ -1,35 +0,0 @@ --- --- 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. --- -create database visitors; -use visitors; -create user 'brooklyn' identified by 'br00k11n'; -grant usage on *.* to 'brooklyn'@'%' identified by 'br00k11n'; -# ''@localhost is sometimes set up, overriding brooklyn@'%', so do a second explicit grant -grant usage on *.* to 'brooklyn'@'localhost' identified by 'br00k11n'; -grant all privileges on visitors.* to 'brooklyn'@'%'; -flush privileges; - -CREATE TABLE MESSAGES ( - id INT NOT NULL AUTO_INCREMENT, - NAME VARCHAR(30) NOT NULL, - MESSAGE VARCHAR(400) NOT NULL, - PRIMARY KEY (ID) - ); - -INSERT INTO MESSAGES values (default, 'Isaac Asimov', 'I grew up in Brooklyn' ); http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/MySampleTest.java ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/MySampleTest.java b/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/MySampleTest.java new file mode 100644 index 0000000..0699189 --- /dev/null +++ b/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/MySampleTest.java @@ -0,0 +1,36 @@ +package com.acme.sample.brooklyn; + +import static org.testng.Assert.assertEquals; + +import org.apache.brooklyn.api.entity.EntitySpec; +import org.apache.brooklyn.api.location.Location; +import org.apache.brooklyn.core.entity.Attributes; +import org.apache.brooklyn.core.entity.EntityAsserts; +import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; +import org.testng.annotations.Test; + +import com.acme.sample.brooklyn.MySample; +import com.google.common.collect.ImmutableList; + +public class MySampleTest extends BrooklynAppUnitTestSupport { + + @Test + public void testEntity() throws Exception { + MySample entity = app.createAndManageChild(EntitySpec.create(MySample.class) + .configure(MySample.MY_CONF, "myConfVal")); + + app.start(ImmutableList.of()); + assertEquals(entity.sensors().get(MySample.LAST_CALL), "start"); + EntityAsserts.assertAttributeEqualsEventually(entity, Attributes.SERVICE_UP, true); + EntityAsserts.assertAttributeEqualsEventually(entity, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); + + entity.myEffector("myArgVal"); + assertEquals(entity.sensors().get(MySample.LAST_CALL), "myEffector(myArgVal)"); + + app.stop(); + assertEquals(entity.sensors().get(MySample.LAST_CALL), "stop"); + EntityAsserts.assertAttributeEqualsEventually(entity, Attributes.SERVICE_UP, false); + EntityAsserts.assertAttributeEqualsEventually(entity, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.STOPPED); + } +} http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/MySampleYamlTest.java ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/MySampleYamlTest.java b/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/MySampleYamlTest.java new file mode 100644 index 0000000..ba1156e --- /dev/null +++ b/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/MySampleYamlTest.java @@ -0,0 +1,73 @@ +package com.acme.sample.brooklyn; + +import static org.testng.Assert.assertEquals; + +import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.camp.brooklyn.AbstractYamlTest; +import org.apache.brooklyn.core.entity.Attributes; +import org.apache.brooklyn.core.entity.Entities; +import org.apache.brooklyn.core.entity.EntityAsserts; +import org.apache.brooklyn.util.core.ResourceUtils; +import org.testng.annotations.Test; + +import com.google.common.base.Joiner; +import com.google.common.collect.Iterables; + +public class MySampleYamlTest extends AbstractYamlTest { + + @Test + public void testEntity() throws Exception { + String yaml = Joiner.on("\n").join( + "name: my test", + "services:", + "- type: com.acme.sample.brooklyn.MySample", + " brooklyn.config:", + " myConf: myConfVal"); + + Entity app = createAndStartApplication(yaml); + waitForApplicationTasks(app); + + Entities.dumpInfo(app); + + MySample entity = (MySample) Iterables.getOnlyElement(app.getChildren()); + assertEquals(entity.config().get(MySample.MY_CONF), "myConfVal"); + } + + @Test + public void testUsingTestFramework() throws Exception { + String yaml = ResourceUtils.create().getResourceAsString("classpath://sample-test.yaml"); + + Entity app = createAndStartApplication(yaml); + waitForApplicationTasks(app); + + Entities.dumpInfo(app); + + EntityAsserts.assertAttributeEqualsEventually(app, Attributes.SERVICE_UP, true); + } + + /** + * For this to pass, the sample.bom's library URL needs to be updated, to point at + * the jar built using {@code mvn clean install} on this project. + * + * The group "WIP" (short for "work in progress") disables this test when run by maven. + */ + @Test(groups="WIP") + public void testViaCatalog() throws Exception { + String catalogYaml = ResourceUtils.create().getResourceAsString("classpath://sample.bom"); + + addCatalogItems(catalogYaml); + + String blueprintYaml = Joiner.on("\n").join( + "name: my test", + "services:", + "- type: com.acme.sample.brooklyn.MySampleInCatalog:1.0"); + + Entity app = createAndStartApplication(blueprintYaml); + waitForApplicationTasks(app); + + Entities.dumpInfo(app); + + MySample entity = (MySample) Iterables.getOnlyElement(app.getChildren()); + EntityAsserts.assertAttributeEqualsEventually(entity, Attributes.SERVICE_UP, true); + } +} http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/sample/app/SampleLocalhostIntegrationTest.java ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/sample/app/SampleLocalhostIntegrationTest.java b/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/sample/app/SampleLocalhostIntegrationTest.java deleted file mode 100644 index f640f6a..0000000 --- a/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/sample/app/SampleLocalhostIntegrationTest.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.acme.sample.brooklyn.sample.app; - -import java.util.Arrays; -import java.util.Iterator; - -import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.api.entity.EntitySpec; -import org.apache.brooklyn.api.mgmt.ManagementContext; -import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; -import org.apache.brooklyn.core.entity.Entities; -import org.apache.brooklyn.core.entity.StartableApplication; -import org.apache.brooklyn.entity.webapp.JavaWebAppService; -import org.apache.brooklyn.util.core.ResourceUtils; -import org.apache.brooklyn.util.text.Strings; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -/** - * Sample integration tests which show how to launch the sample applications on localhost, - * make some assertions about them, and then destroy them. - */ -@Test(groups="Integration") -public class SampleLocalhostIntegrationTest { - - private static final Logger log = LoggerFactory.getLogger(SampleLocalhostIntegrationTest.class); - - private ManagementContext mgmt; - - @BeforeMethod(alwaysRun=true) - public void setup() { - mgmt = new LocalManagementContext(); - } - - @AfterMethod(alwaysRun=true) - public void shutdown() { - if (mgmt != null) Entities.destroyAll(mgmt); - } - - - public void testSingle() { - StartableApplication app = mgmt.getEntityManager().createEntity( - EntitySpec.create(StartableApplication.class, SingleWebServerSample.class)); - Entities.startManagement(app, mgmt); - Entities.start(app, Arrays.asList(mgmt.getLocationRegistry().resolve("localhost"))); - - Iterator children = app.getChildren().iterator(); - if (!children.hasNext()) Assert.fail("Should have had a single JBoss child; had none"); - - Entity web = children.next(); - - if (children.hasNext()) Assert.fail("Should have had a single JBoss child; had too many: "+app.getChildren()); - - String url = web.getAttribute(JavaWebAppService.ROOT_URL); - Assert.assertNotNull(url); - - String page = new ResourceUtils(this).getResourceAsString(url); - log.info("Read web page for "+app+" from "+url+":\n"+page); - Assert.assertTrue(!Strings.isBlank(page)); - } - - public void testCluster() { - StartableApplication app = mgmt.getEntityManager().createEntity( - EntitySpec.create(StartableApplication.class, ClusterWebServerDatabaseSample.class)); - Entities.startManagement(app, mgmt); - Entities.start(app, Arrays.asList(mgmt.getLocationRegistry().resolve("localhost"))); - - log.debug("APP is started"); - - String url = app.getAttribute(JavaWebAppService.ROOT_URL); - Assert.assertNotNull(url); - - String page = new ResourceUtils(this).getResourceAsString(url); - log.info("Read web page for "+app+" from "+url+":\n"+page); - Assert.assertTrue(!Strings.isBlank(page)); - } - -} http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/sample/app/SampleUnitTest.java ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/sample/app/SampleUnitTest.java b/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/sample/app/SampleUnitTest.java deleted file mode 100644 index 6a34301..0000000 --- a/archetypes/quickstart/src/brooklyn-sample/src/test/java/com/acme/sample/brooklyn/sample/app/SampleUnitTest.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.acme.sample.brooklyn.sample.app; - -import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.api.entity.EntitySpec; -import org.apache.brooklyn.api.mgmt.ManagementContext; -import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; -import org.apache.brooklyn.core.entity.Entities; -import org.apache.brooklyn.core.entity.StartableApplication; -import org.apache.brooklyn.entity.database.DatastoreMixins.DatastoreCommon; -import org.apache.brooklyn.entity.database.mysql.MySqlNode; -import org.apache.brooklyn.entity.webapp.JavaWebAppService; -import org.apache.brooklyn.entity.webapp.WebAppService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import com.google.common.base.Predicates; -import com.google.common.collect.Iterables; - -/** - * Unit tests for the sample applications defined in this project. - * Shows how to examine the spec and make assertions about configuration. - */ -@Test -public class SampleUnitTest { - - private static final Logger log = LoggerFactory.getLogger(SampleUnitTest.class); - - - private ManagementContext mgmt; - - @BeforeMethod(alwaysRun=true) - public void setup() { - mgmt = new LocalManagementContext(); - } - - @AfterMethod(alwaysRun=true) - public void shutdown() { - if (mgmt != null) Entities.destroyAll(mgmt); - } - - - public void testSampleSingleStructure() { - StartableApplication app = mgmt.getEntityManager().createEntity( - EntitySpec.create(StartableApplication.class, SingleWebServerSample.class)); - log.info("app from spec is: "+app); - - Assert.assertEquals(app.getChildren().size(), 1); - Assert.assertNotNull( app.getChildren().iterator().next().getConfig(JavaWebAppService.ROOT_WAR) ); - } - - public void testSampleClusterStructure() { - StartableApplication app = mgmt.getEntityManager().createEntity( - EntitySpec.create(StartableApplication.class, ClusterWebServerDatabaseSample.class)); - log.info("app from spec is: "+app); - - Assert.assertEquals(app.getChildren().size(), 2); - - Entity webappCluster = Iterables.find(app.getChildren(), Predicates.instanceOf(WebAppService.class)); - Entity database = Iterables.find(app.getChildren(), Predicates.instanceOf(DatastoreCommon.class)); - - Assert.assertNotNull( webappCluster.getConfig(JavaWebAppService.ROOT_WAR) ); - Assert.assertNotNull( database.getConfig(MySqlNode.CREATION_SCRIPT_URL) ); - } - -} http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/test/resources/sample-test.yaml ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/test/resources/sample-test.yaml b/archetypes/quickstart/src/brooklyn-sample/src/test/resources/sample-test.yaml new file mode 100644 index 0000000..e209da6 --- /dev/null +++ b/archetypes/quickstart/src/brooklyn-sample/src/test/resources/sample-test.yaml @@ -0,0 +1,43 @@ +name: my test +services: +- type: com.acme.sample.brooklyn.MySample + id: sample + +- type: org.apache.brooklyn.test.framework.TestCase + brooklyn.children: + + # Starting + - type: org.apache.brooklyn.test.framework.TestSensor + targetId: sample + sensor: service.isUp + timeout: 10s + assert: + equalTo: true + - type: org.apache.brooklyn.test.framework.TestSensor + targetId: sample + sensor: lastCall + assert: + equalTo: "start" + + # Call our example effector + - type: org.apache.brooklyn.test.framework.TestEffector + targetId: sample + effector: myEffector + params: + arg1: "myVal" + - type: org.apache.brooklyn.test.framework.TestSensor + targetId: sample + sensor: lastCall + assert: + equalTo: "myEffector(myVal)" + + # Stopping + - type: org.apache.brooklyn.test.framework.TestEffector + targetId: sample + effector: stop + - type: org.apache.brooklyn.test.framework.TestSensor + targetId: sample + sensor: service.isUp + timeout: 10s + assert: + equalTo: false http://git-wip-us.apache.org/repos/asf/brooklyn-dist/blob/4545ac2a/archetypes/quickstart/src/brooklyn-sample/src/test/resources/sample.bom ---------------------------------------------------------------------- diff --git a/archetypes/quickstart/src/brooklyn-sample/src/test/resources/sample.bom b/archetypes/quickstart/src/brooklyn-sample/src/test/resources/sample.bom new file mode 100644 index 0000000..adda846 --- /dev/null +++ b/archetypes/quickstart/src/brooklyn-sample/src/test/resources/sample.bom @@ -0,0 +1,12 @@ +brooklyn.catalog: + id: com.acme.sample.brooklyn.MySampleInCatalog + version: 1.0 + description: My Sample + displayName: MySample + iconUrl: classpath:///sample-icon.png + itemType: template + libraries: + - file:///Path/to/archetype/sample/target/brooklyn-sample-0.1.0-SNAPSHOT.jar + item: + services: + - type: com.acme.sample.brooklyn.MySample