Format Java code with respect to ONAP Code Style
[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
15 package org.onap.nbi.test;
16
17 import com.github.tomakehurst.wiremock.WireMockServer;
18 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
19 import com.github.tomakehurst.wiremock.stubbing.StubMapping;
20 import java.util.List;
21 import org.onap.nbi.Application;
22 import org.springframework.context.ConfigurableApplicationContext;
23 import org.springframework.mock.web.MockHttpServletRequest;
24 import org.springframework.web.context.request.RequestContextHolder;
25 import org.springframework.web.context.request.ServletRequestAttributes;
26
27 public class Context {
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(WireMockConfiguration.wireMockConfig().port(8091).jettyStopTimeout(1000L));
56         }
57
58         wireMockServer.start();
59     }
60
61     public static void removeWireMockMapping(String mappingToBeRemoved) {
62
63         if (mappingToBeRemoved == null) {
64             return;
65         }
66
67         // Get current mappings
68         List<StubMapping> mappings = wireMockServer.listAllStubMappings().getMappings();
69
70         StubMapping mappingToDelete = null;
71         for (StubMapping mapping : mappings) {
72             if (mapping.getRequest().getUrl().equals(mappingToBeRemoved)) {
73                 mappingToDelete = mapping;
74             }
75         }
76
77         if (mappingToDelete != null) {
78             wireMockServer.removeStubMapping(mappingToDelete);
79         }
80     }
81
82     public static void stopServers() throws Exception {
83
84         // NBI
85         if (nbiServer != null) { // keep spring boot side alive for all tests including package
86             // 'mock'
87             nbiServer.close();
88         }
89
90         // Wiremock
91         if (wireMockServer != null) {
92             wireMockServer.stop();
93         }
94
95     }
96
97     public static void stopWiremock() throws Exception {
98
99         // Wiremock
100         if (wireMockServer != null) {
101             wireMockServer.stop();
102         }
103
104     }
105 }