CheckboxMultipleDDMFormFieldTypeReportProcessor implements DDMFormFieldTypeReportProcessor { @Override public JSONObject process( DDMFormFieldValue ddmFormFieldValue, JSONObject fieldJSONObject, long formInstanceRecordId, String ddmFormInstanceReportEvent) throws Exception { JSONObject valuesJSONObject = fieldJSONObject.getJSONObject("values"); Value value = ddmFormFieldValue.getValue(); JSONArray valueJSONArray = JSONFactoryUtil.createJSONArray( value.getString(value.getDefaultLocale())); Iterator<String> iterator = valueJSONArray.iterator(); while (iterator.hasNext()) { String key = iterator.next(); int count = valuesJSONObject.getInt(key, 0); if (ddmFormInstanceReportEvent.equals( DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION)) { count++; } else if (ddmFormInstanceReportEvent.equals( DDMFormInstanceReportConstants. EVENT_DELETE_RECORD_VERSION)) { count--; } valuesJSONObject.put(key, 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.CHECKBOX_MULTIPLE ); Value value = new LocalizedValue(); value.addString( value.getDefaultLocale(), JSONUtil.put( "option1" ).toString()); value.setDefaultLocale(LocaleUtil.US); when( ddmFormFieldValue.getValue() ).thenReturn( value ); JSONObject processedFieldJSONObject = _checkboxMultipleDDMFormFieldTypeReportProcessor.process( ddmFormFieldValue, JSONUtil.put( "type", DDMFormFieldType.CHECKBOX_MULTIPLE ).put( "values", JSONUtil.put("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.CHECKBOX_MULTIPLE ); Value value = new LocalizedValue(); value.addString( value.getDefaultLocale(), JSONUtil.put( "option1" ).toString()); value.setDefaultLocale(LocaleUtil.US); when( ddmFormFieldValue.getValue() ).thenReturn( value ); JSONObject processedFieldJSONObject = _checkboxMultipleDDMFormFieldTypeReportProcessor.process( ddmFormFieldValue, JSONUtil.put( "type", DDMFormFieldType.CHECKBOX_MULTIPLE ).put( "values", JSONFactoryUtil.createJSONObject() ), 0, DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION); Assert.assertEquals( DDMFormFieldType.CHECKBOX_MULTIPLE, processedFieldJSONObject.getString("type")); JSONObject valuesJSONObject = processedFieldJSONObject.getJSONObject( "values"); Assert.assertEquals(1, valuesJSONObject.getLong("option1")); }
@Test public void testProcessDDMFormInstanceReportWithExistingData() throws Exception { DDMFormFieldValue ddmFormFieldValue = mock(DDMFormFieldValue.class); when( ddmFormFieldValue.getName() ).thenReturn( "field1" ); when( ddmFormFieldValue.getType() ).thenReturn( DDMFormFieldType.CHECKBOX_MULTIPLE ); Value value = new LocalizedValue(); value.addString( value.getDefaultLocale(), JSONUtil.putAll( "option1", "option2" ).toString()); value.setDefaultLocale(LocaleUtil.US); when( ddmFormFieldValue.getValue() ).thenReturn( value ); JSONObject processedFieldJSONObject = _checkboxMultipleDDMFormFieldTypeReportProcessor.process( ddmFormFieldValue, JSONUtil.put( "type", DDMFormFieldType.CHECKBOX_MULTIPLE ).put( "values", JSONUtil.put("option1", 1) ), 0, DDMFormInstanceReportConstants.EVENT_ADD_RECORD_VERSION); JSONObject valuesJSONObject = processedFieldJSONObject.getJSONObject( "values"); Assert.assertEquals(2, valuesJSONObject.getLong("option1")); Assert.assertEquals(1, valuesJSONObject.getLong("option2")); }