2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.bpmn.mock;
23 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
25 import java.util.HashMap;
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;
35 import com.github.tomakehurst.wiremock.WireMockServer;
36 import com.github.tomakehurst.wiremock.client.WireMock;
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
45 public class MockResource {
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>();
52 public static String getMockProperties(String key) {
53 return mockProperties.get(key);
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/"));
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();
75 public static void main(String [] args) {
76 MockResource mockresource = new MockResource();
77 mockresource.start(28090);
79 // mockresource.setupStub("MockCreateTenant");
83 * Starts the wiremock server in default port
88 @Produces("application/json")
89 public Response start() {
90 return startMockServer(defaultPort);
93 private Response startMockServer(int port) {
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();
99 return Response.status(200).entity("Mock Server is already running").build();
104 * Starts the wiremock server in a different port
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());
118 * Stop the wiremock server
123 @Produces("application/json")
124 public synchronized Response stop() {
125 if (wireMockServer.isRunning()) {
126 wireMockServer.stop();
128 return Response.status(200).entity("Stopped Mock Server in port ").build();
130 return Response.status(200).entity("Mock Server is not running").build();
135 * Return list of mock properties
140 @Produces("application/json")
141 public Response getProperties() {
142 return Response.status(200).entity(mockProperties).build();
146 * Update a particular mock property at run-time
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();
165 @Produces("application/json")
166 public Response reset() {
168 return Response.status(200).entity("Wiremock stubs are reset").build();
173 * Setup a stub selectively
174 * Prior to use, make sure that stub method is available in StubResponse class
179 // commenting for now until we figure out a way to use new StubResponse classes to setupStubs
181 // @Path("/stub/{methodName}")
182 // @Produces("application/json")
183 // public Response setupStub(@PathParam("methodName") String methodName) {
185 // @SuppressWarnings("rawtypes")
186 // Class params[] = {};
187 // Object paramsObj[] = {};
190 // Method thisMethod = StubResponse.class.getDeclaredMethod(methodName, params);
192 // thisMethod.invoke(StubResponse.class, paramsObj);
193 // } catch (IllegalAccessException | IllegalArgumentException
194 // | InvocationTargetException e) {
195 // return Response.status(200).entity("Error invoking " + methodName ).build();
197 // } catch (NoSuchMethodException | SecurityException e) {
198 // return Response.status(200).entity("Stub " + methodName + " not found...").build();
200 // return Response.status(200).entity("Successfully invoked " + methodName).build();
204 public static Map<String,String> getMockProperties(){
205 return mockProperties;