2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.integration.test.mocks.sniroemulator;
23 import static com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder.responseDefinition;
24 import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
25 import static com.github.tomakehurst.wiremock.core.WireMockApp.FILES_ROOT;
26 import static com.github.tomakehurst.wiremock.core.WireMockApp.MAPPINGS_ROOT;
27 import static com.github.tomakehurst.wiremock.http.RequestMethod.ANY;
28 import static com.github.tomakehurst.wiremock.matching.RequestPatternBuilder.newRequestPattern;
29 import static java.lang.System.out;
31 import org.springframework.boot.SpringApplication;
32 import org.springframework.boot.autoconfigure.SpringBootApplication;
34 import com.github.tomakehurst.wiremock.WireMockServer;
35 import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
36 import com.github.tomakehurst.wiremock.common.FatalStartupException;
37 import com.github.tomakehurst.wiremock.common.FileSource;
38 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
39 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
40 import com.github.tomakehurst.wiremock.matching.RequestPattern;
41 import com.github.tomakehurst.wiremock.standalone.MappingsLoader;
42 import com.github.tomakehurst.wiremock.stubbing.StubMapping;
43 import com.github.tomakehurst.wiremock.stubbing.StubMappings;
45 @SpringBootApplication
46 public class MockApplication {
49 private static final String BANNER= " \n" +
50 " ******** **** **** ## \n" +
51 " **######** ###* *### ## \n" +
52 " *##******##* ##*** ***## ##\n" +
53 " **#* *#** ##*#* *#*## ## \n" +
54 " *#* *#* ##****** ******* ##****** ##*#* *#*## ******* ****** ## *** \n" +
55 " *#* *#* ##*####* *######* ##*####** ##*#* *#*## **#####** **####** ## *#** \n" +
56 " *#* *#* ##****#* *#****#* ##** **#* ## *** *** ## *#** **#* *#****#* ## **#** \n" +
57 " *# #* ##* *#* #* ##* *#* ## *#* *#* ## *#* *#* *#* *#* ##**#** \n" +
58 " *#* *#* ##* ## ****## ##* *#* ## *#* *#* ## *#* *#* *#* ##*##* \n" +
59 " *#* *#* ## ## **###### ## #* ## *#* *#* ## *# #* *# ##**#** \n" +
60 " *#* *#* ## ## *#****## ##* *#* ## *#*#* ## *#* *#* *#* ##**##* \n" +
61 " **#* *#** ## ## *#* *## ##* *#* ## *#*#* ## *#* *#* *#* *#* ## *#** \n" +
62 " *##******##* ## ## *#* **##* ##** **#* ## *#*#* ## *#** **#* *#****#* ## **#* \n" +
63 " **######** ## ## *#######* ##*####* ## *###* ## **#####** **####** ## *#** \n" +
64 " ******** ## ## *******#* ##****** ## *#* ## ******* ****** ## *#* \n" +
71 System.setProperty("org.mortbay.log.class", "com.github.tomakehurst.wiremock.jetty.LoggerAdapter");
74 private WireMockServer wireMockServer;
76 public static void main(String[] args) {
77 SpringApplication.run(MockApplication.class, args);
78 //new WireMockServerRunner().run("--port 9999");
79 new MockApplication().run(args);
82 public void run(String... args) {
84 WireMockConfiguration options = WireMockConfiguration.options();
86 FileSource fileSource = options.filesRoot();
87 fileSource.createIfNecessary();
88 FileSource filesFileSource = fileSource.child(FILES_ROOT);
89 filesFileSource.createIfNecessary();
90 FileSource mappingsFileSource = fileSource.child(MAPPINGS_ROOT);
91 mappingsFileSource.createIfNecessary();
94 options.extensions("org.onap.integration.test.mocks.sniroemulator.extension.Webhooks");
96 options.notifier(new ConsoleNotifier(true));
97 wireMockServer = new WireMockServer(options);
99 wireMockServer.enableRecordMappings(mappingsFileSource, filesFileSource);
101 //if (options.specifiesProxyUrl()) {
102 // addProxyMapping(options.proxyUrl());
106 wireMockServer.start();
109 out.println(options);
110 } catch (FatalStartupException e) {
111 System.err.println(e.getMessage());
116 private void addProxyMapping(final String baseUrl) {
117 wireMockServer.loadMappingsUsing(new MappingsLoader() {
119 public void loadMappingsInto(StubMappings stubMappings) {
120 RequestPattern requestPattern = newRequestPattern(ANY, anyUrl()).build();
121 ResponseDefinition responseDef = responseDefinition()
122 .proxiedFrom(baseUrl)
125 StubMapping proxyBasedMapping = new StubMapping(requestPattern, responseDef);
126 proxyBasedMapping.setPriority(10); // Make it low priority so that existing stubs will take precedence
127 stubMappings.addMapping(proxyBasedMapping);
133 wireMockServer.stop();
136 public boolean isRunning() {
137 return wireMockServer.isRunning();
140 public int port() { return wireMockServer.port(); }