714d3a340b651b2657adf2292f3b3620ef547f60
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.integration
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.integration.test.mocks.sniroemulator;
22
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;
30
31 import org.springframework.boot.SpringApplication;
32 import org.springframework.boot.autoconfigure.SpringBootApplication;
33
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;
44
45 @SpringBootApplication
46 public class MockApplication {
47
48     
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" +
65 "                                            ##  \n" +
66 "                                            ##  \n" +
67 "                                            ##  \n" +
68 "                                            **  \n" ;
69                                         
70     static {
71         System.setProperty("org.mortbay.log.class", "com.github.tomakehurst.wiremock.jetty.LoggerAdapter");
72     }
73
74         private WireMockServer wireMockServer;
75         
76         public static void main(String[] args) {
77                 SpringApplication.run(MockApplication.class, args);
78                 //new WireMockServerRunner().run("--port 9999");
79                 new MockApplication().run(args);
80         }
81         
82         public void run(String... args) {
83
84                 WireMockConfiguration options = WireMockConfiguration.options();
85         options.port(9999);
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();
92                 
93                 // Register extension
94                 options.extensions("org.onap.integration.test.mocks.sniroemulator.extension.Webhooks");
95                 // Register notifier
96         options.notifier(new ConsoleNotifier(true));   
97         wireMockServer = new WireMockServer(options);
98         
99         wireMockServer.enableRecordMappings(mappingsFileSource, filesFileSource);
100
101                 //if (options.specifiesProxyUrl()) {
102                 //      addProxyMapping(options.proxyUrl());
103                 //}
104
105         try {
106             wireMockServer.start();
107             out.println(BANNER);
108             out.println();
109             out.println(options);
110         } catch (FatalStartupException e) {
111             System.err.println(e.getMessage());
112             System.exit(1);
113         }
114     }
115         
116         private void addProxyMapping(final String baseUrl) {
117                 wireMockServer.loadMappingsUsing(new MappingsLoader() {
118                         @Override
119                         public void loadMappingsInto(StubMappings stubMappings) {
120                 RequestPattern requestPattern = newRequestPattern(ANY, anyUrl()).build();
121                                 ResponseDefinition responseDef = responseDefinition()
122                                                 .proxiedFrom(baseUrl)
123                                                 .build();
124
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);
128                         }
129                 });
130         }
131         
132         public void stop() {
133                 wireMockServer.stop();
134         }
135
136     public boolean isRunning() {
137         return wireMockServer.isRunning();
138     }
139
140     public int port() { return wireMockServer.port(); } 
141         
142 }