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.bean.swagger;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.apache.juneau.bean.swagger.SwaggerBuilder.*;
21  import static org.apache.juneau.junit.bct.BctAssertions.*;
22  import static org.junit.jupiter.api.Assertions.*;
23  
24  import java.util.*;
25  
26  import org.apache.juneau.*;
27  import org.junit.jupiter.api.*;
28  
29  /**
30   * Testcase for {@link OperationMap}.
31   */
32  class OperationMap_Test extends TestBase {
33  
34  	/**
35  	 * Test method for operation ordering.
36  	 */
37  	@Test void a01_operationOrdering() {
38  		var map = operationMap();
39  
40  		// Add operations in random order
41  		map
42  			.append("custom", operation())
43  			.append("post", operation())
44  			.append("delete", operation())
45  			.append("get", operation())
46  			.append("options", operation())
47  			.append("head", operation())
48  			.append("patch", operation())
49  			.append("put", operation());
50  
51  		// Verify ordering.
52  		assertList(map.keySet(), "get", "put", "post", "delete", "options", "head", "patch", "custom");
53  	}
54  
55  	/**
56  	 * Test method for case-insensitive ordering.
57  	 */
58  	@Test void a02_caseInsensitiveOrdering() {
59  		var map = operationMap();
60  
61  		// Add operations with different cases
62  		map.append("DELETE", operation()).append("Get", operation()).append("POST", operation()).append("put", operation());
63  
64  		// Verify ordering is case-insensitive - all keys should be normalized to lowercase
65  		assertList(map.keySet(), "get", "put", "post", "delete");
66  	}
67  
68  	/**
69  	 * Test method for custom methods.
70  	 */
71  	@Test void a04_customMethods() {
72  		var map = operationMap();
73  
74  		// Add standard methods
75  		map.put("get", operation().setSummary("a"));
76  		map.put("post", operation().setSummary("b"));
77  
78  		// Add custom methods
79  		map.put("custom1", operation().setSummary("c"));
80  		map.put("custom2", operation().setSummary("d"));
81  
82  		// Verify custom methods come after standard methods
83  		assertBeans(map, "key,value{summary}", "get,{a}", "post,{b}", "custom1,{c}", "custom2,{d}");
84  	}
85  
86  	/**
87  	 * Test method for empty map.
88  	 */
89  	@Test void a05_emptyMap() {
90  		var map = operationMap();
91  		assertEmpty(map);
92  		assertEmpty(map);
93  	}
94  
95  	/**
96  	 * Test method for duplicate keys.
97  	 */
98  	@Test void a06_duplicateKeys() {
99  		var map = operationMap();
100 
101 		map.put("get", operation().setSummary("a"));
102 		map.put("get", operation().setSummary("b"));
103 
104 		// Verify the second operation overwrites the first
105 		assertBeans(map, "key,value{summary}", "get,{b}");
106 	}
107 
108 	/**
109 	 * Test method for null values.
110 	 */
111 	@Test void a07_nullValues() {
112 		var map = operationMap();
113 
114 		// Should not throw exception when adding null value
115 		assertDoesNotThrow(() -> map.put("get", null));
116 		assertTrue(map.containsKey("get"));
117 		assertNull(map.get("get"));
118 	}
119 
120 	/**
121 	 * Test method for serialization.
122 	 */
123 	@Test void a08_serialization() {
124 		var map = operationMap();
125 		map.put("get", operation().setSummary("a"));
126 		map.put("post", operation().setSummary("b"));
127 
128 		// Test that it can be serialized to JSON
129 		var json = json(map);
130 		assertNotNull(json);
131 		assertEquals("{get:{summary:'a'},post:{summary:'b'}}", json);
132 	}
133 
134 	/**
135 	 * Test method for deserialization.
136 	 */
137 	@Test void a09_deserialization() {
138 		var json = "{get:{summary:'a'},post:{summary:'b'}}";
139 		var map = json(json, OperationMap.class);
140 		assertBeans(map, "key,value{summary}", "get,{a}", "post,{b}");
141 	}
142 
143 	/**
144 	 * Test method for iteration order.
145 	 */
146 	@Test void a10_iterationOrder() {
147 		var map = operationMap();
148 		map.put("delete", operation());
149 		map.put("get", operation());
150 		map.put("post", operation());
151 		map.put("put", operation());
152 
153 		assertList(map.keySet(), "get", "put", "post", "delete");
154 	}
155 
156 	/**
157 	 * Test method for key normalization in put method.
158 	 */
159 	@Test void a11_keyNormalization() {
160 		var map = operationMap();
161 
162 		// Test that put() normalizes keys to lowercase
163 		map.put("GET", operation().setSummary("a"));
164 		map.put("Post", operation().setSummary("b"));
165 		map.put("DELETE", operation().setSummary("c"));
166 
167 		// Verify that keys are stored in lowercase
168 		assertTrue(map.containsKey("get"));
169 		assertTrue(map.containsKey("post"));
170 		assertTrue(map.containsKey("delete"));
171 
172 		// Verify that original case keys are not present (TreeMap with custom comparator may be case-insensitive)
173 		// Since our comparator normalizes keys, containsKey() may also be case-insensitive
174 		// Let's verify the actual keys in the map instead
175 		var keys = new ArrayList<>(map.keySet());
176 		assertTrue(keys.contains("get"));
177 		assertTrue(keys.contains("post"));
178 		assertTrue(keys.contains("delete"));
179 		assertFalse(keys.contains("GET"));
180 		assertFalse(keys.contains("Post"));
181 		assertFalse(keys.contains("DELETE"));
182 
183 		// Verify that we can retrieve using lowercase keys
184 		assertBeans(map, "value{summary}", "{a}", "{b}", "{c}");
185 	}
186 }