DDMFormInstanceRecordExporterImpl implements DDMFormInstanceRecordExporter { @Override public DDMFormInstanceRecordExporterResponse export( DDMFormInstanceRecordExporterRequest ddmFormInstanceRecordExporterRequest) throws FormInstanceRecordExporterException { long ddmFormInstanceId = ddmFormInstanceRecordExporterRequest.getDDMFormInstanceId(); int status = ddmFormInstanceRecordExporterRequest.getStatus(); int start = ddmFormInstanceRecordExporterRequest.getStart(); int end = ddmFormInstanceRecordExporterRequest.getEnd(); OrderByComparator<DDMFormInstanceRecord> orderByComparator = ddmFormInstanceRecordExporterRequest.getOrderByComparator(); Locale locale = ddmFormInstanceRecordExporterRequest.getLocale(); String type = ddmFormInstanceRecordExporterRequest.getType(); DDMFormInstanceRecordExporterResponse.Builder builder = DDMFormInstanceRecordExporterResponse.Builder.newBuilder(); try { List<DDMFormInstanceRecord> ddmFormInstanceRecords = ddmFormInstanceRecordLocalService.getFormInstanceRecords( ddmFormInstanceId, status, start, end, orderByComparator); Map<String, DDMFormField> ddmFormFields = getDistinctFields( ddmFormInstanceId); byte[] content = write( type, getDDMFormFieldsLabel(ddmFormFields, locale), getDDMFormFieldValues( ddmFormFields, ddmFormInstanceRecords, locale)); builder = builder.withContent(content); } catch (Exception exception) { throw new FormInstanceRecordExporterException(exception); } return builder.build(); }  @Override DDMFormInstanceRecordExporterResponse export(
			DDMFormInstanceRecordExporterRequest
				ddmFormInstanceRecordExporterRequest);  }
@Test public void testExport() throws Exception { DDMFormInstanceRecordExporterImpl ddmFormInstanceRecordExporterImpl = mock(DDMFormInstanceRecordExporterImpl.class); ddmFormInstanceRecordExporterImpl.ddmFormInstanceRecordLocalService = _ddmFormInstanceRecordLocalService; DDMFormInstanceRecordExporterRequest.Builder builder = DDMFormInstanceRecordExporterRequest.Builder.newBuilder(1, "csv"); OrderByComparator<DDMFormInstanceRecord> orderByComparator = mock( OrderByComparator.class); Locale locale = new Locale("pt", "BR"); DDMFormInstanceRecordExporterRequest ddmFormInstanceRecordExporterRequest = builder.withStatus( WorkflowConstants.STATUS_APPROVED ).withLocale( locale ).withStart( 1 ).withEnd( 5 ).withOrderByComparator( orderByComparator ).build(); List<DDMFormInstanceRecord> ddmFormInstanceRecords = Collections.emptyList(); when( _ddmFormInstanceRecordLocalService.getFormInstanceRecords( 1, WorkflowConstants.STATUS_APPROVED, 1, 5, orderByComparator) ).thenReturn( ddmFormInstanceRecords ); Map<String, DDMFormField> ddmFormFields = Collections.emptyMap(); when( ddmFormInstanceRecordExporterImpl.getDistinctFields(1) ).thenReturn( ddmFormFields ); Map<String, String> ddmFormFieldsLabel = Collections.emptyMap(); when( ddmFormInstanceRecordExporterImpl.getDDMFormFieldsLabel( ddmFormFields, locale) ).thenReturn( ddmFormFieldsLabel ); List<Map<String, String>> ddmFormFieldsValues = Collections.emptyList(); when( ddmFormInstanceRecordExporterImpl.getDDMFormFieldValues( ddmFormFields, ddmFormInstanceRecords, locale) ).thenReturn( ddmFormFieldsValues ); when( ddmFormInstanceRecordExporterImpl.write( "csv", ddmFormFieldsLabel, ddmFormFieldsValues) ).thenReturn( new byte[] {1, 2, 3} ); when( ddmFormInstanceRecordExporterImpl.export( ddmFormInstanceRecordExporterRequest) ).thenCallRealMethod(); DDMFormInstanceRecordExporterResponse ddmFormInstanceRecordExporterResponse = ddmFormInstanceRecordExporterImpl.export( ddmFormInstanceRecordExporterRequest); Assert.assertArrayEquals( new byte[] {1, 2, 3}, ddmFormInstanceRecordExporterResponse.getContent()); InOrder inOrder = Mockito.inOrder( _ddmFormInstanceRecordLocalService, ddmFormInstanceRecordExporterImpl); inOrder.verify( _ddmFormInstanceRecordLocalService, Mockito.times(1) ).getFormInstanceRecords( 1, WorkflowConstants.STATUS_APPROVED, 1, 5, orderByComparator ); inOrder.verify( ddmFormInstanceRecordExporterImpl, Mockito.times(1) ).getDistinctFields( 1 ); inOrder.verify( ddmFormInstanceRecordExporterImpl, Mockito.times(1) ).getDDMFormFieldsLabel( ddmFormFields, locale ); inOrder.verify( ddmFormInstanceRecordExporterImpl, Mockito.times(1) ).getDDMFormFieldValues( ddmFormFields, ddmFormInstanceRecords, locale ); inOrder.verify( ddmFormInstanceRecordExporterImpl, Mockito.times(1) ).write( "csv", ddmFormFieldsLabel, ddmFormFieldsValues ); }
@Test(expected = FormInstanceRecordExporterException.class) public void testExportCatchException() throws Exception { DDMFormInstanceRecordExporterImpl ddmFormInstanceRecordExporterImpl = new DDMFormInstanceRecordExporterImpl(); ddmFormInstanceRecordExporterImpl.ddmFormInstanceRecordLocalService = _ddmFormInstanceRecordLocalService; when( _ddmFormInstanceRecordLocalService.getFormInstanceRecords( Matchers.anyLong(), Matchers.anyInt(), Matchers.anyInt(), Matchers.anyInt(), Matchers.any(OrderByComparator.class)) ).thenThrow( Exception.class ); DDMFormInstanceRecordExporterRequest.Builder builder = DDMFormInstanceRecordExporterRequest.Builder.newBuilder(1, "csv"); ddmFormInstanceRecordExporterImpl.export(builder.build()); }