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.http;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.apache.juneau.http.HttpHeaders.*;
21  import static org.apache.juneau.http.HttpParts.*;
22  import static org.junit.jupiter.api.Assertions.*;
23  
24  import java.util.*;
25  import java.util.function.*;
26  
27  import org.apache.http.*;
28  import org.apache.juneau.*;
29  import org.apache.juneau.http.header.*;
30  import org.apache.juneau.http.part.*;
31  import org.apache.juneau.internal.*;
32  import org.apache.juneau.marshaller.*;
33  import org.junit.jupiter.api.*;
34  
35  class BasicPart_Test extends TestBase {
36  
37  	@Test void a01_ofPair() {
38  		var x = basicPart("Foo:bar");
39  		assertEquals("Foo", x.getName());
40  		assertEquals("bar", x.getValue());
41  
42  		x = basicPart(" Foo : bar ");
43  		assertEquals("Foo", x.getName());
44  		assertEquals("bar", x.getValue());
45  
46  		x = basicPart(" Foo : bar : baz ");
47  		assertEquals("Foo", x.getName());
48  		assertEquals("bar : baz", x.getValue());
49  
50  		x = basicPart("Foo");
51  		assertEquals("Foo", x.getName());
52  		assertEquals("", x.getValue());
53  
54  		assertNull(basicPart((String)null));
55  	}
56  
57  	@Test void a02_of() {
58  		var x = part("Foo","bar");
59  		assertString("Foo=bar", x);
60  		x = part("Foo",()->"bar");
61  		assertString("Foo=bar", x);
62  	}
63  
64  	@Test void a03_cast() {
65  		var x1 = part("X1","1");
66  		var x2 = serializedPart("X2","2");
67  		var x3 = header("X3","3");
68  		var x4 = serializedHeader("X4","4");
69  		Map.Entry<String,Object> x5 = CollectionUtils.map("X5",(Object)"5").entrySet().iterator().next();
70  		org.apache.http.message.BasicNameValuePair x6 = new org.apache.http.message.BasicNameValuePair("X6","6");
71  		NameValuePairable x7 = () -> part("X7","7");
72  		Headerable x8 = () -> header("X8","8");
73  
74  		assertTypeAndJson(NameValuePair.class, "'X1=1'", BasicPart.cast(x1));
75  		assertTypeAndJson(NameValuePair.class, "'X2=2'", BasicPart.cast(x2));
76  		assertTypeAndJson(NameValuePair.class, "'X3: 3'", BasicPart.cast(x3));
77  		assertTypeAndJson(NameValuePair.class, "'X4: 4'", BasicPart.cast(x4));
78  		assertTypeAndJson(NameValuePair.class, "'X5=5'", BasicPart.cast(x5));
79  		assertTypeAndJson(NameValuePair.class, "{name:'X6',value:'6'}", BasicPart.cast(x6));
80  		assertTypeAndJson(NameValuePair.class, "'X7=7'", BasicPart.cast(x7));
81  		assertTypeAndJson(NameValuePair.class, "'X8=8'", BasicPart.cast(x8));
82  
83  		assertThrowsWithMessage(BasicRuntimeException.class, "Object of type java.lang.String could not be converted to a Part.", ()->BasicPart.cast("X"));
84  		assertThrowsWithMessage(BasicRuntimeException.class, "Object of type null could not be converted to a Part.", ()->BasicPart.cast(null));
85  
86  		assertTrue(BasicPart.canCast(x1));
87  		assertTrue(BasicPart.canCast(x2));
88  		assertTrue(BasicPart.canCast(x3));
89  		assertTrue(BasicPart.canCast(x4));
90  		assertTrue(BasicPart.canCast(x5));
91  		assertTrue(BasicPart.canCast(x6));
92  		assertTrue(BasicPart.canCast(x7));
93  
94  		assertFalse(BasicPart.canCast("X"));
95  		assertFalse(BasicPart.canCast(null));
96  	}
97  
98  	@Test void a04_asHeader() {
99  		var x = part("X1","1");
100 		assertString("X1: 1", x.asHeader());
101 	}
102 
103 	@Test void a05_assertions() {
104 		var x = part("X1","1");
105 		x.assertName().is("X1").assertValue().is("1");
106 	}
107 
108 	//------------------------------------------------------------------------------------------------------------------
109 	// Utility methods
110 	//------------------------------------------------------------------------------------------------------------------
111 
112 	private BasicHeader header(String name, Object val) {
113 		return basicHeader(name, val);
114 	}
115 
116 	private BasicPart part(String name, Supplier<?> val) {
117 		return basicPart(name, val);
118 	}
119 
120 	private BasicPart part(String name, Object val) {
121 		return basicPart(name, val);
122 	}
123 
124 	/**
125 	 * Asserts the JSON5 representation of the specified object.
126 	 */
127 	private void assertTypeAndJson(Class<?> c, String json, Object value) {
128 		assertInstanceOf(c, value);
129 		assertEquals(json, Json5.DEFAULT.write(value));
130 	}
131 }