001/*-------------------------------------------------------------------------+ 002| | 003| Copyright (c) 2005-2020 The ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016| | 017+-------------------------------------------------------------------------*/ 018package org.conqat.lib.commons.test.mockserver; 019 020import java.net.MalformedURLException; 021import java.net.URI; 022import java.net.URL; 023 024/** 025 * Abstraction of a mock server. The mock server can act as reverse proxy 026 * between your test and a 3rd party system you want to integrate with. The mock 027 * server is able to record HTTP traffic and replay it afterwards so that you do 028 * not need a connection to the 3rd party system during regular test execution. 029 * This speeds up test execution and also separates data changes on the 3rd 030 * party system from your test logic. 031 * 032 * A concrete instance of this class will be injected to test methods if the 033 * class is annotated with {@link MockServerTest}. It provides access to an URL 034 * pointing either to the mock reverse proxy or the live system, depending on 035 * the configured execution mode. 036 */ 037public class MockServer { 038 private final URL url; 039 040 /* package */ MockServer(String url) { 041 try { 042 this.url = URI.create(url).toURL(); 043 } catch (MalformedURLException e) { 044 throw new AssertionError(e); 045 } 046 } 047 048 /** Returns the server URL as string. */ 049 public String getUrlAsString() { 050 return url.toString(); 051 } 052 053 /** Returns the server URL. */ 054 public URL getUrl() { 055 return url; 056 } 057 058 @Override 059 public String toString() { 060 return getUrlAsString(); 061 } 062}