View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.rest.client;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.io.*;
23  
24  import org.apache.http.entity.*;
25  import org.apache.juneau.*;
26  import org.apache.juneau.marshaller.*;
27  import org.apache.juneau.parser.*;
28  import org.apache.juneau.rest.annotation.*;
29  import org.apache.juneau.rest.mock.*;
30  import org.apache.juneau.rest.servlet.*;
31  import org.junit.jupiter.api.*;
32  
33  class RestCallException_Test extends TestBase {
34  
35  	public static class ABean {
36  		public int f;
37  		static ABean get() {
38  			var x = new ABean();
39  			x.f = 1;
40  			return x;
41  		}
42  		@Override
43  		public String toString() {
44  			return Json5.of(this);
45  		}
46  	}
47  
48  	@Rest
49  	public static class A extends BasicRestObject {
50  		@RestPost
51  		public InputStream echo(InputStream is) {
52  			return is;
53  		}
54  	}
55  
56  	@Test void a01_basic() throws Exception {
57  		try {
58  			client().build().get().run();  // NOSONAR
59  			fail();
60  		} catch (RestCallException e) {
61  			assertEquals(404, e.getResponseCode());
62  			assertNull(e.getCause());
63  		}
64  
65  		try {
66  			client().build().post("/echo",new StringEntity("{f:")).run().getContent().as(ABean.class);
67  			fail();
68  		} catch (RestCallException e) {
69  			assertThrowable(Exception.class, "Could not find '}'", e.getCause(ParseException.class));
70  		}
71  
72  		var e = new RestCallException(null, null, null);
73  		assertNotNull(e.getThrown());
74  		assertFalse(e.getThrown().isPresent());
75  		assertEquals(0, e.getResponseCode());
76  	}
77  
78  	//-----------------------------------------------------------------------------------------------------------------
79  	// Helper methods.
80  	//-----------------------------------------------------------------------------------------------------------------
81  
82  	private static RestClient.Builder client() {
83  		return MockRestClient.create(A.class).json5().noTrace();
84  	}
85  }