@Test
  public void testAddCollaborator() {
    River river = riverDao.findById(1L);
    Account account = accountDao.findByUsernameOrEmail("user4");

    riverDao.addCollaborator(river, account, true);
    em.flush();

    String sql =
        "SELECT river_id, account_id, collaborator_active, read_only FROM river_collaborators WHERE river_id = ? AND account_id = ?";
    Map<String, Object> results = this.jdbcTemplate.queryForMap(sql, 1L, 6L);

    assertEquals(false, results.get("collaborator_active"));
    assertEquals(true, results.get("read_only"));
  }
  @Test
  public void findCollaboratorByAccount() {
    RiverCollaborator rc = riverDao.findCollaborator(1L, 4L);

    assertEquals(1L, (long) rc.getId());
    assertEquals(4L, rc.getAccount().getId());
  }
  @Test
  public void getDropsSince() {
    Account account = accountDao.findById(1L);
    DropFilter filter = new DropFilter();
    filter.setSinceId(3L);
    List<Drop> drops = riverDao.getDrops(1L, filter, 1, 1, account);

    assertEquals(1, drops.size());

    Drop drop = drops.get(0);
    assertTrue(drop.getId() > 3);
    assertEquals(false, drop.getRead());
    assertEquals("twitter", drop.getChannel());
    assertEquals("droplet 4 title", drop.getTitle());
    assertEquals("droplet 4 content", drop.getContent());
    assertEquals("4", drop.getOriginalId());
    assertEquals(25, drop.getCommentCount());
    assertEquals(2, drop.getIdentity().getId());
    assertEquals("identity2_name", drop.getIdentity().getName());
    assertEquals("identity2_avatar", drop.getIdentity().getAvatar());
    assertNotNull(drop.getOriginalUrl());
    assertEquals(3, drop.getOriginalUrl().getId());
    assertEquals("http://www.bbc.co.uk/nature/20273855", drop.getOriginalUrl().getUrl());

    assertEquals(1, drop.getTags().size());
    Tag tag = drop.getTags().get(0);
    assertEquals(1, tag.getId());
    assertEquals("Jeremy Hunt", tag.getTag());
    assertEquals("person", tag.getType());

    assertEquals(1, drop.getLinks().size());
    Link link = drop.getLinks().get(0);
    assertEquals(10, link.getId());
    assertEquals("http://www.bbc.co.uk/sport/0/football/20319573", link.getUrl());

    assertEquals(1, drop.getMedia().size());
    Media media = drop.getMedia().get(0);
    assertEquals(1, media.getId());
    assertEquals(
        "http://gigaom2.files.wordpress.com/2012/10/datacapspercentage.jpeg", media.getUrl());
    assertEquals("image", media.getType());

    assertEquals(2, media.getThumbnails().size());
    MediaThumbnail thumbnail = media.getThumbnails().get(0);
    assertEquals(
        "https://2bcbd22fbb0a02d76141-1680e9dfed1be27cdc47787ec5d4ef89.ssl.cf1.rackcdn.com/625dd7cb656d258b4effb325253e880631699d80345016e9e755b4a04341cda1.peg",
        thumbnail.getUrl());
    assertEquals(80, thumbnail.getSize());

    assertEquals(1, drop.getPlaces().size());
    Place place = drop.getPlaces().get(0);
    assertEquals(1, place.getId());
    assertEquals("Wales", place.getPlaceName());
    assertEquals(new Float(146.11), place.getLongitude());
    assertEquals(new Float(-33), place.getLatitude());
  }
  @Test
  public void updateRiver() {
    River river = riverDao.findById(2L);
    river.setRiverName("updated river name");
    river.setDescription("updated description");
    river.setRiverPublic(true);

    riverDao.update(river);
    em.flush();

    String sql =
        "SELECT river_name, river_name_canonical, description, river_public FROM rivers WHERE id = ?";
    Map<String, Object> results = this.jdbcTemplate.queryForMap(sql, 2);
    assertEquals("updated river name", (String) results.get("river_name"));
    assertEquals(
        TextUtil.getURLSlug("updated river name"), (String) results.get("river_name_canonical"));
    assertEquals("updated description", (String) results.get("description"));
    assertEquals(true, results.get("river_public"));
  }
  /**
   * Tests drop fetch where the dateFrom and dateTo are the same
   *
   * @throws Exception
   */
  @Test
  public void getDropsForDate() throws Exception {
    Account account = accountDao.findById(1L);

    DropFilter filter = new DropFilter();
    filter.setDateFrom(dateFormat.parse("15/11/2012"));
    filter.setDateTo(dateFormat.parse("15/11/2012"));

    List<Drop> drops = riverDao.getDrops(1L, filter, 1, 10, account);
    assertEquals(3, drops.size());
  }
  @Test
  public void getDropsForChannelName() {
    Account account = accountDao.findById(3L);

    List<String> channels = new ArrayList<String>();
    channels.add("rss");
    DropFilter filter = new DropFilter();
    filter.setChannels(channels);
    List<Drop> drops = riverDao.getDrops(1L, filter, 1, 10, account);

    assertEquals(3, drops.size());
  }
  @Test
  public void getUnreadDrops() {
    Account account = accountDao.findById(3L);

    DropFilter filter = new DropFilter();
    filter.setRead(false);
    List<Drop> drops = riverDao.getDrops(1L, filter, 1, 10, account);

    assertEquals(3, drops.size());

    Drop drop = drops.get(1);
    assertEquals(3, drop.getId());
  }
  @Test
  public void getDropsFromDate() throws Exception {
    Account account = accountDao.findById(1L);

    DropFilter filter = new DropFilter();
    filter.setDateFrom(dateFormat.parse("01/01/2013"));
    List<Drop> drops = riverDao.getDrops(1L, filter, 1, 10, account);

    assertEquals(1, drops.size());

    Drop drop = drops.get(0);
    assertEquals(5, drop.getId());
  }
  @Test
  public void getReadDropsSince() {
    Account account = accountDao.findById(3L);

    DropFilter filter = new DropFilter();
    filter.setRead(true);
    filter.setSinceId(3L);
    List<Drop> drops = riverDao.getDrops(1L, filter, 1, 10, account);

    assertEquals(1, drops.size());

    Drop drop = drops.get(0);
    assertEquals(5, drop.getId());
  }
  @Test
  public void getDropsForChannelId() {
    Account account = accountDao.findById(1L);

    ArrayList<Long> channels = new ArrayList<Long>();
    channels.add(1L);
    DropFilter filter = new DropFilter();
    filter.setChannelIds(channels);
    List<Drop> drops = riverDao.getDrops(1L, filter, 1, 10, account);

    assertEquals(2, drops.size());

    Drop drop = drops.get(0);
    assertEquals(3, drop.getId());
  }
  @Test
  public void testModifyCollaborator() {
    RiverCollaborator collaborator = riverCollaboratorDao.findById(1L);
    collaborator.setActive(false);
    collaborator.setReadOnly(true);
    riverDao.updateCollaborator(collaborator);
    em.flush();

    String sql =
        "SELECT river_id, account_id, collaborator_active, read_only FROM river_collaborators WHERE id = ?";
    Map<String, Object> results = this.jdbcTemplate.queryForMap(sql, 1L);

    assertEquals(false, results.get("collaborator_active"));
    assertEquals(true, results.get("read_only"));
  }
  @Test
  public void getDropsSinceForChannelName() {
    Account account = accountDao.findById(3L);

    List<String> channels = new ArrayList<String>();
    channels.add("twitter");
    DropFilter filter = new DropFilter();
    filter.setChannels(channels);
    filter.setSinceId(3L);
    List<Drop> drops = riverDao.getDrops(1L, filter, 1, 10, account);

    assertEquals(1, drops.size());

    Drop drop = drops.get(0);
    assertTrue(drop.getId() > 3);
  }
  @Test
  public void testCreateRiver() {
    River river = new River();
    Account account = accountDao.findByUsernameOrEmail("user1");

    river.setRiverName("Test river");
    river.setDescription("test description");
    river.setAccount(account);
    river.setRiverPublic(false);

    riverDao.create(river);

    assertNotNull(river.getId());
    String sql =
        "SELECT account_id, river_name, description, river_public, river_name_canonical FROM rivers WHERE id = ?";

    Map<String, Object> results = this.jdbcTemplate.queryForMap(sql, river.getId());
    assertEquals("Test river", (String) results.get("river_name"));
    assertEquals(3L, ((Number) results.get("account_id")).longValue());
    assertEquals(TextUtil.getURLSlug("Test river"), (String) results.get("river_name_canonical"));
    assertEquals("test description", (String) results.get("description"));
    assertEquals(false, results.get("river_public"));
  }
  @Test
  public void getDrops() {
    Account account = accountDao.findById(1L);
    DropFilter filter = new DropFilter();
    List<Drop> drops = riverDao.getDrops(1L, filter, 1, 10, account);

    assertEquals(5, drops.size());

    Drop drop = drops.get(0);
    assertEquals(5, drop.getId());
    assertEquals(false, drop.getRead());
    assertEquals("rss", drop.getChannel());
    assertEquals("droplet 5 title", drop.getTitle());
    assertEquals("droplet 5 content", drop.getContent());
    assertEquals("5", drop.getOriginalId());
    assertEquals(30, drop.getCommentCount());
    assertEquals(1, drop.getIdentity().getId());
    assertEquals("identity1_name", drop.getIdentity().getName());
    assertEquals("identity1_avatar", drop.getIdentity().getAvatar());
    assertNotNull(drop.getOriginalUrl());
    assertEquals(3, drop.getOriginalUrl().getId());
    assertEquals("http://www.bbc.co.uk/nature/20273855", drop.getOriginalUrl().getUrl());

    assertEquals(2, drop.getTags().size());
    Tag tag = drop.getTags().get(0);
    assertEquals(1, tag.getId());
    assertEquals("Jeremy Hunt", tag.getTag());
    assertEquals("person", tag.getType());

    assertEquals(2, drop.getLinks().size());
    Link link = drop.getLinks().get(0);
    assertEquals(2, link.getId());
    assertEquals(
        "http://news.bbc.co.uk/democracylive/hi/house_of_commons/newsid_9769000/9769109.stm#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa&quot;",
        link.getUrl());

    assertEquals(2, drop.getMedia().size());
    Media media = drop.getMedia().get(0);
    assertEquals(1, media.getId());
    assertEquals(
        "http://gigaom2.files.wordpress.com/2012/10/datacapspercentage.jpeg", media.getUrl());
    assertEquals("image", media.getType());

    assertEquals(2, media.getThumbnails().size());
    MediaThumbnail thumbnail = media.getThumbnails().get(0);
    assertEquals(
        "https://2bcbd22fbb0a02d76141-1680e9dfed1be27cdc47787ec5d4ef89.ssl.cf1.rackcdn.com/625dd7cb656d258b4effb325253e880631699d80345016e9e755b4a04341cda1.peg",
        thumbnail.getUrl());
    assertEquals(80, thumbnail.getSize());

    assertEquals(2, drop.getPlaces().size());
    Place place = drop.getPlaces().get(0);
    assertEquals(1, place.getId());
    assertEquals("Wales", place.getPlaceName());
    assertEquals(new Float(146.11), place.getLongitude());
    assertEquals(new Float(-33), place.getLatitude());

    drop = drops.get(3);
    assertEquals(2, drop.getId());
    assertEquals(2, drop.getForms().size());

    RiverDropForm form = (RiverDropForm) drop.getForms().get(0);
    assertEquals(1, (long) form.getId());
    List<RiverDropFormField> values = form.getValues();
    assertEquals(3, values.size());
    assertEquals(1, (long) values.get(0).getField().getId());
    assertEquals("[\"English\"]", values.get(0).getValue());
    assertEquals(2, (long) values.get(1).getField().getId());
    assertEquals("\"Journalist\"", values.get(1).getValue());
    assertEquals(3, (long) values.get(2).getField().getId());
    assertEquals("\"Kenyans\"", values.get(2).getValue());
  }
  @Test
  public void findNonExistentByName() {
    River r = riverDao.findByName("River That Doesn't Exist");

    assertNull(r);
  }
  @Test
  public void findByName() {
    River r = riverDao.findByName("Private River 1");

    assertEquals(2, (long) r.getId());
  }
  @Test
  public void findById() {
    River r = riverDao.findById(1L);

    assertEquals("Public River 1", r.getRiverName());
  }
  @Test
  public void findNonExistentCollaboratorByAccount() {
    RiverCollaborator rc = riverDao.findCollaborator(1L, 10L);

    assertNull(rc);
  }