Ray Milkey | 8320088 | 2017-11-13 19:18:21 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.onlab.junit; |
| 18 | |
| 19 | import java.io.IOException; |
| 20 | import java.io.InputStream; |
| 21 | import java.net.HttpURLConnection; |
| 22 | import java.net.URL; |
| 23 | import java.net.URLConnection; |
| 24 | import java.net.URLStreamHandler; |
| 25 | import java.net.URLStreamHandlerFactory; |
| 26 | |
| 27 | /** |
| 28 | * Intercepts HTTP URL connections and supplies predefined data from a resource. Used for supplying data to HTTP |
| 29 | * connections in unit tests. |
| 30 | */ |
| 31 | public class HttpResourceUrlInterceptor { |
| 32 | |
| 33 | /** |
| 34 | * Handles creation of HTTP Connections to the resource data. |
| 35 | */ |
| 36 | private static class HttpResourceUrlInterceptorHandler extends URLStreamHandler { |
| 37 | |
| 38 | String resourceName; |
| 39 | |
| 40 | HttpResourceUrlInterceptorHandler(String resourceName) { |
| 41 | this.resourceName = resourceName; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | protected URLConnection openConnection(URL u) throws IOException { |
| 46 | return new InterceptedHttpUrlConnection(u, resourceName); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Creates stream handlers for the interceptor. |
| 52 | */ |
| 53 | public static class HttpResourceUrlInterceptorFactory implements URLStreamHandlerFactory { |
| 54 | |
| 55 | String resourceName; |
| 56 | |
| 57 | public HttpResourceUrlInterceptorFactory(String resourceName) { |
| 58 | this.resourceName = resourceName; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public URLStreamHandler createURLStreamHandler(String protocol) { |
| 63 | return new HttpResourceUrlInterceptorHandler(resourceName); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * HTTP Url Connection that is backed by the data in the resource. |
| 69 | */ |
| 70 | private static final class InterceptedHttpUrlConnection extends HttpURLConnection { |
| 71 | |
| 72 | private final String resourceName; |
| 73 | |
| 74 | private InterceptedHttpUrlConnection(URL url, String resourceName) { |
| 75 | super(url); |
| 76 | this.resourceName = resourceName; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public int getResponseCode() { |
| 81 | return HTTP_OK; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public boolean usingProxy() { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void disconnect() { |
| 91 | // noop |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void connect() { |
| 96 | // noop |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public InputStream getInputStream() throws IOException { |
| 101 | return this.getClass().getResource(resourceName).openStream(); |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | |