@Test public void shouldValidateFileAndSetData() throws Exception { String[] propertyNames = {"displayName"}; MultipartFile file = mock(MultipartFile.class); when(file.getOriginalFilename()).thenReturn("file.jrxml"); 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); when(report.getParameters()).thenReturn(new JRParameter[]{param1, param2}); when(JasperCompileManager.compileReport(inputStream)).thenReturn(report); when(propertiesMap.getPropertyNames()).thenReturn(propertyNames); when(propertiesMap.getProperty("displayName")).thenReturn("Param Display Name"); when(param1.getPropertiesMap()).thenReturn(propertiesMap); when(param1.getValueClassName()).thenReturn("String"); when(param1.getName()).thenReturn("name"); when(param1.getDescription()).thenReturn("desc"); when(param1.getDefaultValueExpression()).thenReturn(jrExpression); when(jrExpression.getText()).thenReturn("text"); when(param2.getPropertiesMap()).thenReturn(propertiesMap); when(param2.getValueClassName()).thenReturn("Integer"); when(param2.getName()).thenReturn("name"); when(param2.getDescription()).thenReturn("desc"); when(param2.getDefaultValueExpression()).thenReturn(jrExpression); 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); Template template = new Template(); Right right = new Right(); ReportRight reportRight = new ReportRight(); whenNew(Right.class).withAnyArguments().thenReturn(right); whenNew(ReportRight.class).withArguments(template, right).thenReturn(reportRight); service.validateFileAndInsertTemplate(template, file); verify(repository).insertWithParameters(template); verify(rightService).insertRight(right); verify(reportRightService).insert(reportRight); assertThat(template.getParameters().get(0).getDisplayName(), is("Param Display Name")); assertThat(template.getParameters().get(0).getDescription(), is("desc")); assertThat(template.getParameters().get(0).getName(), is("name")); assertThat(template.getParameters().get(0).getCreatedBy(), is(template.getCreatedBy())); }
public void validateFileAndInsertTemplate(Template template, MultipartFile file) throws IOException { throwIfTemplateWithSameNameAlreadyExists(template.getName()); validateFile(template, file); repository.insertWithParameters(template); Right right = new Right(template.getName(), REPORTING); rightService.insertRight(right); ReportRight reportRight = new ReportRight(template, right); reportRightService.insert(reportRight); }
TemplateService { public void validateFileAndInsertTemplate(Template template, MultipartFile file) throws IOException { throwIfTemplateWithSameNameAlreadyExists(template.getName()); validateFile(template, file); repository.insertWithParameters(template); Right right = new Right(template.getName(), REPORTING); rightService.insertRight(right); ReportRight reportRight = new ReportRight(template, right); reportRightService.insert(reportRight); } }
TemplateService { public void validateFileAndInsertTemplate(Template template, MultipartFile file) throws IOException { throwIfTemplateWithSameNameAlreadyExists(template.getName()); validateFile(template, file); repository.insertWithParameters(template); Right right = new Right(template.getName(), REPORTING); rightService.insertRight(right); ReportRight reportRight = new ReportRight(template, right); reportRightService.insert(reportRight); }  }
TemplateService { public void validateFileAndInsertTemplate(Template template, MultipartFile file) throws IOException { throwIfTemplateWithSameNameAlreadyExists(template.getName()); validateFile(template, file); repository.insertWithParameters(template); Right right = new Right(template.getName(), REPORTING); rightService.insertRight(right); ReportRight reportRight = new ReportRight(template, right); reportRightService.insert(reportRight); }  List<Template> getAllTemplatesForUser(Long userId); Template getByName(String name); Template getById(Long id); Template getLWById(Long id); void validateFileAndInsertTemplate(Template template, MultipartFile file); Map<String, Object> getParametersMap(Template template, int userId, HttpServletRequest request, String format); }
TemplateService { public void validateFileAndInsertTemplate(Template template, MultipartFile file) throws IOException { throwIfTemplateWithSameNameAlreadyExists(template.getName()); validateFile(template, file); repository.insertWithParameters(template); Right right = new Right(template.getName(), REPORTING); rightService.insertRight(right); ReportRight reportRight = new ReportRight(template, right); reportRightService.insert(reportRight); }  List<Template> getAllTemplatesForUser(Long userId); Template getByName(String name); Template getById(Long id); Template getLWById(Long id); void validateFileAndInsertTemplate(Template template, MultipartFile file); Map<String, Object> getParametersMap(Template template, int userId, HttpServletRequest request, String format); static final String PDF_VIEW; static final String USER_ID_PARAM; }