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