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.commons.utils.CollectionUtils.*;
21  import static org.apache.juneau.http.HttpHeaders.*;
22  import static org.apache.juneau.httppart.HttpPartDataType.*;
23  import static org.apache.juneau.httppart.HttpPartSchema.*;
24  import static org.apache.juneau.junit.bct.BctAssertions.*;
25  import static org.junit.jupiter.api.Assertions.*;
26  
27  import org.apache.juneau.*;
28  import org.apache.juneau.http.header.*;
29  import org.apache.juneau.httppart.*;
30  import org.apache.juneau.oapi.*;
31  import org.apache.juneau.serializer.*;
32  import org.junit.jupiter.api.*;
33  
34  class SerializedHeader_Test extends TestBase {
35  
36  	private static final OpenApiSerializerSession OAPI_SESSION = OpenApiSerializer.DEFAULT.getSession();
37  	private static final OpenApiSerializer OAPI_SERIALIZER = OpenApiSerializer.DEFAULT;
38  
39  	@Test void a01_basic() {
40  		var x1 = new SerializedHeader("Foo",l("bar","baz"),OAPI_SESSION,T_ARRAY_PIPES,true);
41  		assertString("Foo: bar|baz", x1);
42  	}
43  
44  	@Test void a02_type() {
45  		var x1 = serializedHeader("Foo",2).serializer(OAPI_SERIALIZER).schema(schema(INTEGER).maximum(1).build());
46  		assertThrowsWithMessage(RuntimeException.class, "Validation error on request HEADER parameter 'Foo'='2'", x1::toString);
47  	}
48  
49  	@Test void a03_serializer() {
50  		var x1 = serializedHeader("Foo",l("bar","baz")).serializer((HttpPartSerializer)null);
51  		assertEquals("[bar, baz]", x1.getValue());
52  		var x2 = serializedHeader("Foo",l("bar","baz")).serializer((HttpPartSerializer)null).serializer(OAPI_SERIALIZER);
53  		assertEquals("bar,baz", x2.getValue());
54  		var x3 = serializedHeader("Foo",l("bar","baz")).serializer(OAPI_SERIALIZER).serializer((HttpPartSerializerSession)null);
55  		assertEquals("[bar, baz]", x3.getValue());
56  		var x4 = serializedHeader("Foo",l("bar","baz")).serializer(OAPI_SERIALIZER).copyWith(null,null);
57  		assertEquals("bar,baz", x4.getValue());
58  		var x5 = serializedHeader("Foo",l("bar","baz")).copyWith(OAPI_SERIALIZER.getSession(),null);
59  		assertEquals("bar,baz", x5.getValue());
60  	}
61  
62  	@Test void a04_skipIfEmpty() {
63  		var x1 = serializedHeader("Foo",null).skipIfEmpty();
64  		assertNull(x1.getValue());
65  		var x2 = serializedHeader("Foo","").skipIfEmpty();
66  		assertNull(x2.getValue());
67  		var x3 = serializedHeader("Foo","").schema(schema(STRING).default_("bar").build()).serializer(OAPI_SERIALIZER).skipIfEmpty();
68  		assertThrowsWithMessage(Exception.class, "Empty value not allowed.", x3::getValue);
69  	}
70  
71  	@Test void a05_getValue_defaults() {
72  		var x1 = serializedHeader("Foo",null).schema(schema(INTEGER).default_("1").build()).serializer(OAPI_SESSION);
73  		assertEquals("1", x1.getValue());
74  
75  		var x2 = serializedHeader("Foo",null).schema(schema(STRING).required().allowEmptyValue().build()).serializer(OAPI_SESSION);
76  		assertNull(x2.getValue());
77  
78  		var x3 = serializedHeader("Foo",null).schema(schema(STRING).required(false).build()).serializer(OAPI_SESSION);
79  		assertNull(x3.getValue());
80  
81  		var x4 = serializedHeader("Foo",null).schema(schema(STRING).required().build()).serializer(OAPI_SESSION);
82  		assertThrowsWithMessage(Exception.class, "Required value not provided.", x4::getValue);
83  
84  		var x5 = serializedHeader("Foo",null).schema(schema(STRING).required().build()).serializer(new BadPartSerializerSession());
85  		assertThrowsWithMessage(Exception.class, "Bad", x5::getValue);
86  	}
87  
88  	private static class BadPartSerializerSession implements HttpPartSerializerSession {
89  		@Override
90  		public String serialize(HttpPartType type, HttpPartSchema schema, Object value) throws SerializeException, SchemaValidationException {
91  			throw new SerializeException("Bad");
92  		}
93  	}
94  
95  	//------------------------------------------------------------------------------------------------------------------
96  	// Utility methods
97  	//------------------------------------------------------------------------------------------------------------------
98  
99  	private static HttpPartSchema.Builder schema(HttpPartDataType dataType) {
100 		return HttpPartSchema.create().type(dataType);
101 	}
102 }