Search in sources :

Example 51 with CatsResponse

use of com.endava.cats.model.CatsResponse in project cats by Endava.

the class HttpMethodsFuzzerTest method givenAnOperation_whenCallingTheHttpMethodsFuzzer_thenResultsAreCorrectlyReported.

@Test
void givenAnOperation_whenCallingTheHttpMethodsFuzzer_thenResultsAreCorrectlyReported() {
    FuzzingData data = FuzzingData.builder().pathItem(new PathItem()).reqSchema(new StringSchema()).build();
    CatsResponse catsResponse = CatsResponse.builder().body("{}").responseCode(405).httpMethod("POST").build();
    Mockito.when(serviceCaller.call(Mockito.any())).thenReturn(catsResponse);
    httpMethodsFuzzer.fuzz(data);
    Mockito.verify(testCaseListener, Mockito.times(7)).reportInfo(Mockito.any(), Mockito.anyString(), AdditionalMatchers.aryEq(new Object[] { "POST", 405 }));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) CatsResponse(com.endava.cats.model.CatsResponse) FuzzingData(com.endava.cats.model.FuzzingData) StringSchema(io.swagger.v3.oas.models.media.StringSchema) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 52 with CatsResponse

use of com.endava.cats.model.CatsResponse in project cats by Endava.

the class NonRestHttpMethodsFuzzerTest method shouldRunOncePerPath.

@Test
void shouldRunOncePerPath() {
    FuzzingData data = FuzzingData.builder().pathItem(new PathItem()).reqSchema(new StringSchema()).path("/test").build();
    CatsResponse catsResponse = CatsResponse.builder().body("{}").responseCode(405).httpMethod("POST").build();
    Mockito.when(serviceCaller.call(Mockito.any())).thenReturn(catsResponse);
    nonRestHttpMethodsFuzzer.fuzz(data);
    Mockito.verify(testCaseListener, Mockito.times(17)).reportInfo(Mockito.any(), Mockito.anyString(), AdditionalMatchers.aryEq(new Object[] { "POST", 405 }));
    Mockito.clearInvocations(testCaseListener);
    nonRestHttpMethodsFuzzer.fuzz(data);
    Mockito.verifyNoMoreInteractions(testCaseListener);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) CatsResponse(com.endava.cats.model.CatsResponse) FuzzingData(com.endava.cats.model.FuzzingData) StringSchema(io.swagger.v3.oas.models.media.StringSchema) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 53 with CatsResponse

use of com.endava.cats.model.CatsResponse in project cats by Endava.

the class TestCaseListenerTest method givenAnUndocumentedResponseThatIsNotExpected_whenReportingTheResult_thenTheResultIsCorrectlyReported.

@Test
void givenAnUndocumentedResponseThatIsNotExpected_whenReportingTheResult_thenTheResultIsCorrectlyReported() {
    FuzzingData data = Mockito.mock(FuzzingData.class);
    CatsResponse response = Mockito.mock(CatsResponse.class);
    Mockito.when(response.getBody()).thenReturn("{'test':1}");
    Mockito.when(data.getResponseCodes()).thenReturn(Collections.singleton("200"));
    Mockito.when(data.getResponses()).thenReturn(Collections.singletonMap("200", Collections.singletonList("test")));
    Mockito.when(response.responseCodeAsString()).thenReturn("400");
    testCaseListener.createAndExecuteTest(logger, fuzzer, () -> testCaseListener.reportResult(logger, data, response, ResponseCodeFamily.TWOXX));
    Mockito.verify(executionStatisticsListener, Mockito.times(1)).increaseErrors();
    Mockito.verify(executionStatisticsListener, Mockito.never()).increaseSuccess();
    CatsTestCase testCase = testCaseListener.testCaseMap.get("Test 1");
    Assertions.assertThat(testCase.getResultDetails()).startsWith("Unexpected behaviour");
}
Also used : CatsResponse(com.endava.cats.model.CatsResponse) FuzzingData(com.endava.cats.model.FuzzingData) CatsTestCase(com.endava.cats.model.report.CatsTestCase) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 54 with CatsResponse

use of com.endava.cats.model.CatsResponse in project cats by Endava.

the class TestCaseListenerTest method shouldStorePostRequestAndRemoveAfterDelete.

@Test
void shouldStorePostRequestAndRemoveAfterDelete() {
    CatsResponse response = CatsResponse.builder().body("{}").responseCode(200).build();
    FuzzingData data = Mockito.mock(FuzzingData.class);
    Mockito.when(data.getResponseCodes()).thenReturn(Set.of("300", "400"));
    Mockito.when(data.getResponses()).thenReturn(Map.of("300", Collections.emptyList()));
    Mockito.when(data.getMethod()).thenReturn(HttpMethod.POST);
    Mockito.when(data.getPath()).thenReturn("/test");
    MDC.put(TestCaseListener.ID, "Test 1");
    testCaseListener.testCaseMap.put("Test 1", new CatsTestCase());
    testCaseListener.reportResult(logger, data, response, ResponseCodeFamily.TWOXX);
    Assertions.assertThat(catsGlobalContext.getPostSuccessfulResponses()).hasSize(1).containsKey("/test");
    Assertions.assertThat(catsGlobalContext.getPostSuccessfulResponses().get("/test")).isNotEmpty();
    Mockito.when(data.getMethod()).thenReturn(HttpMethod.DELETE);
    Mockito.when(data.getPath()).thenReturn("/test/{testId}");
    testCaseListener.reportResult(logger, data, response, ResponseCodeFamily.TWOXX);
    Assertions.assertThat(catsGlobalContext.getPostSuccessfulResponses()).hasSize(1).containsKey("/test");
    Assertions.assertThat(catsGlobalContext.getPostSuccessfulResponses().get("/test")).isEmpty();
    MDC.remove(TestCaseListener.ID);
    testCaseListener.testCaseMap.clear();
}
Also used : CatsResponse(com.endava.cats.model.CatsResponse) FuzzingData(com.endava.cats.model.FuzzingData) CatsTestCase(com.endava.cats.model.report.CatsTestCase) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 55 with CatsResponse

use of com.endava.cats.model.CatsResponse in project cats by Endava.

the class TestCaseListenerTest method shouldReportInfoWhenResponseCode200IsExpectedAndResponseBodyIsEmptyArrayButResponseIsNotArray.

@Test
void shouldReportInfoWhenResponseCode200IsExpectedAndResponseBodyIsEmptyArrayButResponseIsNotArray() {
    FuzzingData data = Mockito.mock(FuzzingData.class);
    CatsResponse response = Mockito.mock(CatsResponse.class);
    TestCaseListener spyListener = Mockito.spy(testCaseListener);
    Mockito.when(response.getBody()).thenReturn("[]");
    Mockito.when(data.getResponseCodes()).thenReturn(Sets.newHashSet("200", "400"));
    Mockito.when(data.getResponses()).thenReturn(ImmutableMap.of("400", Collections.singletonList("{'test':'4'}"), "200", Collections.singletonList("{'other':'2'}")));
    Mockito.when(response.responseCodeAsString()).thenReturn("400");
    spyListener.createAndExecuteTest(logger, fuzzer, () -> spyListener.reportResult(logger, data, response, ResponseCodeFamily.FOURXX));
    Mockito.verify(executionStatisticsListener, Mockito.times(1)).increaseWarns();
    Mockito.verify(spyListener, Mockito.times(1)).reportWarn(logger, "Response does NOT match expected result. Response code [{}] is documented, but response body does NOT matches the corresponding schema.", response.responseCodeAsString());
}
Also used : CatsResponse(com.endava.cats.model.CatsResponse) FuzzingData(com.endava.cats.model.FuzzingData) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

CatsResponse (com.endava.cats.model.CatsResponse)67 FuzzingData (com.endava.cats.model.FuzzingData)44 QuarkusTest (io.quarkus.test.junit.QuarkusTest)42 Test (org.junit.jupiter.api.Test)42 StringSchema (io.swagger.v3.oas.models.media.StringSchema)23 List (java.util.List)15 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 HashMap (java.util.HashMap)14 CatsHeader (com.endava.cats.model.CatsHeader)7 PathItem (io.swagger.v3.oas.models.PathItem)7 ServiceData (com.endava.cats.io.ServiceData)6 CatsTestCase (com.endava.cats.model.report.CatsTestCase)5 CsvSource (org.junit.jupiter.params.provider.CsvSource)5 ResponseCodeFamily (com.endava.cats.http.ResponseCodeFamily)4 ArrayList (java.util.ArrayList)4 FuzzingStrategy (com.endava.cats.model.FuzzingStrategy)3 TestCaseListener (com.endava.cats.report.TestCaseListener)3 JsonObject (com.google.gson.JsonObject)3 PrettyLogger (io.github.ludovicianul.prettylogger.PrettyLogger)3 PrettyLoggerFactory (io.github.ludovicianul.prettylogger.PrettyLoggerFactory)3