DDMFormEvaluatorHelper { protected Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> getDDMFormFieldsPropertyChanges() { return _ddmFormFieldsPropertyChanges; } DDMFormEvaluatorHelper(
		DDMFormEvaluatorEvaluateRequest ddmFormEvaluatorEvaluateRequest,
		DDMExpressionFactory ddmExpressionFactory,
		DDMFormFieldTypeServicesTracker ddmFormFieldTypeServicesTracker); DDMFormEvaluatorEvaluateResponse evaluate();  }
@Test public void testAllCondition() throws Exception { DDMForm ddmForm = new DDMForm(); DDMFormField ddmFormField0 = createDDMFormField( "field0", "text", FieldConstants.STRING); DDMFormField ddmFormField1 = createDDMFormField( "field1", "number", FieldConstants.DOUBLE); ddmFormField1.setRepeatable(true); ddmForm.addDDMFormField(ddmFormField0); ddmForm.addDDMFormField(ddmFormField1); ddmForm.addDDMFormRule( new DDMFormRule( Arrays.asList("setEnabled(\"field0\", false)"), "all('#value# <= 10', getValue('field1'))")); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue(""))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field1_0", "field1", new UnlocalizedValue("1"))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field1_1", "field1", new UnlocalizedValue("5"))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field1_2", "field1", new UnlocalizedValue("10"))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 1, ddmFormFieldsPropertyChanges.size()); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field0", "field0_instanceId")); Assert.assertTrue((boolean)ddmFormFieldPropertyChanges.get("readOnly")); }
@Test public void testBelongsToCondition() throws Exception { DDMForm ddmForm = new DDMForm(); DDMFormField ddmFormField0 = createDDMFormField( "field0", "text", FieldConstants.STRING); ddmForm.addDDMFormField(ddmFormField0); ddmForm.addDDMFormRule( new DDMFormRule( Arrays.asList("setEnabled(\"field0\", false)"), "belongsTo([\"Role1\"])")); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue(""))); Mockito.when( _roleLocalService.fetchRole( Matchers.anyLong(), Matchers.anyString()) ).thenReturn( _role ); Mockito.when( _role.getType() ).thenReturn( RoleConstants.TYPE_REGULAR ); Mockito.when( _userLocalService.hasRoleUser( Matchers.anyLong(), Matchers.eq("Role1"), Matchers.anyLong(), Matchers.eq(true)) ).thenReturn( true ); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 1, ddmFormFieldsPropertyChanges.size()); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field0", "field0_instanceId")); Assert.assertTrue((boolean)ddmFormFieldPropertyChanges.get("readOnly")); }
@Test public void testNotAllCondition() throws Exception { DDMForm ddmForm = new DDMForm(); DDMFormField ddmFormField0 = createDDMFormField( "field0", "text", FieldConstants.STRING); DDMFormField ddmFormField1 = createDDMFormField( "field1", "number", FieldConstants.DOUBLE); ddmFormField1.setRepeatable(true); ddmForm.addDDMFormField(ddmFormField0); ddmForm.addDDMFormField(ddmFormField1); ddmForm.addDDMFormRule( new DDMFormRule( Arrays.asList("setVisible(\"field0\", false)"), "not(all('between(#value#,2,6)', getValue('field1')))")); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue(""))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field1_0", "field1", new UnlocalizedValue("1"))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field1_1", "field1", new UnlocalizedValue("5"))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 1, ddmFormFieldsPropertyChanges.size()); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field0", "field0_instanceId")); Assert.assertFalse((boolean)ddmFormFieldPropertyChanges.get("visible")); }
@Test public void testNotBelongsToCondition() throws Exception { DDMForm ddmForm = new DDMForm(); DDMFormField ddmFormField0 = createDDMFormField( "field0", "text", FieldConstants.STRING); ddmForm.addDDMFormField(ddmFormField0); ddmForm.addDDMFormRule( new DDMFormRule( Arrays.asList("setVisible(\"field0\", false)"), "not(belongsTo([\"Role1\"]))")); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue(""))); Mockito.when( _userLocalService.hasRoleUser( _company.getCompanyId(), "Role1", _user.getUserId(), true) ).thenReturn( false ); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 1, ddmFormFieldsPropertyChanges.size()); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field0", "field0_instanceId")); Assert.assertFalse((boolean)ddmFormFieldPropertyChanges.get("visible")); }
@Test public void testRequiredValidationWithCheckboxField() throws Exception { DDMForm ddmForm = new DDMForm(); DDMFormField ddmFormField = createDDMFormField( "field0", "checkbox", FieldConstants.BOOLEAN); ddmFormField.setRequired(true); ddmForm.addDDMFormField(ddmFormField); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue("false"))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 1, ddmFormFieldsPropertyChanges.size()); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field0", "field0_instanceId")); Assert.assertEquals( "This field is required.", ddmFormFieldPropertyChanges.get("errorMessage")); Assert.assertFalse((boolean)ddmFormFieldPropertyChanges.get("valid")); }
@Test public void testRequiredValidationWithHiddenField() throws Exception { DDMForm ddmForm = new DDMForm(); ddmForm.addDDMFormField( createDDMFormField("field0", "text", FieldConstants.INTEGER)); DDMFormField field1DDMFormField = createDDMFormField( "field1", "text", FieldConstants.STRING); field1DDMFormField.setRequired(true); field1DDMFormField.setVisibilityExpression("field0 > 5"); ddmForm.addDDMFormField(field1DDMFormField); DDMFormValues ddmFormValues = new DDMFormValues(ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue("4"))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field1_instanceId", "field1", new UnlocalizedValue(""))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 1, ddmFormFieldsPropertyChanges.size()); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field1", "field1_instanceId")); Assert.assertNull(ddmFormFieldPropertyChanges.get("errorMessage")); Assert.assertNull(ddmFormFieldPropertyChanges.get("valid")); }
@Test public void testRequiredValidationWithinRuleAction() throws Exception { DDMForm ddmForm = new DDMForm(); DDMFormField ddmFormField0 = createDDMFormField( "field0", "text", FieldConstants.NUMBER); DDMFormField ddmFormField1 = createDDMFormField( "field1", "text", FieldConstants.STRING); ddmForm.addDDMFormField(ddmFormField0); ddmForm.addDDMFormField(ddmFormField1); ddmForm.addDDMFormRule( new DDMFormRule( Arrays.asList("setRequired(\"field1\", true)"), "getValue(\"field0\") > 10")); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue("11"))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field1_instanceId", "field1", new UnlocalizedValue(""))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 1, ddmFormFieldsPropertyChanges.size()); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field1", "field1_instanceId")); Assert.assertEquals( "This field is required.", ddmFormFieldPropertyChanges.get("errorMessage")); Assert.assertFalse((boolean)ddmFormFieldPropertyChanges.get("valid")); }
@Test public void testRequiredValidationWithTextField() throws Exception { DDMForm ddmForm = new DDMForm(); DDMFormField ddmFormField = createDDMFormField( "field0", "text", FieldConstants.STRING); ddmFormField.setRequired(true); ddmForm.addDDMFormField(ddmFormField); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue("\n"))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 1, ddmFormFieldsPropertyChanges.size()); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field0", "field0_instanceId")); Assert.assertEquals( "This field is required.", ddmFormFieldPropertyChanges.get("errorMessage")); Assert.assertFalse((boolean)ddmFormFieldPropertyChanges.get("valid")); }
@Test public void testRuleConditionWithNestedFunctions() throws Exception { DDMForm ddmForm = DDMFormTestUtil.createDDMForm(); ddmForm.addDDMFormField( createDDMFormField("field0", "numeric", FieldConstants.DOUBLE)); ddmForm.addDDMFormField( createDDMFormField("field1", "numeric", FieldConstants.DOUBLE)); ddmForm.addDDMFormRule( new DDMFormRule( "equals(sum(getValue('field0'), 10), 28)", "setValue('field1', getValue('field0'))")); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue("18"))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field1_instanceId", "field1", new UnlocalizedValue(""))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field1", "field1_instanceId")); Assert.assertEquals( new BigDecimal(18), ddmFormFieldPropertyChanges.get("value")); }
@Test public void testShowHideAndEnableDisableRules() throws Exception { DDMForm ddmForm = new DDMForm(); ddmForm.addDDMFormField( createDDMFormField("field0", "text", FieldConstants.DOUBLE)); ddmForm.addDDMFormField( createDDMFormField("field1", "text", FieldConstants.DOUBLE)); ddmForm.addDDMFormField( createDDMFormField("field2", "text", FieldConstants.DOUBLE)); ddmForm.addDDMFormRule( new DDMFormRule( Arrays.asList( "setVisible(\"field1\", false)", "setEnabled(\"field2\", false)"), "getValue(\"field0\") >= 30")); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue("30"))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field1_instanceId", "field1", new UnlocalizedValue("15"))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field2_instanceId", "field2", new UnlocalizedValue("10"))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 2, ddmFormFieldsPropertyChanges.size()); Assert.assertNull( ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field0", "field0_instanceId"))); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field1", "field1_instanceId")); Assert.assertEquals( ddmFormFieldPropertyChanges.toString(), 1, ddmFormFieldPropertyChanges.size()); Assert.assertFalse((boolean)ddmFormFieldPropertyChanges.get("visible")); ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey("field2", "field2_instanceId")); Assert.assertEquals( ddmFormFieldPropertyChanges.toString(), 1, ddmFormFieldPropertyChanges.size()); Assert.assertTrue((boolean)ddmFormFieldPropertyChanges.get("readOnly")); }
@Test public void testSumValuesForRepeatableField() throws Exception { DDMForm ddmForm = DDMFormTestUtil.createDDMForm(); DDMFormField ddmFormField0 = createDDMFormField( "field0", "numeric", FieldConstants.DOUBLE); ddmForm.addDDMFormField(ddmFormField0); DDMFormField ddmFormField1 = createDDMFormField( "field1", "numeric", FieldConstants.DOUBLE); ddmFormField1.setRepeatable(true); ddmForm.addDDMFormField(ddmFormField1); ddmForm.addDDMFormRule( new DDMFormRule( Arrays.asList("setValue('field0', sum(getValue('field1')))"), "TRUE")); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue(""))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field1_0", "field1", new UnlocalizedValue("1"))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field1_1", "field1", new UnlocalizedValue("1"))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field1_2", "field1", new UnlocalizedValue("2"))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field0", "field0_instanceId")); Assert.assertEquals( new BigDecimal(4), ddmFormFieldPropertyChanges.get("value")); }
@Test public void testUpdateAndCalculateRule() throws Exception { DDMForm ddmForm = new DDMForm(); ddmForm.addDDMFormField( createDDMFormField("field0", "numeric", FieldConstants.DOUBLE)); ddmForm.addDDMFormField( createDDMFormField("field1", "numeric", FieldConstants.DOUBLE)); ddmForm.addDDMFormField( createDDMFormField("field2", "numeric", FieldConstants.DOUBLE)); ddmForm.addDDMFormRule( new DDMFormRule( Arrays.asList( "calculate(\"field2\", getValue(\"field0\") * " + "getValue(\"field1\"))"), "getValue(\"field0\") > 0 && getValue(\"field1\") > 0")); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue("5"))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field1_instanceId", "field1", new UnlocalizedValue("2"))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field2_instanceId", "field2", new UnlocalizedValue("0"))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 1, ddmFormFieldsPropertyChanges.size()); Assert.assertNull( ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field0", "field0_instanceId"))); Assert.assertNull( ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field1", "field1_instanceId"))); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field2", "field2_instanceId")); Assert.assertEquals( ddmFormFieldPropertyChanges.toString(), 1, ddmFormFieldPropertyChanges.size()); Assert.assertEquals( ddmFormFieldPropertyChanges.toString(), new BigDecimal(10.0), ddmFormFieldPropertyChanges.get("value")); }
@Test public void testValidationExpression() throws Exception { DDMForm ddmForm = new DDMForm(); DDMFormField ddmFormField = createDDMFormField( "field0", "text", FieldConstants.INTEGER); DDMFormFieldValidation ddmFormFieldValidation = new DDMFormFieldValidation(); ddmFormFieldValidation.setDDMFormFieldValidationExpression( new DDMFormFieldValidationExpression() { { setName("equals"); setValue("field0=={parameter}"); } }); ddmFormFieldValidation.setErrorMessageLocalizedValue( DDMFormValuesTestUtil.createLocalizedValue( "This field should be zero.", LocaleUtil.US)); ddmFormFieldValidation.setParameterLocalizedValue( DDMFormValuesTestUtil.createLocalizedValue("0", LocaleUtil.US)); ddmFormField.setDDMFormFieldValidation(ddmFormFieldValidation); ddmForm.addDDMFormField(ddmFormField); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue("1"))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 1, ddmFormFieldsPropertyChanges.size()); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field0", "field0_instanceId")); Assert.assertEquals( "This field should be zero.", ddmFormFieldPropertyChanges.get("errorMessage")); Assert.assertFalse((boolean)ddmFormFieldPropertyChanges.get("valid")); }
@Test public void testValidationExpressionWithEmptyNumericField() throws Exception { DDMForm ddmForm = new DDMForm(); DDMFormField ddmFormField = createDDMFormField( "field0", "numeric", FieldConstants.INTEGER); DDMFormFieldValidation ddmFormFieldValidation = new DDMFormFieldValidation(); ddmFormFieldValidation.setDDMFormFieldValidationExpression( new DDMFormFieldValidationExpression() { { setName("lt"); setValue("field0<{parameter}"); } }); ddmFormFieldValidation.setErrorMessageLocalizedValue( DDMFormValuesTestUtil.createLocalizedValue( "This field should be less than zero.", LocaleUtil.US)); ddmFormFieldValidation.setParameterLocalizedValue( DDMFormValuesTestUtil.createLocalizedValue("0", LocaleUtil.US)); ddmFormField.setDDMFormFieldValidation(ddmFormFieldValidation); ddmForm.addDDMFormField(ddmFormField); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue(""))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 0, ddmFormFieldsPropertyChanges.size()); }
@Test public void testValidationExpressionWithNoErrorMessage() throws Exception { DDMForm ddmForm = new DDMForm(); DDMFormField ddmFormField = createDDMFormField( "field0", "numeric", FieldConstants.INTEGER); DDMFormFieldValidation ddmFormFieldValidation = new DDMFormFieldValidation(); ddmFormFieldValidation.setDDMFormFieldValidationExpression( new DDMFormFieldValidationExpression() { { setName("gt"); setValue("field0>{parameter}"); } }); ddmFormFieldValidation.setParameterLocalizedValue( DDMFormValuesTestUtil.createLocalizedValue("10", LocaleUtil.US)); ddmFormField.setDDMFormFieldValidation(ddmFormFieldValidation); ddmForm.addDDMFormField(ddmFormField); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue("1"))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 1, ddmFormFieldsPropertyChanges.size()); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field0", "field0_instanceId")); Assert.assertEquals( "This field is invalid.", ddmFormFieldPropertyChanges.get("errorMessage")); Assert.assertFalse((boolean)ddmFormFieldPropertyChanges.get("valid")); }
@Test public void testValidationForRepeatableField() throws Exception { DDMForm ddmForm = new DDMForm(); DDMFormField ddmFormField = createDDMFormField( "field0", "text", FieldConstants.STRING); DDMFormFieldValidation ddmFormFieldValidation = new DDMFormFieldValidation(); ddmFormFieldValidation.setDDMFormFieldValidationExpression( new DDMFormFieldValidationExpression() { { setName("contains"); setValue("NOT(contains(field0, \"{parameter}\"))"); } }); ddmFormFieldValidation.setErrorMessageLocalizedValue( DDMFormValuesTestUtil.createLocalizedValue( "This field should not contain zero.", LocaleUtil.US)); ddmFormFieldValidation.setParameterLocalizedValue( DDMFormValuesTestUtil.createLocalizedValue("0", LocaleUtil.US)); ddmFormField.setDDMFormFieldValidation(ddmFormFieldValidation); ddmForm.addDDMFormField(ddmFormField); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_0", "field0", new UnlocalizedValue("0"))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_1", "field0", new UnlocalizedValue("1"))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 2, ddmFormFieldsPropertyChanges.size()); Map<String, Object> ddmFormFieldPropertyChanges1 = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey("field0", "field0_0")); Map<String, Object> ddmFormFieldPropertyChanges2 = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey("field0", "field0_1")); Assert.assertEquals( "This field should not contain zero.", ddmFormFieldPropertyChanges1.get("errorMessage")); Assert.assertNull(ddmFormFieldPropertyChanges2.get("errorMessage")); Assert.assertFalse((boolean)ddmFormFieldPropertyChanges1.get("valid")); Assert.assertTrue((boolean)ddmFormFieldPropertyChanges2.get("valid")); }
@Test public void testValidationRule() throws Exception { DDMForm ddmForm = new DDMForm(); ddmForm.addDDMFormField( createDDMFormField("field0", "numeric", FieldConstants.DOUBLE)); ddmForm.addDDMFormRule( new DDMFormRule( Arrays.asList( "setInvalid(" + "\"field0\", \"The value should be greater than " + "10.\")"), "getValue(\"field0\") <= 10")); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue("5"))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 1, ddmFormFieldsPropertyChanges.size()); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field0", "field0_instanceId")); Assert.assertEquals( "The value should be greater than 10.", ddmFormFieldPropertyChanges.get("errorMessage")); Assert.assertFalse((boolean)ddmFormFieldPropertyChanges.get("valid")); }
@Test public void testVisibilityExpression() throws Exception { DDMForm ddmForm = new DDMForm(); ddmForm.addDDMFormField( createDDMFormField("field0", "text", FieldConstants.INTEGER)); DDMFormField field1DDMFormField = createDDMFormField( "field1", "text", FieldConstants.STRING); field1DDMFormField.setVisibilityExpression("field0 > 5"); ddmForm.addDDMFormField(field1DDMFormField); DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues( ddmForm); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field0_instanceId", "field0", new UnlocalizedValue("6"))); ddmFormValues.addDDMFormFieldValue( DDMFormValuesTestUtil.createDDMFormFieldValue( "field1_instanceId", "field1", new UnlocalizedValue(""))); DDMFormEvaluatorEvaluateResponse ddmFormEvaluatorEvaluateResponse = doEvaluate(ddmForm, ddmFormValues); Map<DDMFormEvaluatorFieldContextKey, Map<String, Object>> ddmFormFieldsPropertyChanges = ddmFormEvaluatorEvaluateResponse. getDDMFormFieldsPropertyChanges(); Assert.assertEquals( ddmFormFieldsPropertyChanges.toString(), 1, ddmFormFieldsPropertyChanges.size()); Map<String, Object> ddmFormFieldPropertyChanges = ddmFormFieldsPropertyChanges.get( new DDMFormEvaluatorFieldContextKey( "field1", "field1_instanceId")); Assert.assertTrue((boolean)ddmFormFieldPropertyChanges.get("visible")); }