Skip to content

Commit

Permalink
Add tests for hasList, hasNested, and getListOf
Browse files Browse the repository at this point in the history
  • Loading branch information
dmikurube committed Oct 27, 2023
1 parent a44173b commit 8b55288
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/test/java/org/embulk/util/config/TestDataSourceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.embulk.util.config;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Expand All @@ -32,6 +33,17 @@ public class TestDataSourceImpl {
@Test
public void testGetList() {
final DataSourceImpl impl = createTestNode();

assertTrue(impl.hasList("array"));
assertTrue(impl.hasList("arrayOfStrings"));
assertTrue(impl.hasList("arrayOfIntegers"));
assertFalse(impl.hasList("noexist"));
assertFalse(impl.hasList("object"));
assertFalse(impl.hasList("string"));
assertFalse(impl.hasList("boolean"));
assertFalse(impl.hasList("int"));
assertFalse(impl.hasList("double"));

final List<?> list = impl.get(List.class, "array");
assertEquals(4, list.size());
assertTrue(list.get(0) instanceof String);
Expand All @@ -40,18 +52,62 @@ public void testGetList() {
assertEquals("fuga", list.get(1));
assertTrue(list.get(2) instanceof Map);
assertTrue(list.get(3) instanceof List);

final List<String> listOfStrings = impl.getListOf(String.class, "arrayOfStrings");
assertEquals("foo", listOfStrings.get(0));
assertEquals("bar", listOfStrings.get(1));
assertEquals("baz", listOfStrings.get(2));

final List<Long> listOfIntegers = impl.getListOf(Long.class, "arrayOfIntegers");
assertEquals(124L, listOfIntegers.get(0));
assertEquals(-4014L, listOfIntegers.get(1));
assertEquals(9241L, listOfIntegers.get(2));
}

@Test
public void testGetObject() {
final DataSourceImpl impl = createTestNode();

assertFalse(impl.hasNested("array"));
assertFalse(impl.hasNested("arrayOfStrings"));
assertFalse(impl.hasNested("arrayOfIntegers"));
assertFalse(impl.hasNested("noexist"));
assertTrue(impl.hasNested("object"));
assertFalse(impl.hasNested("string"));
assertFalse(impl.hasNested("boolean"));
assertFalse(impl.hasNested("int"));
assertFalse(impl.hasNested("double"));

final DataSourceImpl object = impl.getNested("object");
assertTrue(object.has("key1"));
assertTrue(object.has("key2"));
assertEquals("value1", object.get(String.class, "key1"));
assertEquals("value2", object.get(String.class, "key2"));
}

@Test
public void testFailToGetListOfObject() {
final DataSourceImpl impl = createTestNode();
try {
impl.getListOf(String.class, "object");
} catch (final ConfigException ex) {
return;
}
fail("ConfigException should be thrown by getting a JSON object as a List.");
}

@Test
public void testFailToGetListOfDifferentTypes() {
final DataSourceImpl impl = createTestNode();
try {
impl.getListOf(String.class, "array");
} catch (final ConfigException ex) {
assertTrue(ex.getCause() instanceof com.fasterxml.jackson.core.JsonProcessingException);
return;
}
fail("ConfigException should be thrown by getting a mixed JSON array as a List of Strings.");
}

@Test
public void testFailToGetNestedAsList() {
final DataSourceImpl impl = createTestNode();
Expand Down Expand Up @@ -116,6 +172,19 @@ private static DataSourceImpl createTestNode() {
arrayUnderList.add("somewhat");
array.add(arrayUnderList);
root.put("array", array);

final ArrayNode arrayOfStrings = SIMPLE_MAPPER.createArrayNode();
arrayOfStrings.add("foo");
arrayOfStrings.add("bar");
arrayOfStrings.add("baz");
root.put("arrayOfStrings", arrayOfStrings);

final ArrayNode arrayOfIntegers = SIMPLE_MAPPER.createArrayNode();
arrayOfIntegers.add(124);
arrayOfIntegers.add(-4014);
arrayOfIntegers.add(9241);
root.put("arrayOfIntegers", arrayOfIntegers);

final ObjectNode object = SIMPLE_MAPPER.createObjectNode();
object.put("key1", "value1");
object.put("key2", "value2");
Expand Down

0 comments on commit 8b55288

Please sign in to comment.