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.svl;
18  
19  import static org.apache.juneau.commons.utils.CollectionUtils.*;
20  import static org.apache.juneau.junit.bct.BctAssertions.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import java.util.*;
24  
25  import org.apache.juneau.*;
26  import org.apache.juneau.collections.*;
27  import org.junit.jupiter.api.*;
28  
29  class ResolvingJsonMapTest extends TestBase {
30  
31  	//====================================================================================================
32  	// test - Basic tests
33  	//====================================================================================================
34  	@Test void a01_basic() {
35  		var vr = VarResolver.create().defaultVars().vars(XVar.class).build();
36  		var m = new ResolvingJsonMap(vr.createSession());
37  
38  		m.put("foo", "$X{a}");
39  		assertEquals("1", m.get("foo"));
40  
41  		m.put("foo", a("$X{a}"));
42  		assertList(m.get("foo"), "1");
43  
44  		m.put("foo", list("$X{a}"));
45  		assertList(m.get("foo"), "1");
46  
47  		m.put("foo", m("k1","$X{a}"));
48  		assertBean(m, "foo{k1}", "{1}");
49  	}
50  
51  	public static class XVar extends MapVar {
52  		public XVar() {
53  			super("X", JsonMap.of("a", 1, "b", 2, "c", 3));
54  		}
55  	}
56  
57  	//====================================================================================================
58  	// testNulls
59  	//====================================================================================================
60  	@Test void a02_nulls() {
61  		var vr = VarResolver.create().defaultVars().vars(XVar.class).build();
62  		var m = new ResolvingJsonMap(vr.createSession());
63  
64  		m.put("foo", null);
65  		assertNull(m.get("foo"));
66  
67  		m.put("foo", a((String)null));
68  		assertList(m.get("foo"), (Object)null);
69  
70  		m.put("foo", l((String)null));
71  		assertList(m.get("foo"), (Object)null);
72  
73  		m.put("foo", m("k1",null));
74  		assertBean(m, "foo{k1}", "{<null>}");
75  	}
76  
77  	//====================================================================================================
78  	// testNonStrings
79  	//====================================================================================================
80  	@Test void a03_nonStrings() {
81  		var vr = VarResolver.create().defaultVars().vars(XVar.class).build();
82  		var m = new ResolvingJsonMap(vr.createSession());
83  
84  		m.put("foo", FooEnum.ONE);
85  		assertString("ONE", m.get("foo"));
86  		m.put("foo", ao(FooEnum.ONE));
87  		assertList(m.get("foo"), "ONE");
88  
89  		m.put("foo", l(FooEnum.ONE));
90  		assertList(m.get("foo"), "ONE");
91  
92  		m.put("foo", m(FooEnum.ONE,FooEnum.ONE));
93  		assertBean(m, "foo", "{ONE=ONE}");
94  	}
95  
96  	public enum FooEnum {
97  		ONE
98  	}
99  
100 	//====================================================================================================
101 	// testInner - Test inner maps
102 	//====================================================================================================
103 	@Test void a04_inner() {
104 		var vr = VarResolver.create().defaultVars().vars(XVar.class).build();
105 		var m = new ResolvingJsonMap(vr.createSession());
106 		var m2 = new JsonMap();
107 		var m3 = new JsonMap();
108 		m.inner(m2);
109 		m2.inner(m3);
110 
111 		m3.put("foo", "$X{a}");
112 		assertEquals("1", m.get("foo"));
113 
114 		m3.put("foo", a("$X{a}"));
115 		assertList(m.get("foo"), "1");
116 
117 		m3.put("foo", l("$X{a}"));
118 		assertList(m.get("foo"), "1");
119 
120 		m3.put("foo", m("k1","$X{a}"));
121 		assertBean(m, "foo{k1}", "{1}");
122 	}
123 
124 	//====================================================================================================
125 	// testFluentSetters - Test fluent setter overrides
126 	//====================================================================================================
127 	@Test void a05_fluentSetters() {
128 		var vr = VarResolver.create().defaultVars().vars(XVar.class).build();
129 		var m = new ResolvingJsonMap(vr.createSession());
130 
131 		// Test inner() returns same instance for fluent chaining
132 		var innerMap = new HashMap<String,Object>();
133 		innerMap.put("test", "$X{a}");
134 		assertSame(m, m.inner(innerMap));
135 		assertEquals("1", m.get("test"));
136 
137 		// Test session() returns same instance
138 		BeanSession session = BeanContext.DEFAULT.getSession();
139 		assertSame(m, m.session(session));
140 
141 		// Test append(String, Object) returns same instance
142 		assertSame(m, m.append("key1", "$X{b}"));
143 		assertEquals("2", m.get("key1"));
144 
145 		// Test append(Map) returns same instance
146 		var appendMap = new HashMap<String,Object>();
147 		appendMap.put("key2", "$X{c}");
148 		assertSame(m, m.append(appendMap));
149 		assertEquals("3", m.get("key2"));
150 
151 		// Test appendIf() returns same instance
152 		assertSame(m, m.appendIf(true, "key3", "value3"));
153 		assertEquals("value3", m.get("key3"));
154 		assertSame(m, m.appendIf(false, "key4", "value4"));
155 		assertNull(m.get("key4"));
156 
157 		// Test filtered() returns same instance
158 		assertSame(m, m.filtered(x -> x != null));
159 
160 		// Test keepAll() returns same instance
161 		assertSame(m, m.keepAll("key1", "key2"));
162 
163 		// Test setBeanSession() returns same instance
164 		assertSame(m, m.setBeanSession(session));
165 
166 		// Test modifiable() returns a new instance when unmodifiable
167 		assertSame(m, m.modifiable());
168 
169 		// Test unmodifiable() returns same instance
170 		assertSame(m, m.unmodifiable());
171 	}
172 
173 	@Test void a06_fluentChaining() {
174 		var vr = VarResolver.create().defaultVars().vars(XVar.class).build();
175 		// Test multiple fluent calls can be chained
176 		var m = new ResolvingJsonMap(vr.createSession())
177 			.append("key1", "$X{a}")
178 			.append("key2", "$X{b}")
179 			.appendIf(true, "key3", "$X{c}");
180 
181 		assertEquals("1", m.get("key1"));
182 		assertEquals("2", m.get("key2"));
183 		assertEquals("3", m.get("key3"));
184 	}
185 }