Skip to content

Commit e3ce40f

Browse files
Max Xandecojoshka
authored andcommitted
add support for lists when using JsonFileSource.
1 parent 46fbc3b commit e3ce40f

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

src/main/java/net/joshka/junit/json/params/JsonFileArgumentsProvider.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package net.joshka.junit.json.params;
22

3+
import static java.util.Arrays.stream;
4+
5+
import java.util.Collection;
6+
import java.util.List;
7+
import javax.json.JsonValue.ValueType;
38
import org.junit.jupiter.api.extension.ExtensionContext;
49
import org.junit.jupiter.params.provider.Arguments;
510
import org.junit.jupiter.params.provider.ArgumentsProvider;
@@ -29,12 +34,9 @@ public class JsonFileArgumentsProvider implements AnnotationConsumer<JsonFileSou
2934
this.inputStreamProvider = inputStreamProvider;
3035
}
3136

32-
private static Stream<JsonValue> values(InputStream inputStream) {
37+
private static JsonValue values(InputStream inputStream) {
3338
try (JsonReader reader = Json.createReader(inputStream)) {
34-
JsonStructure structure = reader.read();
35-
return structure.getValueType() == JsonValue.ValueType.ARRAY
36-
? structure.asJsonArray().stream()
37-
: Stream.of(structure);
39+
return reader.read();
3840
}
3941
}
4042

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

4648
@Override
4749
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
48-
return Arrays.stream(resources)
50+
boolean isList = stream(context.getRequiredTestMethod().getParameterTypes())
51+
.anyMatch(List.class::isAssignableFrom);
52+
return stream(resources)
4953
.map(resource -> openInputStream(context, resource))
50-
.flatMap(JsonFileArgumentsProvider::values)
51-
.map(Arguments::of);
54+
.map(JsonFileArgumentsProvider::values)
55+
.flatMap(json -> {
56+
if(json.getValueType() == ValueType.ARRAY && !isList){
57+
return json.asJsonArray().stream();
58+
}
59+
return Stream.of(json);
60+
})
61+
.map(Arguments::arguments);
5262
}
5363

5464
private InputStream openInputStream(ExtensionContext context, String resource) {

src/test/java/net/joshka/junit/json/params/JsonFileArgumentsProviderTest.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package net.joshka.junit.json.params;
22

3+
import java.lang.reflect.Method;
4+
import java.util.List;
5+
import javax.json.JsonArray;
36
import org.junit.jupiter.api.DisplayName;
47
import org.junit.jupiter.api.Test;
58
import org.junit.jupiter.api.extension.ExtensionContext;
@@ -71,13 +74,47 @@ void arrayOfStrings(JsonString string) {
7174
assertThat(string.getString()).startsWith("value");
7275
}
7376

77+
/**
78+
* When passed <code>[{"key":"value1"},{"key","value2"}]</code>
79+
* and argument is a JsonArray, test is executed only once.
80+
* @param object the parsed JsonArray object
81+
*/
82+
@ParameterizedTest
83+
@JsonFileSource(resources = "/array-of-objects.json")
84+
void jsonArray(JsonArray object) {
85+
assertThat(object).hasSize(2);
86+
}
87+
88+
/**
89+
* When passed <code>[{"key":"value1"},{"key","value2"}]</code>
90+
* and argument is a List, test is executed only once.
91+
* @param object the parsed List object
92+
*/
93+
@ParameterizedTest
94+
@JsonFileSource(resources = "/array-of-objects.json")
95+
void listJsonObject(List<JsonObject> object) {
96+
assertThat(object).hasSize(2);
97+
}
98+
99+
/**
100+
* When passed <code>[{"key":"value1"},{"key","value2"}]</code>
101+
* and argument is a List, test is executed only once.
102+
* @param object the parsed List object
103+
*/
104+
@ParameterizedTest
105+
@JsonFileSource(resources = "/array-of-objects.json")
106+
void listString(List<String> object) {
107+
assertThat(object).hasSize(2);
108+
}
109+
74110
@Test
75111
@DisplayName("missing resource throws exception")
76-
void missingResource() {
112+
void missingResource() throws Exception {
77113
BiFunction<Class, String, InputStream> inputStreamProvider = (aClass, resource) -> null;
78114
ExtensionContext context = mock(ExtensionContext.class);
79115
JsonFileSource source = mock(JsonFileSource.class);
80116
when(source.resources()).thenReturn(new String[]{"not-found.json"});
117+
when(context.getRequiredTestMethod()).thenReturn(this.getClass().getDeclaredMethod("missingResource"));
81118
JsonFileArgumentsProvider provider = new JsonFileArgumentsProvider(inputStreamProvider);
82119
provider.accept(source);
83120

0 commit comments

Comments
 (0)