brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ahgittin <...@git.apache.org>
Subject [GitHub] incubator-brooklyn pull request: Implementation of proposed Test E...
Date Wed, 11 Nov 2015 09:36:23 GMT
Github user ahgittin commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/999#discussion_r44515398
  
    --- Diff: usage/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java
---
    @@ -0,0 +1,107 @@
    +package org.apache.brooklyn.test.framework;
    +
    +import com.google.api.client.util.Objects;
    +import com.google.common.collect.ImmutableMap;
    +import com.google.common.collect.Lists;
    +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.util.core.flags.TypeCoercions;
    +import org.apache.brooklyn.util.exceptions.Exceptions;
    +import org.apache.brooklyn.util.time.Duration;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.util.Collection;
    +import java.util.Map;
    +
    +import static org.apache.brooklyn.util.http.HttpAsserts.assertContentEventuallyContainsText;
    +import static org.apache.brooklyn.util.http.HttpAsserts.assertContentEventuallyMatches;
    +import static org.apache.brooklyn.util.http.HttpAsserts.assertHttpStatusCodeEventuallyEquals;
    +
    +/**
    + * {@inheritDoc}
    + */
    +public class TestHttpCallImpl extends AbstractEntity implements TestHttpCall {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(TestHttpCallImpl.class);
    +
    +    /**
    +     * {@inheritDoc}
    +     */
    +    public void start(Collection<? extends Location> locations) {
    +        ServiceStateLogic.setExpectedState(this, Lifecycle.STARTING);
    +        final String url = getConfig(TARGET_URL);
    +        final Map assertions = getConfig(ASSERTIONS);
    +        final Duration timeout = getConfig(TIMEOUT);
    +        try {
    +            checkAssertions(url.toString(), ImmutableMap.of("timeout", timeout), assertions);
    +            sensors().set(SERVICE_UP, true);
    +            ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING);
    +        } catch (Throwable t) {
    +            LOG.info("Url [{}] test failed", url);
    +            sensors().set(SERVICE_UP, false);
    +            ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE);
    +            throw Exceptions.propagate(t);
    +        }
    +    }
    +
    +    /**
    +     * Tests HTTP Request reponse matches assertions
    +     * <p>
    +     * Supported keys in the <code>assertions</code> {@link Map} include:
    +     * <ul>
    +     * <li>string - simple string match
    +     * <li>regex - uses {@link java.lang.String#matches(String)}, if the url returns
a multi-line response you should
    +     * use the embedded dotall flag expression <code>(?s)</code> in your
regex.
    +     * <li>status - HTTP status code
    +     * </ul>
    +     * Wraps the {@link org.apache.brooklyn.util.http.HttpAsserts} immediate assertion
methods.
    +     * <p>
    +     * See the test/resources directory for examples.
    +     *
    +     * @param url        The target URL to be tested
    +     * @param flags      Passed to {@link org.apache.brooklyn.util.http.HttpAsserts#assertContentEventuallyContainsText(Map,
String, String, String...)},
    +     *                   {@link org.apache.brooklyn.util.http.HttpAsserts#assertContentEventuallyMatches(Map,
String, String)},
    +     *                   {@link org.apache.brooklyn.util.http.HttpAsserts#assertHttpStatusCodeEventuallyEquals(Map,
String, int)}
    +     *
    +     * @param assertions The map of assertions
    +     */
    +    private void checkAssertions(final String url, final Map<String,?> flags, final
Map<?, ?> assertions) {
    --- End diff --
    
    this logic should be pulled out to a convenience class, e.g. `TestFrameworkAssertions`
    
    in time i'd like to make it so the set of assertions is extensible using the catalog (this
is the work I've been doing with `BrooklynTypeRegister.createBean`)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message