Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package net.joshka.junit.json.params;

import static java.util.Arrays.stream;

import java.util.Collection;
import java.util.List;
import javax.json.JsonValue.ValueType;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
Expand Down Expand Up @@ -29,12 +34,9 @@ public class JsonFileArgumentsProvider implements AnnotationConsumer<JsonFileSou
this.inputStreamProvider = inputStreamProvider;
}

private static Stream<JsonValue> values(InputStream inputStream) {
private static JsonValue values(InputStream inputStream) {
try (JsonReader reader = Json.createReader(inputStream)) {
JsonStructure structure = reader.read();
return structure.getValueType() == JsonValue.ValueType.ARRAY
? structure.asJsonArray().stream()
: Stream.of(structure);
return reader.read();
}
}

Expand All @@ -45,10 +47,18 @@ public void accept(JsonFileSource jsonFileSource) {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Arrays.stream(resources)
boolean isList = stream(context.getRequiredTestMethod().getParameterTypes())
.anyMatch(List.class::isAssignableFrom);
return stream(resources)
.map(resource -> openInputStream(context, resource))
.flatMap(JsonFileArgumentsProvider::values)
.map(Arguments::of);
.map(JsonFileArgumentsProvider::values)
.flatMap(json -> {
if(json.getValueType() == ValueType.ARRAY && !isList){
return json.asJsonArray().stream();
}
return Stream.of(json);
})
.map(Arguments::arguments);
}

private InputStream openInputStream(ExtensionContext context, String resource) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package net.joshka.junit.json.params;

import java.lang.reflect.Method;
import java.util.List;
import javax.json.JsonArray;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
Expand Down Expand Up @@ -71,13 +74,47 @@ void arrayOfStrings(JsonString string) {
assertThat(string.getString()).startsWith("value");
}

/**
* When passed <code>[{"key":"value1"},{"key","value2"}]</code>
* and argument is a JsonArray, test is executed only once.
* @param object the parsed JsonArray object
*/
@ParameterizedTest
@JsonFileSource(resources = "/array-of-objects.json")
void jsonArray(JsonArray object) {
assertThat(object).hasSize(2);
}

/**
* When passed <code>[{"key":"value1"},{"key","value2"}]</code>
* and argument is a List, test is executed only once.
* @param object the parsed List object
*/
@ParameterizedTest
@JsonFileSource(resources = "/array-of-objects.json")
void listJsonObject(List<JsonObject> object) {
assertThat(object).hasSize(2);
}

/**
* When passed <code>[{"key":"value1"},{"key","value2"}]</code>
* and argument is a List, test is executed only once.
* @param object the parsed List object
*/
@ParameterizedTest
@JsonFileSource(resources = "/array-of-objects.json")
void listString(List<String> object) {
assertThat(object).hasSize(2);
}

@Test
@DisplayName("missing resource throws exception")
void missingResource() {
void missingResource() throws Exception {
BiFunction<Class, String, InputStream> inputStreamProvider = (aClass, resource) -> null;
ExtensionContext context = mock(ExtensionContext.class);
JsonFileSource source = mock(JsonFileSource.class);
when(source.resources()).thenReturn(new String[]{"not-found.json"});
when(context.getRequiredTestMethod()).thenReturn(this.getClass().getDeclaredMethod("missingResource"));
JsonFileArgumentsProvider provider = new JsonFileArgumentsProvider(inputStreamProvider);
provider.accept(source);

Expand Down