View Javadoc
1   package org.itracker.services;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.itracker.model.Report;
6   import org.itracker.persistence.dao.ReportDAO;
7   import org.junit.Test;
8   import org.springframework.dao.DataAccessException;
9   
10  import java.text.DateFormat;
11  import java.text.ParseException;
12  import java.text.SimpleDateFormat;
13  import java.util.List;
14  
15  import static org.itracker.Assert.*;
16  public class ReportServiceImplIT extends AbstractServicesIntegrationTest {
17  
18      private static final Logger log = Logger.getLogger(ReportServiceImplIT.class);
19  
20      private ReportDAO reportDAO;
21  
22      /**
23       * Object to be Tested: ReportService
24       */
25      private ReportService reportService;
26  
27      @Test
28      public void testGetAllReports() {
29          // defined in reportbean_dataset.xml
30          try {
31              List<Report> reports = reportService.getAllReports();
32  
33              assertNotNull("reports ", reports);
34              assertEquals("reports size ", 1, reports.size());
35              Report report = reports.get(0);
36              assertEquals("id", 1000, report.getId().intValue());
37              assertEquals("name", "DailyReport Report", report.getName());
38              assertEquals("nameKey", "0001", report.getNameKey());
39              assertEquals("description", "This is a daily report", report.getDescription());
40              DateFormat format = new SimpleDateFormat("yy-MM-dd hh:mm:ss");
41              try {
42                  assertEquals("createDate", format.parse("2004-01-01 13:00:00"), report.getCreateDate());
43                  assertEquals("modified date", format.parse("2005-01-01 15:00:00"), report.getLastModifiedDate());
44              } catch (ParseException e) {
45                  log.error("testGetAllReports: failed to parse date for assertion", e);
46                  fail("failed to parse date for assertion: " + e.getMessage());
47              }
48              assertNotNull("contents", report.getFileData());
49              assertTrue("contents.size", report.getFileData().length > 1);
50  
51          } catch (Exception e) {
52              log.error("testGetAllReports: failed to getAllReports in ReportService", e);
53              fail("failed to getAllReports: " + e.getMessage());
54          }
55  
56      }
57  
58      @Test
59      public void testGetNumberReports() {
60          try {
61              // defined in reportbean_dataset.xml
62              assertEquals("reports size", 1, reportService.getNumberReports());
63          } catch (Exception e) {
64              log.error("testGetNumberReports: failed to getNumberReports in ReportService", e);
65              fail("failed to getNumberReports: " + e.getMessage());
66          }
67      }
68  
69      @Test
70      public void testGetReportDAO() {
71          assertNotNull("getReportDAO", reportService.getReportDAO());
72      }
73  
74      @Test
75      public void testCreateReport() {
76          //test save
77          Report report = new Report();
78          report.setName("weekly report");
79          report.setNameKey("0002");
80          report.setDescription("This is a weekly report");
81          report.setClassName("xxx.xxx.Weekly");
82          report.setFileData(new byte[]{1,2,3});
83          Report result = reportService.createReport(report);
84          Report reportFind = reportDAO.findByPrimaryKey(result.getId());
85          assertNotNull(reportFind);
86          assertEquals("id", result.getId(), reportFind.getId());
87          assertEquals("name", "weekly report", reportFind.getName());
88  
89          //test update
90          report.setName("monthly report");
91          Report result1 = reportService.createReport(report);
92          reportFind = reportDAO.findByPrimaryKey(result1.getId());
93          assertEquals("name", "monthly report", reportFind.getName());
94          for (int b : new byte[]{1,2,3}) {
95              assertEquals("fileData byte " + b, b, reportFind.getFileData()[b-1]);
96          }
97  
98          //test null
99          try {
100             report = null;
101             reportService.createReport(report);
102             fail("do not throw DataAccessException ");
103         } catch (DataAccessException e) {
104             assertTrue(true);
105         }
106 
107     }
108 
109 
110     @Override
111     public void onSetUp() throws Exception {
112         super.onSetUp();
113         reportService = (ReportService) applicationContext.getBean("reportService");
114         this.reportDAO = (ReportDAO) applicationContext.getBean("reportDAO");
115 
116     }
117 
118     protected String[] getDataSetFiles() {
119         return new String[]{
120                 "dataset/reportbean_dataset.xml"
121         };
122     }
123 
124 }