Skip to content

Commit

Permalink
chore: Add unit test for additional branches
Browse files Browse the repository at this point in the history
  • Loading branch information
en-milie committed Sep 7, 2024
1 parent 60ed30c commit 3889bf7
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
66 changes: 66 additions & 0 deletions src/test/java/com/endava/cats/context/CatsGlobalContextTest.java
Original file line number Diff line number Diff line change
@@ -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<String, Parameter> 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<String, io.swagger.v3.oas.models.headers.Header> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/com/endava/cats/util/JsonSetTest.java
Original file line number Diff line number Diff line change
@@ -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<Object[]> 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}
);
}
}

0 comments on commit 3889bf7

Please sign in to comment.