CouchDbTimesheetService extends AbstractCouchDbService implements TimesheetService, TimesheetContextConstants { @Override public Timesheet updateExistingTimesheet(Timesheet timesheet, Map<String, Object> context) { if (!(timesheet instanceof TimesheetCouch)) { throw new IllegalArgumentException("Can only update Timesheets coming from the Couch!"); } try { JSONObject couchTs = getCouch(((TimesheetCouch) timesheet).getCouchId()); if (!(couchTs.getString("_rev").equals(((TimesheetCouch) timesheet).getCouchRev()))) { throw new BusinessException("Timesheet was updated by s/o else in the meantime!"); } JSONArray couchEntriesList = new JSONArray(); for (TimesheetDay day : timesheet.getDays()) { for (TimesheetDayEntry dayEntry : day.getEntries()) { if (dayEntry.getProject() == null || Math.abs(dayEntry.getEffectiveDurationHours()) < Float.MIN_NORMAL) { continue; } JSONObject couchEntry = new JSONObject(); couchEntry.put("dayInMonth", day.getDay()); couchEntry.put("project", dayEntry.getProject().getName()); couchEntry.put("hours", dayEntry.getHours()); couchEntriesList.put(couchEntry); } } couchTs.put("entries", couchEntriesList); JSONObject couchPutObj = putCouch(((TimesheetCouch) timesheet).getCouchId(), couchTs); if (!couchPutObj.getBoolean("ok")) { throw new TechnicalException("Saving failed: " + couchPutObj.toString()); } ((TimesheetCouch) timesheet).setCouchRev(couchPutObj.getString("rev")); return timesheet; } catch (JSONException e) { throw new TechnicalException(e); } catch (MalformedURLException e) { throw new TechnicalException(e); } catch (IOException e) { throw new TechnicalException(e); } } CouchDbTimesheetService(); @Override List<Timesheet> listAllTimesheet(Map<String, Object> context); @Override Timesheet addNewTimesheet(Timesheet timesheet, Map<String, Object> context); @Override Timesheet updateExistingTimesheet(Timesheet timesheet, Map<String, Object> context); @Override void removeExistingTimesheet(Timesheet timesheet,
			Map<String, Object> context); @Override long countTimesheet(TimesheetQuery query, Map<String, Object> context); @Override List<Timesheet> listTimesheet(TimesheetQuery query, Map<String, Object> context);  }
@Test public void testUpdateExistingTimesheet() throws JSONException { getResults .put("ts_sebastian_2014_07", "{\"_id\":\"ts_sebastian_2014_07\",\"_rev\":\"2-1983e046a14377c433c08711fbb57b28\", \"other\": 88,\"entries\":[{\"dayInMonth\":1,\"project\":\"P1\",\"hours\":\"2:31\"},{\"dayInMonth\":2,\"project\":\"P1\",\"hours\":\"1:11\"},{\"dayInMonth\":2,\"project\":\"P2\",\"hours\":\"2:22\"},{\"dayInMonth\":2,\"project\":\"P2\",\"hours\":\"3:33\"},{\"dayInMonth\":14,\"project\":\"P1\",\"hours\":\"1:00\"}]}"); putResults.put("ts_sebastian_2014_07", "{\"ok\": true, \"id\": \"ts_sebastian_2014_07\", \"rev\": \"3-123456789\"}"); TimesheetCouch ts = new TimesheetCouch("ts_sebastian_2014_07", "2-1983e046a14377c433c08711fbb57b28"); { TimesheetDay emptyDay = new TimesheetDay(); emptyDay.setDay(2); ts.addDay(emptyDay); } { TimesheetDay dayWithOne = new TimesheetDay(); dayWithOne.setDay(3); { TimesheetDayEntry normalEntry = new TimesheetDayEntry(); normalEntry.setProject(new Project("bla")); normalEntry.setHours("1:30"); dayWithOne.addEntry(normalEntry); } ts.addDay(dayWithOne); } { TimesheetDay dayWithSeveral = new TimesheetDay(); dayWithSeveral.setDay(14); { TimesheetDayEntry oneMinuteEntry = new TimesheetDayEntry(); oneMinuteEntry.setProject(new Project("bla")); oneMinuteEntry.setHours("00:01"); dayWithSeveral.addEntry(oneMinuteEntry); } { TimesheetDayEntry oneHourEntry = new TimesheetDayEntry(); oneHourEntry.setProject(new Project("blup")); oneHourEntry.setHours("01:00"); dayWithSeveral.addEntry(oneHourEntry); } { TimesheetDayEntry noProjectEntry = new TimesheetDayEntry(); noProjectEntry.setProject(null); noProjectEntry.setHours("01:00"); dayWithSeveral.addEntry(noProjectEntry); } { TimesheetDayEntry noTimeEntry = new TimesheetDayEntry(); noTimeEntry.setProject(new Project("blup")); noTimeEntry.setHours(null); dayWithSeveral.addEntry(noTimeEntry); } { TimesheetDayEntry zeroTimeEntry = new TimesheetDayEntry(); zeroTimeEntry.setProject(new Project("blup")); zeroTimeEntry.setHours("0:00"); dayWithSeveral.addEntry(zeroTimeEntry); } ts.addDay(dayWithSeveral); } Timesheet res = timesheetService.updateExistingTimesheet(ts, context); assertEquals(1, getUrls.size()); assertEquals(1, putUrls.size()); assertEquals(1, putContents.size()); assertEquals(TimesheetCouch.class, res.getClass()); assertEquals("ts_sebastian_2014_07", ((TimesheetCouch) res).getCouchId()); assertEquals("3-123456789", ((TimesheetCouch) res).getCouchRev()); JSONObject sentObj = new JSONObject(putContents.get(0)); assertEquals("ts_sebastian_2014_07", sentObj.getString("_id")); assertEquals("2-1983e046a14377c433c08711fbb57b28", sentObj.getString("_rev")); assertEquals(88, sentObj.getInt("other")); JSONArray sentEntries = sentObj.getJSONArray("entries"); assertNotNull(sentEntries); assertEquals(3, sentEntries.length()); assertEquals(3, sentEntries.getJSONObject(0).getInt("dayInMonth")); assertEquals("bla", sentEntries.getJSONObject(0).getString("project")); assertEquals("1:30", sentEntries.getJSONObject(0).getString("hours")); assertEquals(14, sentEntries.getJSONObject(1).getInt("dayInMonth")); assertEquals("bla", sentEntries.getJSONObject(1).getString("project")); assertEquals("00:01", sentEntries.getJSONObject(1).getString("hours")); assertEquals(14, sentEntries.getJSONObject(2).getInt("dayInMonth")); assertEquals("blup", sentEntries.getJSONObject(2).getString("project")); assertEquals("01:00", sentEntries.getJSONObject(2).getString("hours")); }
@Test public void testUpdateExistingTimesheetWrongResponse() { getResults .put("ts_sebastian_2014_07", "{\"_id\":\"ts_sebastian_2014_07\",\"_rev\":\"2-1983e046a14377c433c08711fbb57b28\", \"other\": 88,\"entries\":[{\"dayInMonth\":1,\"project\":\"P1\",\"hours\":\"2:31\"},{\"dayInMonth\":2,\"project\":\"P1\",\"hours\":\"1:11\"},{\"dayInMonth\":2,\"project\":\"P2\",\"hours\":\"2:22\"},{\"dayInMonth\":2,\"project\":\"P2\",\"hours\":\"3:33\"},{\"dayInMonth\":14,\"project\":\"P1\",\"hours\":\"1:00\"}]}"); putResults.put("ts_sebastian_2014_07", "{\"ok\": false, \"id\": \"ts_sebastian_2014_07\", \"rev\": \"3-123456789\"}"); TimesheetCouch ts = new TimesheetCouch("ts_sebastian_2014_07", "2-1983e046a14377c433c08711fbb57b28"); Exception exc = null; try { timesheetService.updateExistingTimesheet(ts, context); } catch (Exception e) { exc = e; } assertNotNull(exc); assertEquals(TechnicalException.class, exc.getClass()); assertTrue(exc.getMessage().contains("Saving failed")); assertTrue(exc.getMessage().contains("\"ok\":false")); }
@Test public void testUpdateExistingTimesheetWrongRevision() { Exception exc = null; try { getResults .put("ts_sebastian_2014_07", "{\"_id\":\"ts_sebastian_2014_07\",\"_rev\":\"2-1983e046a14377c433c08711fbb57b28\",\"entries\":[{\"dayInMonth\":1,\"project\":\"P1\",\"hours\":\"2:31\"},{\"dayInMonth\":2,\"project\":\"P1\",\"hours\":\"1:11\"},{\"dayInMonth\":2,\"project\":\"P2\",\"hours\":\"2:22\"},{\"dayInMonth\":2,\"project\":\"P2\",\"hours\":\"3:33\"},{\"dayInMonth\":14,\"project\":\"P1\",\"hours\":\"1:00\"}]}"); TimesheetCouch ts = new TimesheetCouch("ts_sebastian_2014_07", "1-abcdef"); timesheetService.updateExistingTimesheet(ts, context); } catch (Exception e) { exc = e; } assertNotNull(exc); assertEquals(BusinessException.class, exc.getClass()); assertEquals("Timesheet was updated by s/o else in the meantime!", exc.getMessage()); }
@Test(expected = IllegalArgumentException.class) public void testUpdateExistingTimesheetNoCouch() { timesheetService.updateExistingTimesheet(new Timesheet(), context); }