2 * ============LICENSE_START=======================================================
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
19 package org.openecomp.mso.bpmn.mock;
21 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
23 import java.util.HashMap;
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;
33 import com.github.tomakehurst.wiremock.WireMockServer;
34 import com.github.tomakehurst.wiremock.client.WireMock;
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
43 public class MockResource {
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<>();
50 public static String getMockProperties(String key) {
51 return mockProperties.get(key);
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/"));
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();
73 public static void main(String [] args) {
74 MockResource mockresource = new MockResource();
75 mockresource.start(28090);
77 // mockresource.setupStub("MockCreateTenant");
81 * Starts the wiremock server in default port
86 @Produces("application/json")
87 public Response start() {
88 return startMockServer(defaultPort);
91 private Response startMockServer(int port) {
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();
97 return Response.status(200).entity("Mock Server is already running").build();
102 * Starts the wiremock server in a different port
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.intValue());
116 * Stop the wiremock server
121 @Produces("application/json")
122 public synchronized Response stop() {
123 if (wireMockServer.isRunning()) {
124 wireMockServer.stop();
126 return Response.status(200).entity("Stopped Mock Server in port ").build();
128 return Response.status(200).entity("Mock Server is not running").build();
133 * Return list of mock properties
138 @Produces("application/json")
139 public Response getProperties() {
140 return Response.status(200).entity(mockProperties).build();
144 * Update a particular mock property at run-time
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();
163 @Produces("application/json")
164 public Response reset() {
166 return Response.status(200).entity("Wiremock stubs are reset").build();
171 * Setup a stub selectively
172 * Prior to use, make sure that stub method is available in StubResponse class
177 // commenting for now until we figure out a way to use new StubResponse classes to setupStubs
179 // @Path("/stub/{methodName}")
180 // @Produces("application/json")
181 // public Response setupStub(@PathParam("methodName") String methodName) {
183 // @SuppressWarnings("rawtypes")
184 // Class params[] = {};
185 // Object paramsObj[] = {};
188 // Method thisMethod = StubResponse.class.getDeclaredMethod(methodName, params);
190 // thisMethod.invoke(StubResponse.class, paramsObj);
191 // } catch (IllegalAccessException | IllegalArgumentException
192 // | InvocationTargetException e) {
193 // return Response.status(200).entity("Error invoking " + methodName ).build();
195 // } catch (NoSuchMethodException | SecurityException e) {
196 // return Response.status(200).entity("Stub " + methodName + " not found...").build();
198 // return Response.status(200).entity("Successfully invoked " + methodName).build();
202 public static Map<String,String> getMockProperties(){
203 return mockProperties;