Skip to content

Commit b511080

Browse files
pboosclaude
andcommitted
refactor: extract helper methods in OpenApiRequestValidatorTest for better readability
- Add createRequest() and createResponse() helper methods to reduce test setup duplication - Add mockRequestValidation() and mockResponseValidation() to simplify mock configuration - Add assertSingleViolationReturned() and assertNoViolationsReturned() assertion helpers - Simplify test methods by reducing boilerplate and improving focus on test intent 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 30bb227 commit b511080

File tree

1 file changed

+51
-61
lines changed

1 file changed

+51
-61
lines changed

openapi-validation-core/src/test/java/com/getyourguide/openapi/validation/core/OpenApiRequestValidatorTest.java

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -102,62 +102,42 @@ public void testWhenViolationIsExcludedThenItShouldNotBeReturned() {
102102
@Test
103103
@DisplayName("When violation has log level IGNORE then it should not be returned")
104104
public void testWhenRequestViolationHasLogLevelIgnoreThenItShouldNotBeReturned() {
105-
var uri = URI.create("https://api.example.com/path");
106-
var request = new RequestMetaData("GET", uri, new HashMap<>());
107-
var validationReport = mock(ValidationReport.class);
108-
when(validator.validateRequest(any())).thenReturn(validationReport);
109-
105+
var request = createRequest("/path");
110106
var violationIgnored = createViolation(LogLevel.IGNORE);
111107
var violationError = createViolation(LogLevel.ERROR);
112-
113-
var violations = List.of(violationIgnored, violationError);
114-
when(mapper.map(any(), any(), any(), any(), any())).thenReturn(violations);
108+
mockRequestValidation(List.of(violationIgnored, violationError));
115109

116110
var result = openApiRequestValidator.validateRequestObject(request, null);
117111

118-
assertEquals(1, result.size());
119-
assertEquals(violationError, result.getFirst());
112+
assertSingleViolationReturned(result, violationError);
120113
}
121114

122115
@Test
123116
@DisplayName("When violation has log level IGNORE and another is excluded then both should not be returned")
124117
public void testWhenRequestViolationHasLogLevelIgnoreAndIsExcludedThenItShouldNotBeReturned() {
125-
var uri = URI.create("https://api.example.com/path");
126-
var request = new RequestMetaData("GET", uri, new HashMap<>());
127-
var validationReport = mock(ValidationReport.class);
128-
when(validator.validateRequest(any())).thenReturn(validationReport);
129-
118+
var request = createRequest("/path");
130119
var violationIgnored = createViolation(LogLevel.IGNORE);
131120
var violationExcluded = createViolation(LogLevel.WARN);
132121
when(internalViolationExclusions.isExcluded(violationExcluded)).thenReturn(true);
133122
var violationError = createViolation(LogLevel.ERROR);
134-
135-
var violations = List.of(violationIgnored, violationExcluded, violationError);
136-
when(mapper.map(any(), any(), any(), any(), any())).thenReturn(violations);
123+
mockRequestValidation(List.of(violationIgnored, violationExcluded, violationError));
137124

138125
var result = openApiRequestValidator.validateRequestObject(request, null);
139126

140-
assertEquals(1, result.size());
141-
assertEquals(violationError, result.getFirst());
127+
assertSingleViolationReturned(result, violationError);
142128
}
143129

144130
@Test
145131
@DisplayName("When all violations are ignored then empty list is returned")
146132
public void testWhenAllRequestViolationsAreIgnoredThenEmptyListIsReturned() {
147-
var uri = URI.create("https://api.example.com/path");
148-
var request = new RequestMetaData("GET", uri, new HashMap<>());
149-
var validationReport = mock(ValidationReport.class);
150-
when(validator.validateRequest(any())).thenReturn(validationReport);
151-
133+
var request = createRequest("/path");
152134
var violation1 = createViolation(LogLevel.IGNORE);
153135
var violation2 = createViolation(LogLevel.IGNORE);
154-
155-
var violations = List.of(violation1, violation2);
156-
when(mapper.map(any(), any(), any(), any(), any())).thenReturn(violations);
136+
mockRequestValidation(List.of(violation1, violation2));
157137

158138
var result = openApiRequestValidator.validateRequestObject(request, null);
159139

160-
assertEquals(0, result.size());
140+
assertNoViolationsReturned(result);
161141
}
162142
}
163143

@@ -168,65 +148,45 @@ class ValidateResponseObjectTests {
168148
@Test
169149
@DisplayName("When violation has log level IGNORE then it should not be returned")
170150
public void testWhenResponseViolationHasLogLevelIgnoreThenItShouldNotBeReturned() {
171-
var uri = URI.create("https://api.example.com/path");
172-
var request = new RequestMetaData("GET", uri, new HashMap<>());
173-
var response = new ResponseMetaData(200, "application/json", new HashMap<>());
174-
var validationReport = mock(ValidationReport.class);
175-
when(validator.validateResponse(any(), any(), any())).thenReturn(validationReport);
176-
151+
var request = createRequest("/path");
152+
var response = createResponse();
177153
var violationIgnored = createViolation(LogLevel.IGNORE);
178154
var violationWarn = createViolation(LogLevel.WARN);
179-
180-
var violations = List.of(violationIgnored, violationWarn);
181-
when(mapper.map(any(), any(), any(), any(), any())).thenReturn(violations);
155+
mockResponseValidation(List.of(violationIgnored, violationWarn));
182156

183157
var result = openApiRequestValidator.validateResponseObject(request, response, null);
184158

185-
assertEquals(1, result.size());
186-
assertEquals(violationWarn, result.getFirst());
159+
assertSingleViolationReturned(result, violationWarn);
187160
}
188161

189162
@Test
190163
@DisplayName("When violation has log level IGNORE and another is excluded then both should not be returned")
191164
public void testWhenResponseViolationHasLogLevelIgnoreAndIsExcludedThenItShouldNotBeReturned() {
192-
var uri = URI.create("https://api.example.com/path");
193-
var request = new RequestMetaData("GET", uri, new HashMap<>());
194-
var response = new ResponseMetaData(200, "application/json", new HashMap<>());
195-
var validationReport = mock(ValidationReport.class);
196-
when(validator.validateResponse(any(), any(), any())).thenReturn(validationReport);
197-
165+
var request = createRequest("/path");
166+
var response = createResponse();
198167
var violationIgnored = createViolation(LogLevel.IGNORE);
199168
var violationExcluded = createViolation(LogLevel.INFO);
200169
when(internalViolationExclusions.isExcluded(violationExcluded)).thenReturn(true);
201170
var violationError = createViolation(LogLevel.ERROR);
202-
203-
var violations = List.of(violationIgnored, violationExcluded, violationError);
204-
when(mapper.map(any(), any(), any(), any(), any())).thenReturn(violations);
171+
mockResponseValidation(List.of(violationIgnored, violationExcluded, violationError));
205172

206173
var result = openApiRequestValidator.validateResponseObject(request, response, null);
207174

208-
assertEquals(1, result.size());
209-
assertEquals(violationError, result.getFirst());
175+
assertSingleViolationReturned(result, violationError);
210176
}
211177

212178
@Test
213179
@DisplayName("When all violations are ignored then empty list is returned")
214180
public void testWhenAllResponseViolationsAreIgnoredThenEmptyListIsReturned() {
215-
var uri = URI.create("https://api.example.com/path");
216-
var request = new RequestMetaData("GET", uri, new HashMap<>());
217-
var response = new ResponseMetaData(200, "application/json", new HashMap<>());
218-
var validationReport = mock(ValidationReport.class);
219-
when(validator.validateResponse(any(), any(), any())).thenReturn(validationReport);
220-
181+
var request = createRequest("/path");
182+
var response = createResponse();
221183
var violation1 = createViolation(LogLevel.IGNORE);
222184
var violation2 = createViolation(LogLevel.IGNORE);
223-
224-
var violations = List.of(violation1, violation2);
225-
when(mapper.map(any(), any(), any(), any(), any())).thenReturn(violations);
185+
mockResponseValidation(List.of(violation1, violation2));
226186

227187
var result = openApiRequestValidator.validateResponseObject(request, response, null);
228188

229-
assertEquals(0, result.size());
189+
assertNoViolationsReturned(result);
230190
}
231191
}
232192

@@ -244,4 +204,34 @@ private OpenApiViolation createViolation(LogLevel level) {
244204
when(violation.getLevel()).thenReturn(level);
245205
return violation;
246206
}
207+
208+
private RequestMetaData createRequest(String path) {
209+
var uri = URI.create("https://api.example.com" + path);
210+
return new RequestMetaData("GET", uri, new HashMap<>());
211+
}
212+
213+
private ResponseMetaData createResponse() {
214+
return new ResponseMetaData(200, "application/json", new HashMap<>());
215+
}
216+
217+
private void mockRequestValidation(List<OpenApiViolation> violations) {
218+
var validationReport = mock(ValidationReport.class);
219+
when(validator.validateRequest(any())).thenReturn(validationReport);
220+
when(mapper.map(any(), any(), any(), any(), any())).thenReturn(violations);
221+
}
222+
223+
private void mockResponseValidation(List<OpenApiViolation> violations) {
224+
var validationReport = mock(ValidationReport.class);
225+
when(validator.validateResponse(any(), any(), any())).thenReturn(validationReport);
226+
when(mapper.map(any(), any(), any(), any(), any())).thenReturn(violations);
227+
}
228+
229+
private void assertSingleViolationReturned(List<OpenApiViolation> result, OpenApiViolation expected) {
230+
assertEquals(1, result.size());
231+
assertEquals(expected, result.getFirst());
232+
}
233+
234+
private void assertNoViolationsReturned(List<OpenApiViolation> result) {
235+
assertEquals(0, result.size());
236+
}
247237
}

0 commit comments

Comments
 (0)