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