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 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;
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;
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();
45 public void start() throws Exception {
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 {
56 Future<?> serverStarted = executorService.submit(() -> {
60 if (_server.isStarted()) {
63 } catch (InterruptedException e) {
67 serverStarted.get(30, TimeUnit.SECONDS);
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() {
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);
90 public void stop() throws Exception {