Skip to content

Commit

Permalink
Added ANSITerminal back. it is operational alongside the SLF4J for th…
Browse files Browse the repository at this point in the history
…e cases when stubby is running as a standalone jar
  • Loading branch information
azagniotov committed Jan 20, 2018
1 parent deaaae0 commit 2c1712e
Show file tree
Hide file tree
Showing 20 changed files with 393 additions and 98 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ main/.classpath
unit/.classpath
build/*
out/*
**/out/*
.idea/*
.DS_Store
Thumbs.db
Expand Down
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ A highly flexible and configurable tool for testing interactions of SOA applicat
##### Why the word "stubby"?
It is a stub HTTP server after all, hence the "stubby". Also, in Australian slang "stubby" means _beer bottle_

## User manual for stubby4j v5.1.0
## User manual for stubby4j v5.1.1
### Table of contents

* [Quick start example](#quick-start-example)
Expand Down Expand Up @@ -137,8 +137,8 @@ Run `gradle cobertura` command to:
### Third-party dependencies

* __javax.servlet-api-3.1.0.jar__
* jetty-server-9.4.6.v20170531.jar
* jetty-servlets-9.4.6.v20170531.jar
* jetty-server-9.4.8.v20171121.jar
* jetty-servlets-9.4.8.v20171121.jar
* commons-cli-1.2.jar
* snakeyaml-1.17.jar
* jsonassert-1.3.0.jar
Expand All @@ -158,20 +158,20 @@ The following are the stubby4j artifacts that are hosted on [Maven Central][mave

##### Gradle
```xml
compile("io.github.azagniotov:stubby4j:5.1.0")
compile("io.github.azagniotov:stubby4j:5.1.1")
```
or by adding a `classifier` to the JAR name like `no-dependencies` or `no-jetty`, i.e.:

```xml
compile("io.github.azagniotov:stubby4j:5.1.0:no-jetty")
compile("io.github.azagniotov:stubby4j:5.1.1:no-jetty")
```

##### Maven
```xml
<dependency>
<groupId>io.github.azagniotov</groupId>
<artifactId>stubby4j</artifactId>
<version>5.1.0</version>
<version>5.1.1</version>
</dependency>
```
or by adding a `classifier` to the JAR name like `no-dependencies` or `no-jetty`, i.e.:
Expand All @@ -180,7 +180,7 @@ or by adding a `classifier` to the JAR name like `no-dependencies` or `no-jetty`
<dependency>
<groupId>io.github.azagniotov</groupId>
<artifactId>stubby4j</artifactId>
<version>5.1.0</version>
<version>5.1.1</version>
<classifier>no-dependencies</classifier>
</dependency>
```
Expand All @@ -189,17 +189,17 @@ or by adding a `classifier` to the JAR name like `no-dependencies` or `no-jetty`

Run `gradle install` command to:

* Install `stubby4j-5.1.1-SNAPSHOT*.jar` to local `~/.m2/repository`
* All the artifacts will be installed under `~/.m2/repository/{groupId}/{artifactId}/{version}/`, e.g.: `~/.m2/repository/io/github/azagniotov/stubby4j/5.1.1-SNAPSHOT/`
* Install `stubby4j-5.1.2-SNAPSHOT*.jar` to local `~/.m2/repository`
* All the artifacts will be installed under `~/.m2/repository/{groupId}/{artifactId}/{version}/`, e.g.: `~/.m2/repository/io/github/azagniotov/stubby4j/5.1.2-SNAPSHOT/`

Now you can include locally installed stubby4j `SNAPSHOT` artifacts in your project:
```xml
compile("io.github.azagniotov:stubby4j:5.1.1-SNAPSHOT")
compile("io.github.azagniotov:stubby4j:5.1.2-SNAPSHOT")
```
or by adding a `classifier` to the JAR name like `no-dependencie`s or `no-jetty`, i.e.:

```xml
compile("io.github.azagniotov:stubby4j:5.1.1-SNAPSHOT:no-jetty")
compile("io.github.azagniotov:stubby4j:5.1.2-SNAPSHOT:no-jetty")
```


Expand Down Expand Up @@ -1179,7 +1179,11 @@ You can start-up and manage stubby4j with the help of [StubbyClient](main/java/i
### Change log
##### 5.1.1-SNAPSHOT
##### 5.1.2-SNAPSHOT
##### 5.1.1
* Added ANSITerminal back. it is operational alongside the SLF4J for the cases when stubby is running as a standalone jar
* Upgraded from Jetty `9.4.6.v20170531` to `9.4.8.v20171121`
##### 5.1.0
* Pull request #83 - ANSITerminal was replaced with SLF4J. It is up to the stuby4j consumer to choose their own logging implementation (https://github.com/asarkar)
Expand Down
4 changes: 2 additions & 2 deletions conf/gradle/dependency.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// vim: ft=groovy
dependencies {
compile "org.eclipse.jetty:jetty-server:9.4.6.v20170531"
compile "org.eclipse.jetty:jetty-servlets:9.4.6.v20170531"
compile "org.eclipse.jetty:jetty-server:9.4.8.v20171121"
compile "org.eclipse.jetty:jetty-servlets:9.4.8.v20171121"
compile "commons-cli:commons-cli:1.2"
compile "org.yaml:snakeyaml:1.17"
compile "org.skyscreamer:jsonassert:1.3.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,8 @@ public void should_NotReturnExpectedRecordedResponse_FromValidUrl_WhenQueryValue
@Test
public void should_ReturnExpectedRecordedResponse_OnSubsequentCallToValidUrl() throws Exception {

ANSITerminal.muteConsole(false);

final ByteArrayOutputStream consoleCaptor = new ByteArrayOutputStream();
final boolean NO_AUTO_FLUSH = false;
final PrintStream oldPrintStream = System.out;
Expand All @@ -1023,11 +1025,13 @@ public void should_ReturnExpectedRecordedResponse_OnSubsequentCallToValidUrl() t

String firstCallResponseContent = actualResponse.parseAsString().trim();
assertThat(firstCallResponseContent).contains("<payment><invoiceTypeLookupCode>STANDARD</invoiceTypeLookupCode></payment>");
// Make sure we only hitting recordable source once
assertThat(actualConsoleOutput).contains("Recording HTTP response using");

if (idx == LIMIT) {
System.setOut(oldPrintStream);
System.out.println(actualConsoleOutput);
}
}
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ org.gradle.parallel=true
configureondemand=true
stubbyProjectName=stubby4j
stubbyProjectGroup=io.github.azagniotov
version=5.1.1-SNAPSHOT
version=5.1.2-SNAPSHOT

Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package io.github.azagniotov.stubby4j.utils;

import io.github.azagniotov.stubby4j.cli.ANSITerminal;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;

/**
* @author: Alexander Zagniotov
* Created: 4/22/13 3:33 PM
*/
@RunWith(MockitoJUnitRunner.class)
public class ConsoleUtilsTest {

private static final String URI = "/some/uri/to/resource/123";

@Mock
private HttpServletRequest mockHttpServletRequest;

@Mock
private HttpServletResponse mockHttpServletResponse;

private ByteArrayOutputStream consoleCaptor;

@BeforeClass
public static void beforeClass() throws Exception {
ANSITerminal.muteConsole(false);
}

@Before
public void beforeEach() throws Exception {
consoleCaptor = new ByteArrayOutputStream();
final boolean NO_AUTO_FLUSH = false;
System.setOut(new PrintStream(consoleCaptor, NO_AUTO_FLUSH, StringUtils.UTF_8));

when(mockHttpServletRequest.getRequestURI()).thenReturn(URI);
}

@After
public void afterEach() throws Exception {
System.setOut(System.out);
}

@Test
public void shouldPrintToConsoleExpectedErrorWithColor_WhenStatus_500() throws Exception {

final int expectedStatus = 500;
final String expectedConsoleOutput = String.format("%s [%s] Server Error\u001B[0m", expectedStatus, URI);
final String expectedConsoleColor = "[31m";

when(mockHttpServletResponse.getStatus()).thenReturn(expectedStatus);

ConsoleUtils.logOutgoingResponse(mockHttpServletRequest.getRequestURI(), mockHttpServletResponse);
final String actualConsoleOutput = consoleCaptor.toString(StringUtils.UTF_8).trim();

assertThat(actualConsoleOutput).contains(expectedConsoleOutput);
assertThat(actualConsoleOutput).contains(expectedConsoleColor);
}

@Test
public void shouldPrintToConsoleExpectedErrorWithColor_WhenStatus_301() throws Exception {

final int expectedStatus = 301;
final String expectedConsoleOutput = String.format("%s [%s] Moved Permanently\u001B[0m", expectedStatus, URI);
final String expectedConsoleColor = "[33m";

when(mockHttpServletResponse.getStatus()).thenReturn(expectedStatus);

ConsoleUtils.logOutgoingResponse(mockHttpServletRequest.getRequestURI(), mockHttpServletResponse);
final String actualConsoleOutput = consoleCaptor.toString(StringUtils.UTF_8).trim();

assertThat(actualConsoleOutput).contains(expectedConsoleOutput);
assertThat(actualConsoleOutput).contains(expectedConsoleColor);
}

@Test
public void shouldPrintToConsoleExpectedErrorWithColor_WhenStatus_201() throws Exception {

final int expectedStatus = 201;
final String expectedConsoleOutput = String.format("%s [%s] Created\u001B[0m", expectedStatus, URI);
final String expectedConsoleColor = "[32m";

when(mockHttpServletResponse.getStatus()).thenReturn(expectedStatus);

ConsoleUtils.logOutgoingResponse(mockHttpServletRequest.getRequestURI(), mockHttpServletResponse);
final String actualConsoleOutput = consoleCaptor.toString(StringUtils.UTF_8).trim();

assertThat(actualConsoleOutput).contains(expectedConsoleOutput);
assertThat(actualConsoleOutput).contains(expectedConsoleColor);
}

@Test
public void shouldPrintToConsoleExpectedErrorWithColor_WhenStatus_200() throws Exception {

final int expectedStatus = 200;
final String expectedConsoleOutput = String.format("%s [%s] OK\u001B[0m", expectedStatus, URI);
final String expectedConsoleColor = "[32m";

when(mockHttpServletResponse.getStatus()).thenReturn(expectedStatus);

ConsoleUtils.logOutgoingResponse(mockHttpServletRequest.getRequestURI(), mockHttpServletResponse);
final String actualConsoleOutput = consoleCaptor.toString(StringUtils.UTF_8).trim();

assertThat(actualConsoleOutput).contains(expectedConsoleOutput);
assertThat(actualConsoleOutput).contains(expectedConsoleColor);
}

@Test
public void shouldPrintToConsoleExpectedErrorWithColor_WhenStatus_100() throws Exception {

final int expectedStatus = 100;
final String expectedConsoleOutput = String.format("%s [%s] Continue\u001B[0m", expectedStatus, URI);
final String expectedConsoleColor = "[34m";

when(mockHttpServletResponse.getStatus()).thenReturn(expectedStatus);

ConsoleUtils.logOutgoingResponse(mockHttpServletRequest.getRequestURI(), mockHttpServletResponse);
final String actualConsoleOutput = consoleCaptor.toString(StringUtils.UTF_8).trim();

assertThat(actualConsoleOutput).contains(expectedConsoleOutput);
assertThat(actualConsoleOutput).contains(expectedConsoleColor);
}

@Test
public void shouldPrintToConsoleExpectedErrorWithColor_WhenStatus_LessThan100() throws Exception {

final int expectedStatus = 99;
final String expectedConsoleOutput = String.format("%s [%s] 99\u001B[0m", expectedStatus, URI);

when(mockHttpServletResponse.getStatus()).thenReturn(expectedStatus);

ConsoleUtils.logOutgoingResponse(mockHttpServletRequest.getRequestURI(), mockHttpServletResponse);
final String actualConsoleOutput = consoleCaptor.toString(StringUtils.UTF_8).trim();

assertThat(actualConsoleOutput).contains(expectedConsoleOutput);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;

import static com.google.common.truth.Truth.assertThat;
Expand Down Expand Up @@ -298,6 +300,36 @@ public void shouldUnmarshall_WhenYAMLValid_WithFileFailedToLoadAndPostSet() thro
assertThat(actualRequest.getPostBody()).isEqualTo(expectedPost);
}

@Test
public void shouldCaptureConsoleErrorOutput_WhenYAMLValid_WithFileFailedToLoadAndPostSet() throws Exception {

final String stubbedRequestFile = "../../very.big.soap.request.xml";

final String expectedPost = "{\"message\", \"Hello, this is HTTP request post\"}";
final String yaml = YAML_BUILDER.newStubbedRequest()
.withMethodGet()
.withUrl("/some/uri")
.withFile(stubbedRequestFile)
.withFoldedPost(expectedPost)
.newStubbedResponse()
.withLiteralBody("OK")
.withStatus("201").build();

final ByteArrayOutputStream consoleCaptor = new ByteArrayOutputStream();
final boolean NO_AUTO_FLUSH = false;
System.setOut(new PrintStream(consoleCaptor, NO_AUTO_FLUSH, StringUtils.UTF_8));

unmarshall(yaml);

System.setOut(System.out);

final String actualConsoleOutput = consoleCaptor.toString(StringUtils.UTF_8).trim();

assertThat(actualConsoleOutput).contains("Could not load file from path: ../../very.big.soap.request.xml");
assertThat(actualConsoleOutput).contains(YAMLParser.FAILED_TO_LOAD_FILE_ERR);
}


@Test
public void shouldUnmarshall_WhenYAMLValid_WithFileFailedToLoadAndBodySet() throws Exception {

Expand All @@ -321,6 +353,34 @@ public void shouldUnmarshall_WhenYAMLValid_WithFileFailedToLoadAndBodySet() thro
assertThat(StringUtils.newStringUtf8(actualResponse.getResponseBodyAsBytes())).isEqualTo(expectedBody);
}

@Test
public void shouldCaptureConsoleErrorOutput_WhenYAMLValid_WithFileFailedToLoadAndBodySet() throws Exception {

final String stubbedResponseFile = "../../very.big.soap.response.xml";

final String expectedBody = "{\"message\", \"Hello, this is HTTP response body\"}";
final String yaml = YAML_BUILDER.newStubbedRequest()
.withMethodGet()
.withUrl("/some/uri")
.newStubbedResponse()
.withFoldedBody(expectedBody)
.withFile(stubbedResponseFile)
.withStatus("201").build();

final ByteArrayOutputStream consoleCaptor = new ByteArrayOutputStream();
final boolean NO_AUTO_FLUSH = false;
System.setOut(new PrintStream(consoleCaptor, NO_AUTO_FLUSH, StringUtils.UTF_8));

unmarshall(yaml);

System.setOut(System.out);

final String actualConsoleOutput = consoleCaptor.toString(StringUtils.UTF_8).trim();

assertThat(actualConsoleOutput).contains("Could not load file from path: ../../very.big.soap.response.xml");
assertThat(actualConsoleOutput).contains(YAMLParser.FAILED_TO_LOAD_FILE_ERR);
}

@Test
public void shouldUnmarshall_WhenYAMLValid_WithLiteralPost() throws Exception {

Expand Down Expand Up @@ -732,4 +792,4 @@ public void shouldContainExpectedResourceIdHeaderUponSuccessfulYamlMarshall_When
private List<StubHttpLifecycle> unmarshall(final String yaml) throws Exception {
return new YAMLParser().parse(".", yaml);
}
}
}
Loading

0 comments on commit 2c1712e

Please sign in to comment.