2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
14 package org.onap.nbi.test;
16 import com.github.tomakehurst.wiremock.WireMockServer;
17 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
18 import com.github.tomakehurst.wiremock.stubbing.StubMapping;
19 import java.util.List;
20 import org.onap.nbi.Application;
21 import org.springframework.context.ConfigurableApplicationContext;
22 import org.springframework.mock.web.MockHttpServletRequest;
23 import org.springframework.web.context.request.RequestContextHolder;
24 import org.springframework.web.context.request.ServletRequestAttributes;
26 public class Context {
29 public static ConfigurableApplicationContext nbiServer;
31 public static WireMockServer wireMockServer;
33 public static void startServers() throws Exception {
37 if (nbiServer == null) {
38 String[] args = new String[1];
39 args[0] = "--spring.profiles.active=test";
40 nbiServer = Application.run(args);
41 MockHttpServletRequest request = new MockHttpServletRequest();
42 RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
47 public static void startWiremock() {
49 // If wireMockServer was previously created
50 if (wireMockServer != null) {
51 wireMockServer.stop();
52 wireMockServer.resetToDefaultMappings();
56 WireMockConfiguration.wireMockConfig().port(8091).jettyStopTimeout(1000L));
59 wireMockServer.start();
62 public static void removeWireMockMapping(String mappingToBeRemoved) {
64 if (mappingToBeRemoved == null) {
68 // Get current mappings
69 List<StubMapping> mappings = wireMockServer.listAllStubMappings().getMappings();
71 StubMapping mappingToDelete = null;
72 for (StubMapping mapping : mappings) {
73 if (mapping.getRequest().getUrl().equals(mappingToBeRemoved)) {
74 mappingToDelete = mapping;
78 if (mappingToDelete != null) {
79 wireMockServer.removeStubMapping(mappingToDelete);
83 public static void stopServers() throws Exception {
86 if (nbiServer != null) { // keep spring boot side alive for all tests including package
92 if (wireMockServer != null) {
93 wireMockServer.stop();