|
| 1 | +package com.getyourguide.openapi.validation.core; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.Mockito.doAnswer; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import com.atlassian.oai.validator.report.ValidationReport; |
| 11 | +import com.getyourguide.openapi.validation.api.exclusions.ViolationExclusions; |
| 12 | +import com.getyourguide.openapi.validation.api.log.ViolationLogger; |
| 13 | +import com.getyourguide.openapi.validation.api.metrics.MetricsReporter; |
| 14 | +import com.getyourguide.openapi.validation.api.model.Direction; |
| 15 | +import com.getyourguide.openapi.validation.api.model.OpenApiViolation; |
| 16 | +import com.getyourguide.openapi.validation.api.model.RequestMetaData; |
| 17 | +import com.getyourguide.openapi.validation.core.throttle.ValidationReportThrottler; |
| 18 | +import io.swagger.v3.oas.models.parameters.Parameter; |
| 19 | +import java.net.URI; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Optional; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.mockito.ArgumentCaptor; |
| 26 | + |
| 27 | +class ValidationReportHandlerTest { |
| 28 | + private ValidationReportThrottler throttleHelper; |
| 29 | + private ViolationLogger logger; |
| 30 | + |
| 31 | + private ValidationReportHandler validationReportHandler; |
| 32 | + |
| 33 | + @BeforeEach |
| 34 | + public void setUp() { |
| 35 | + throttleHelper = mock(); |
| 36 | + logger = mock(); |
| 37 | + MetricsReporter metrics = mock(); |
| 38 | + ViolationExclusions violationExclusions = mock(); |
| 39 | + |
| 40 | + validationReportHandler = new ValidationReportHandler(throttleHelper, logger, metrics, violationExclusions); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testWhenParameterNameIsPresentThenItShouldAddItToTheMessage() { |
| 45 | + mockNoThrottling(); |
| 46 | + var request = mockRequestMetaData(); |
| 47 | + var validationReport = mockValidationReport("parameterName"); |
| 48 | + |
| 49 | + validationReportHandler.handleValidationReport(request, Direction.REQUEST, null, validationReport); |
| 50 | + |
| 51 | + var argumentCaptor = ArgumentCaptor.forClass(OpenApiViolation.class); |
| 52 | + verify(logger).log(argumentCaptor.capture()); |
| 53 | + var openApiViolation = argumentCaptor.getValue(); |
| 54 | + assertEquals(Optional.of("parameterName"), openApiViolation.getParameter()); |
| 55 | + assertEquals( |
| 56 | + String.join("\n", |
| 57 | + "OpenAPI spec validation error [key]", |
| 58 | + "GET https://api.example.com/index", |
| 59 | + "User Agent: null", |
| 60 | + "Parameter: parameterName", |
| 61 | + "", |
| 62 | + "Violation message (toString)"), |
| 63 | + openApiViolation.getLogMessage()); |
| 64 | + } |
| 65 | + |
| 66 | + private static RequestMetaData mockRequestMetaData() { |
| 67 | + var request = new RequestMetaData("GET", URI.create("https://api.example.com/index"), new HashMap<>()); |
| 68 | + return request; |
| 69 | + } |
| 70 | + |
| 71 | + private static ValidationReport mockValidationReport(String parameterName) { |
| 72 | + var validationReport = mock(ValidationReport.class); |
| 73 | + var message = mock(ValidationReport.Message.class); |
| 74 | + when(message.getKey()).thenReturn("key"); |
| 75 | + when(message.getMessage()).thenReturn("Violation message"); |
| 76 | + when(message.toString()).thenReturn("Violation message (toString)"); |
| 77 | + var context = mock(ValidationReport.MessageContext.class); |
| 78 | + var parameter = mock(Parameter.class); |
| 79 | + when(parameter.getName()).thenReturn(parameterName); |
| 80 | + when(context.getParameter()).thenReturn(Optional.of(parameter)); |
| 81 | + when(message.getContext()).thenReturn(Optional.of(context)); |
| 82 | + when(validationReport.getMessages()).thenReturn(List.of(message)); |
| 83 | + return validationReport; |
| 84 | + } |
| 85 | + |
| 86 | + private void mockNoThrottling() { |
| 87 | + doAnswer(invocation -> { |
| 88 | + ((Runnable) invocation.getArguments()[1]).run(); |
| 89 | + return null; |
| 90 | + }).when(throttleHelper).throttle(any(), any()); |
| 91 | + } |
| 92 | +} |
0 commit comments