-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add new linter to check to check relevant response codes have r…
…esponse bodies
- Loading branch information
Showing
6 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/com/endava/cats/fuzzer/contract/ResponsesWithBodiesLinterFuzzer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.endava.cats.fuzzer.contract; | ||
|
||
import com.endava.cats.annotations.LinterFuzzer; | ||
import com.endava.cats.http.HttpMethod; | ||
import com.endava.cats.model.FuzzingData; | ||
import com.endava.cats.report.TestCaseListener; | ||
import io.github.ludovicianul.prettylogger.PrettyLogger; | ||
import io.github.ludovicianul.prettylogger.PrettyLoggerFactory; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Checks if the http response codes have a response body. | ||
*/ | ||
@Singleton | ||
@LinterFuzzer | ||
public class ResponsesWithBodiesLinterFuzzer extends BaseLinterFuzzer { | ||
private final PrettyLogger log = PrettyLoggerFactory.getLogger(this.getClass()); | ||
|
||
/** | ||
* Creates a new ResponsesWithBodiesLinterFuzzer instance. | ||
* | ||
* @param tcl the test case listener | ||
*/ | ||
public ResponsesWithBodiesLinterFuzzer(TestCaseListener tcl) { | ||
super(tcl); | ||
} | ||
|
||
@Override | ||
public void process(FuzzingData data) { | ||
testCaseListener.addScenario(log, "Check if all http response codes for HTTP method {} have a response body", data.getMethod()); | ||
testCaseListener.addExpectedResult(log, "All HTTP response codes (except for 204 and 304) have a response body"); | ||
|
||
List<String> httpResponseCodesMissingBody = data.getResponses() | ||
.entrySet() | ||
.stream() | ||
.filter(entry -> entry.getValue().isEmpty()) | ||
.map(Map.Entry::getKey) | ||
.toList(); | ||
|
||
if (httpResponseCodesMissingBody.isEmpty()) { | ||
testCaseListener.reportResultInfo(log, data, "All HTTP response codes have a response body"); | ||
} else { | ||
testCaseListener.reportResultError(log, data, "Missing response body for some HTTP response codes", "The following HTTP response codes are missing a response body: {}", httpResponseCodesMissingBody); | ||
} | ||
} | ||
|
||
@Override | ||
public List<HttpMethod> skipForHttpMethods() { | ||
return List.of(HttpMethod.HEAD); | ||
} | ||
|
||
@Override | ||
protected String runKey(FuzzingData data) { | ||
return data.getPath() + data.getMethod(); | ||
} | ||
|
||
@Override | ||
public String description() { | ||
return "verifies that HTTP response codes (except for 204 and 304) have a response body"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/test/java/com/endava/cats/fuzzer/contract/ResponsesWithBodiesLinterFuzzerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.endava.cats.fuzzer.contract; | ||
|
||
import com.endava.cats.args.IgnoreArguments; | ||
import com.endava.cats.args.ReportingArguments; | ||
import com.endava.cats.context.CatsGlobalContext; | ||
import com.endava.cats.model.FuzzingData; | ||
import com.endava.cats.report.ExecutionStatisticsListener; | ||
import com.endava.cats.report.TestCaseExporter; | ||
import com.endava.cats.report.TestCaseExporterHtmlJs; | ||
import com.endava.cats.report.TestCaseListener; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import jakarta.enterprise.inject.Instance; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
@QuarkusTest | ||
class ResponsesWithBodiesLinterFuzzerTest { | ||
|
||
private TestCaseListener testCaseListener; | ||
|
||
private ResponsesWithBodiesLinterFuzzer responsesWithBodiesLinterFuzzer; | ||
|
||
@BeforeEach | ||
void setup() { | ||
Instance<TestCaseExporter> exporters = Mockito.mock(Instance.class); | ||
TestCaseExporter exporter = Mockito.mock(TestCaseExporterHtmlJs.class); | ||
Mockito.when(exporters.stream()).thenReturn(Stream.of(exporter)); | ||
testCaseListener = Mockito.spy(new TestCaseListener(Mockito.mock(CatsGlobalContext.class), Mockito.mock(ExecutionStatisticsListener.class), exporters, | ||
Mockito.mock(IgnoreArguments.class), Mockito.mock(ReportingArguments.class))); | ||
responsesWithBodiesLinterFuzzer = new ResponsesWithBodiesLinterFuzzer(testCaseListener); | ||
} | ||
|
||
@Test | ||
void shouldReturnInfo() { | ||
FuzzingData data = FuzzingData.builder().responses(Map.of("200", List.of("1", "2"))).build(); | ||
responsesWithBodiesLinterFuzzer.fuzz(data); | ||
|
||
Mockito.verify(testCaseListener, Mockito.times(1)).reportResultInfo(Mockito.any(), Mockito.any(), Mockito.eq("All HTTP response codes have a response body")); | ||
} | ||
|
||
@Test | ||
void shouldReturnError() { | ||
FuzzingData data = FuzzingData.builder().responses(Map.of("200", List.of())).build(); | ||
responsesWithBodiesLinterFuzzer.fuzz(data); | ||
|
||
Mockito.verify(testCaseListener, Mockito.times(1)).reportResultError(Mockito.any(), Mockito.any(), | ||
Mockito.eq("Missing response body for some HTTP response codes"), Mockito.eq("The following HTTP response codes are missing a response body: {}"), Mockito.eq(List.of("200"))); | ||
} | ||
|
||
@Test | ||
void shouldReturnSimpleClassNameForToString() { | ||
Assertions.assertThat(responsesWithBodiesLinterFuzzer).hasToString(responsesWithBodiesLinterFuzzer.getClass().getSimpleName()); | ||
} | ||
|
||
@Test | ||
void shouldReturnMeaningfulDescription() { | ||
Assertions.assertThat(responsesWithBodiesLinterFuzzer.description()).isEqualTo("verifies that HTTP response codes (except for 204 and 304) have a response body"); | ||
} | ||
} |