8b8bea6a8dae616d7872064a16aaa77f39e17003
[so.git] / bpmn / MSOMockServer / src / main / java / org / openecomp / mso / bpmn / mock / MockResource.java
1 /*- 
2  * ============LICENSE_START======================================================= 
3  * OPENECOMP - MSO 
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.openecomp.mso.bpmn.mock;
22
23 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
24
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import javax.ws.rs.GET;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.Response;
34
35 import com.github.tomakehurst.wiremock.WireMockServer;
36 import com.github.tomakehurst.wiremock.client.WireMock;
37
38 /**
39  * 
40  * Mock Resource which is used to start, stop the WireMock Server
41  * Also up to 50 mock properties can be added at run-time to change the properties used in transformers such as sdnc_delay in SDNCAdapterMockTransformer
42  * You can also selectively setup a stub (use reset before setting up), reset all stubs
43  */
44 @Path("/server")
45 public class MockResource {
46
47         private boolean started = false;
48         private final Integer defaultPort = 28090;
49         private WireMockServer wireMockServer = null;
50         private static Map<String,String> mockProperties = new HashMap<String,String>();
51
52         public static String getMockProperties(String key) {
53                 return mockProperties.get(key);
54         }
55
56         private synchronized void initMockServer(int portNumber) {
57                 String path = FileUtil.class.getClassLoader().getResource("__files/sdncSimResponse.xml").getFile();
58                 path = path.substring(0,path.indexOf("__files/"));
59
60                 wireMockServer = new WireMockServer(wireMockConfig().port(portNumber).extensions("org.openecomp.mso.bpmn.mock.SDNCAdapterMockTransformer")
61                                                                                                                                                         .extensions("org.openecomp.mso.bpmn.mock.SDNCAdapterNetworkTopologyMockTransformer")
62                                                                                                                                                         .extensions("org.openecomp.mso.bpmn.mock.VnfAdapterCreateMockTransformer")
63                                                                                                                                                         .extensions("org.openecomp.mso.bpmn.mock.VnfAdapterDeleteMockTransformer")
64                                                                                                                                                         .extensions("org.openecomp.mso.bpmn.mock.VnfAdapterUpdateMockTransformer")
65                                                                                                                                                         .extensions("org.openecomp.mso.bpmn.mock.VnfAdapterRollbackMockTransformer")
66                                                                                                                                                         .extensions("org.openecomp.mso.bpmn.mock.VnfAdapterQueryMockTransformer"));
67                                                                                                                                                         //.withRootDirectory(path));
68                 //Mocks were failing - commenting out for now, both mock and transformers seem to work fine
69                 WireMock.configureFor("localhost", portNumber);
70                 wireMockServer.start();
71 //              StubResponse.setupAllMocks();
72                 started= true;
73         }
74
75         public static void main(String [] args) {
76                 MockResource mockresource = new MockResource();
77                 mockresource.start(28090);
78                 mockresource.reset();
79 //              mockresource.setupStub("MockCreateTenant");
80         }
81         
82         /**
83          * Starts the wiremock server in default port
84          * @return
85          */
86         @GET
87         @Path("/start")
88         @Produces("application/json")
89         public Response start() {
90                 return startMockServer(defaultPort);
91         }
92
93         private Response startMockServer(int port) {
94                 if (!started) {
95                         initMockServer(defaultPort);
96                         System.out.println("Started Mock Server in port " + port);
97                         return Response.status(200).entity("Started Mock Server in port " + port).build();
98                 } else {
99                         return Response.status(200).entity("Mock Server is already running").build();
100                 }
101         }
102
103         /**
104          * Starts the wiremock server in a different port
105          * @param portNumber
106          * @return
107          */
108         @GET
109         @Path("/start/{portNumber}")
110         @Produces("application/json")
111         public Response start(@PathParam("portNumber") Integer portNumber) {
112                 if (portNumber == null) portNumber = defaultPort;
113                 return startMockServer(portNumber.intValue());
114         }
115
116
117         /**
118          * Stop the wiremock server
119          * @return
120          */
121         @GET
122         @Path("/stop")
123         @Produces("application/json")
124         public synchronized Response stop() {
125                 if (wireMockServer.isRunning()) {
126                         wireMockServer.stop();
127                         started = false;
128                         return Response.status(200).entity("Stopped Mock Server in port ").build();
129                 }
130                 return Response.status(200).entity("Mock Server is not running").build();
131         }
132
133
134         /**
135          * Return list of mock properties
136          * @return
137          */
138         @GET
139         @Path("/properties")
140         @Produces("application/json")
141         public Response getProperties() {
142                 return Response.status(200).entity(mockProperties).build();
143         }
144
145         /**
146          * Update a particular mock property at run-time
147          * @param name
148          * @param value
149          * @return
150          */
151         @POST
152         @Path("/properties/{name}/{value}")
153         public Response updateProperties(@PathParam("name") String name, @PathParam("value") String value) {
154                 if (mockProperties.size() > 50) return Response.serverError().build();
155                 mockProperties.put(name, value);
156                 return Response.status(200).build();
157         }
158
159         /**
160          * Reset all stubs
161          * @return
162          */
163         @GET
164         @Path("/reset")
165         @Produces("application/json")
166         public Response reset() {
167                 WireMock.reset();
168                 return Response.status(200).entity("Wiremock stubs are reset").build();
169         }
170
171         
172         /**
173          * Setup a stub selectively
174          * Prior to use, make sure that stub method is available in StubResponse class
175          * @param methodName
176          * @return
177          */
178         
179         // commenting for now until we figure out a way to use new StubResponse classes to setupStubs
180 //      @GET
181 //      @Path("/stub/{methodName}")
182 //      @Produces("application/json")
183 //      public Response setupStub(@PathParam("methodName") String methodName) {
184 //              
185 //          @SuppressWarnings("rawtypes")
186 //              Class params[] = {};
187 //          Object paramsObj[] = {};
188 //
189 //          try {
190 //                      Method thisMethod = StubResponse.class.getDeclaredMethod(methodName, params);
191 //                      try {
192 //                              thisMethod.invoke(StubResponse.class, paramsObj);
193 //                      } catch (IllegalAccessException | IllegalArgumentException
194 //                                      | InvocationTargetException e) {
195 //                              return Response.status(200).entity("Error invoking " + methodName ).build();
196 //                      }
197 //              } catch (NoSuchMethodException | SecurityException e) {
198 //                      return Response.status(200).entity("Stub " + methodName + " not found...").build();
199 //              }               
200 //              return Response.status(200).entity("Successfully invoked " + methodName).build();
201 //      }
202         
203         
204         public static Map<String,String> getMockProperties(){
205                 return mockProperties;
206         }
207 }