Skip to content

Commit 762a26a

Browse files
committed
Merge branch 'wiremock'
Use WireMock in integration tests for mocking external services. Fix #934
2 parents 27d9f52 + 513dbad commit 762a26a

22 files changed

+221
-170
lines changed

NEWS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- (functionality) add support for specifying Zagorski catalog numbers
2323
- (functionality) user may specify how many stamps from a series in his/her collection
2424
- (functionality) paid users may specify a price that he/she paid for a series
25+
- (infrastructure) use WireMock in integration tests for mocking external services
2526

2627
0.3
2728
- (functionality) implemented possibility to user to add series to his collection

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ If you are programmer/sysadmin or you just feeling that you are able to run a lo
4545
* *Validation*: JSR-303 (Hibernate Validator)
4646
* *Logging*: Slf4j (Logback)
4747
* *Unit tests*: Groovy and Spock Framework (for Java code), jasmine (for JavaScript code)
48-
* *Integration tests*: Selenium2, RobotFramework, TestNG and fest-assert
48+
* *Integration tests*: Selenium2, RobotFramework, WireMock, TestNG and fest-assert
4949
* *Others*: Lombok, Togglz, WebJars

docs/wiremock.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# WireMock
2+
3+
## What and why
4+
WireMock is a simulator for HTTP-based APIs. We use this tool in the integration tests for mocking
5+
external services. For example, for testing import of the stamps series from sites.
6+
7+
## Running on TravisCI
8+
WireMock is automatically running/stopping during integration tests execution (`mvn verify`).
9+
10+
## Running manually
11+
In order to run the server as a standlone process:
12+
* uncomment `<keepRunning>` option in the `pom.xml` file
13+
* execute the `mvn wiremock:run` command
14+
15+
After that the mock server will be available on 8888 port.
16+
17+
## Troubleshooting
18+
* [run the plugin separately](#running-manually) to be able to test it with a browser/curl
19+
* append `--verbose` flag to the options from `<params>` tag in the `pom.xml` file. This instructs
20+
WireMock to produce more detailed output to the console
21+
22+
## Configuration
23+
WireMock is fully configured by the `wiremock-maven-plugin` in the `pom.xml` file:
24+
25+
* the server is listening on `8888` port
26+
* you can refer to the mock server from within RobotFramework test cases by using `${MOCK_SERVER}`
27+
variable.
28+
* static files are reside inside the `src/test/wiremock/__files` directory
29+
* mocks and stubs are reside inside the `src/test/wiremock/mappings` directory
30+
31+
By our convention the files in these directories should have the same hierarchy as their
32+
RobotFramework test cases. For instance, the mocks/files used solely by
33+
`category/creation/logic.robot` test case, should be placed into
34+
`src/test/wiremock/__files/category/creation/logic` and
35+
`src/test/wiremock/mappings/category/creation/logic` directories.
36+
37+
In the case, mocks/files are being used by the different test cases, they should reside in a
38+
directory that is the base for all such test cases.
39+
40+
## Links
41+
* http://wiremock.org/
42+
* https://github.com/tomakehurst/wiremock
43+
* https://github.com/automatictester/wiremock-maven-plugin

pom.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@
583583

584584
<togglz.version>2.5.0.Final</togglz.version>
585585
<war.plugin.version>3.2.0</war.plugin.version>
586+
<wiremock.plugin.version>2.13.1</wiremock.plugin.version>
586587
</properties>
587588

588589
<build>
@@ -986,6 +987,7 @@
986987
<variable>BROWSER:htmlunitwithjs</variable>
987988
<variable>MAIN_RESOURCE_DIR:${basedir}/src/main/resources/test</variable>
988989
<variable>TEST_RESOURCE_DIR:${basedir}/src/test/resources</variable>
990+
<variable>MOCK_SERVER:http://127.0.0.1:8888</variable>
989991
<!-- See also ru.mystamps.web.Url.SITE constant -->
990992
<variable>SITE_URL:http://127.0.0.1:8080</variable>
991993
</variables>
@@ -1046,6 +1048,36 @@
10461048
</executions>
10471049
</plugin>
10481050

1051+
<!--
1052+
Usage:
1053+
mvn wiremock:run (run WireMock server with mocks from src/test/wiremock/mappings)
1054+
-->
1055+
<plugin>
1056+
<groupId>uk.co.automatictester</groupId>
1057+
<artifactId>wiremock-maven-plugin</artifactId>
1058+
<version>${wiremock.plugin.version}</version>
1059+
<configuration>
1060+
<!-- Handy for manual debugging -->
1061+
<!--
1062+
<keepRunning>true</keepRunning>
1063+
-->
1064+
<dir>${basedir}/src/test/wiremock</dir>
1065+
<!--
1066+
The list of available options:
1067+
http://wiremock.org/docs/running-standalone/
1068+
-->
1069+
<!-- Handy for debugging: add &#45;&#45;verbose option -->
1070+
<params>--port=8888 --disable-banner --no-request-journal --container-threads=4 --jetty-acceptor-threads=1</params>
1071+
</configuration>
1072+
<executions>
1073+
<execution>
1074+
<goals>
1075+
<goal>run</goal>
1076+
</goals>
1077+
</execution>
1078+
</executions>
1079+
</plugin>
1080+
10491081
</plugins>
10501082

10511083
</build>

src/main/java/ru/mystamps/web/config/ControllersConfig.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.context.annotation.Bean;
2323
import org.springframework.context.annotation.Configuration;
2424
import org.springframework.context.annotation.Import;
25-
import org.springframework.context.annotation.Profile;
2625

2726
import lombok.RequiredArgsConstructor;
2827

@@ -131,11 +130,5 @@ public SitemapController getSitemapController() {
131130
public SuggestionController getSuggestionController() {
132131
return new SuggestionController(countryService);
133132
}
134-
135-
@Bean
136-
@Profile({ "test", "travis" })
137-
public TestController getTestController() {
138-
return new TestController();
139-
}
140133

141134
}

src/main/java/ru/mystamps/web/controller/TestController.java

Lines changed: 0 additions & 135 deletions
This file was deleted.

src/main/java/ru/mystamps/web/util/ControllerUtils.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
*/
1818
package ru.mystamps.web.util;
1919

20-
import java.io.IOException;
21-
22-
import javax.servlet.http.HttpServletResponse;
23-
2420
import org.springframework.web.util.UriComponentsBuilder;
2521

2622
public final class ControllerUtils {
@@ -36,10 +32,4 @@ public static String redirectTo(String url, Object... args) {
3632
return "redirect:" + dstUrl;
3733
}
3834

39-
public static void printHtml(HttpServletResponse response, String html) throws IOException {
40-
response.setContentType("text/html");
41-
response.setCharacterEncoding("UTF-8");
42-
response.getWriter().println(html);
43-
}
44-
4535
}

src/main/resources/application-test.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ app.site-parser[1].seller-locator = #test-seller
5959
app.site-parser[1].price-locator = #test-price
6060
app.site-parser[1].currency-value = RUB
6161

62+
app.site-parser[2].name = mockserver
63+
app.site-parser[2].matched-url = http://127.0.0.1:8888
64+
app.site-parser[2].image-url-locator = #test-image
65+
app.site-parser[2].short-description-locator = #test-description
66+
app.site-parser[2].seller-locator = #test-seller
67+
app.site-parser[2].price-locator = #test-price
68+
app.site-parser[2].currency-value = RUB
69+
6270
# Full list of autoconfiguration classes:
6371
# http://docs.spring.io/spring-boot/docs/1.5.x/reference/html/auto-configuration-classes.html
6472
spring.autoconfigure.exclude: \

src/main/resources/application-travis.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ app.site-parser[1].seller-locator = #test-seller
5656
app.site-parser[1].price-locator = #test-price
5757
app.site-parser[1].currency-value = RUB
5858

59+
app.site-parser[2].name = mockserver
60+
app.site-parser[2].matched-url = http://127.0.0.1:8888
61+
app.site-parser[2].image-url-locator = #test-image
62+
app.site-parser[2].short-description-locator = #test-description
63+
app.site-parser[2].seller-locator = #test-seller
64+
app.site-parser[2].price-locator = #test-price
65+
app.site-parser[2].currency-value = RUB
66+
5967
# Full list of autoconfiguration classes:
6068
# http://docs.spring.io/spring-boot/docs/1.5.x/reference/html/auto-configuration-classes.html
6169
# The difference between test profile is that we don't need H2ConsoleAutoConfiguration

src/main/scripts/ci/check-build-and-verify.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ if [ "$RUN_ONLY_INTEGRATION_TESTS" = 'no' ]; then
135135
print_status "$POM_STATUS" 'Check sorting of pom.xml'
136136

137137
if [ "$BOOTLINT_STATUS" != 'skip' ]; then
138-
find src -type f -name '*.html' -print0 | xargs -0 bootlint \
138+
find src/main -type f -name '*.html' -print0 | xargs -0 bootlint \
139139
>bootlint.log 2>&1 || BOOTLINT_STATUS=fail
140140
fi
141141
print_status "$BOOTLINT_STATUS" 'Run bootlint'

src/test/robotframework/series/add-image/validation.robot

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,27 @@ Add image with invalid URL
3030
Element Text Should Be id=image-url.errors Value must be a valid URL
3131

3232
Add image with URL with invalid response
33-
Input Text id=image-url ${SITE_URL}/test/invalid/response-400
33+
Input Text id=image-url ${MOCK_SERVER}/series/response-400
3434
Submit Form id=add-image-form
3535
Element Text Should Be id=image-url.errors Could not download file
3636

3737
Add image with URL to a file that does not exist
38-
Input Text id=image-url ${SITE_URL}/test/invalid/response-404
38+
Input Text id=image-url ${MOCK_SERVER}/series/response-404
3939
Submit Form id=add-image-form
4040
Element Text Should Be id=image-url.errors File not found
4141

4242
Add image with URL that causes a redirect
43-
Input Text id=image-url ${SITE_URL}/test/invalid/response-301
43+
Input Text id=image-url ${MOCK_SERVER}/series/response-301
4444
Submit Form id=add-image-form
4545
Element Text Should Be id=image-url.errors URL must not redirect to another address
4646

4747
Add image with URL to an empty file
48-
Input Text id=image-url ${SITE_URL}/test/invalid/empty-jpeg-file
48+
Input Text id=image-url ${MOCK_SERVER}/series/empty-jpeg-file
4949
Submit Form id=add-image-form
5050
Element Text Should Be id=image-url.errors File must not be empty
5151

5252
Add image with URL to a file of unsupported type (not an image)
53-
Input Text id=image-url ${SITE_URL}/test/invalid/not-image-file
53+
Input Text id=image-url ${MOCK_SERVER}/series/not-image-file
5454
Submit Form id=add-image-form
5555
Element Text Should Be id=image-url.errors Invalid file type
5656

src/test/robotframework/series/creation/validation-admin.robot

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,27 @@ Create series with invalid image URL
4747
Element Text Should Be id=image-url.errors Value must be a valid URL
4848

4949
Create series with image URL with invalid response
50-
Input Text id=image-url ${SITE_URL}/test/invalid/response-400
50+
Input Text id=image-url ${MOCK_SERVER}/series/response-400
5151
Submit Form id=add-series-form
5252
Element Text Should Be id=image-url.errors Could not download file
5353

5454
Create series with image URL to a file that does not exist
55-
Input Text id=image-url ${SITE_URL}/test/invalid/response-404
55+
Input Text id=image-url ${MOCK_SERVER}/series/response-404
5656
Submit Form id=add-series-form
5757
Element Text Should Be id=image-url.errors File not found
5858

5959
Create series with image URL that causes a redirect
60-
Input Text id=image-url ${SITE_URL}/test/invalid/response-301
60+
Input Text id=image-url ${MOCK_SERVER}/series/response-301
6161
Submit Form id=add-series-form
6262
Element Text Should Be id=image-url.errors URL must not redirect to another address
6363

6464
Create series with image URL to an empty file
65-
Input Text id=image-url ${SITE_URL}/test/invalid/empty-jpeg-file
65+
Input Text id=image-url ${MOCK_SERVER}/series/empty-jpeg-file
6666
Submit Form id=add-series-form
6767
Element Text Should Be id=image-url.errors File must not be empty
6868

6969
Create series with image URL to a file of unsupported type (not an image)
70-
Input Text id=image-url ${SITE_URL}/test/invalid/not-image-file
70+
Input Text id=image-url ${MOCK_SERVER}/series/not-image-file
7171
Submit Form id=add-series-form
7272
Element Text Should Be id=image-url.errors Invalid file type
7373

0 commit comments

Comments
 (0)