use of net.sf.jasperreports.engine.JRPropertiesMap in project mots by motech-implementations.
the class JasperTemplateServiceTest method shouldValidateFileAndSetDataIfDefaultValueExpressionIsNull.
@Test
public void shouldValidateFileAndSetDataIfDefaultValueExpressionIsNull() throws Exception {
MultipartFile file = mock(MultipartFile.class);
when(file.getOriginalFilename()).thenReturn(NAME_OF_FILE);
mockStatic(JasperCompileManager.class);
JasperReport report = mock(JasperReport.class);
InputStream inputStream = mock(InputStream.class);
when(file.getInputStream()).thenReturn(inputStream);
JRParameter param1 = mock(JRParameter.class);
JRParameter param2 = mock(JRParameter.class);
JRPropertiesMap propertiesMap = mock(JRPropertiesMap.class);
JRExpression jrExpression = mock(JRExpression.class);
String[] propertyNames = { DISPLAY_NAME };
when(report.getParameters()).thenReturn(new JRParameter[] { param1, param2 });
when(JasperCompileManager.compileReport(inputStream)).thenReturn(report);
when(propertiesMap.getPropertyNames()).thenReturn(propertyNames);
when(propertiesMap.getProperty(DISPLAY_NAME)).thenReturn(PARAM_DISPLAY_NAME);
when(param1.getPropertiesMap()).thenReturn(propertiesMap);
when(param1.getValueClassName()).thenReturn("java.lang.String");
when(param1.isForPrompting()).thenReturn(true);
when(param1.getDefaultValueExpression()).thenReturn(jrExpression);
when(jrExpression.getText()).thenReturn("text");
when(param2.getPropertiesMap()).thenReturn(propertiesMap);
when(param2.getValueClassName()).thenReturn("java.lang.Integer");
when(param2.isForPrompting()).thenReturn(true);
when(param2.getDefaultValueExpression()).thenReturn(null);
ByteArrayOutputStream byteOutputStream = mock(ByteArrayOutputStream.class);
whenNew(ByteArrayOutputStream.class).withAnyArguments().thenReturn(byteOutputStream);
ObjectOutputStream objectOutputStream = spy(new ObjectOutputStream(byteOutputStream));
whenNew(ObjectOutputStream.class).withArguments(byteOutputStream).thenReturn(objectOutputStream);
doNothing().when(objectOutputStream).writeObject(report);
byte[] byteData = new byte[1];
when(byteOutputStream.toByteArray()).thenReturn(byteData);
JasperTemplate jasperTemplate = new JasperTemplate();
jasperTemplateService.validateFileAndInsertTemplate(jasperTemplate, file);
verify(jasperTemplateRepository).save(jasperTemplate);
assertThat(jasperTemplate.getTemplateParameters().get(0).getDisplayName(), is(PARAM_DISPLAY_NAME));
}
use of net.sf.jasperreports.engine.JRPropertiesMap in project mots by motech-implementations.
the class JasperTemplateServiceTest method shouldThrowErrorIfDisplayNameOfParameterIsMissing.
@Test
public void shouldThrowErrorIfDisplayNameOfParameterIsMissing() throws Exception {
expectedException.expect(ReportingException.class);
expectedException.expectMessage(MessageFormat.format(ERROR_REPORTING_PARAMETER_MISSING, "displayName"));
MultipartFile file = mock(MultipartFile.class);
when(file.getOriginalFilename()).thenReturn(NAME_OF_FILE);
mockStatic(JasperCompileManager.class);
JasperReport report = mock(JasperReport.class);
InputStream inputStream = mock(InputStream.class);
when(file.getInputStream()).thenReturn(inputStream);
JRParameter param1 = mock(JRParameter.class);
JRParameter param2 = mock(JRParameter.class);
JRPropertiesMap propertiesMap = mock(JRPropertiesMap.class);
when(report.getParameters()).thenReturn(new JRParameter[] { param1, param2 });
when(JasperCompileManager.compileReport(inputStream)).thenReturn(report);
when(param1.getPropertiesMap()).thenReturn(propertiesMap);
when(param1.isForPrompting()).thenReturn(true);
when(param2.isForPrompting()).thenReturn(true);
String[] propertyNames = { "name1" };
when(propertiesMap.getPropertyNames()).thenReturn(propertyNames);
when(propertiesMap.getProperty(DISPLAY_NAME)).thenReturn(null);
JasperTemplate jasperTemplate = new JasperTemplate();
jasperTemplateService.validateFileAndInsertTemplate(jasperTemplate, file);
verify(jasperTemplateService, never()).saveWithParameters(jasperTemplate);
}
use of net.sf.jasperreports.engine.JRPropertiesMap in project mots by motech-implementations.
the class JasperTemplateServiceTest method shouldValidateFileAndSetData.
@Test
public void shouldValidateFileAndSetData() throws Exception {
MultipartFile file = mock(MultipartFile.class);
when(file.getOriginalFilename()).thenReturn(NAME_OF_FILE);
mockStatic(JasperCompileManager.class);
JasperReport report = mock(JasperReport.class);
InputStream inputStream = mock(InputStream.class);
when(file.getInputStream()).thenReturn(inputStream);
JRParameter param1 = mock(JRParameter.class);
JRParameter param2 = mock(JRParameter.class);
JRParameter param3 = mock(JRParameter.class);
JRPropertiesMap propertiesMap = mock(JRPropertiesMap.class);
JRExpression jrExpression = mock(JRExpression.class);
when(report.getProperty(REPORT_TYPE_PROPERTY)).thenReturn("test type");
when(report.getProperty(SUPPORTED_FORMATS_PROPERTY)).thenReturn("csv,xls");
when(report.getParameters()).thenReturn(new JRParameter[] { param1, param2, param3 });
when(JasperCompileManager.compileReport(inputStream)).thenReturn(report);
String[] propertyNames = { DISPLAY_NAME };
when(propertiesMap.getPropertyNames()).thenReturn(propertyNames);
when(propertiesMap.getProperty(DISPLAY_NAME)).thenReturn(PARAM_DISPLAY_NAME);
when(propertiesMap.getProperty(REQUIRED)).thenReturn("true");
when(propertiesMap.getProperty("options")).thenReturn("option 1,opt\\,ion 2");
when(param1.getPropertiesMap()).thenReturn(propertiesMap);
when(param1.getValueClassName()).thenReturn("java.lang.String");
when(param1.getName()).thenReturn("name");
when(param1.isForPrompting()).thenReturn(true);
when(param1.getDescription()).thenReturn("desc");
when(param1.getDefaultValueExpression()).thenReturn(jrExpression);
when(jrExpression.getText()).thenReturn("text");
when(param2.getPropertiesMap()).thenReturn(propertiesMap);
when(param2.getValueClassName()).thenReturn("java.lang.Integer");
when(param2.getName()).thenReturn("name");
when(param2.isForPrompting()).thenReturn(true);
when(param2.getDescription()).thenReturn("desc");
when(param2.getDefaultValueExpression()).thenReturn(jrExpression);
when(param3.getValueClassName()).thenReturn("java.awt.Image");
when(param3.isForPrompting()).thenReturn(false);
when(param3.isSystemDefined()).thenReturn(false);
when(param3.getName()).thenReturn(IMAGE_NAME);
ByteArrayOutputStream byteOutputStream = mock(ByteArrayOutputStream.class);
whenNew(ByteArrayOutputStream.class).withAnyArguments().thenReturn(byteOutputStream);
ObjectOutputStream objectOutputStream = spy(new ObjectOutputStream(byteOutputStream));
whenNew(ObjectOutputStream.class).withArguments(byteOutputStream).thenReturn(objectOutputStream);
doNothing().when(objectOutputStream).writeObject(report);
byte[] byteData = new byte[1];
when(byteOutputStream.toByteArray()).thenReturn(byteData);
JasperTemplate jasperTemplate = new JasperTemplate();
jasperTemplateService.validateFileAndInsertTemplate(jasperTemplate, file);
verify(jasperTemplateRepository).save(jasperTemplate);
assertEquals("test type", jasperTemplate.getType());
assertThat(jasperTemplate.getSupportedFormats(), hasSize(2));
assertThat(jasperTemplate.getSupportedFormats(), hasItems("csv", "xls"));
assertThat(jasperTemplate.getTemplateParameters().get(0).getDisplayName(), is(PARAM_DISPLAY_NAME));
assertThat(jasperTemplate.getTemplateParameters().get(0).getDescription(), is("desc"));
assertThat(jasperTemplate.getTemplateParameters().get(0).getName(), is("name"));
assertThat(jasperTemplate.getTemplateParameters().get(0).getRequired(), is(true));
assertThat(jasperTemplate.getTemplateParameters().get(0).getOptions(), contains("option 1", "opt,ion 2"));
}
Aggregations