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  
22  import org.apache.juneau.*;
23  import org.junit.jupiter.api.*;
24  
25  /**
26   * Testcase for {@link SwaggerBuilder}.
27   * Ensures Swagger beans are serialized to proper JSON.
28   */
29  class SwaggerBuilder_Test extends TestBase {
30  
31  	@Test void a01_contact() {
32  		var t = contact();
33  		assertJson("{}", t);
34  
35  		t = contact("foo");
36  		assertJson("{name:'foo'}", t);
37  
38  		t = contact("foo", "bar", "baz");
39  		assertJson("{email:'baz',name:'foo',url:'bar'}", t);
40  	}
41  
42  	@Test void a02_externalDocumentation() {
43  		var t = externalDocumentation();
44  		assertJson("{}", t);
45  
46  		t = externalDocumentation("foo");
47  		assertJson("{url:'foo'}", t);
48  
49  		t = externalDocumentation("foo", "bar");
50  		assertJson("{description:'bar',url:'foo'}", t);
51  	}
52  
53  	@Test void a03_headerInfo() {
54  		var t = headerInfo();
55  		assertJson("{}", t);
56  
57  		t = headerInfo("foo");
58  		assertJson("{type:'foo'}", t);
59  
60  		t = headerInfoStrict("string");
61  		assertJson("{type:'string'}", t);
62  		assertThrowsWithMessage(BasicRuntimeException.class, "Invalid value passed in to setType(String).  Value='foo', valid values=['string','number','integer','boolean','array']", ()->headerInfoStrict("foo"));
63  	}
64  
65  	@Test void a04_info() {
66  		var t = info();
67  		assertJson("{}", t);
68  
69  		t = info("foo", "bar");
70  		assertJson("{title:'foo',version:'bar'}", t);
71  	}
72  
73  	@Test void a05_items() {
74  		var t = items();
75  		assertJson("{}", t);
76  
77  		t = items("foo");
78  		assertJson("{type:'foo'}", t);
79  
80  		t = itemsStrict("string");
81  		assertJson("{type:'string'}", t);
82  		assertThrowsWithMessage(BasicRuntimeException.class, "Invalid value passed in to setType(String).  Value='foo', valid values=['string','number','integer','boolean','array']", ()->itemsStrict("foo"));
83  	}
84  
85  	@Test void a06_license() {
86  		var t = license();
87  		assertJson("{}", t);
88  
89  		t = license("foo");
90  		assertJson("{name:'foo'}", t);
91  	}
92  
93  	@Test void a07_operation() {
94  		var t = operation();
95  		assertJson("{}", t);
96  	}
97  
98  	@Test void a08_parameterInfo() {
99  		var t = parameterInfo();
100 		assertJson("{}", t);
101 
102 		t = parameterInfo("foo", "bar");
103 		assertJson("{'in':'foo',name:'bar'}", t);
104 
105 		t = parameterInfoStrict("query", "bar");
106 		assertJson("{'in':'query',name:'bar'}", t);
107 		assertThrowsWithMessage(BasicRuntimeException.class, "Invalid value passed in to setIn(String).  Value='foo', valid values=['query','header','path','formData','body']", ()->parameterInfoStrict("foo", "bar"));
108 	}
109 
110 	@Test void a09_responseInfo() {
111 		var t = responseInfo();
112 		assertJson("{}", t);
113 
114 		t = responseInfo("foo");
115 		assertJson("{description:'foo'}", t);
116 	}
117 
118 	@Test void a10_schemaInfo() {
119 		var t = schemaInfo();
120 		assertJson("{}", t);
121 	}
122 
123 	@Test void a11_securityScheme() {
124 		var t = securityScheme();
125 		assertJson("{}", t);
126 
127 		t = securityScheme("foo");
128 		assertJson("{type:'foo'}", t);
129 
130 		t = securityScheme("foo");
131 		assertJson("{type:'foo'}", t);
132 	}
133 
134 	@Test void a12_swagger() {
135 		var t = swagger();
136 		assertJson("{swagger:'2.0'}", t);
137 
138 		t = swagger(info());
139 		assertJson("{info:{},swagger:'2.0'}", t);
140 	}
141 
142 	@Test void a13_tag() {
143 		var t = tag();
144 		assertJson("{}", t);
145 
146 		t = tag("foo");
147 		assertJson("{name:'foo'}", t);
148 	}
149 
150 	@Test void a14_xml() {
151 		var t = xml();
152 		assertJson("{}", t);
153 	}
154 
155 	@Test void a15_license() {
156 		var t = license();
157 		assertJson("{}", t);
158 
159 		t = license("MIT");
160 		assertJson("{name:'MIT'}", t);
161 
162 		t = license("MIT", java.net.URI.create("https://opensource.org/licenses/MIT"));
163 		assertJson("{name:'MIT',url:'https://opensource.org/licenses/MIT'}", t);
164 	}
165 
166 	@Test void a16_operation() {
167 		var t = operation();
168 		assertJson("{}", t);
169 	}
170 
171 	@Test void a17_operationMap() {
172 		var t = operationMap();
173 		assertJson("{}", t);
174 	}
175 
176 	@Test void a18_itemsStrict() {
177 		var t = itemsStrict("string");
178 		assertJson("{type:'string'}", t);
179 		assertThrowsWithMessage(BasicRuntimeException.class, "Invalid value passed in to setType(String).  Value='foo', valid values=['string','number','integer','boolean','array']", ()->itemsStrict("foo"));
180 	}
181 
182 	@Test void a19_securitySchemeStrict() {
183 		var t = securitySchemeStrict("basic");
184 		assertJson("{type:'basic'}", t);
185 		assertThrowsWithMessage(BasicRuntimeException.class, "Invalid value passed in to setType(String).  Value='foo', valid values=['basic','apiKey','oauth2']", ()->securitySchemeStrict("foo"));
186 	}
187 }