Search in sources :

Example 1 with JasperTemplate

use of org.motechproject.mots.domain.JasperTemplate in project mots by motech-implementations.

the class JasperTemplateServiceTest method shouldThrowErrorIfFileEmpty.

@Test
public void shouldThrowErrorIfFileEmpty() throws Exception {
    expectedException.expect(ReportingException.class);
    expectedException.expectMessage(ERROR_REPORTING_FILE_EMPTY);
    MockMultipartFile file = new MockMultipartFile(NAME_OF_FILE, NAME_OF_FILE, "", new byte[0]);
    jasperTemplateService.validateFileAndInsertTemplate(new JasperTemplate(), file);
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) JasperTemplate(org.motechproject.mots.domain.JasperTemplate) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with JasperTemplate

use of org.motechproject.mots.domain.JasperTemplate 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));
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) JRParameter(net.sf.jasperreports.engine.JRParameter) InputStream(java.io.InputStream) JRExpression(net.sf.jasperreports.engine.JRExpression) JasperReport(net.sf.jasperreports.engine.JasperReport) JRPropertiesMap(net.sf.jasperreports.engine.JRPropertiesMap) Matchers.anyString(org.mockito.Matchers.anyString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) JasperTemplate(org.motechproject.mots.domain.JasperTemplate) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with JasperTemplate

use of org.motechproject.mots.domain.JasperTemplate in project mots by motech-implementations.

the class JasperTemplateServiceTest method shouldUpdateValidExistentTemplate.

@Test
public void shouldUpdateValidExistentTemplate() throws Exception {
    // given
    UUID oldId = UUID.randomUUID();
    JasperTemplate oldTemplate = new JasperTemplate();
    oldTemplate.setName(DISPLAY_NAME);
    oldTemplate.setId(oldId);
    given(jasperTemplateRepository.findByName(anyString())).willReturn(oldTemplate);
    // when
    template = testSaveTemplate(DISPLAY_NAME);
    // then
    assertEquals(template.getId(), oldId);
}
Also used : UUID(java.util.UUID) JasperTemplate(org.motechproject.mots.domain.JasperTemplate) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with JasperTemplate

use of org.motechproject.mots.domain.JasperTemplate in project mots by motech-implementations.

the class JasperTemplateServiceTest method testSaveTemplate.

private JasperTemplate testSaveTemplate(String name) throws ReportingException {
    JasperTemplateService service = spy(jasperTemplateService);
    MultipartFile file = mock(MultipartFile.class);
    String description = "description";
    // validating and saving file is checked by other tests
    doNothing().when(service).validateFileAndSaveTemplate(any(JasperTemplate.class), eq(file));
    // when
    JasperTemplate resultTemplate = service.saveTemplate(file, name, description);
    // then
    assertEquals(name, resultTemplate.getName());
    assertEquals(description, resultTemplate.getDescription());
    return resultTemplate;
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) Matchers.anyString(org.mockito.Matchers.anyString) JasperTemplate(org.motechproject.mots.domain.JasperTemplate)

Example 5 with JasperTemplate

use of org.motechproject.mots.domain.JasperTemplate in project mots by motech-implementations.

the class JasperTemplateServiceTest method shouldThrowErrorIfFileIsInvalid.

@Test
public void shouldThrowErrorIfFileIsInvalid() throws Exception {
    expectedException.expect(ReportingException.class);
    expectedException.expectMessage(ERROR_REPORTING_FILE_INVALID);
    jasperTemplateService.validateFileAndInsertTemplate(new JasperTemplate(), new MockMultipartFile(NAME_OF_FILE, NAME_OF_FILE, "", new byte[1]));
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) JasperTemplate(org.motechproject.mots.domain.JasperTemplate) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

JasperTemplate (org.motechproject.mots.domain.JasperTemplate)18 Test (org.junit.Test)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)7 Matchers.anyString (org.mockito.Matchers.anyString)4 MultipartFile (org.springframework.web.multipart.MultipartFile)4 InputStream (java.io.InputStream)3 JRParameter (net.sf.jasperreports.engine.JRParameter)3 JRPropertiesMap (net.sf.jasperreports.engine.JRPropertiesMap)3 JasperReport (net.sf.jasperreports.engine.JasperReport)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 JRExpression (net.sf.jasperreports.engine.JRExpression)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 UUID (java.util.UUID)1 JasperTemplateParameter (org.motechproject.mots.domain.JasperTemplateParameter)1 EntityNotFoundException (org.motechproject.mots.exception.EntityNotFoundException)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1