001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest.mock; 014 015import java.util.*; 016import java.util.concurrent.*; 017 018import org.apache.juneau.rest.*; 019import org.apache.juneau.utils.*; 020 021/** 022 * Creates a mocked interface against a REST resource class. 023 * 024 * <p> 025 * Allows you to test your REST resource classes without a running servlet container. 026 * 027 * <h5 class='figure'>Example:</h5> 028 * <p class='bcode w800'> 029 * <jk>public class</jk> MockTest { 030 * 031 * <jc>// Our REST resource to test.</jc> 032 * <ja>@Rest</ja>(serializers=JsonSerializer.Simple.<jk>class</jk>, parsers=JsonParser.<jk>class</jk>) 033 * <jk>public static class</jk> MyRest { 034 * 035 * <ja>@RestMethod</ja>(name=<jsf>PUT</jsf>, path=<js>"/String"</js>) 036 * <jk>public</jk> String echo(<ja>@Body</ja> String b) { 037 * <jk>return</jk> b; 038 * } 039 * } 040 * 041 * <ja>@Test</ja> 042 * <jk>public void</jk> testEcho() <jk>throws</jk> Exception { 043 * MockRest 044 * .<jsf>create</jsf>(MyRest.<jk>class</jk>) 045 * .put(<js>"/String"</js>, <js>"'foo'"</js>) 046 * .execute() 047 * .assertStatus(200) 048 * .assertBody(<js>"'foo'"</js>); 049 * } 050 * </p> 051 * 052 * @deprecated Use <c>org.apache.juneau.rest.mock2</c> 053 */ 054@Deprecated 055public class MockRest implements MockHttpConnection { 056 private static Map<Class<?>,RestContext> CONTEXTS = new ConcurrentHashMap<>(); 057 058 private final RestContext rc; 059 060 private MockRest(Class<?> c, boolean debug) throws Exception { 061 if (! CONTEXTS.containsKey(c)) { 062 Object r = c.newInstance(); 063 RestContext rc = RestContext.create(r).logger(debug ? BasicRestLogger.class : NoOpRestLogger.class).build(); 064 if (r instanceof RestServlet) { 065 ((RestServlet)r).setContext(rc); 066 } else { 067 rc.postInit(); 068 } 069 rc.postInitChildFirst(); 070 CONTEXTS.put(c, rc); 071 } 072 rc = CONTEXTS.get(c); 073 } 074 075 /** 076 * Create a new mock REST interface 077 * 078 * @param c The REST class. 079 * @return A new mock interface. 080 * @throws RuntimeException 081 * For testing conveniences, this method wraps all exceptions in a RuntimeException so that you can easily define mocks as reusable fields. 082 */ 083 public static MockRest create(Class<?> c) throws RuntimeException { 084 return create(c, false); 085 } 086 087 /** 088 * Create a new mock REST interface 089 * 090 * @param c The REST class. 091 * @param debug 092 * If <jk>true</jk>, the REST interface will use the {@link BasicRestLogger} for logging. 093 * <br>Otherwise, uses {@link NoOpRestLogger}. 094 * @return A new mock interface. 095 * @throws RuntimeException 096 * For testing conveniences, this method wraps all exceptions in a RuntimeException so that you can easily define mocks as reusable fields. 097 */ 098 public static MockRest create(Class<?> c, boolean debug) throws RuntimeException { 099 try { 100 return new MockRest(c, debug); 101 } catch (Exception e) { 102 throw new RuntimeException(e); 103 } 104 } 105 106 /** 107 * Performs a REST request against the REST interface. 108 * 109 * @param method The HTTP method 110 * @param path The URI path. 111 * @param body The body of the request. 112 * @return A new servlet request. 113 * @throws Exception Error occurred. 114 */ 115 @Override /* MockHttpConnection */ 116 public MockServletRequest request(String method, String path, Object body) throws Exception { 117 return MockServletRequest.create(method, path).body(body).restContext(rc); 118 } 119 120 /** 121 * Performs a REST request against the REST interface. 122 * 123 * @param method The HTTP method 124 * @param path The URI path. 125 * @return A new servlet request. 126 * @throws Exception Error occurred. 127 */ 128 public MockServletRequest request(String method, String path) throws Exception { 129 return request(method, path, null); 130 } 131 132 /** 133 * Perform a GET request. 134 * 135 * @param path The URI path. 136 * @return A new servlet request. 137 * @throws Exception Error occurred. 138 */ 139 public MockServletRequest get(String path) throws Exception { 140 return request("GET", path, null); 141 } 142 143 /** 144 * Perform a PUT request. 145 * 146 * @param path The URI path. 147 * @param body The body of the request. 148 * @return A new servlet request. 149 * @throws Exception Error occurred. 150 */ 151 public MockServletRequest put(String path, Object body) throws Exception { 152 return request("PUT", path, body); 153 } 154 155 /** 156 * Perform a POST request. 157 * 158 * @param path The URI path. 159 * @param body The body of the request. 160 * @return A new servlet request. 161 * @throws Exception Error occurred. 162 */ 163 public MockServletRequest post(String path, Object body) throws Exception { 164 return request("POST", path, body); 165 } 166 167 /** 168 * Perform a DELETE request. 169 * 170 * @param path The URI path. 171 * @return A new servlet request. 172 * @throws Exception Error occurred. 173 */ 174 public MockServletRequest delete(String path) throws Exception { 175 return request("DELETE", path, null); 176 } 177 178 /** 179 * Perform an OPTIONS request. 180 * 181 * @param path The URI path. 182 * @return A new servlet request. 183 * @throws Exception Error occurred. 184 */ 185 public MockServletRequest options(String path) throws Exception { 186 return request("OPTIONS", path, null); 187 } 188}