BodyParserEnginePost implements BodyParserEngine { @Override public <T> T invoke(Context context, Class<T> classOfT) { Map<String, String[]> parameters = context.getParameters(); return invoke(context, parameters, classOfT, ""); } @Inject  BodyParserEnginePost(ParamParsers paramParsers); @Override T invoke(Context context, Class<T> classOfT); String getContentType();  }
@Test public void testBodyParser() { String dateString = "2014-10-10"; String dateTimeString = "2014-10-10T20:09:10"; Map<String, String[]> map = new HashMap<>(); map.put("integerPrimitive", new String [] {"1000"}); map.put("integerObject", new String [] {"2000"}); map.put("longPrimitive", new String [] {"3000"}); map.put("longObject", new String [] {"4000"}); map.put("floatPrimitive", new String [] {"1.234"}); map.put("floatObject", new String [] {"2.345"}); map.put("doublePrimitive", new String [] {"3.456"}); map.put("doubleObject", new String [] {"4.567"}); map.put("string", new String [] {"aString"}); map.put("characterPrimitive", new String [] {"a"}); map.put("characterObject", new String [] {"b"}); map.put("date", new String[]{dateString}); map.put("timestamp", new String[]{dateTimeString}); map.put("somethingElseWhatShouldBeSkipped", new String [] {"somethingElseWhatShouldBeSkipped"}); Mockito.when(context.getParameters()).thenReturn(map); Mockito.when(context.getValidation()).thenReturn(validation); TestObject testObject = bodyParserEnginePost.invoke(context, TestObject.class); assertThat(testObject.integerPrimitive, CoreMatchers.equalTo(1000)); assertThat(testObject.integerObject, CoreMatchers.equalTo(2000)); assertThat(testObject.longPrimitive, CoreMatchers.equalTo(3000L)); assertThat(testObject.longObject, CoreMatchers.equalTo(4000L)); assertThat(testObject.floatPrimitive, CoreMatchers.equalTo(1.234F)); assertThat(testObject.floatObject, CoreMatchers.equalTo(2.345F)); assertThat(testObject.doublePrimitive, CoreMatchers.equalTo(3.456D)); assertThat(testObject.doubleObject, CoreMatchers.equalTo(4.567D)); assertThat(testObject.string, equalTo("aString")); assertThat(testObject.characterPrimitive, equalTo('a')); assertThat(testObject.characterObject, equalTo('b')); assertThat(testObject.date, CoreMatchers.equalTo(new LocalDateTime(dateString).toDate())); assertThat(testObject.timestamp, CoreMatchers.equalTo(new LocalDateTime(dateTimeString).toDate())); assertFalse(validation.hasViolations()); }
@Test public void testBodyParserWithValidationErrors() { Map<String, String[]> map = new HashMap<>(); map.put("integerPrimitive", new String [] {"a"}); map.put("integerObject", new String [] {"b"}); map.put("longPrimitive", new String [] {"c"}); map.put("longObject", new String [] {"d"}); map.put("floatPrimitive", new String [] {"e"}); map.put("floatObject", new String [] {"f"}); map.put("doublePrimitive", new String [] {"g"}); map.put("doubleObject", new String [] {"h"}); map.put("characterPrimitive", new String [] {null}); map.put("characterObject", new String [] {null}); map.put("date", new String[]{"cc"}); map.put("timestamp", new String[]{"dd"}); map.put("uuid", new String[]{"ee"}); map.put("somethingElseWhatShouldBeSkipped", new String [] {"somethingElseWhatShouldBeSkipped"}); Mockito.when(context.getParameters()).thenReturn(map); Mockito.when(context.getValidation()).thenReturn(validation); TestObject testObject = bodyParserEnginePost.invoke(context, TestObject.class); assertTrue(validation.hasViolations()); assertViolation("integerPrimitive", IsInteger.KEY); assertThat(testObject.integerPrimitive, equalTo(0)); assertViolation("integerObject", IsInteger.KEY); assertNull(testObject.integerObject); assertViolation("longPrimitive", IsInteger.KEY); assertThat(testObject.longPrimitive, equalTo(0L)); assertViolation("longObject", IsInteger.KEY); assertNull(testObject.longObject); assertViolation("floatPrimitive", IsFloat.KEY); assertThat(testObject.floatPrimitive, equalTo(0F)); assertViolation("floatObject", IsFloat.KEY); assertNull(testObject.floatObject); assertViolation("doublePrimitive", IsFloat.KEY); assertThat(testObject.doublePrimitive, equalTo(0D)); assertViolation("doubleObject", IsFloat.KEY); assertNull(testObject.doubleObject); assertViolation("date", IsDate.KEY); assertNull(testObject.date); assertViolation("timestamp", IsDate.KEY); assertNull(testObject.timestamp); assertViolation("uuid", IsUUID.KEY); assertNull(testObject.uuid); assertNull(testObject.string); assertThat(testObject.characterPrimitive, equalTo('\0')); assertNull(testObject.characterObject); assertNull(testObject.string); }
@Test public void testBodyParserWhenThereIsAnUnsupportedFieldType() { Map<String, String[]> map = new HashMap<>(); map.put("string", new String [] {"aString"}); map.put("iAmNotSupportedField", new String [] {"iAmNotSupportedField"}); map.put("longs", new String[] {"1", "2"}); Mockito.when(context.getParameters()).thenReturn(map); Mockito.when(context.getValidation()).thenReturn(validation); TestObjectWithUnsupportedFields testObject = bodyParserEnginePost.invoke(context, TestObjectWithUnsupportedFields.class); assertThat(testObject.string, equalTo("aString")); assertThat(testObject.iAmNotSupportedField, CoreMatchers.equalTo(null)); assertThat(testObject.longs, CoreMatchers.equalTo(null)); }
@Test public void testBodyParserWithCollectionAndArray() { Map<String, String[]> map = new HashMap<>(); map.put("integers", new String[] {"1", "2"}); map.put("strings", new String[] {"hello", "world"}); Mockito.when(context.getParameters()).thenReturn(map); Mockito.when(context.getValidation()).thenReturn(validation); TestObjectWithArraysAndCollections testObject = bodyParserEnginePost.invoke(context, TestObjectWithArraysAndCollections.class); assertThat(testObject.integers.length, equalTo(2)); assertThat(testObject.integers[0], equalTo(1)); assertThat(testObject.integers[1], equalTo(2)); assertThat(testObject.strings.size(), equalTo(2)); assertThat(testObject.strings.get(0), equalTo("hello")); assertThat(testObject.strings.get(1), equalTo("world")); assertFalse(validation.hasViolations()); }
@Test public void testBodyParserWithEnumerations() { Map<String, String[]> map = new HashMap<>(); map.put("enum1", new String[] {MyEnum.VALUE_A.name()}); map.put("enum2", new String[] {new String("value_b")}); Mockito.when(context.getParameters()).thenReturn(map); Mockito.when(context.getValidation()).thenReturn(validation); TestObjectWithEnum testObject = bodyParserEnginePost.invoke(context, TestObjectWithEnum.class); assertThat(testObject.enum1, equalTo(MyEnum.VALUE_A)); assertThat(testObject.enum2, equalTo(MyEnum.VALUE_B)); assertFalse(validation.hasViolations()); }
@Test public void testBodyParserWithCustomNeedingInjectionParamParser() { Map<String, String[]> map = new HashMap<>(); map.put("dep", new String[] {"dep1"}); map.put("depArray", new String[] {"depArray1", "depArray2"}); map.put("depList", new String[] {"depList1", "depList2"}); Mockito.when(context.getParameters()).thenReturn(map); Mockito.when(context.getValidation()).thenReturn(validation); TestObjectWithCustomType testObject = bodyParserEnginePost.invoke(context, TestObjectWithCustomType.class); assertThat(testObject.dep, equalTo(new Dep("hello_dep1"))); assertNotNull(testObject.depArray); assertThat(testObject.depArray.length, equalTo(2)); assertThat(testObject.depArray[0], equalTo(new Dep("hello_depArray1"))); assertThat(testObject.depArray[1], equalTo(new Dep("hello_depArray2"))); assertNotNull(testObject.depList); assertThat(testObject.depList.size(), equalTo(2)); assertThat(testObject.depList.get(0), equalTo(new Dep("hello_depList1"))); assertThat(testObject.depList.get(1), equalTo(new Dep("hello_depList2"))); assertFalse(validation.hasViolations()); }
@Test public void testBodyParserWithInnerObjects() { Map<String, String[]> map = new HashMap<>(); map.put("object1.integerPrimitive", new String [] {"1000"}); map.put("object1.integerObject", new String [] {"2000"}); map.put("object2.integerPrimitive", new String [] {"3000"}); map.put("object2.integerObject", new String [] {"4000"}); Mockito.when(context.getParameters()).thenReturn(map); Mockito.when(context.getValidation()).thenReturn(validation); TestObjectWithInnerObjects testObject = bodyParserEnginePost.invoke(context, TestObjectWithInnerObjects.class); assertNotNull(testObject.object1); assertThat(testObject.object1.integerPrimitive, equalTo(1000)); assertThat(testObject.object1.integerObject, equalTo(2000)); assertNotNull(testObject.object2); assertThat(testObject.object2.integerPrimitive, equalTo(3000)); assertThat(testObject.object2.integerObject, equalTo(4000)); assertFalse(validation.hasViolations()); }
@Test public void testBodyParserWithEmptyInnerObjects() { Map<String, String[]> map = new HashMap<>(); map.put("object1.integerPrimitive", new String [] {""}); map.put("object1.integerObject", new String [] {""}); map.put("object2.integerPrimitive", new String [] {null}); map.put("object2.integerObject", new String [] {null}); Mockito.when(context.getParameters()).thenReturn(map); TestObjectWithInnerObjects testObject = bodyParserEnginePost.invoke(context, TestObjectWithInnerObjects.class); assertNull(testObject.object1); assertNull(testObject.object2); assertFalse(validation.hasViolations()); }