Fix unit testing instability
[externalapi/nbi.git] / src / test / java / org / onap / nbi / test / Context.java
1 /**
2  * Copyright (c) 2018 Orange
3  *
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
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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
12  * the License.
13  */
14 package org.onap.nbi.test;
15
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;
25
26 public class Context {
27
28
29   public static ConfigurableApplicationContext nbiServer;
30
31   public static WireMockServer wireMockServer;
32
33   public static void startServers() throws Exception {
34     startWiremock();
35
36     // NBI
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));
43     }
44
45   }
46
47   public static void startWiremock() {
48     // Wiremock
49     // If wireMockServer was previously created
50     if (wireMockServer != null) {
51       wireMockServer.stop();
52       wireMockServer.resetToDefaultMappings();
53     } else {
54       wireMockServer =
55           new WireMockServer(
56               WireMockConfiguration.wireMockConfig().port(8091).jettyStopTimeout(1000L));
57     }
58
59     wireMockServer.start();
60   }
61
62   public static void removeWireMockMapping(String mappingToBeRemoved) {
63
64     if (mappingToBeRemoved == null) {
65       return;
66     }
67
68     // Get current mappings
69     List<StubMapping> mappings = wireMockServer.listAllStubMappings().getMappings();
70
71     StubMapping mappingToDelete = null;
72     for (StubMapping mapping : mappings) {
73       if (mapping.getRequest().getUrl().equals(mappingToBeRemoved)) {
74         mappingToDelete = mapping;
75       }
76     }
77
78     if (mappingToDelete != null) {
79       wireMockServer.removeStubMapping(mappingToDelete);
80     }
81   }
82
83   public static void stopServers() throws Exception {
84
85     // NBI
86     if (nbiServer != null) { // keep spring boot side alive for all tests including package
87       // 'mock'
88       nbiServer.close();
89     }
90
91     // Wiremock
92     if (wireMockServer != null) {
93       wireMockServer.stop();
94     }
95
96   }
97
98 }