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  
18  package org.apache.juneau.utest.utils;
19  
20  import static org.apache.juneau.common.utils.Utils.*;
21  import static org.apache.juneau.internal.ArrayUtils.toList;
22  
23  import java.io.*;
24  import java.util.*;
25  import java.util.function.*;
26  
27  import org.apache.juneau.common.utils.*;
28  import org.apache.juneau.httppart.*;
29  import org.apache.juneau.internal.*;
30  import org.apache.juneau.serializer.*;
31  
32  /**
33   * Utility class for creating mocked writer serializers.
34   */
35  public class FakeWriterSerializer extends WriterSerializer implements HttpPartSerializer {
36  
37  	//-----------------------------------------------------------------------------------------------------------------
38  	// Predefined types
39  	//-----------------------------------------------------------------------------------------------------------------
40  
41  	public static final X X = new X(create());
42  
43  	public static class X extends FakeWriterSerializer {
44  		public X(Builder builder) {
45  			super(builder
46  				.function((s,o) -> out(o))
47  				.partFunction((s,t,o) -> out(o))
48  			);
49  		}
50  
51  		private static String out(Object value) {
52  			if (value instanceof List<?> x)
53  				value = Utils.join(x, '|');
54  			if (value instanceof Collection<?> x)
55  				value = Utils.join(x, '|');
56  			if (isArray(value))
57  				value = Utils.join(toList(value, Object.class), "|");
58  			return "x" + value + "x";
59  		}
60  	}
61  
62  	//-------------------------------------------------------------------------------------------------------------------
63  	// Static
64  	//-------------------------------------------------------------------------------------------------------------------
65  
66  	public static Builder create() {
67  		return new Builder();
68  	}
69  
70  	//-------------------------------------------------------------------------------------------------------------------
71  	// Builder
72  	//-------------------------------------------------------------------------------------------------------------------
73  
74  	public static class Builder extends WriterSerializer.Builder {
75  		Function2<WriterSerializerSession,Object,String> function = (s,o) -> s(o);
76  		Function3<HttpPartType,HttpPartSchema,Object,String> partFunction = (t,s,o) -> s(o);
77  		Function<WriterSerializerSession,Map<String,String>> headers = s -> Collections.emptyMap();
78  
79  		public Builder function(Function2<WriterSerializerSession,Object,String> value) {
80  			function = value;
81  			return this;
82  		}
83  
84  		public Builder partFunction(Function3<HttpPartType,HttpPartSchema,Object,String> value) {
85  			partFunction = value;
86  			return this;
87  		}
88  
89  		public Builder headers(Function<WriterSerializerSession,Map<String,String>> value) {
90  			headers = value;
91  			return this;
92  		}
93  
94  		@Override
95  		public Builder produces(String value) {
96  			super.produces(value);
97  			return this;
98  		}
99  
100 		@Override
101 		public Builder accept(String value) {
102 			super.accept(value);
103 			return this;
104 		}
105 
106 		@Override
107 		public Builder copy() {
108 			return this;
109 		}
110 	}
111 
112 	private final Function2<WriterSerializerSession,Object,String> function;
113 	private final Function3<HttpPartType,HttpPartSchema,Object,String> partFunction;
114 	private final Function<WriterSerializerSession,Map<String,String>> headers;
115 
116 	public FakeWriterSerializer(Builder builder) {
117 		super(builder);
118 		this.function = builder.function;
119 		this.partFunction = builder.partFunction;
120 		this.headers = builder.headers;
121 	}
122 
123 	@Override /* SerializerSession */
124 	protected void doSerialize(SerializerSession session, SerializerPipe out, Object o) throws IOException, SerializeException {
125 		out.getWriter().write(function.apply((WriterSerializerSession)session,o));
126 	}
127 
128 	@Override /* SerializerSession */
129 	public Map<String,String> getResponseHeaders(SerializerSession session) {
130 		return headers.apply((WriterSerializerSession)session);
131 	}
132 
133 	@Override
134 	public HttpPartSerializerSession getPartSession() {
135 		return partFunction::apply;
136 	}
137 }