@Test public void testNonoptionalFields() throws JsonProtocolModelParseException, JsonProtocolParseException { SeveralTypesWithSomethingParser parser; { List<Class<?>> types = new ArrayList<Class<?>>(3); types.add(TypeWithNullableLong.class); types.add(TypeWithSomething.class); types.add(Something.class); parser = createDynamicParser(SeveralTypesWithSomethingParser.class, types); } // First couple of checks that parser does work { JSONObject json = parseJson("{'val': null}"); TypeWithNullableLong val = parser.parseTypeWithNullableLong(json); assertNull(val.val()); } { JSONObject json = parseJson("{'data': {} }"); TypeWithSomething val = parser.parseTypeWithSomething(json); assertNotNull(val.data()); } JSONObject emptyJson = parseJson("{}"); { try { TypeWithNullableLong val = parser.parseTypeWithNullableLong(emptyJson); val.val(); fail(); } catch (Exception e) { // expected } } { try { TypeWithSomething val = parser.parseTypeWithSomething(emptyJson); fail(); } catch (Exception e) { // expected } } }
@Test public void testBrokenNullStructValue() throws JsonProtocolModelParseException, JsonProtocolParseException { TypeWithSomethingParser parser = createDynamicParser( TypeWithSomethingParser.class, Something.class, TypeWithSomething.class); { JSONObject json = parseJson("{'data': {} }"); TypeWithSomething val = parser.parseTypeWithSomething(json); assertNotNull(val.data()); } { JSONObject json = parseJson("{'data': null }"); try { parser.parseTypeWithSomething(json); fail(); } catch (Exception e) { // expected } } }