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