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