Removed Spring-Autowireing-Annotations to SimpleMapperService
[simple-mapper] / src / test / java / de / juplo / jackson / SimpleMapperTest.java
index 474cafc..e5769e4 100644 (file)
@@ -5,16 +5,12 @@ import com.fasterxml.jackson.core.JsonParser;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Spliterator;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -42,6 +38,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyInputToArraySpliterator() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Spliterator<Object> spliterator;
 
     spliterator = SimpleMapper.getArraySpliterator(get("/empty/1.json"));
@@ -59,6 +57,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyInputToList() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     List<Object> list;
 
     list = SimpleMapper.convertArray(get("/empty/1.json"));
@@ -76,6 +76,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyInputToObjectSpliterator() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Spliterator<Entry<String, Object>> spliterator;
 
     spliterator = SimpleMapper.getObjectSpliterator(get("/empty/1.json"));
@@ -93,6 +95,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyInputToMap() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Map<String, Object> map;
 
     map = SimpleMapper.convertObject(get("/empty/1.json"));
@@ -110,6 +114,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyInput() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Object object;
 
     object = SimpleMapper.convert(get("/empty/1.json"));
@@ -128,6 +134,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyArrayToArraySpliterator() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Spliterator<Object> spliterator;
 
     spliterator = SimpleMapper.getArraySpliterator(get("/array/empty/1.json"));
@@ -163,6 +171,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyArrayToList() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     List<Object> list;
 
     list = SimpleMapper.convertArray(get("/array/empty/1.json"));
@@ -178,6 +188,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyArrayToObjectSpliterator() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     try
     {
       SimpleMapper.getObjectSpliterator(get("/array/empty/1.json"));
@@ -185,7 +197,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -195,7 +207,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -205,7 +217,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -215,13 +227,15 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
   }
 
   @Test
   public void testConvertEmptyArrayToMap() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     try
     {
       SimpleMapper.convertObject(get("/array/empty/1.json"));
@@ -229,7 +243,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -239,7 +253,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -249,7 +263,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -259,13 +273,15 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
   }
 
   @Test
   public void testConvertEmptyArray() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Object object;
 
     object = SimpleMapper.convert(get("/array/empty/1.json"));
@@ -290,6 +306,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyObjectToArraySpliterator() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     try
     {
       SimpleMapper.getArraySpliterator(get("/object/empty/1.json"));
@@ -297,7 +315,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -307,7 +325,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -317,7 +335,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -327,13 +345,15 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
   }
 
   @Test
   public void testConvertEmptyObjectToList() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     try
     {
       SimpleMapper.convertArray(get("/object/empty/1.json"));
@@ -341,7 +361,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -351,7 +371,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -361,7 +381,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -371,13 +391,15 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
   }
 
   @Test
   public void testConvertEmptyObjectToObjectSpliterator() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Spliterator<Entry<String, Object>> spliterator;
 
     spliterator = SimpleMapper.getObjectSpliterator(get("/object/empty/1.json"));
@@ -413,6 +435,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyObjectToMap() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Map<String, Object> map;
 
     map = SimpleMapper.convertObject(get("/object/empty/1.json"));
@@ -428,6 +452,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertEmptyObject() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Object object;
 
     object = SimpleMapper.convert(get("/object/empty/1.json"));
@@ -452,6 +478,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertArrayToArraySpliterator() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Spliterator<Object> spliterator;
 
     spliterator = SimpleMapper.getArraySpliterator(get("/array/1.json"));
@@ -484,6 +512,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertArrayToList() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     List<Object> list;
 
     list = SimpleMapper.convertArray(get("/array/1.json"));
@@ -499,6 +529,8 @@ public class SimpleMapperTest
   @Test
   public void testConvertArrayToObjectSpliterator() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     try
     {
       SimpleMapper.getObjectSpliterator(get("/array/1.json"));
@@ -506,7 +538,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -516,13 +548,15 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
   }
 
   @Test
   public void testConvertArrayToMap() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     try
     {
       SimpleMapper.convertObject(get("/array/1.json"));
@@ -530,7 +564,7 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
 
     try
@@ -540,13 +574,15 @@ public class SimpleMapperTest
     }
     catch(IllegalArgumentException e)
     {
-      LOG.info(e.getMessage());
+      LOG.trace("expected exception", e);
     }
   }
 
   @Test
   public void testConvertArray() throws Exception
   {
+    LOG.info("<-- Start Of New Test-Case!");
+
     Object object;
     List<Object> list;
 
@@ -564,6 +600,175 @@ public class SimpleMapperTest
     checkPartnerPageData(list.get(3));
   }
 
+
+  @Test
+  public void testConvertObjectToArraySpliterator() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    try
+    {
+      SimpleMapper.getArraySpliterator(get("/object/1.json"));
+      fail("it must not be possible, to get an array-spliterator for an object");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.trace("expected exception", e);
+    }
+
+    try
+    {
+      SimpleMapper.getArraySpliterator(get("/object/2.json"));
+      fail("it must not be possible, to get an array-spliterator for an object");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.trace("expected exception", e);
+    }
+  }
+
+  @Test
+  public void testConvertObjectToList() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    try
+    {
+      SimpleMapper.convertArray(get("/object/1.json"));
+      fail("it must not be possible, to get a list for an object");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.trace("expected exception", e);
+    }
+
+    try
+    {
+      SimpleMapper.convertArray(get("/object/2.json"));
+      fail("it must not be possible, to get a list for an object");
+    }
+    catch(IllegalArgumentException e)
+    {
+      LOG.trace("expected exception", e);
+    }
+  }
+
+  @Test
+  public void testConvertObjectToObjectSpliterator() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    Spliterator<Entry<String, Object>> spliterator;
+
+    spliterator = SimpleMapper.getObjectSpliterator(get("/object/1.json"));
+    checkObjectSpliterator(spliterator);
+    spliterator = SimpleMapper.getObjectSpliterator(get("/object/2.json"));
+    checkObjectSpliterator(spliterator);
+  }
+
+  void checkObjectSpliterator(Spliterator<Entry<String, Object>> spliterator)
+      throws
+        Exception
+  {
+    assertNotNull(spliterator);
+
+    final LinkedHashMap<String, Object> map = new LinkedHashMap<>();
+    for (int i = 0; i < 4; i++)
+      assertTrue(
+          "The created splitter should have a " + (i+1) + ". entry",
+          spliterator.tryAdvance((Entry<String, Object> e) -> {
+            map.put(e.getKey(), e.getValue());
+          }));
+
+    assertFalse(
+        "The created splitter should have no more entries",
+        spliterator.tryAdvance((Object t) ->
+        {
+          fail("The consumer should not have been called");
+        }));
+
+    checkVariables(map);
+  }
+
+  @Test
+  public void testConvertObjectToMap() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    Map<String, Object> map;
+
+    map = SimpleMapper.convertObject(get("/object/1.json"));
+    assertNotNull(map);
+    assertEquals(4, map.size());
+    checkVariables(map);
+    map = SimpleMapper.convertObject(get("/object/2.json"));
+    assertNotNull(map);
+    assertEquals(4, map.size());
+    checkVariables(map);
+  }
+
+  @Test
+  public void testConvertObject() throws Exception
+  {
+    LOG.info("<-- Start Of New Test-Case!");
+
+    Object object;
+    Map<String, Object> map;
+
+    object = SimpleMapper.convert(get("/object/1.json"));
+    assertNotNull(object);
+    assertTrue("the returned object should be a map", object instanceof Map);
+    map = (Map<String, Object>)object;
+    assertEquals(4, map.size());
+    checkVariables(map);
+    object = SimpleMapper.convert(get("/object/2.json"));
+    assertNotNull(object);
+    assertTrue("the returned object should be a map", object instanceof Map);
+    map = (Map<String, Object>)object;
+    assertEquals(4, map.size());
+    checkVariables(map);
+  }
+
+  void checkVariables(Map<String, Object> map)
+  {
+    Iterator<Entry<String, Object>> iterator = map.entrySet().iterator();
+    Entry<String, Object> entry;
+
+    assertTrue("The map should have a first element", iterator.hasNext());
+    entry = iterator.next();
+    assertEquals("bootstrap", entry.getKey());
+    assertEquals("bootstrap.min.css", entry.getValue());
+
+    assertTrue("The map should have a second element", iterator.hasNext());
+    entry = iterator.next();
+    assertEquals("pages", entry.getKey());
+    assertTrue(
+        "The pages-variable should be of type List",
+        entry.getValue() instanceof List
+        );
+    assertEquals(4, ((List)entry.getValue()).size());
+    checkPartnerPageData(((List)entry.getValue()).get(3));
+
+    assertTrue("The map should have a third element", iterator.hasNext());
+    entry = iterator.next();
+    assertEquals("footer", entry.getKey());
+    assertTrue(
+        "The footer-variable should be of type List",
+        entry.getValue() instanceof List
+        );
+    assertEquals(3, ((List)entry.getValue()).size());
+
+    assertTrue("The map should have a third element", iterator.hasNext());
+    entry = iterator.next();
+    assertEquals("sponsorship", entry.getKey());
+    assertTrue(
+        "The sponsorship-variable should be of type List",
+        entry.getValue() instanceof List
+        );
+    assertEquals(3, ((List)entry.getValue()).size());
+  }
+
+
   void checkPartnerPageData(Object page)
   {
     assertTrue("The page-data should be of type Map", page instanceof Map);