Skip to content

Commit

Permalink
Added additional Await steps
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyBuzdin committed Jun 18, 2016
1 parent 241b998 commit ec70e05
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void run(SampleConfiguration configuration, Environment environment) {
Injector injector = Guice.createInjector();
environment.addResource(injector.getInstance(GadgetResource.class));
environment.addResource(injector.getInstance(StaticTypesResource.class));
environment.addResource(injector.getInstance(CustomHeadersResource.class));
environment.addHealthCheck(injector.getInstance(SampleHealthCheck.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package lv.ctco.cukesrest.healthcheck;

import javax.ws.rs.*;
import javax.ws.rs.core.*;

@Path(CustomHeadersResource.API)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class CustomHeadersResource {

protected static final String API = "/customHeaders";

@GET
public Response headers() {
return Response.ok()
.header("Custom-Header", "2000000000000")
.build();
}
}
79 changes: 79 additions & 0 deletions cukes-rest/src/main/java/lv/ctco/cukesrest/api/AwaitSteps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package lv.ctco.cukesrest.api;

import com.google.inject.*;
import cucumber.api.java.en.*;
import lv.ctco.cukesrest.internal.*;
import lv.ctco.cukesrest.internal.helpers.*;

@Singleton
public class AwaitSteps {

@Inject
RequestSpecificationFacade facade;

@Given("^should wait at most (\\d+) ([^ ]+) with interval (\\d+) ([^ ]+) until status code (\\d+)$")
public void should_wait_at_most_until_status_code_with_interval
(int atMostTime, String atMostUnit,
int intervalTime, String intervalUnit,
int statusCode) {
facade.shouldWaitWithIntervalUntilStatusCodeReceived(
Time.of(atMostTime, atMostUnit),
Time.of(intervalTime, intervalUnit),
statusCode);
}

@Given("^should wait at most (\\d+) ([^ ]+) with interval (\\d+) ([^ ]+) until status code (\\d+) or fail with \"([^\"]+)\"$")
public void should_wait_at_most_until_status_code_with_interval_or_fail_with
(int atMostTime, String atMostUnit,
int intervalTime, String intervalUnit,
int statusCode, int failStatusCode) {
facade.shouldWaitWithIntervalUntilStatusCodeReceived(
Time.of(atMostTime, atMostUnit),
Time.of(intervalTime, intervalUnit),
statusCode, failStatusCode);
}

@Given("^should wait at most (\\d+) ([^ ]+) with interval (\\d+) ([^ ]+) until property \"([^\"]+)\" equal to \"([^\"]+)\"$")
public void should_wait_at_most_until_property_equals_with_interval
(int atMostTime, String atMostUnit,
int intervalTime, String intervalUnit,
String property, String value) {
facade.shouldWaitWithIntervalUntilPropertyEqualToValue(
Time.of(atMostTime, atMostUnit),
Time.of(intervalTime, intervalUnit),
property, value);
}

@Given("^should wait at most (\\d+) ([^ ]+) with interval (\\d+) ([^ ]+) until property \"([^\"]+)\" equal to \"([^\"]+)\" or fail with \"([^\"]+)\"$")
public void should_wait_at_most_until_property_equals_with_interval_or_fail_with
(int atMostTime, String atMostUnit,
int intervalTime, String intervalUnit,
String property, String value, String failValue) {
facade.shouldWaitWithIntervalUntilPropertyEqualToValue(
Time.of(atMostTime, atMostUnit),
Time.of(intervalTime, intervalUnit),
property, value, failValue);
}

@Given("^should wait at most (\\d+) ([^ ]+) with interval (\\d+) ([^ ]+) until header \"([^\"]+)\" equal to \"([^\"]+)\"$")
public void should_wait_at_most_until_header_equals_with_interval
(int atMostTime, String atMostUnit,
int intervalTime, String intervalUnit,
String property, String value) {
facade.shouldWaitWithIntervalUntilHeaderEqualToValue(
Time.of(atMostTime, atMostUnit),
Time.of(intervalTime, intervalUnit),
property, value);
}

@Given("^should wait at most (\\d+) ([^ ]+) with interval (\\d+) ([^ ]+) until header \"([^\"]+)\" equal to \"([^\"]+)\" or fail with \"([^\"]+)\"$")
public void should_wait_at_most_until_header_equals_with_interval_or_fail_with
(int atMostTime, String atMostUnit,
int intervalTime, String intervalUnit,
String property, String value, String failValue) {
facade.shouldWaitWithIntervalUntilHeaderEqualToValue(
Time.of(atMostTime, atMostUnit),
Time.of(intervalTime, intervalUnit),
property, value, failValue);
}
}
35 changes: 0 additions & 35 deletions cukes-rest/src/main/java/lv/ctco/cukesrest/api/GivenSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lv.ctco.cukesrest.*;
import lv.ctco.cukesrest.internal.*;
import lv.ctco.cukesrest.internal.context.*;
import lv.ctco.cukesrest.internal.helpers.*;
import lv.ctco.cukesrest.internal.resources.*;

@Singleton
Expand Down Expand Up @@ -109,38 +108,4 @@ public void authentication(String username, String password) {
public void authentication(String authenticationType) {
facade.authenticationType(authenticationType);
}

@Given("^should wait at most (\\d+) ([^ ]+) with interval (\\d+) ([^ ]+) until status code (\\d+)$")
public void should_wait_at_most_seconds_until_status_code_with_interval_second
(int atMostTime, String atMostUnit,
int intervalTime, String intervalUnit,
int statusCode) {
facade.shouldWaitWithIntervalUntilStatusCodeReceived(
Time.of(atMostTime, atMostUnit),
Time.of(intervalTime, intervalUnit),
statusCode);
}

@Given("^should wait at most (\\d+) ([^ ]+) with interval (\\d+) ([^ ]+) until property \"([^\"]+)\" equal to \"([^\"]+)\"$")
public void should_wait_at_most_until_property_equals_with_interval
(int atMostTime, String atMostUnit,
int intervalTime, String intervalUnit,
String property, String value) {
facade.shouldWaitWithIntervalUntilPropertyEqualToValue(
Time.of(atMostTime, atMostUnit),
Time.of(intervalTime, intervalUnit),
property, value);
}

@Given("^should wait at most (\\d+) ([^ ]+) with interval (\\d+) ([^ ]+) until property \"([^\"]+)\" equal to \"([^\"]+)\" or fail with \"([^\"]+)\"$")
public void should_wait_at_most_until_property_equals_or_fail_with_interval
(int atMostTime, String atMostUnit,
int intervalTime, String intervalUnit,
String property, String value, String failValue) {
facade.shouldWaitWithIntervalUntilPropertyEqualToValue(
Time.of(atMostTime, atMostUnit),
Time.of(intervalTime, intervalUnit),
property, value, failValue);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,27 @@ public void initNewSpecification() {
}

public void shouldWaitWithIntervalUntilStatusCodeReceived(Time waitTime, Time interval, int sCode) {
this.awaitCondition = new AwaitCondition(waitTime, interval, statusCode(equalTo(sCode)));
this.awaitCondition = new AwaitCondition(waitTime, interval, aStatusCode(equalTo(sCode)));
}

public void shouldWaitWithIntervalUntilStatusCodeReceived(Time waitTime, Time interval, int sCode, int failCode) {
this.awaitCondition = new AwaitCondition(waitTime, interval, aStatusCode(equalTo(sCode)), aStatusCode(equalTo(failCode)));
}

public void shouldWaitWithIntervalUntilPropertyEqualToValue(Time waitTime, Time interval, String property, String value) {
this.awaitCondition = new AwaitCondition(waitTime, interval, property(property, equalTo(value)));
this.awaitCondition = new AwaitCondition(waitTime, interval, aProperty(property, equalTo(value)));
}

public void shouldWaitWithIntervalUntilPropertyEqualToValue(Time waitTime, Time interval, String property, String value, String failValue) {
this.awaitCondition = new AwaitCondition(waitTime, interval, property(property, equalTo(value)), property(property, equalTo(failValue)));
this.awaitCondition = new AwaitCondition(waitTime, interval, aProperty(property, equalTo(value)), aProperty(property, equalTo(failValue)));
}

public void shouldWaitWithIntervalUntilHeaderEqualToValue(Time waitTime, Time interval, String header, String value) {
this.awaitCondition = new AwaitCondition(waitTime, interval, aHeader(header, equalTo(value)));
}

public void shouldWaitWithIntervalUntilHeaderEqualToValue(Time waitTime, Time interval, String header, String value, String failValue) {
this.awaitCondition = new AwaitCondition(waitTime, interval, aHeader(header, equalTo(value)), aHeader(header, equalTo(failValue)));
}

public AwaitCondition awaitCondition() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import org.hamcrest.*;

import static lv.ctco.cukesrest.internal.matchers.JsonMatchers.*;
import static org.hamcrest.Matchers.*;

public class ResponseMatcher {

public static Matcher<ResponseWrapper> property(final String path, final Matcher<?> matcher) {
// TODO
public static Matcher<ResponseWrapper> aProperty(final String path, final Matcher<?> matcher) {
return new TypeSafeMatcher<ResponseWrapper>() {

@Override
Expand All @@ -29,11 +29,28 @@ protected void describeMismatchSafely(ResponseWrapper item, Description mismatch
};
}

public static Matcher<ResponseWrapper> statusCode(final int statusCode) {
return statusCode(equalTo(statusCode));
public static Matcher<ResponseWrapper> aHeader(final String header, final Matcher<?> matcher) {
return new TypeSafeMatcher<ResponseWrapper>() {

@Override
protected boolean matchesSafely(ResponseWrapper response) {
String actualHeaderValue = response.getResponse().header(header);
return matcher.matches(actualHeaderValue);
}

@Override
public void describeTo(Description description) {
// description.appendText("has statusCode").appendDescriptionOf(statusCodeMatches);
}

@Override
protected void describeMismatchSafely(ResponseWrapper item, Description mismatchDescription) {
// mismatchDescription.appendText("statusCode<").appendValue(item.statusCode()+"").appendText(">");
}
};
}

public static Matcher<ResponseWrapper> statusCode(final Matcher<Integer> statusCodeMatches) {
public static Matcher<ResponseWrapper> aStatusCode(final Matcher<Integer> statusCodeMatches) {
return new TypeSafeMatcher<ResponseWrapper>() {

@Override
Expand Down

0 comments on commit ec70e05

Please sign in to comment.