/
SwaggerTest.java
253 lines (194 loc) · 7.29 KB
/
SwaggerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package io.swagger.models;
import io.swagger.models.auth.BasicAuthDefinition;
import io.swagger.models.auth.SecuritySchemeDefinition;
import io.swagger.models.parameters.Parameter;
import org.mockito.Mockito;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
public class SwaggerTest {
private Swagger swagger;
private SecurityRequirement requirement;
@BeforeMethod
public void setup() {
swagger = new Swagger();
requirement = new SecurityRequirement();
}
@Test
public void testAddScheme() {
// when
swagger.addScheme(Scheme.HTTP);
// then
assertTrue(swagger.getSchemes().contains(Scheme.HTTP),
"The newly added scheme must be contained in the schemes list");
}
@Test
public void testScheme() {
// when
swagger.scheme(Scheme.HTTPS);
// then
assertTrue(swagger.getSchemes().contains(Scheme.HTTPS),
"The newly added scheme must be contained in the schemes list");
}
@Test
public void testConsumes() {
// when
swagger.consumes("consumes");
// then
assertTrue(swagger.getConsumes().contains("consumes"),
"The newly added consumes must be contained in the consumes list");
}
@Test
public void testProduces() {
// when
swagger.produces("produces");
// then
assertTrue(swagger.getProduces().contains("produces"),
"The newly added produces must be contained in the produces list");
}
@Test
public void testSecurity() {
// when
swagger.security(requirement);
// then
assertTrue(swagger.getSecurity().contains(requirement),
"The newly added requiement must be contained in the requiements list");
}
@Test
public void testSetSecurityRequirement() {
// when
swagger.setSecurityRequirement(new ArrayList<SecurityRequirement>(Arrays.asList(requirement)));
// then
assertTrue(swagger.getSecurity().contains(requirement),
"The newly added requiement must be contained in the requiement list");
}
@Test
public void testAddSecurityDefinition() {
// when
swagger.addSecurityDefinition(requirement);
// then
assertTrue(swagger.getSecurity().contains(requirement),
"The newly added requiement must be contained in the requiement list");
}
@Test
public void testParameter() {
// given
Parameter parameter = Mockito.mock(Parameter.class);
// when
swagger.setParameters(null);
String key = "key";
// then
assertNull(swagger.getParameter(key), "Cannot retrieve a key without adding it first");
// when
swagger.parameter(key, parameter);
// then
assertEquals(swagger.getParameters().get(key), parameter, "Must be able to retrieve the added key");
assertEquals(swagger.getParameter(key), parameter, "Must be able to retrieave the added key");
}
@Test
public void testResponse() {
// given
Response response = Mockito.mock(Response.class);
// when
swagger.response("44", response);
// then
assertEquals(swagger.getResponses().get("44"), response, "Must be able to retrieve the added response");
}
@Test
public void testVendorExtension() {
// given
String vendorName = "x-vendor";
String value = "value";
// when
swagger.vendorExtension(vendorName, value);
swagger.vendorExtensions(new HashMap<String, Object>());
// then
assertEquals(swagger.getVendorExtensions().get(vendorName), value,
"Must be able to retrieve the same value from the map");
// when
swagger.setVendorExtension(vendorName, value);
// then
assertEquals(swagger.getVendorExtensions().get(vendorName), value,
"Must be able to retrieve the same value from the map");
// when
swagger.vendorExtensions(null);
// then
assertEquals(swagger.getVendorExtensions().get(vendorName), value,
"Must be able to retrieve the same value from the map");
// given
swagger = new Swagger();
// when
swagger.vendorExtensions(new HashMap<String, Object>());
// then
assertNull(swagger.getVendorExtensions().get(vendorName), "Can not retrieve the key from an empty map");
}
@Test
public void testTag() {
// given
Tag tag = new Tag();
tag.setName("name");
// when
swagger.tag(tag);
// then
assertTrue(swagger.getTags().contains(tag), "The newly added tag must be contained in the tags list");
// when
swagger.tag(tag);
// then
assertTrue(swagger.getTags().contains(tag), "The newly added tag must be contained in the tags list");
// when
swagger.tags(Arrays.asList(tag));
// then
assertTrue(swagger.getTags().contains(tag), "The newly added tag must be contained in the tags list");
}
@Test
public void testSetExternalDocs() {
// given
ExternalDocs externalDocs = new ExternalDocs();
// when
swagger.setExternalDocs(externalDocs);
// then
assertEquals(swagger.getExternalDocs(), externalDocs, "The get externalDocs must be the same as the set one");
}
@Test
public void testPath() {
// when
String key = "key";
swagger.setPaths(null);
// then
assertNull(swagger.getPaths(), "If paths is set to null, then it must resolve to null");
assertNull(swagger.getPath(key), "If paths is set to null, then you cannot retrieve any key from it");
// given
Path path = new Path();
// when
swagger.path(key, path);
// then
assertEquals(swagger.getPaths().get(key), path, "Must be able to retrieve the path");
assertEquals(swagger.getPath(key), path, "Must be able to retrieve the path");
}
@Test
public void testSecurityDefinition() {
// given
SecuritySchemeDefinition securityDefinition = new BasicAuthDefinition();
String name = "name";
// when
swagger.securityDefinition(name, securityDefinition);
// then
assertEquals(swagger.getSecurityDefinitions().get(name), securityDefinition,
"Must be able to retrieve the added security definition");
}
@Test
public void testModel() {
// given
Model model = new ComposedModel();
String name = "name";
// when
swagger.model(name, model);
// then
assertEquals(swagger.getDefinitions().get(name), model, "Must be able to retrieve the added model");
}
}