Skip to content

Commit

Permalink
bastion-dev#71: Removed the thenDo() fluent-builder method.
Browse files Browse the repository at this point in the history
  • Loading branch information
KPull committed Jan 23, 2017
1 parent d61ec2b commit aa20723
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 144 deletions.
28 changes: 4 additions & 24 deletions src/main/java/rocks/bastion/Bastion.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import rocks.bastion.core.Assertions;
import rocks.bastion.core.BastionFactory;
import rocks.bastion.core.Callback;
import rocks.bastion.core.HttpRequest;
import rocks.bastion.core.builder.BastionBuilder;
import rocks.bastion.core.builder.ExecuteRequestBuilder;
import rocks.bastion.core.builder.PostExecutionBuilder;
import rocks.bastion.core.configuration.BastionConfigurationLoader;
import rocks.bastion.core.configuration.Configuration;
import rocks.bastion.core.configuration.GlobalRequestAttributes;
import rocks.bastion.core.json.JsonRequest;
import rocks.bastion.core.resource.ResourceLoader;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -38,7 +35,7 @@
* <p>
* Inside your test method or test case, make a call to the {@link Bastion#request(String, HttpRequest)} method. This will
* return a <a href="https://en.wikipedia.org/wiki/Fluent_interface">fluent-builder style interface</a> that will allow you
* to further specify the response model, assertions and callbacks on your test.
* to further specify the response model and assertions on your test.
* </p>
* <p>
* The returned builder will allow you call any of the following methods:
Expand All @@ -48,14 +45,12 @@
* interpret and decode the received response object into an instance of whatever type is supplied to this method.</li>
* <li>{@link rocks.bastion.core.builder.AssertionsBuilder#withAssertions(Assertions)}: Specify what test assertions to apply to the response. We recommend supplying the
* assertions as a lambda or using one of the available subclass implementations of {@link Assertions}.</li>
* <li>{@link rocks.bastion.core.builder.CallbackBuilder#thenDo(Callback)}: Specify a callback function to execute when the response is received and it passes its assertions. We
* recommend supplying the {@link Callback} as a lambda function.</li>
* <li>{@link ExecuteRequestBuilder#call()}: Starts the Bastion test by executing the HTTP request.</li>
* </ul>
* <p>
* You cannot call any of the methods above before any of the methods listed before it. Therefore, in your test, you should call
* the methods above one after each other as listed above: you can skip any of the methods and Bastion will assign defaults.
* For example, if you want to make an HTTP request, apply some assertions without binding a model or using a callback, you would
* For example, if you want to make an HTTP request and apply some assertions without binding a model, you would
* call the {@link #request(String, HttpRequest)} method first, followed by the {@link rocks.bastion.core.builder.AssertionsBuilder#withAssertions(Assertions)}
* method to configure your assertions, followed by the {@link ExecuteRequestBuilder#call()} method to send the request
* and start the test.
Expand Down Expand Up @@ -125,21 +120,6 @@
* {@link rocks.bastion.core.builder.AssertionsBuilder#withAssertions(Assertions)} method of the Bastion test builder as
* a Java 8 lambda.
* </p>
* <h1>Callbacks</h1>
* <p>
* {@link Callback Callback objects} can be provided for a Bastion test using the {@link rocks.bastion.core.builder.CallbackBuilder#thenDo(Callback)}
* method. Callbacks are executed right after the test passes its assertions. Using a callback you may perform additional
* post processing to the response object which is not necessarily related to test assertions. This can include logging,
* setting variables for use further in your test and so on.
* </p>
* <p>You are encouraged to define your own {@link Callback callback} implementations. Just like requests and assertions,
* this promotes code reuse and maintainability especially if you are performing an action multiple times across your
* Bastion tests.</p>
* <p>
* If you do not want to implement your own callback classes though you can supply a callback object directly into the
* {@link rocks.bastion.core.builder.CallbackBuilder#thenDo(Callback)} method of the Bastion test builder as a Java 8
* lambda.
* </p>
* <h1>Groovy Tests</h1>
* <p>
* Certain features of Bastion such as the {@link rocks.bastion.core.json.JsonRequest} and the {@link rocks.bastion.core.json.JsonResponseAssertions}
Expand Down Expand Up @@ -170,7 +150,7 @@ public final class Bastion {
*
* @param message A descriptive message for this Bastion test.
* @param request The HTTP request that Bastion will execute for this test.
* @return A fluent-builder object which will let you bind a model type, add assertions, add callbacks and execute the test.
* @return A fluent-builder object which will let you bind a model type, add assertions and execute the test.
*/
public static BastionBuilder<Object> request(String message, HttpRequest request) {
return BastionFactory.getDefaultBastionFactory().getBastion(message, request);
Expand All @@ -187,7 +167,7 @@ public static BastionBuilder<Object> request(String message, HttpRequest request
* </p>
*
* @param request The HTTP request that Bastion will execute for this test.
* @return A fluent-builder object which will let you bind a model type, add assertions, add callbacks and execute the test.
* @return A fluent-builder object which will let you bind a model type, add assertions and execute the test.
*/
public static BastionBuilder<Object> request(HttpRequest request) {
return BastionFactory.getDefaultBastionFactory().getBastion("", request);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/rocks/bastion/core/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* <p>
* Technically, the assertions can be any executable code but it is strongly recommended that a user only performs
* stateless checks on the provided model and response. If you would like to change the state of the current test
* you can use the {@link BastionBuilderImpl#thenDo(Callback)} method using a {@link Callback}. You can also retrieve the returned
* object using the {@link BastionBuilderImpl#getModel()} and {@link BastionBuilderImpl#getResponse()} methods.
* you can retrieve the returned object using the {@link BastionBuilderImpl#getModel()} and {@link BastionBuilderImpl#getResponse()}
* methods for use later on.
*
* @param <M> The type of response model the assertions will apply for.
*/
Expand Down
16 changes: 1 addition & 15 deletions src/main/java/rocks/bastion/core/BastionBuilderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class BastionBuilderImpl<MODEL> implements BastionBuilder<MODEL>, Respons
private Class<MODEL> modelType;
private boolean suppressAssertions;
private Assertions<? super MODEL> assertions;
private Callback<? super MODEL> callback;
private MODEL model;
private ModelResponse<MODEL> modelResponse;
private Configuration configuration;
Expand All @@ -45,7 +44,6 @@ public class BastionBuilderImpl<MODEL> implements BastionBuilder<MODEL>, Respons
modelType = null;
suppressAssertions = false;
assertions = Assertions.noAssertions();
callback = Callback.noCallback();
}

public void addBastionListener(BastionListener newListener) {
Expand Down Expand Up @@ -102,7 +100,6 @@ public PostExecutionBuilder<? extends MODEL> call() {
model = decodeModel(response);
modelResponse = new ModelResponse<>(response, model);
executeAssertions(modelResponse);
executeCallback(modelResponse);
return this;
} catch (AssertionError error) {
notifyListenersCallFailed(new BastionFailureEvent(request, response, error));
Expand All @@ -125,19 +122,12 @@ public <T> AssertionsBuilder<? extends T> bind(Class<T> modelType) {
}

@Override
public CallbackBuilder<? extends MODEL> withAssertions(Assertions<? super MODEL> assertions) {
public ExecuteRequestBuilder<? extends MODEL> withAssertions(Assertions<? super MODEL> assertions) {
Objects.requireNonNull(assertions);
this.assertions = assertions;
return this;
}

@Override
public ExecuteRequestBuilder<? extends MODEL> thenDo(Callback<? super MODEL> callback) {
Objects.requireNonNull(callback);
this.callback = callback;
return this;
}

@Override
public MODEL getModel() {
return model;
Expand All @@ -162,10 +152,6 @@ private String getDescriptiveText() {
}
}

private void executeCallback(ModelResponse<MODEL> modelResponse) {
callback.execute(modelResponse.getStatusCode(), modelResponse, modelResponse.getModel());
}

private void executeAssertions(ModelResponse<MODEL> modelResponse) {
if (!suppressAssertions) {
assertions.execute(modelResponse.getStatusCode(), modelResponse, modelResponse.getModel());
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/rocks/bastion/core/BastionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import rocks.bastion.core.configuration.BastionConfigurationLoader;
import rocks.bastion.core.configuration.Configuration;

import java.util.Objects;

import static java.util.Objects.*;
import static java.util.Objects.requireNonNull;

/**
* Creates and configures an instance of the {@link BastionBuilderImpl} fluent builder. A single factory can be designated as the
Expand Down Expand Up @@ -86,8 +84,8 @@ public BastionBuilder<Object> getBastion(String message, HttpRequest request) {

/**
* Configures whether {@link BastionBuilderImpl} objects returned by this factory should be configured to suppress assertions or
* not. When set to suppress assertions, Bastion will execute the HTTP request as normal as well as any callbacks provided
* but will skip executing any assertions provided to the {@link BastionBuilderImpl#withAssertions(Assertions)} method.
* not. When set to suppress assertions, Bastion will execute the HTTP request as normal but will skip executing any assertions
* provided to the {@link BastionBuilderImpl#withAssertions(Assertions)} method.
*
* @param suppressAssertions {@literal true} to suppress assertions; {@literal false}, otherwise.
*/
Expand Down
38 changes: 0 additions & 38 deletions src/main/java/rocks/bastion/core/Callback.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package rocks.bastion.core.builder;

import rocks.bastion.core.Assertions;
import rocks.bastion.core.Callback;

/**
* Defines the operations which a user can perform on a Bastion builder before any {@link Assertions assertions} object is added to the test.
* At this point, a user may perform one of the following:
* <ul>
* <li>{@link #withAssertions(Assertions)}: Specify what test assertions to apply to the response. We recommend supplying the
* assertions as a lambda or using one of the available subclass implementations of {@link Assertions}.</li>
* <li>{@link #thenDo(Callback)}: Specify a callback function to execute when the response is received and it passes its assertions. We
* recommend supplying the {@link Callback} as a lambda function.</li>
* <li>{@link #call()}: Starts the Bastion test by executing the HTTP request.</li>
* </ul>
* <p>
* After using the {@linkplain #call()} method, the user may obtain the response, for further use in the ongoing test, using
* methods defined in the {@link PostExecutionBuilder} interface.
*/
public interface AssertionsBuilder<MODEL> extends CallbackBuilder<MODEL> {
public interface AssertionsBuilder<MODEL> extends ExecuteRequestBuilder<MODEL> {

/**
* Add an assertions object which determines whether the received response is as expected. We recommend
Expand All @@ -29,8 +26,8 @@ public interface AssertionsBuilder<MODEL> extends CallbackBuilder<MODEL> {
* always pass are set by default.
*
* @param assertions A non-{@literal null} {@linkplain Assertions} object
* @return A fluent-builder which will allow you to add callbacks, execute the HTTP request and retrieve the response information
* @return A fluent-builder which will allow you to execute the HTTP request and retrieve the response information
*/
CallbackBuilder<? extends MODEL> withAssertions(Assertions<? super MODEL> assertions);
ExecuteRequestBuilder<? extends MODEL> withAssertions(Assertions<? super MODEL> assertions);

}
9 changes: 3 additions & 6 deletions src/main/java/rocks/bastion/core/builder/BastionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@

import rocks.bastion.Bastion;
import rocks.bastion.core.Assertions;
import rocks.bastion.core.Callback;
import rocks.bastion.core.HttpRequest;

/**
* The first interface for the Bastion fluent-builder allowing the user to {@link #bind(Class) bind} a model type, apply {@link #withAssertions(Assertions) assertions},
* apply a {@link #thenDo(Callback) callback} function and {@link #call() initiate} the test. This interface serves to group together all the options for Bastion users
* The first interface for the Bastion fluent-builder allowing the user to {@link #bind(Class) bind} a model type, apply {@link #withAssertions(Assertions) assertions}
* and {@link #call() initiate} the test. This interface serves to group together all the options for Bastion users
* immediately after invoking the Bastion builder using {@link Bastion#request(String, HttpRequest)}. Using this builder, a user can:
* <ul>
* <li>{@link #bind(Class)}: Specify which class type to use when constructing a model of the received response. Bastion will
* interpret and decode the received response object into an instance of whatever type is supplied to this method.</li>
* <li>{@link #withAssertions(Assertions)}: Specify what test assertions to apply to the response. We recommend supplying the
* assertions as a lambda or using one of the available subclass implementations of {@link Assertions}.</li>
* <li>{@link #thenDo(Callback)}: Specify a callback function to execute when the response is received and it passes its assertions. We
* recommend supplying the {@link Callback} as a lambda function.</li>
* <li>{@link #call()}: Starts the Bastion test by executing the HTTP request.</li>
* </ul>
* After using the {@linkplain #call()} method, the user may obtain the response, for further use in the ongoing test, using
* methods defined in the {@link PostExecutionBuilder} interface.
* <br>
* Note that the user must perform the steps detailed above, in
* the order they are listed: the user may skip any of the steps but they cannot perform steps out of order (for example, if you
* supply a callback function using {@linkplain #thenDo(Callback)} then you cannot use the {@linkplain #bind(Class)} method afterwards.
* supply an assertion lambda using {@linkplain #withAssertions(Assertions)} then you cannot use the {@linkplain #bind(Class)} method afterwards.
*
* @param <MODEL> The model type which was bound with the {@link #bind(Class)} method.
*/
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/rocks/bastion/core/builder/BindBuilder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rocks.bastion.core.builder;

import rocks.bastion.core.Assertions;
import rocks.bastion.core.Callback;

/**
* Defines the operations that can be performed on a Bastion builder before it has been bound to a model type. Binding the
Expand All @@ -26,8 +25,8 @@ public interface BindBuilder {
* pass in a constructable type (a non-abstract class which can be instantiated) because the various decoders used by
* Bastion, such as the Jackson JSON parser, only work if it can construct an instance of the model.
* <br><br>
* The bound type will be used further on when the user specifies {@link AssertionsBuilder#withAssertions(Assertions) assertions},
* {@link CallbackBuilder#thenDo(Callback) callbacks} or outright retrieves the {@link PostExecutionBuilder#getModel() decoded response model}
* The bound type will be used further on when the user specifies {@link AssertionsBuilder#withAssertions(Assertions)
* assertions} or outright retrieves the {@link PostExecutionBuilder#getModel() decoded response model}
* as the bound model (of the correct type) will be provided by Bastion.
*
* @param modelType A non-{@literal null} class type which will be used when constructing the response model
Expand Down
33 changes: 0 additions & 33 deletions src/main/java/rocks/bastion/core/builder/CallbackBuilder.java

This file was deleted.

Loading

0 comments on commit aa20723

Please sign in to comment.