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