1 package org.onap.integration.test.mocks.sniroemulator;
3 import static com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder.responseDefinition;
4 import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
5 import static com.github.tomakehurst.wiremock.core.WireMockApp.FILES_ROOT;
6 import static com.github.tomakehurst.wiremock.core.WireMockApp.MAPPINGS_ROOT;
7 import static com.github.tomakehurst.wiremock.http.RequestMethod.ANY;
8 import static com.github.tomakehurst.wiremock.matching.RequestPatternBuilder.newRequestPattern;
9 import static java.lang.System.out;
11 import org.springframework.boot.SpringApplication;
12 import org.springframework.boot.autoconfigure.SpringBootApplication;
14 import com.github.tomakehurst.wiremock.WireMockServer;
15 import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
16 import com.github.tomakehurst.wiremock.common.FatalStartupException;
17 import com.github.tomakehurst.wiremock.common.FileSource;
18 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
19 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
20 import com.github.tomakehurst.wiremock.matching.RequestPattern;
21 import com.github.tomakehurst.wiremock.standalone.MappingsLoader;
22 import com.github.tomakehurst.wiremock.stubbing.StubMapping;
23 import com.github.tomakehurst.wiremock.stubbing.StubMappings;
25 @SpringBootApplication
26 public class MockApplication {
29 private static final String BANNER= " \n" +
30 " ******** **** **** ## \n" +
31 " **######** ###* *### ## \n" +
32 " *##******##* ##*** ***## ##\n" +
33 " **#* *#** ##*#* *#*## ## \n" +
34 " *#* *#* ##****** ******* ##****** ##*#* *#*## ******* ****** ## *** \n" +
35 " *#* *#* ##*####* *######* ##*####** ##*#* *#*## **#####** **####** ## *#** \n" +
36 " *#* *#* ##****#* *#****#* ##** **#* ## *** *** ## *#** **#* *#****#* ## **#** \n" +
37 " *# #* ##* *#* #* ##* *#* ## *#* *#* ## *#* *#* *#* *#* ##**#** \n" +
38 " *#* *#* ##* ## ****## ##* *#* ## *#* *#* ## *#* *#* *#* ##*##* \n" +
39 " *#* *#* ## ## **###### ## #* ## *#* *#* ## *# #* *# ##**#** \n" +
40 " *#* *#* ## ## *#****## ##* *#* ## *#*#* ## *#* *#* *#* ##**##* \n" +
41 " **#* *#** ## ## *#* *## ##* *#* ## *#*#* ## *#* *#* *#* *#* ## *#** \n" +
42 " *##******##* ## ## *#* **##* ##** **#* ## *#*#* ## *#** **#* *#****#* ## **#* \n" +
43 " **######** ## ## *#######* ##*####* ## *###* ## **#####** **####** ## *#** \n" +
44 " ******** ## ## *******#* ##****** ## *#* ## ******* ****** ## *#* \n" +
51 System.setProperty("org.mortbay.log.class", "com.github.tomakehurst.wiremock.jetty.LoggerAdapter");
54 private WireMockServer wireMockServer;
56 public static void main(String[] args) {
57 SpringApplication.run(MockApplication.class, args);
58 //new WireMockServerRunner().run("--port 9999");
59 new MockApplication().run(args);
62 public void run(String... args) {
64 WireMockConfiguration options = WireMockConfiguration.options();
66 FileSource fileSource = options.filesRoot();
67 fileSource.createIfNecessary();
68 FileSource filesFileSource = fileSource.child(FILES_ROOT);
69 filesFileSource.createIfNecessary();
70 FileSource mappingsFileSource = fileSource.child(MAPPINGS_ROOT);
71 mappingsFileSource.createIfNecessary();
74 options.extensions("org.onap.integration.test.mocks.sniroemulator.extension.Webhooks");
76 options.notifier(new ConsoleNotifier(true));
77 wireMockServer = new WireMockServer(options);
79 wireMockServer.enableRecordMappings(mappingsFileSource, filesFileSource);
81 //if (options.specifiesProxyUrl()) {
82 // addProxyMapping(options.proxyUrl());
86 wireMockServer.start();
90 } catch (FatalStartupException e) {
91 System.err.println(e.getMessage());
96 private void addProxyMapping(final String baseUrl) {
97 wireMockServer.loadMappingsUsing(new MappingsLoader() {
99 public void loadMappingsInto(StubMappings stubMappings) {
100 RequestPattern requestPattern = newRequestPattern(ANY, anyUrl()).build();
101 ResponseDefinition responseDef = responseDefinition()
102 .proxiedFrom(baseUrl)
105 StubMapping proxyBasedMapping = new StubMapping(requestPattern, responseDef);
106 proxyBasedMapping.setPriority(10); // Make it low priority so that existing stubs will take precedence
107 stubMappings.addMapping(proxyBasedMapping);
113 wireMockServer.stop();
116 public boolean isRunning() {
117 return wireMockServer.isRunning();
120 public int port() { return wireMockServer.port(); }