瀏覽代碼

Improved testing of reports.

peter_backlund 17 年之前
父節點
當前提交
f1f63f59f6

+ 6
- 1
dddsample/external/reporting/src/main/java/com.reporting/ReportingService.java 查看文件

@@ -4,6 +4,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
4 4
 import org.springframework.jdbc.core.RowCallbackHandler;
5 5
 import org.springframework.transaction.annotation.Transactional;
6 6
 import org.springframework.dao.EmptyResultDataAccessException;
7
+import org.springframework.dao.DataAccessException;
7 8
 
8 9
 import javax.sql.DataSource;
9 10
 import javax.ws.rs.GET;
@@ -83,7 +84,11 @@ public class ReportingService {
83 84
   private VoyageReport loadVoyageReport(String voyageNumber) {
84 85
     String[] args = {voyageNumber};
85 86
     String sql = "select * from voyage where voyage_number = ?";
86
-    return (VoyageReport) jdbc.queryForObject(sql, args, rowMapper);
87
+    try {
88
+      return (VoyageReport) jdbc.queryForObject(sql, args, rowMapper);
89
+    } catch (EmptyResultDataAccessException e) {
90
+      return null;
91
+    }
87 92
   }
88 93
 
89 94
   ReportingService() {

+ 13
- 12
dddsample/external/reporting/src/main/java/com.reporting/VoyageReport.java 查看文件

@@ -6,20 +6,12 @@ import java.util.Date;
6 6
 @XmlRootElement(name = "voyage")
7 7
 public class VoyageReport {
8 8
 
9
-  private Date lastUpdatedOn;
10 9
   private String voyageNumber;
11 10
   private String nextStop;
12
-  private Date etaNextStop;
11
+  private String etaNextStop;
13 12
   private String currentStatus;
14 13
   private int delayedByMinutes;
15
-
16
-  public Date getLastUpdatedOn() {
17
-    return lastUpdatedOn;
18
-  }
19
-
20
-  public void setLastUpdatedOn(Date lastUpdatedOn) {
21
-    this.lastUpdatedOn = lastUpdatedOn;
22
-  }
14
+  private String lastUpdatedOn;
23 15
 
24 16
   public String getVoyageNumber() {
25 17
     return voyageNumber;
@@ -37,11 +29,11 @@ public class VoyageReport {
37 29
     this.nextStop = nextStop;
38 30
   }
39 31
 
40
-  public Date getEtaNextStop() {
32
+  public String getEtaNextStop() {
41 33
     return etaNextStop;
42 34
   }
43 35
 
44
-  public void setEtaNextStop(Date etaNextStop) {
36
+  public void setEtaNextStop(String etaNextStop) {
45 37
     this.etaNextStop = etaNextStop;
46 38
   }
47 39
 
@@ -60,4 +52,13 @@ public class VoyageReport {
60 52
   public void setDelayedByMinutes(int delayedByMinutes) {
61 53
     this.delayedByMinutes = delayedByMinutes;
62 54
   }
55
+
56
+  public String getLastUpdatedOn() {
57
+    return lastUpdatedOn;
58
+  }
59
+
60
+  public void setLastUpdatedOn(String lastUpdatedOn) {
61
+    this.lastUpdatedOn = lastUpdatedOn;
62
+  }
63
+
63 64
 }

+ 4
- 2
dddsample/external/reporting/src/main/java/com.reporting/VoyageReportRowMapper.java 查看文件

@@ -5,6 +5,8 @@ import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
5 5
 import java.sql.ResultSet;
6 6
 import java.sql.SQLException;
7 7
 
8
+import static com.reporting.Constants.*;
9
+
8 10
 class VoyageReportRowMapper implements ParameterizedRowMapper<VoyageReport> {
9 11
   @Override
10 12
   public VoyageReport mapRow(ResultSet rs, int rowNum) throws SQLException {
@@ -12,8 +14,8 @@ class VoyageReportRowMapper implements ParameterizedRowMapper<VoyageReport> {
12 14
     voyageReport.setVoyageNumber(rs.getString("voyage_number"));
13 15
     voyageReport.setCurrentStatus(rs.getString("current_status"));
14 16
     voyageReport.setDelayedByMinutes(rs.getInt("delayed_by_min"));
15
-    voyageReport.setEtaNextStop(rs.getDate("eta_next_stop"));
16
-    voyageReport.setLastUpdatedOn(rs.getDate("last_updated_on"));
17
+    voyageReport.setEtaNextStop(US_DATETIME.format(rs.getTimestamp("eta_next_stop")));
18
+    voyageReport.setLastUpdatedOn(US_DATETIME.format(rs.getTimestamp("last_updated_on")));
17 19
     voyageReport.setNextStop(rs.getString("next_stop"));
18 20
     return voyageReport;
19 21
   }

+ 29
- 10
dddsample/external/reporting/src/test/java/com/reporting/ReportServiceTest.java 查看文件

@@ -21,7 +21,7 @@ import java.io.IOException;
21 21
 public class ReportServiceTest {
22 22
 
23 23
   @Test
24
-  public void deliveryReport() throws Exception {
24
+  public void cargoReport() throws Exception {
25 25
     JSONObject json = readJSON("/report/cargo/ABC");
26 26
     JSONObject cargo = json.getJSONObject("cargo");
27 27
     
@@ -44,6 +44,29 @@ public class ReportServiceTest {
44 44
     verifyHandling(handlings.getJSONObject(3), "Load", "Long Beach", "V0200");
45 45
   }
46 46
 
47
+  @Test
48
+  public void cargoNotFound() throws Exception {
49
+    assertEquals("", readString("/report/cargo/NOSUCH"));
50
+  }
51
+
52
+  @Test
53
+  public void voyageReport() throws Exception {
54
+    JSONObject json = readJSON("/report/voyage/V0200");
55
+    JSONObject voyage = json.getJSONObject("voyage");
56
+
57
+    assertEquals("V0200", voyage.get("voyageNumber"));
58
+    assertEquals("Seattle", voyage.get("nextStop"));
59
+    assertEquals("6/7/09 12:45 PM", voyage.get("etaNextStop"));
60
+    assertEquals("In transit", voyage.get("currentStatus"));
61
+    assertEquals(0, voyage.get("delayedByMinutes"));
62
+    assertEquals("6/6/09 2:01 PM", voyage.get("lastUpdatedOn"));
63
+  }
64
+
65
+  @Test
66
+  public void voyageNotFound() throws Exception {
67
+    assertEquals("", readString("/report/voyage/NOSUCH"));
68
+  }
69
+
47 70
   private void verifyHandling(JSONObject handling, String type, String location, String voyage) throws JSONException {
48 71
     assertEquals(type, handling.get("type"));
49 72
     assertEquals(location, handling.get("location"));
@@ -54,18 +77,14 @@ public class ReportServiceTest {
54 77
     }
55 78
   }
56 79
 
57
-  @Test
58
-  public void voyageReport() throws Exception {
59
-    JSONObject json = readJSON("/report/voyage/V0200");
60
-    JSONObject voyage = json.getJSONObject("voyage");
61
-
62
-    assertEquals("V0200", voyage.get("voyageNumber"));
80
+  private JSONObject readJSON(String path) throws IOException, JSONException {
81
+    String jsonString = readString(path);
82
+    return new JSONObject(jsonString);
63 83
   }
64 84
 
65
-  private JSONObject readJSON(String path) throws IOException, JSONException {
85
+  private String readString(String path) throws IOException {
66 86
     URLConnection urlConnection = new URL("http://localhost:14000" + path).openConnection();
67
-    String jsonString = IOUtils.toString(urlConnection.getInputStream());
68
-    return new JSONObject(jsonString);
87
+    return IOUtils.toString(urlConnection.getInputStream());
69 88
   }
70 89
 
71 90
 }

+ 7
- 9
dddsample/external/reporting/src/test/resources/testdata.sql 查看文件

@@ -39,14 +39,13 @@ insert into voyage (
39 39
   current_status,
40 40
   delayed_by_min,
41 41
   last_updated_on
42
-)
43
-values (
42
+) values (
44 43
   'V0100',
45 44
   'Honolulu',
46
-  '',
45
+  '2009-06-10 04:25:00.000',
47 46
   'In port',
48
-  '300',
49
-  ''
47
+  '1400',
48
+  '2009-06-06 14:01:22.331'
50 49
 );
51 50
 
52 51
 insert into voyage (
@@ -56,12 +55,11 @@ insert into voyage (
56 55
   current_status,
57 56
   delayed_by_min,
58 57
   last_updated_on
59
-)
60
-values (
58
+) values (
61 59
   'V0200',
62 60
   'Seattle',
63
-  '',
61
+  '2009-06-07 12:45:00.000',
64 62
   'In transit',
65 63
   '0',
66
-  ''
64
+  '2009-06-06 14:01:19.901'
67 65
 );