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.openapi3;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.apache.juneau.bean.openapi3.OpenApiBuilder.*;
21  import java.net.*;
22  
23  import org.apache.juneau.*;
24  import org.junit.jupiter.api.*;
25  
26  /**
27   * Testcase for {@link OpenApiBuilder}.
28   */
29  class OpenApiBuilder_Test extends TestBase {
30  
31  	/**
32  	 * Test method for builder methods and marshalling.
33  	 */
34  	@Test void a01_builderMethodsAndMarshalling() {
35  		// Test contact builder
36  		var contact = contact("a1", "a2", "a3");
37  		assertJson("{email:'a3',name:'a1',url:'a2'}", contact);
38  
39  		// Test external documentation builder
40  		var externalDocs = externalDocumentation("a1", "a2");
41  		assertJson("{description:'a2',url:'a1'}", externalDocs);
42  
43  		// Test header info builder
44  		var headerInfo = headerInfo(schemaInfo("a1"));
45  		assertJson("{schema:{type:'a1'}}", headerInfo);
46  
47  		// Test info builder
48  		var info = info("a1", "a2");
49  		assertJson("{title:'a1',version:'a2'}", info);
50  
51  		// Test license builder
52  		var license = license("a1", URI.create("a2"));
53  		assertJson("{name:'a1',url:'a2'}", license);
54  
55  		// Test operation builder
56  		var operation = operation().setSummary("a1");
57  		assertJson("{summary:'a1'}", operation);
58  
59  		// Test parameter builder
60  		var parameter = parameter("a1", "a2");
61  		assertJson("{'in':'a1',name:'a2'}", parameter);
62  
63  		// Test path item builder
64  		var pathItem = pathItem().setGet(operation().setSummary("a1"));
65  		assertJson("{get:{summary:'a1'}}", pathItem);
66  
67  		// Test response builder
68  		var response = response("a1");
69  		assertJson("{description:'a1'}", response);
70  
71  		// Test schema info builder
72  		var schemaInfo = schemaInfo("a1");
73  		assertJson("{type:'a1'}", schemaInfo);
74  
75  		// Test security scheme info builder
76  		var securitySchemeInfo = securitySchemeInfo("a1");
77  		assertJson("{type:'a1'}", securitySchemeInfo);
78  
79  		// Test server builder
80  		var server = server(URI.create("a1"));
81  		assertJson("{url:'a1'}", server);
82  
83  		// Test tag builder
84  		var tag = tag("a1");
85  		assertJson("{name:'a1'}", tag);
86  
87  		// Test openApi builder
88  		var openApi = openApi().setInfo(info("a1", "a2"));
89  		assertJson("{info:{title:'a1',version:'a2'},openapi:'3.0.0'}", openApi);
90  
91  		// Test components builder
92  		var components = components().setSchemas(map("a1", schemaInfo("a2")));
93  		assertJson("{schemas:{a1:{type:'a2'}}}", components);
94  	}
95  
96  	/**
97  	 * Test default values.
98  	 */
99  	@Test void a02_defaultValues() {
100 		assertJson("{}", contact());
101 		assertJson("{}", externalDocumentation());
102 		assertJson("{}", headerInfo());
103 		assertJson("{}", info());
104 		assertJson("{}", license());
105 		assertJson("{}", operation());
106 		assertJson("{}", parameter());
107 		assertJson("{}", pathItem());
108 		assertJson("{}", response());
109 		assertJson("{}", schemaInfo());
110 		assertJson("{}", securityRequirement());
111 		assertJson("{}", securitySchemeInfo());
112 		assertJson("{}", server());
113 		assertJson("{}", tag());
114 		assertJson("{openapi:'3.0.0'}", openApi());
115 		assertJson("{}", components());
116 		assertJson("{}", xml());
117 		assertJson("{}", requestBodyInfo());
118 		assertJson("{}", example());
119 		assertJson("{}", link());
120 		assertJson("{}", callback());
121 		assertJson("{}", discriminator());
122 		assertJson("{}", encoding());
123 		assertJson("{}", mediaType());
124 		assertJson("{}", oAuthFlow());
125 		assertJson("{}", oAuthFlows());
126 		assertJson("{}", serverVariable());
127 		assertJson("{}", items());
128 	}
129 
130 	/**
131 	 * Test single-parameter builders.
132 	 */
133 	@Test void a03_singleParameterBuilders() {
134 		assertJson("{name:'a1'}", contact("a1"));
135 		assertJson("{url:'a1'}", externalDocumentation("a1"));
136 		assertJson("{type:'a1'}", items("a1"));
137 		assertJson("{name:'a1'}", license("a1"));
138 		assertJson("{info:{title:'a1',version:'a2'},openapi:'3.0.0'}", openApi(info("a1", "a2")));
139 		assertJson("{propertyName:'a1'}", discriminator("a1"));
140 		assertJson("{contentType:'a1'}", encoding("a1"));
141 		assertJson("{'default':'a1'}", serverVariable("a1"));
142 	}
143 }