2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm;
19 import com.google.common.io.ByteStreams;
20 import java.io.IOException;
21 import java.nio.file.Path;
22 import java.nio.file.Paths;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.Future;
28 import java.util.concurrent.TimeUnit;
29 import javax.servlet.ServletException;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import org.eclipse.jetty.server.NetworkTrafficServerConnector;
33 import org.eclipse.jetty.server.Server;
34 import org.eclipse.jetty.server.handler.AbstractHandler;
35 import org.eclipse.jetty.util.ssl.SslContextFactory;
37 public class HttpTestServer {
38 public Server _server;
39 public volatile List<String> requests = new ArrayList<>();
40 public volatile List<Integer> codes = new ArrayList<>();
41 public volatile List<String> respones = new ArrayList<>();
42 ExecutorService executorService = Executors.newCachedThreadPool();
44 public void start() throws Exception {
49 //the server starts up asynchronously there is no other way to wait for the server to be up and running
50 @SuppressWarnings("squid:S2925")
51 private void startServer() throws Exception {
55 Future<?> serverStarted = executorService.submit(() -> {
59 if (_server.isStarted()) {
62 } catch (InterruptedException e) {
66 serverStarted.get(30, TimeUnit.SECONDS);
69 protected void configureServer() throws Exception {
70 Path jksPath = Paths.get(TestCbamTokenProvider.class.getResource("/unittests/localhost.jks").toURI());
71 String path = jksPath.normalize().toAbsolutePath().toUri().toString();
72 _server = new Server();
73 SslContextFactory factory = new SslContextFactory(path);
74 factory.setKeyStorePassword("changeit");
75 NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(_server, factory);
76 connector.setHost("127.0.0.1");
77 _server.addConnector(connector);
78 _server.setHandler(new AbstractHandler() {
80 public void handle(String target, org.eclipse.jetty.server.Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
81 requests.add(new String(ByteStreams.toByteArray(request.getInputStream())));
82 httpServletResponse.getWriter().write(respones.remove(0));
83 httpServletResponse.setStatus(codes.remove(0));
84 request.setHandled(true);
89 public void stop() throws Exception {