5e47c7ca066a9930398c48b10d8e09dfa7341eae
[integration.git] /
1 package org.onap.integration.test.mocks.sniroemulator;
2
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;
10
11 import org.springframework.boot.SpringApplication;
12 import org.springframework.boot.autoconfigure.SpringBootApplication;
13
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;
24
25 @SpringBootApplication
26 public class MockApplication {
27
28     
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" +
45 "                                            ##  \n" +
46 "                                            ##  \n" +
47 "                                            ##  \n" +
48 "                                            **  \n" ;
49                                         
50     static {
51         System.setProperty("org.mortbay.log.class", "com.github.tomakehurst.wiremock.jetty.LoggerAdapter");
52     }
53
54         private WireMockServer wireMockServer;
55         
56         public static void main(String[] args) {
57                 SpringApplication.run(MockApplication.class, args);
58                 //new WireMockServerRunner().run("--port 9999");
59                 new MockApplication().run(args);
60         }
61         
62         public void run(String... args) {
63
64                 WireMockConfiguration options = WireMockConfiguration.options();
65         options.port(9999);
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();
72                 
73                 // Register extension
74                 options.extensions("org.onap.integration.test.mocks.sniroemulator.extension.Webhooks");
75                 // Register notifier
76         options.notifier(new ConsoleNotifier(true));   
77         wireMockServer = new WireMockServer(options);
78         
79         wireMockServer.enableRecordMappings(mappingsFileSource, filesFileSource);
80
81                 //if (options.specifiesProxyUrl()) {
82                 //      addProxyMapping(options.proxyUrl());
83                 //}
84
85         try {
86             wireMockServer.start();
87             out.println(BANNER);
88             out.println();
89             out.println(options);
90         } catch (FatalStartupException e) {
91             System.err.println(e.getMessage());
92             System.exit(1);
93         }
94     }
95         
96         private void addProxyMapping(final String baseUrl) {
97                 wireMockServer.loadMappingsUsing(new MappingsLoader() {
98                         @Override
99                         public void loadMappingsInto(StubMappings stubMappings) {
100                 RequestPattern requestPattern = newRequestPattern(ANY, anyUrl()).build();
101                                 ResponseDefinition responseDef = responseDefinition()
102                                                 .proxiedFrom(baseUrl)
103                                                 .build();
104
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);
108                         }
109                 });
110         }
111         
112         public void stop() {
113                 wireMockServer.stop();
114         }
115
116     public boolean isRunning() {
117         return wireMockServer.isRunning();
118     }
119
120     public int port() { return wireMockServer.port(); } 
121         
122 }