From 3dd2b77c1aba46e8db42d86fb0943aa365076e0d Mon Sep 17 00:00:00 2001 From: Madalin Ilie Date: Thu, 5 Nov 2020 08:09:00 +0300 Subject: [PATCH] Add more unit tests for additional scenarios --- .../com/endava/cats/model/CatsHeaderTest.java | 53 ++++++++++++++++++- .../endava/cats/model/CatsResponseTest.java | 13 +++++ .../cats/model/FuzzingConstraintsTest.java | 6 +++ .../cats/model/FuzzingStrategyTest.java | 16 +++++- .../cats/report/TestCaseListenerTest.java | 3 -- .../com/endava/cats/util/CatsUtilTest.java | 10 ++++ 6 files changed, 95 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/endava/cats/model/CatsHeaderTest.java b/src/test/java/com/endava/cats/model/CatsHeaderTest.java index ec9bd4f02..ec8a10686 100644 --- a/src/test/java/com/endava/cats/model/CatsHeaderTest.java +++ b/src/test/java/com/endava/cats/model/CatsHeaderTest.java @@ -2,6 +2,7 @@ import io.swagger.v3.oas.models.media.NumberSchema; import io.swagger.v3.oas.models.media.ObjectSchema; +import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.StringSchema; import io.swagger.v3.oas.models.parameters.Parameter; import org.apache.commons.lang3.StringUtils; @@ -108,9 +109,57 @@ void shouldMakeCatsHeaderRequired() { @Test void shouldTruncateValue() { - CatsHeader header = CatsHeader.builder().value(StringUtils.repeat("a", 100)).build(); + CatsHeader header = CatsHeader.builder().value(StringUtils.repeat("a", 51)).build(); + Assertions.assertThat(header.getTruncatedValue()).isEqualTo("aaaaaaaaaaaaaaaaaaaa...[Total length:51]"); + } + + @Test + void shouldNotTruncateValue() { + CatsHeader header = CatsHeader.builder().value(StringUtils.repeat("a", 50)).build(); + Assertions.assertThat(header.getTruncatedValue()).isEqualTo(StringUtils.repeat("a", 50)); + } + + @Test + void shouldReturnTruncatedValueAsNull() { + CatsHeader header = CatsHeader.builder().value(null).build(); + Assertions.assertThat(header.getTruncatedValue()).isNull(); + } + + @Test + void shouldGetValueMinLengthAsHalfOfMaxLengthWhenMinLengthZero() { + Parameter parameter = new Parameter(); + StringSchema schema = new StringSchema(); + schema.setMinLength(0); + schema.setMaxLength(10); + parameter.setName("header"); + parameter.setSchema(schema); + + CatsHeader header = CatsHeader.fromHeaderParameter(parameter); + Assertions.assertThat(header.getValue()).hasSizeLessThanOrEqualTo(10).hasSizeGreaterThanOrEqualTo(10 / 2); + } + + @Test + void shouldGenerateValueBasedOnPattern() { + Parameter parameter = new Parameter(); + Schema schema = new Schema(); + schema.setPattern("[A-Z]+"); + parameter.setName("header"); + parameter.setSchema(schema); + CatsHeader header = CatsHeader.fromHeaderParameter(parameter); - Assertions.assertThat(header.getTruncatedValue()).isEqualTo("aaaaaaaaaaaaaaaaaaaa...[Total length:100]"); + Assertions.assertThat(header.getValue()).matches("[A-Z]+").hasSizeGreaterThan(1); } + @Test + void shouldGetNameAndValue() { + Parameter parameter = new Parameter(); + StringSchema schema = new StringSchema(); + schema.setMaxLength(20); + parameter.setName("header"); + parameter.setSchema(schema); + CatsHeader header = CatsHeader.fromHeaderParameter(parameter); + + Assertions.assertThat(header.nameAndValue()).contains("header", "{", "}", "="); + Assertions.assertThat(header.toString()).contains("header", "required", "{", "}", "="); + } } diff --git a/src/test/java/com/endava/cats/model/CatsResponseTest.java b/src/test/java/com/endava/cats/model/CatsResponseTest.java index 262b084de..f6e57833d 100644 --- a/src/test/java/com/endava/cats/model/CatsResponseTest.java +++ b/src/test/java/com/endava/cats/model/CatsResponseTest.java @@ -1,8 +1,12 @@ package com.endava.cats.model; +import com.google.common.collect.Sets; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.Collections; +import java.util.HashSet; + class CatsResponseTest { @Test @@ -12,4 +16,13 @@ void givenAStringJson_whenCreatingACatsResponseObject_thenTheStringIsParsedAsAJs Assertions.assertThat(catsResponse.getJsonBody()).isNotNull(); } + + @Test + void shouldParseFuzzedField() { + String body = "{'test': 'value'}"; + HashSet fuzzedFields = Sets.newHashSet("test#subTest"); + CatsResponse catsResponse = CatsResponse.from(200, body, "DELETE", 2, Collections.singletonList(CatsHeader.builder().build()), fuzzedFields); + + Assertions.assertThat(catsResponse.getFuzzedField()).isEqualTo("subTest"); + } } diff --git a/src/test/java/com/endava/cats/model/FuzzingConstraintsTest.java b/src/test/java/com/endava/cats/model/FuzzingConstraintsTest.java index 57f26c9c6..729cdd446 100644 --- a/src/test/java/com/endava/cats/model/FuzzingConstraintsTest.java +++ b/src/test/java/com/endava/cats/model/FuzzingConstraintsTest.java @@ -30,4 +30,10 @@ void shouldHaveMinLength_whenHasMinLengthIsTrueAndHasRequiredFieldsFuzzedIsTrue( Assertions.assertThat(fuzzingConstraints.hasMinLengthOrMandatoryFieldsFuzzed()).isTrue(); Assertions.assertThat(fuzzingConstraints.getRequiredString()).isEqualTo("TRUE"); } + + @Test + void shouldReturnHasMinLengthFalse() { + fuzzingConstraints = FuzzingConstraints.builder().hasMinlength(false).hasRequiredFieldsFuzzed(false).build(); + Assertions.assertThat(fuzzingConstraints.hasMinLengthOrMandatoryFieldsFuzzed()).isFalse(); + } } diff --git a/src/test/java/com/endava/cats/model/FuzzingStrategyTest.java b/src/test/java/com/endava/cats/model/FuzzingStrategyTest.java index 1abf8a57c..cf570cc2f 100644 --- a/src/test/java/com/endava/cats/model/FuzzingStrategyTest.java +++ b/src/test/java/com/endava/cats/model/FuzzingStrategyTest.java @@ -61,7 +61,7 @@ void givenAFuzzingStrategy_whenSettingAnInnerValueAboveTruncationThreshold_thenT @Test void givenAFuzzingStrategy_whenSettingAnInnerValueBelowTruncationThreshold_thenTheValueIsNotTruncated() { - FuzzingStrategy strategy = FuzzingStrategy.replace().withData(StringUtils.repeat("t", 29)); + FuzzingStrategy strategy = FuzzingStrategy.replace().withData(StringUtils.repeat("t", 30)); Assertions.assertThat(strategy).hasToString(strategy.truncatedValue()); } @@ -72,4 +72,18 @@ void givenAFuzzingStrategy_whenSettingAnInnerValueWithNull_thenToStringMatchesNa Assertions.assertThat(strategy).hasToString(strategy.name()); } + + @Test + void shouldTruncateValue() { + FuzzingStrategy strategy = FuzzingStrategy.replace().withData(StringUtils.repeat("a", 31)); + + Assertions.assertThat(strategy.truncatedValue()).contains("REPLACE", "with", StringUtils.repeat("a", 30), "..."); + } + + @Test + void shouldReturnNameOnlyWhenDataIsNull() { + FuzzingStrategy strategy = FuzzingStrategy.replace().withData(null); + + Assertions.assertThat(strategy.truncatedValue()).isEqualTo(strategy.name()); + } } \ No newline at end of file diff --git a/src/test/java/com/endava/cats/report/TestCaseListenerTest.java b/src/test/java/com/endava/cats/report/TestCaseListenerTest.java index 954316692..8926880aa 100644 --- a/src/test/java/com/endava/cats/report/TestCaseListenerTest.java +++ b/src/test/java/com/endava/cats/report/TestCaseListenerTest.java @@ -220,9 +220,6 @@ void givenADocumentedResponseThatIsNotExpected_whenReportingTheResult_thenTheRes 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).isNotNull(); - Assertions.assertThat(testCase.getResultDetails()).startsWith("Call returned an unexpected result, but with documented code"); } @Test diff --git a/src/test/java/com/endava/cats/util/CatsUtilTest.java b/src/test/java/com/endava/cats/util/CatsUtilTest.java index 3ca1792f9..d304ca873 100644 --- a/src/test/java/com/endava/cats/util/CatsUtilTest.java +++ b/src/test/java/com/endava/cats/util/CatsUtilTest.java @@ -93,4 +93,14 @@ void shouldNotAddTopElement() { String updatedPayload = catsUtil.setAdditionalPropertiesToPayload(currentPathValues, payload); Assertions.assertThat(updatedPayload).doesNotContain("metadata").contains("test1"); } + + @Test + void shouldReturnEmptyFuzzingResultWhenEmptyJson() { + CatsUtil catsUtil = new CatsUtil(new CatsDSLParser()); + FuzzingStrategy strategy = FuzzingStrategy.replace().withData("fuzzed"); + FuzzingResult result = catsUtil.replaceField("", "test", strategy); + + Assertions.assertThat(result.getFuzzedValue()).isEmpty(); + Assertions.assertThat(result.getJson()).isEmpty(); + } }