Search in sources :

Example 1 with RequiredFieldNotPresentException

use of com.linkedin.data.template.RequiredFieldNotPresentException in project rest.li by linkedin.

the class TestRecord method testField.

private void testField(RecordTemplate record, String fieldName, final Object value) {
    Class<?> valueClass = value.getClass();
    TestDataTemplateUtil.FieldInfo fieldInfo = TestDataTemplateUtil.fieldInfo(record, fieldName);
    String getterPrefix = fieldInfo.getFieldClass() == Boolean.class ? "is" : "get";
    String getMethodName = TestDataTemplateUtil.methodName(getterPrefix, fieldName);
    String setMethodName = TestDataTemplateUtil.methodName("set", fieldName);
    String hasMethodName = TestDataTemplateUtil.methodName("has", fieldName);
    String removeMethodName = TestDataTemplateUtil.methodName("remove", fieldName);
    Class<? extends RecordTemplate> recordClass = record.getClass();
    DataMap dataMap = record.data();
    boolean isOptional = fieldInfo.getField().getOptional();
    try {
        Method getMethod = recordClass.getMethod(getMethodName);
        Method getModeMethod = recordClass.getMethod(getMethodName, GetMode.class);
        Method setMethod = recordClass.getMethod(setMethodName, valueClass);
        Method setModeMethod = recordClass.getMethod(setMethodName, valueClass, SetMode.class);
        Method hasMethod = recordClass.getMethod(hasMethodName);
        Method removeMethod = recordClass.getMethod(removeMethodName);
        Object prevValue;
        // fields
        TestDataTemplateUtil.assertPresentInFields(recordClass, fieldName);
        // has
        if (dataMap.containsKey(fieldName)) {
            assertTrue((Boolean) hasMethod.invoke(record));
            prevValue = getMethod.invoke(record);
        } else {
            assertFalse((Boolean) hasMethod.invoke(record));
            prevValue = null;
        }
        // set
        Object result = setMethod.invoke(record, value);
        assertSame(result, record);
        // has with field present
        assertTrue((Boolean) hasMethod.invoke(record));
        // get with field present
        result = getMethod.invoke(record);
        if (value instanceof DataTemplate || value instanceof Enum) {
            assertSame(result, value);
        } else {
            assertEquals(result, value);
        }
        // GetMode.NULL, GetMode.DEFAULT, GetMode.STRICT with field present
        assertSame(getModeMethod.invoke(record, GetMode.NULL), result);
        assertSame(getModeMethod.invoke(record, GetMode.DEFAULT), result);
        assertSame(getModeMethod.invoke(record, GetMode.STRICT), result);
        // remove
        removeMethod.invoke(record);
        // has with field absent
        assertFalse((Boolean) hasMethod.invoke(record));
        assertNull(getModeMethod.invoke(record, GetMode.NULL));
        // GetMode.NULL with field absent
        result = getModeMethod.invoke(record, GetMode.NULL);
        assertNull(result);
        // GetMode.DEFAULT with field absent
        Object defaultValue = getModeMethod.invoke(record, GetMode.DEFAULT);
        Object defaultValueFromSchema = fieldInfo.getField().getDefault();
        assertEquals(defaultValue == null, defaultValueFromSchema == null);
        if (defaultValue != null) {
            if (defaultValue instanceof DataTemplate) {
                assertEquals(((DataTemplate) defaultValue).data(), defaultValueFromSchema);
            } else if (defaultValue instanceof Enum) {
                assertEquals(defaultValue.toString(), defaultValueFromSchema);
            } else {
                assertSame(defaultValue, defaultValueFromSchema);
            }
        }
        // GetMode.STRICT with field absent
        boolean expectRequiredFieldNotFoundException = (!isOptional && defaultValue == null);
        try {
            result = getModeMethod.invoke(record, GetMode.STRICT);
            assertFalse(expectRequiredFieldNotFoundException);
            assertSame(result, defaultValue);
        } catch (InvocationTargetException exc) {
            assertTrue(expectRequiredFieldNotFoundException);
            assertTrue(exc.getTargetException() instanceof RequiredFieldNotPresentException);
        }
        // SetMode.IGNORE_NULL
        setModeMethod.invoke(record, value, SetMode.IGNORE_NULL);
        assertSame(getMethod.invoke(record), value);
        setModeMethod.invoke(record, null, SetMode.IGNORE_NULL);
        assertSame(getMethod.invoke(record), value);
        // SetMode.REMOVE_IF_NULL
        removeMethod.invoke(record);
        setModeMethod.invoke(record, value, SetMode.REMOVE_IF_NULL);
        assertSame(getMethod.invoke(record), value);
        setModeMethod.invoke(record, null, SetMode.REMOVE_IF_NULL);
        assertFalse((Boolean) hasMethod.invoke(record));
        // SetMode.REMOVE_OPTIONAL_IF_NULL
        removeMethod.invoke(record);
        setModeMethod.invoke(record, value, SetMode.REMOVE_OPTIONAL_IF_NULL);
        assertSame(getMethod.invoke(record), value);
        try {
            setModeMethod.invoke(record, null, SetMode.REMOVE_OPTIONAL_IF_NULL);
            assertTrue(isOptional);
            assertFalse((Boolean) hasMethod.invoke(record));
        } catch (InvocationTargetException exc) {
            assertFalse(isOptional);
            assertTrue(exc.getTargetException() instanceof IllegalArgumentException);
        }
        // SetMode.DISALLOW_NULL
        try {
            setModeMethod.invoke(record, null, SetMode.DISALLOW_NULL);
        } catch (InvocationTargetException exc) {
            assertTrue(exc.getTargetException() instanceof NullPointerException);
        }
        // restore original value
        if (prevValue != null) {
            result = setMethod.invoke(record, prevValue);
            assertSame(result, record);
            assertTrue((Boolean) hasMethod.invoke(record));
            assertEquals(getMethod.invoke(record), prevValue);
        }
    } catch (IllegalAccessException exc) {
        fail("Unexpected exception", exc);
    } catch (InvocationTargetException exc) {
        fail("Unexpected exception", exc);
    } catch (NoSuchMethodException exc) {
        fail("Unexpected exception", exc);
    }
}
Also used : RequiredFieldNotPresentException(com.linkedin.data.template.RequiredFieldNotPresentException) TestDataTemplateUtil(com.linkedin.data.template.TestDataTemplateUtil) DataTemplate(com.linkedin.data.template.DataTemplate) ByteString(com.linkedin.data.ByteString) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataMap(com.linkedin.data.DataMap)

Example 2 with RequiredFieldNotPresentException

use of com.linkedin.data.template.RequiredFieldNotPresentException in project rest.li by linkedin.

the class TestRecord method testIntField.

@Test
public void testIntField() {
    RecordTest record = new RecordTest();
    assertFalse(record.hasIntField());
    assertEquals(record.getIntField(GetMode.NULL), null);
    assertEquals(record.getIntField(GetMode.DEFAULT), null);
    Exception exc;
    try {
        exc = null;
        record.getIntField(GetMode.STRICT);
    } catch (Exception e) {
        exc = e;
    }
    assertTrue(exc != null);
    assertTrue(exc instanceof RequiredFieldNotPresentException);
    try {
        exc = null;
        record.getIntField();
    } catch (Exception e) {
        exc = e;
    }
    assertTrue(exc != null);
    assertTrue(exc instanceof RequiredFieldNotPresentException);
    Integer intValue = 13;
    record.setIntField(intValue);
    assertTrue(record.hasIntField());
    assertEquals(record.getIntField(GetMode.NULL), intValue);
    assertEquals(record.getIntField(GetMode.DEFAULT), intValue);
    assertEquals(record.getIntField(GetMode.STRICT), intValue);
    assertEquals(record.getIntField(), intValue);
}
Also used : RequiredFieldNotPresentException(com.linkedin.data.template.RequiredFieldNotPresentException) RequiredFieldNotPresentException(com.linkedin.data.template.RequiredFieldNotPresentException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.testng.annotations.Test)

Aggregations

RequiredFieldNotPresentException (com.linkedin.data.template.RequiredFieldNotPresentException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ByteString (com.linkedin.data.ByteString)1 DataMap (com.linkedin.data.DataMap)1 DataTemplate (com.linkedin.data.template.DataTemplate)1 TestDataTemplateUtil (com.linkedin.data.template.TestDataTemplateUtil)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 Test (org.testng.annotations.Test)1