Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable int, bool, String method parameter conversion
Introduce JsonConverter that converts a single parameter by name from Json to the parameter type (String, int, or boolean). Only single parameters are supported right now. #3
- Loading branch information
Showing
4 changed files
with
103 additions
and
0 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
40 changes: 40 additions & 0 deletions
40
src/main/java/net/joshka/junit/json/params/JsonConverter.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,40 @@ | ||
package net.joshka.junit.json.params; | ||
|
||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.jupiter.params.converter.ArgumentConversionException; | ||
import org.junit.jupiter.params.converter.ArgumentConverter; | ||
|
||
import javax.json.JsonObject; | ||
|
||
public class JsonConverter implements ArgumentConverter { | ||
|
||
/** | ||
* Convert the supplied {@code source} object according to the supplied | ||
* {@code context}. | ||
* | ||
* @param source the source object to convert; may be {@code null} | ||
* @param context the parameter context where the converted object will be | ||
* used; never {@code null} | ||
* @return the converted object; may be {@code null} but only if the target | ||
* type is a reference type | ||
* @throws ArgumentConversionException if an error occurs during the | ||
* conversion | ||
*/ | ||
@Override | ||
public Object convert(Object source, ParameterContext context) { | ||
if (!(source instanceof JsonObject)) { | ||
throw new ArgumentConversionException("Not a JsonObject"); | ||
} | ||
JsonObject json = (JsonObject) source; | ||
String name = context.getParameter().getName(); | ||
Class<?> type = context.getParameter().getType(); | ||
if (type == String.class) { | ||
return json.getString(name); | ||
} else if (type == int.class) { | ||
return json.getInt(name); | ||
} else if (type == boolean.class) { | ||
return json.getBoolean(name); | ||
} | ||
throw new ArgumentConversionException("Can't convert to type: '" + type.getName() + "'"); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/net/joshka/junit/json/params/JsonArgumentsToValuesTest.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,46 @@ | ||
package net.joshka.junit.json.params; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.converter.ConvertWith; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class JsonArgumentsToValuesTest { | ||
@ParameterizedTest | ||
@JsonSource("{'key':'value'}") | ||
@DisplayName("Converts to Strings") | ||
void strings(@ConvertWith(JsonConverter.class) String key) { | ||
assertEquals("value", key); | ||
} | ||
|
||
@ParameterizedTest | ||
@JsonSource("{'key': true}") | ||
@DisplayName("Converts to booleans") | ||
void ints(@ConvertWith(JsonConverter.class) boolean key) { | ||
assertTrue(key); | ||
} | ||
|
||
|
||
@ParameterizedTest | ||
@JsonSource("{'key': 1 }") | ||
@DisplayName("Converts to ints") | ||
void booleans(@ConvertWith(JsonConverter.class) int key) { | ||
assertEquals(1, key); | ||
} | ||
|
||
@ParameterizedTest | ||
@Disabled("Doesn't yet work as only the first parameter gets a value") | ||
@JsonSource("{'stringKey':'value', 'boolKey': true, 'intKey': 1 }") | ||
@DisplayName("provides multiple objects") | ||
void multipleObjects( | ||
@ConvertWith(JsonConverter.class) String stringKey, | ||
@ConvertWith(JsonConverter.class) int intKey, | ||
@ConvertWith(JsonConverter.class) boolean boolKey) { | ||
assertEquals("value", stringKey); | ||
assertTrue(boolKey); | ||
assertEquals(1, intKey); | ||
} | ||
} |