4109b13297e663781ff7159710a3614a11b296b2
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / vnfm / HttpTestServer.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm;
18
19 import com.google.common.io.ByteStreams;
20 import org.eclipse.jetty.server.NetworkTrafficServerConnector;
21 import org.eclipse.jetty.server.Server;
22 import org.eclipse.jetty.server.handler.AbstractHandler;
23 import org.eclipse.jetty.util.ssl.SslContextFactory;
24
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import java.io.IOException;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.concurrent.ExecutorService;
34 import java.util.concurrent.Executors;
35 import java.util.concurrent.Future;
36 import java.util.concurrent.TimeUnit;
37
38 public class HttpTestServer {
39     public Server _server;
40     public volatile List<String> requests = new ArrayList<>();
41     public volatile List<Integer> codes = new ArrayList<>();
42     public volatile List<String> respones = new ArrayList<>();
43     ExecutorService executorService = Executors.newCachedThreadPool();
44
45     public void start() throws Exception {
46         configureServer();
47         startServer();
48     }
49
50     //the server starts up asynchronously there is no other way to wait for the server to be up and running
51     @SuppressWarnings("squid:S2925")
52     private void startServer() throws Exception {
53         requests.clear();
54         codes.clear();
55         _server.start();
56         Future<?> serverStarted = executorService.submit(() -> {
57             while (true) {
58                 try {
59                     Thread.sleep(10);
60                     if (_server.isStarted()) {
61                         return;
62                     }
63                 } catch (InterruptedException e) {
64                 }
65             }
66         });
67         serverStarted.get(30, TimeUnit.SECONDS);
68     }
69
70     protected void configureServer() throws Exception {
71         Path jksPath = Paths.get(TestCbamTokenProvider.class.getResource("/unittests/localhost.jks").toURI());
72         String path = jksPath.normalize().toAbsolutePath().toUri().toString();
73         _server = new Server();
74         SslContextFactory factory = new SslContextFactory(path);
75         factory.setKeyStorePassword("changeit");
76         NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(_server, factory);
77         connector.setHost("127.0.0.1");
78         _server.addConnector(connector);
79         _server.setHandler(new AbstractHandler() {
80             @Override
81             public void handle(String target, org.eclipse.jetty.server.Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
82                 requests.add(new String(ByteStreams.toByteArray(request.getInputStream())));
83                 httpServletResponse.getWriter().write(respones.remove(0));
84                 httpServletResponse.setStatus(codes.remove(0));
85                 request.setHandled(true);
86             }
87         });
88     }
89
90     public void stop() throws Exception {
91         _server.stop();
92     }
93 }