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