Skip to content

Commit

Permalink
[wssimulator-0.2.10] -New lastRequest feature (see change log and rea…
Browse files Browse the repository at this point in the history
…dme for more info)
  • Loading branch information
kellizer committed Aug 22, 2016
1 parent ea74397 commit e751b55
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 40 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# 0.2.8 (20/08/2016)
# 0.2.10 (21/08/2016)
- Bug fixes
- Added the ability to retrieve the last request from the route

# 0.2.9 (20/08/2016)
- Bug fixes

# 0.2.8 (29/07/2016)
Expand Down
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ WSSimulator is an open source library that easily allows for you to simulate ext
**Getting Started**

* Java 8
* The dependency from JCenter or the Standalone [distro](https://github.com/CognitiveJ/wssimulator/releases/download/0.2.9/wssimulator-0.2.9.zip "Download Standalone Version")
* The dependency from JCenter or the Standalone [distro](https://github.com/CognitiveJ/wssimulator/releases/download/0.2.10/wssimulator-0.2.10.zip "Download Standalone Version")


To simulate web service calls, you first need to describe the simulation. This process is very easy as Simulations are created in a YAML format and you don't need to 'simulate' much to get start as the only required field for you to define is _path_ and WSSimulator will default the other options.
Expand Down Expand Up @@ -54,15 +54,15 @@ There are 2 ways to use WSSimulator; as a standalone application or as an embedd
WSSimulator is packaged here and supports been executed on both *nix & windows systems;
* For *nix systems
- Unizip the ws-simulator-0.2.9.zip file to a local directory
- Unizip the ws-simulator-0.2.10.zip file to a local directory
- Then Call:
```shell
./wssimulator <options>
```

* For Windows
- Unizip the ws-simulator-0.2.9.zip file to a local directory
- Unizip the ws-simulator-0.2.10.zip file to a local directory
- Then Call:
```shell
./wssimulator.bat <options>
Expand Down Expand Up @@ -96,7 +96,7 @@ repositories {
}
dependencies {
compile "cognitivej:wssimulator:0.2.9"
compile "cognitivej:wssimulator:0.2.10"
...
}
Expand All @@ -107,7 +107,7 @@ repositories {
<dependency>
<groupId>cognitivej</groupId>
<artifactId>wssimulator</artifactId>
<version>0.2.9</version>
<version>0.2.10</version>
<type>pom</type>
</dependency>
```
Expand Down Expand Up @@ -170,4 +170,24 @@ request:
filterType: contains
filter: Hello World
```
_Filters by any request containing the phrase 'Hello World'_
_Filters by any request containing the phrase 'Hello World'_
### Call Count & Last Request
WSSimulator can return the total call count and the last route request body for routes. For example
```java
int addedSimulationId = WSSimulator.addSimulation(new File(getClass().getResource("/lastRequest/request.yml").toURI()))
given().port(port)
.contentType(ContentType.XML)
.body("Last Request - 1")
.post("/hello");

given().port(port)
.contentType(ContentType.XML)
.body("Last Request - 2")
.post("/hello");

WSSimulator.calledCount(addedSimulationId) == 2;
WSSimulator.lastRequest(addedSimulationId) == "Last Request - 2";
```
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ dependencies {
compile "com.intellij:annotations:12.0"
compile 'org.slf4j:slf4j-simple:1.7.21'
compile 'commons-cli:commons-cli:1.3.1'
compile('com.github.fge:json-schema-validator:2.2.6');
compile('com.github.fge:json-schema-validator:2.2.6')

testCompile('org.codehaus.groovy:groovy-all:2.4.6')
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
testCompile 'io.rest-assured:rest-assured:3.0.0'


}

mainClassName = "wssimulator.Bootstrap"
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Jul 06 09:30:23 BST 2016
#Fri Aug 19 14:49:38 BST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14-all.zip
1 change: 1 addition & 0 deletions src/main/java/wssimulator/WSSimulationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,5 @@ public class WSSimulationContext {
* Holds the call count for this simulation
*/
public int callCount;
public String lastRequest;
}
12 changes: 12 additions & 0 deletions src/main/java/wssimulator/WSSimulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,18 @@ public static int calledCount(int simulationId) {
return wsSimulatorServiceManager.calledCounter(simulationId);
}


/**
* Returns the last request for a given simulation.
*
* @param simulationId - the simulation id
* @return the the text of the last request
*/
public static String lastRequest(int simulationId) {
return wsSimulatorServiceManager.lastRequest(simulationId);
}


/**
* Return the ID of a simulation path based on its logical path
*
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/wssimulator/WSSimulatorServiceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ private static WSSimulatorServiceManager startup() {
}



/**
* Adds and starts a web service simulator simulation
*
Expand Down Expand Up @@ -345,7 +344,7 @@ public void shutdown() {
Spark.stop();
validSimulations.clear();
handlers.clear();
counter=0;
counter = 0;
}

/**
Expand Down Expand Up @@ -383,6 +382,18 @@ public int calledCounter(int simulationId) {
return wsSimulation.wsSimulationContext.callCount;
}


/**
* Last request received for an endpoint
*
* @param simulationId
* @return the last request (or null if not yet called)
*/
public String lastRequest(int simulationId) {
WSSimulation wsSimulation = getWSSimulation(simulationId);
return wsSimulation.wsSimulationContext.lastRequest;
}

public int findSimulationIdByPath(@NotNull String path, @NotNull HttpMethod httpMethod) {
return validSimulations.entrySet()
.stream()
Expand Down
1 change: 1 addition & 0 deletions src/main/java/wssimulator/handler/BaseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ public final Object processRequest(@NotNull Request request, @NotNull Response r
WSSimulation wsSimulation = loadSimulation(request);
if (wsSimulation != null) {
wsSimulation.wsSimulationContext.callCount++;
wsSimulation.wsSimulationContext.lastRequest = request.body();
if (!validate(wsSimulation, request.body())) {
LOG.info("Validation failed for request, returning status code:{}", wsSimulation.badRequestResponseCode);
response.status(wsSimulation.badRequestResponseCode);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/wssimulator-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.9
0.2.10
26 changes: 0 additions & 26 deletions src/test/groovy/wssimulator/CalledCounterTestSpecification.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -230,30 +230,4 @@ class CalledCounterTestSpecification extends Specification {
cleanup:
WSSimulator.shutdown()
}

def "simulator Test to lookup path"() {
setup:
int port = TestUtils.randomPort()
when:
WSSimulator.setPort(port)
int addedSimulationId = WSSimulator.addSimulation(new File(getClass().getResource("/simple.yml").toURI()))
int foundSimulationId = WSSimulator.findSimulationId("/hello",HttpMethod.get)
then:
addedSimulationId == foundSimulationId
cleanup:
WSSimulator.shutdown()
}

def "simulator Test to lookup path that is not found"() {
setup:
int port = TestUtils.randomPort()
when:
WSSimulator.setPort(port)
int addedSimulationId = WSSimulator.addSimulation(new File(getClass().getResource("/simple.yml").toURI()))
int foundSimulationId = WSSimulator.findSimulationId("/notfound",HttpMethod.get)
then:
addedSimulationId != foundSimulationId
cleanup:
WSSimulator.shutdown()
}
}
Loading

0 comments on commit e751b55

Please sign in to comment.