-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathRequestBodyImplTest.java
More file actions
137 lines (115 loc) · 6.38 KB
/
RequestBodyImplTest.java
File metadata and controls
137 lines (115 loc) · 6.38 KB
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
/*
* Copyright (c) 2023, SAP SE
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
*/
package io.vertx.tests.contract.impl;
import static com.google.common.truth.Truth.assertThat;
import static io.vertx.openapi.contract.ContractErrorType.INVALID_SPEC;
import static io.vertx.openapi.contract.ContractErrorType.UNSUPPORTED_FEATURE;
import static io.vertx.openapi.mediatype.impl.DefaultMediaTypeRegistration.APPLICATION_JSON;
import static io.vertx.openapi.mediatype.impl.DefaultMediaTypeRegistration.APPLICATION_JSON_UTF8;
import static io.vertx.tests.ResourceHelper.getRelatedTestResourcePath;
import static org.junit.jupiter.api.Assertions.assertThrows;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.junit5.VertxExtension;
import io.vertx.openapi.contract.ContractErrorType;
import io.vertx.openapi.contract.OpenAPIContractException;
import io.vertx.openapi.contract.RequestBody;
import io.vertx.openapi.contract.impl.RequestBodyImpl;
import io.vertx.openapi.mediatype.MediaTypeRegistry;
import java.nio.file.Path;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@ExtendWith(VertxExtension.class)
class RequestBodyImplTest {
private static final Path RESOURCE_PATH = getRelatedTestResourcePath(RequestBodyImplTest.class);
private static final Path VALID_REQUEST_BODY_JSON = RESOURCE_PATH.resolve("requestBody_valid.json");
private static final Path INVALID_REQUEST_BODY_JSON = RESOURCE_PATH.resolve("requestBody_invalid.json");
private static final String DUMMY_OPERATION_ID = "dummyOperation";
private static JsonObject validTestData;
private static JsonObject invalidTestData;
@BeforeAll
static void setUp(Vertx vertx) {
validTestData = vertx.fileSystem().readFileBlocking(VALID_REQUEST_BODY_JSON.toString()).toJsonObject();
invalidTestData = vertx.fileSystem().readFileBlocking(INVALID_REQUEST_BODY_JSON.toString()).toJsonObject();
}
private static Stream<Arguments> testGetters() {
return Stream.of(
Arguments.of("0000_Test_Getters_Required_True_Content_Schema_String", true),
Arguments.of("0001_Test_Getters_Required_False_Content_Schema_String", false),
Arguments.of("0002_Test_Getters_Without_Required_Content_Schema_String", false));
}
private static Stream<Arguments> testExceptions() {
return Stream.of(
Arguments.of("0000_RequestBody_Without_Content", INVALID_SPEC,
"The passed OpenAPI contract is invalid: Operation dummyOperation defines a request body without or with "
+ "empty property \"content\""),
Arguments.of("0001_RequestBody_With_Empty_Content", INVALID_SPEC,
"The passed OpenAPI contract is invalid: Operation dummyOperation defines a request body without or with "
+ "empty property \"content\""),
Arguments.of("0002_RequestBody_With_Content_Type_Application_Png", UNSUPPORTED_FEATURE,
"The passed OpenAPI contract contains a feature that is not supported: Operation dummyOperation defines a "
+ "request body with an unsupported media type. Supported: application/json, application/json; charset=utf-8,"
+ " application/hal+json, multipart/form-data, application/octet-stream, application/x-www-form-urlencoded, text/plain, text/plain; charset=utf-8, ^[^/]+/vnd\\.[\\w.-]+\\+json$"));
}
@ParameterizedTest(name = "{index} test getters for scenario: {0}")
@MethodSource
void testGetters(String testId, boolean required) {
JsonObject requestBodyModel = validTestData.getJsonObject(testId);
RequestBodyImpl requestBody =
new RequestBodyImpl(requestBodyModel, DUMMY_OPERATION_ID, MediaTypeRegistry.createDefault());
assertThat(requestBody.isRequired()).isEqualTo(required);
assertThat(requestBody.getOpenAPIModel()).isEqualTo(requestBodyModel);
assertThat(requestBody.getContent()).hasSize(1);
assertThat(requestBody.getContent()).containsKey(APPLICATION_JSON);
}
@ParameterizedTest(name = "{index} should throw an exception for scenario: {0}")
@MethodSource
void testExceptions(String testId, ContractErrorType type, String msg) {
JsonObject requestBody = invalidTestData.getJsonObject(testId);
OpenAPIContractException exception =
assertThrows(OpenAPIContractException.class,
() -> new RequestBodyImpl(requestBody, DUMMY_OPERATION_ID, MediaTypeRegistry.createDefault()));
assertThat(exception.type()).isEqualTo(type);
assertThat(exception).hasMessageThat().isEqualTo(msg);
}
private RequestBodyImpl buildWithContent(String... contentTypes) {
JsonObject dummySchema = new JsonObject().put("schema", new JsonObject().put("type", "string"));
JsonObject content = new JsonObject();
for (String type : contentTypes) {
content.put(type, dummySchema);
}
return new RequestBodyImpl(new JsonObject().put("content", content), DUMMY_OPERATION_ID,
MediaTypeRegistry.createDefault());
}
@Test
void testDetermineContentType() {
String appJson = APPLICATION_JSON;
String appJsonUTF8 = APPLICATION_JSON_UTF8;
RequestBody bodyBoth = buildWithContent(appJson, appJsonUTF8);
RequestBody bodyAppJson = buildWithContent(appJson);
RequestBody bodyAppJsonUTF8 = buildWithContent(appJsonUTF8);
assertThat(bodyBoth.determineContentType(null)).isNull();
assertThat(bodyBoth.determineContentType(appJson).getIdentifier()).isEqualTo(appJson);
assertThat(bodyBoth.determineContentType(appJsonUTF8).getIdentifier()).isEqualTo(appJsonUTF8);
assertThat(bodyAppJson.determineContentType(appJsonUTF8).getIdentifier()).isEqualTo(appJson);
assertThat(bodyAppJsonUTF8.determineContentType(appJson)).isNull();
// No Whitespace before semicolon
assertThat(bodyBoth.determineContentType(appJson + ";charset=utf-8").getIdentifier())
.isEqualTo(APPLICATION_JSON_UTF8);
assertThat(bodyBoth.determineContentType("application/text")).isNull();
}
}