From 3889bf70095e5d5b9e645d25216186beda666223 Mon Sep 17 00:00:00 2001 From: Madalin Ilie Date: Sat, 7 Sep 2024 23:41:17 +0300 Subject: [PATCH] chore: Add unit test for additional branches --- .../cats/context/CatsGlobalContextTest.java | 66 +++++++++++++++++++ .../cats/factory/FuzzingDataFactoryTest.java | 2 +- .../fuzzer/fields/NewFieldsFuzzerTest.java | 3 +- .../com/endava/cats/util/JsonSetTest.java | 42 ++++++++++++ 4 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/endava/cats/context/CatsGlobalContextTest.java create mode 100644 src/test/java/com/endava/cats/util/JsonSetTest.java diff --git a/src/test/java/com/endava/cats/context/CatsGlobalContextTest.java b/src/test/java/com/endava/cats/context/CatsGlobalContextTest.java new file mode 100644 index 000000000..952ecb576 --- /dev/null +++ b/src/test/java/com/endava/cats/context/CatsGlobalContextTest.java @@ -0,0 +1,66 @@ +package com.endava.cats.context; + +import io.quarkus.test.junit.QuarkusTest; +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.headers.Header; +import io.swagger.v3.oas.models.parameters.Parameter; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.List; +import java.util.Map; +import java.util.Properties; + +@QuarkusTest +class CatsGlobalContextTest { + + @Test + void shouldAddExample() { + CatsGlobalContext context = new CatsGlobalContext(); + context.addGeneratedExample("/test", List.of("example")); + + Assertions.assertThat(context.getGeneratedExamplesCache().get("/test")).containsExactly("example"); + } + + @Test + void shouldResolveParametersReferences() { + CatsGlobalContext context = new CatsGlobalContext(); + OpenAPI openAPI = Mockito.mock(OpenAPI.class); + Components components = Mockito.mock(Components.class); + Mockito.when(openAPI.getComponents()).thenReturn(components); + Map parameterMap = Map.of("Example", new Parameter()); + Mockito.when(components.getParameters()).thenReturn(parameterMap); + context.init(openAPI, List.of(), new Properties(), new CatsConfiguration("1", "2", "3", List.of(), 4, 5, 6)); + + String reference = "#/components/parameters/Example"; + Object resolvedObject = context.getObjectFromPathsReference(reference); + + Assertions.assertThat(resolvedObject).isNotNull(); + + String otherReference = "#/components/parameters/OtherExample"; + Object otherResolvedObject = context.getObjectFromPathsReference(otherReference); + Assertions.assertThat(otherResolvedObject).isNull(); + } + + @Test + void shouldResolveHeadersReferences() { + CatsGlobalContext context = new CatsGlobalContext(); + OpenAPI openAPI = Mockito.mock(OpenAPI.class); + Components components = Mockito.mock(Components.class); + Mockito.when(openAPI.getComponents()).thenReturn(components); + Map headerMap = Map.of("Example", new Header()); + Mockito.when(components.getHeaders()).thenReturn(headerMap); + context.init(openAPI, List.of(), new Properties(), new CatsConfiguration("1", "2", "3", List.of(), 4, 5, 6)); + + String reference = "#/components/headers/Example"; + Object resolvedObject = context.getObjectFromPathsReference(reference); + + Assertions.assertThat(resolvedObject).isNotNull(); + + String otherReference = "#/components/headers/OtherExample"; + Object otherResolvedObject = context.getObjectFromPathsReference(otherReference); + Assertions.assertThat(otherResolvedObject).isNull(); + } +} diff --git a/src/test/java/com/endava/cats/factory/FuzzingDataFactoryTest.java b/src/test/java/com/endava/cats/factory/FuzzingDataFactoryTest.java index 54ed46c66..aab11f466 100644 --- a/src/test/java/com/endava/cats/factory/FuzzingDataFactoryTest.java +++ b/src/test/java/com/endava/cats/factory/FuzzingDataFactoryTest.java @@ -320,7 +320,7 @@ void shouldLimitXxxCombinationsWhenCombinationsExceedArgument() throws Exception Assertions.assertThat(dataList).hasSize(100); Assertions.assertThat(dataList.stream().map(FuzzingData::getPayload).toList()) .filteredOn(payload -> payload.contains("ANY_OF") || payload.contains("ONE_OF") || payload.contains("ALL_OF")) - .hasSize(0); + .isEmpty(); FuzzingData firstData = dataList.getFirst(); boolean isActionsArray = JsonUtils.isArray(firstData.getPayload(), "$.subject-profile.claims"); diff --git a/src/test/java/com/endava/cats/fuzzer/fields/NewFieldsFuzzerTest.java b/src/test/java/com/endava/cats/fuzzer/fields/NewFieldsFuzzerTest.java index 29763cae0..fc8c1cbdc 100644 --- a/src/test/java/com/endava/cats/fuzzer/fields/NewFieldsFuzzerTest.java +++ b/src/test/java/com/endava/cats/fuzzer/fields/NewFieldsFuzzerTest.java @@ -77,8 +77,7 @@ void shouldAddANewFieldToFuzzToSingleElement() { setup(HttpMethod.POST); String element = newFieldsFuzzer.addNewField(data); - Assertions.assertThat(element).contains(NEW_FIELD); - Assertions.assertThat(element).doesNotContain(NEW_FIELD + "random"); + Assertions.assertThat(element).contains(NEW_FIELD).doesNotContain(NEW_FIELD + "random"); } @Test diff --git a/src/test/java/com/endava/cats/util/JsonSetTest.java b/src/test/java/com/endava/cats/util/JsonSetTest.java new file mode 100644 index 000000000..8d72dfa9f --- /dev/null +++ b/src/test/java/com/endava/cats/util/JsonSetTest.java @@ -0,0 +1,42 @@ +package com.endava.cats.util; + +import io.quarkus.test.junit.QuarkusTest; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; + +@QuarkusTest +class JsonSetTest { + + @Test + void shouldReturnSize() { + JsonSet jsonSet = new JsonSet(); + Assertions.assertThat(jsonSet.size()).isZero(); + + jsonSet.add("test"); + Assertions.assertThat(jsonSet.size()).isOne(); + } + + @ParameterizedTest + @MethodSource("jsonSetTestDataProvider") + void testJsonSet(String[] jsonStrings, int expectedSize) { + JsonSet jsonSet = new JsonSet(); + for (String jsonString : jsonStrings) { + jsonSet.add(jsonString); + } + Assertions.assertThat(jsonSet.size()).isEqualTo(expectedSize); + } + + private static Stream jsonSetTestDataProvider() { + return Stream.of( + new Object[]{new String[]{}, 0}, + new Object[]{new String[]{"test"}, 1}, + new Object[]{new String[]{"{\"field\":\"value\"}", "{\"field\":\"value\"}"}, 1}, + new Object[]{new String[]{"{\"field\":\"value\"}", "{\"field\":\"otherValue\"}"}, 1}, + new Object[]{new String[]{"{\"field\":\"value\"}", "{\"field1\":\"otherValue\"}"}, 2} + ); + } +}