RadioDDMFormFieldTypeReportProcessor implements DDMFormFieldTypeReportProcessor { @Override public JSONObject process( DDMFormFieldValue ddmFormFieldValue, JSONObject fieldJSONObject, long formInstanceRecordId, String ddmFormInstanceReportEvent) throws Exception { Value value = ddmFormFieldValue.getValue(); String valueString = value.getString(value.getDefaultLocale()); if (Validator.isNotNull(valueString)) { JSONObject valuesJSONObject = fieldJSONObject.getJSONObject( "values"); int count = valuesJSONObject.getInt(valueString, 0); if (ddmFormInstanceReportEvent.equals( DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION)) { count++; } else if (ddmFormInstanceReportEvent.equals( DDMFormInstanceReportConstants. EVENT_DELETE_RECORD_VERSION)) { count--; } valuesJSONObject.put(valueString, count); } return fieldJSONObject; }  @Override JSONObject process(
			DDMFormFieldValue ddmFormFieldValue, JSONObject fieldJSONObject,
			long formInstanceRecordId, String ddmFormInstanceReportEvent);  }
@Test public void testProcessDDMFormInstanceReportOnDeleteEvent() throws Exception { DDMFormFieldValue ddmFormFieldValue = mock(DDMFormFieldValue.class); when( ddmFormFieldValue.getName() ).thenReturn( "field1" ); when( ddmFormFieldValue.getType() ).thenReturn( DDMFormFieldType.RADIO ); Value value = new LocalizedValue(); value.addString(value.getDefaultLocale(), "option1"); value.setDefaultLocale(LocaleUtil.US); when( ddmFormFieldValue.getValue() ).thenReturn( value ); JSONObject processedFieldJSONObject = _radioDDMFormFieldTypeReportProcessor.process( ddmFormFieldValue, JSONUtil.put( "type", DDMFormFieldType.RADIO ).put( "values", JSONFactoryUtil.createJSONObject("{option1 : 1}") ), 0, DDMFormInstanceReportConstants.EVENT_DELETE_RECORD_VERSION); JSONObject valuesJSONObject = processedFieldJSONObject.getJSONObject( "values"); Assert.assertEquals(0, valuesJSONObject.getLong("option1")); }
@Test public void testProcessDDMFormInstanceReportWithEmptyData() throws Exception { DDMFormFieldValue ddmFormFieldValue = mock(DDMFormFieldValue.class); when( ddmFormFieldValue.getName() ).thenReturn( "field1" ); when( ddmFormFieldValue.getType() ).thenReturn( DDMFormFieldType.RADIO ); Value value = new LocalizedValue(); value.addString(value.getDefaultLocale(), "option1"); value.setDefaultLocale(LocaleUtil.US); when( ddmFormFieldValue.getValue() ).thenReturn( value ); JSONObject processedFieldJSONObject = _radioDDMFormFieldTypeReportProcessor.process( ddmFormFieldValue, JSONUtil.put( "type", DDMFormFieldType.RADIO ).put( "values", JSONFactoryUtil.createJSONObject() ), 0, DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION); Assert.assertEquals( DDMFormFieldType.RADIO, processedFieldJSONObject.getString("type")); JSONObject valuesJSONObject = processedFieldJSONObject.getJSONObject( "values"); Assert.assertEquals(1, valuesJSONObject.getLong("option1")); }
@Test public void testProcessDDMFormInstanceReportWithEmptyField() throws Exception { DDMFormFieldValue ddmFormFieldValue = mock(DDMFormFieldValue.class); when( ddmFormFieldValue.getName() ).thenReturn( "field1" ); when( ddmFormFieldValue.getType() ).thenReturn( DDMFormFieldType.RADIO ); Value value = new LocalizedValue(); value.addString(value.getDefaultLocale(), ""); value.setDefaultLocale(LocaleUtil.US); when( ddmFormFieldValue.getValue() ).thenReturn( value ); JSONObject processedFieldJSONObject = _radioDDMFormFieldTypeReportProcessor.process( ddmFormFieldValue, JSONUtil.put( "type", DDMFormFieldType.RADIO ).put( "values", JSONFactoryUtil.createJSONObject() ), 0, DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION); Assert.assertEquals( DDMFormFieldType.RADIO, processedFieldJSONObject.getString("type")); JSONObject valuesJSONObject = processedFieldJSONObject.getJSONObject( "values"); Assert.assertFalse(valuesJSONObject.has("")); }
@Test public void testProcessDDMFormInstanceReportWithExistingData() throws Exception { DDMFormFieldValue ddmFormFieldValue = mock(DDMFormFieldValue.class); when( ddmFormFieldValue.getName() ).thenReturn( "field1" ); when( ddmFormFieldValue.getType() ).thenReturn( DDMFormFieldType.RADIO ); Value value = new LocalizedValue(); value.addString(value.getDefaultLocale(), "option1"); value.setDefaultLocale(LocaleUtil.US); when( ddmFormFieldValue.getValue() ).thenReturn( value ); JSONObject processedFieldJSONObject = _radioDDMFormFieldTypeReportProcessor.process( ddmFormFieldValue, JSONUtil.put( "type", DDMFormFieldType.RADIO ).put( "values", JSONFactoryUtil.createJSONObject("{option1 : 1}") ), 0, DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION); JSONObject valuesJSONObject = processedFieldJSONObject.getJSONObject( "values"); Assert.assertEquals(2, valuesJSONObject.getLong("option1")); }