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.apache.juneau.commons.utils.CollectionUtils.*;
21  import static org.apache.juneau.http.HttpParts.*;
22  import static org.apache.juneau.httppart.HttpPartSchema.*;
23  
24  import java.io.*;
25  
26  import org.apache.juneau.*;
27  import org.apache.juneau.http.part.*;
28  import org.apache.juneau.httppart.*;
29  import org.apache.juneau.rest.annotation.*;
30  import org.apache.juneau.rest.mock.*;
31  import org.apache.juneau.rest.servlet.*;
32  import org.apache.juneau.uon.*;
33  import org.apache.juneau.utest.utils.*;
34  import org.junit.jupiter.api.*;
35  
36  class RestClient_Query_Test extends TestBase {
37  
38  	@Rest
39  	public static class A extends BasicRestObject {
40  		@RestGet
41  		public Reader query(org.apache.juneau.rest.RestRequest req) {
42  			return reader(req.getQueryParams().asQueryString());
43  		}
44  	}
45  
46  	//------------------------------------------------------------------------------------------------------------------
47  	// Method tests
48  	//------------------------------------------------------------------------------------------------------------------
49  
50  	@Test void a01_query_String_Object() throws Exception {
51  		client().queryData("foo","bar").queryData(part("foo",new StringBuilder("baz"),null)).build().get("/query").run().assertContent("foo=bar&foo=baz");
52  		client().build().get("/query").queryData("foo","bar").run().assertContent().isContains("foo=bar");
53  	}
54  
55  	@Test void a02_query_String_Object_Schema() throws Exception {
56  		var l = l("bar","baz");
57  		client().build().get("/query").queryData(part("foo",l,T_ARRAY_PIPES)).run().assertContent().asString().asUrlDecode().is("foo=bar|baz");
58  		client().queryData(part("foo",l,T_ARRAY_PIPES)).build().get("/query").run().assertContent().asString().asUrlDecode().is("foo=bar|baz");
59  	}
60  
61  	@Test void a03_query_String_Object_Schema_Serializer() throws Exception {
62  		var l = l("bar","baz");
63  		client().queryData(part("foo",l,T_ARRAY_PIPES).serializer(UonSerializer.DEFAULT)).build().get("/query").run().assertContent().asString().asUrlDecode().is("foo=@(bar,baz)");
64  	}
65  
66  	@Test void a06_query_String_Supplier() throws Exception {
67  		var l1 = l("foo","bar");
68  		var l2 = l("bar","baz");
69  		var s = MutableSupplier.of(l1);
70  		var x = client().queryData(part("foo",s,null)).build();
71  		x.get("/query").run().assertContent().asString().asUrlDecode().is("foo=foo,bar");
72  		s.set(l2);
73  		x.get("/query").run().assertContent().asString().asUrlDecode().is("foo=bar,baz");
74  	}
75  
76  	@Test void a07_query_String_Supplier_Schema() throws Exception {
77  		var l1 = a("foo","bar");
78  		var l2 = a("bar","baz");
79  		var s = MutableSupplier.of(l1);
80  		var x = client().queryData(part("foo",s,T_ARRAY_PIPES)).build();
81  		x.get("/query").queryData(part("bar",s,T_ARRAY_PIPES)).run().assertContent().asString().asUrlDecode().is("foo=foo|bar&bar=foo|bar");
82  		s.set(l2);
83  		x.get("/query").queryData(part("bar",s,T_ARRAY_PIPES)).run().assertContent().asString().asUrlDecode().is("foo=bar|baz&bar=bar|baz");
84  	}
85  
86  	@Test void a08_query_String_Supplier_Schema_Serializer() throws Exception {
87  		var l1 = l("foo","bar");
88  		var l2 = l("bar","baz");
89  		var s = MutableSupplier.of(l1);
90  		var x = client().queryData(part("foo",s,T_ARRAY_PIPES).serializer(FakeWriterSerializer.X)).build();
91  		x.get("/query").run().assertContent().asString().asUrlDecode().is("foo=xfoo|barx");
92  		s.set(l2);
93  		x.get("/query").run().assertContent().asString().asUrlDecode().is("foo=xbar|bazx");
94  	}
95  
96  	@Test void a09_query_NameValuePair() throws Exception {
97  		client().queryData(part("foo","bar")).build().get("/query").queryData(part("foo","baz")).run().assertContent().isContains("foo=bar&foo=baz");
98  	}
99  
100 	public static class A10 {
101 		public String foo="bar";
102 	}
103 
104 	@Test void a10_queries_Objects() throws Exception {
105 		client().build().get("/query").queryData(part("foo","bar")).run().assertContent("foo=bar");
106 	}
107 
108 	public static class A12 extends Reader {
109 		@Override
110 		public int read(char[] cbuf,int off,int len) throws IOException {
111 			throw new IOException("foo");
112 		}
113 		@Override
114 		public void close() throws IOException {}  // NOSONAR
115 	}
116 
117 	@Test void a12_queryCustom_Object() throws Exception {
118 		client().build().get("/query").queryCustom("foo=bar").run().assertContent().isContains("foo=bar");
119 		assertThrowsWithMessage(Exception.class, "foo", ()->client().build().get("").queryCustom(new A12()));
120 	}
121 
122 	//------------------------------------------------------------------------------------------------------------------
123 	// Helper methods.
124 	//------------------------------------------------------------------------------------------------------------------
125 
126 	private static BasicPart part(String name, Object val) {
127 		return basicPart(name, val);
128 	}
129 
130 	private static SerializedPart part(String name, Object val, HttpPartSchema schema) {
131 		return serializedPart(name, val).schema(schema);
132 	}
133 
134 	private static RestClient.Builder client() {
135 		return MockRestClient.create(A.class).json5();
136 	}
137 }