@Before public void setup() { mockAccountDao = mock(AccountDao.class); mockFormDao = mock(FormDao.class); mockFormFieldDao = mock(FormFieldDao.class); mockAccountService = mock(AccountService.class); account = new Account(); account.setId(1L); form = new Form(); form.setAccount(account); form.setFields(new ArrayList<FormField>()); field = new FormField(); field.setForm(form); form.getFields().add(field); when(mockAccountDao.findByUsernameOrEmail("user")).thenReturn(account); when(mockFormDao.findById(1L)).thenReturn(form); when(mockFormFieldDao.findById(1L)).thenReturn(field); formService = new FormService(); formService.setAccountDao(mockAccountDao); formService.setMapper(mapper); formService.setFormDao(mockFormDao); formService.setFormFieldDao(mockFormFieldDao); formService.setAccountService(mockAccountService); }
@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()); }
/** * 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 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 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 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&ns_source=PublicRSS20-sa"", 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()); }