/*
 * Copyright (c) 2014 Lucas Holt
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

package com.justjournal.services;

import com.justjournal.Util;
import com.justjournal.model.Entry;
import com.justjournal.model.Statistics;
import com.justjournal.model.UserStatistics;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.util.List;

import static org.junit.Assert.*;

/**
 * @author Lucas Holt
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("file:src/test/resources/mvc-dispatcher-servlet.xml")
public class ServiceTests {

    private static StatisticsService statisticsService;
    private static EntryService entryService;

    public void setStatisticsService(StatisticsService statisticsService1) {
        this.statisticsService = statisticsService1;
    }

    public static void setEntryService(final EntryService entryService) {
        ServiceTests.entryService = entryService;
    }

    private static final String TEST_USER = "testuser";
    private static final int PUBLIC_ENTRY_ID = 33661;

    @BeforeClass
    public static void setup() throws Exception {
        Util.setupDb();
    }

    @Test
    public void entryGetPublicEntry() throws ServiceException {
        Entry entry = entryService.getPublicEntry(PUBLIC_ENTRY_ID, TEST_USER);
        assertNotNull(entry);
        assertEquals(PUBLIC_ENTRY_ID, entry.getId());
        assertEquals(TEST_USER, entry.getUser().getUsername());
        assertEquals(2, entry.getSecurity().getId());
    }

    @Test
    public void entryGetPublicEntries() throws ServiceException {
        List<Entry> entryList = entryService.getPublicEntries("testuser");
        assertNotNull(entryList);
        assertTrue(entryList.size() > 0);
    }

    @Test
    public void testGetStatistics() throws ServiceException {
        Statistics statistics = statisticsService.getStatistics();
        assertNotNull(statistics);
        assertTrue(statistics.getComments() > 0);
        assertTrue(statistics.getEntries() > 0);
        //assertTrue(statistics.getTags() > 0);
        // assertTrue(statistics.getStyles() > 0);
        // assertTrue(statistics.getPrivateEntries() > 0);
        assertTrue(statistics.getPublicEntries() > 0);
        // assertTrue(statistics.getFriendsEntries() > 0);
    }

    @Test
    public void testGetUserStatistics() throws ServiceException {
        UserStatistics statistics = statisticsService.getUserStatistics("testuser");
        assertNotNull(statistics);
        assertEquals("testuser", statistics.getUsername());
        assertTrue(statistics.getEntryCount() > 0);
        assertTrue(statistics.getCommentCount() > 0);
    }

    @Test
    public void testGetUserStatisticsBadUser() throws ServiceException {
        UserStatistics statistics = statisticsService.getUserStatistics("root");
        assertNull(statistics);
    }
}

@@ -1442,99 +1442,9 @@
 del.
-Entry;%0Aimport com.justjournal.model.Statistics;%0Aimport com.justjournal.model.UserStatistics
+*
 ;%0Aim
@@ -2005,129 +2005,118 @@
 tic 
-StatisticsService statisticsService;%0A    private static EntryService entryService;%0A%0A    public void setStatisticsService(
+final String TEST_USER = %22testuser%22;%0A    private static final int PUBLIC_ENTRY_ID = 33661;%0A    private static 
 Stat
@@ -2150,71 +2150,55 @@
 vice
-1) %7B
+;
 %0A    
-    this.statisticsService = statistics
+private static EntryService entry
 Service
-1
 ;
-%0A    %7D
 %0A%0A  
@@ -2334,204 +2334,230 @@
     
-private static final String TEST_USER = %22testuser%22;%0A    private static final int PUBLIC_ENTRY_ID = 33661;%0A%0A    @BeforeClass%0A    public static void setup() throws Exception %7B%0A        Util.setupDb()
+@BeforeClass%0A    public static void setup() throws Exception %7B%0A        Util.setupDb();%0A    %7D%0A%0A    public void setStatisticsService(StatisticsService statisticsService1) %7B%0A        this.statisticsService = statisticsService1
 ;%0A  
@@ -2953,30 +2953,281 @@
 oid entryGet
-Public
+RecentEntriesPublic() throws ServiceException %7B%0A        List%3CRecentEntry%3E entryList = entryService.getRecentEntriesPublic(TEST_USER);%0A        assertNotNull(entryList);%0A        assertTrue(entryList.size() %3E 0);%0A    %7D%0A%0A    @Test%0A    public void entryGetRecent
 Entries() th
@@ -3263,16 +3263,22 @@
    List%3C
+Recent
 Entry%3E e
@@ -3308,32 +3308,264 @@
 .get
-PublicEntries(%22testuser%22
+RecentEntries(TEST_USER);%0A        assertNotNull(entryList);%0A        assertTrue(entryList.size() %3E 0);%0A    %7D%0A%0A    @Test%0A    public void entryGetPublicEntries() throws ServiceException %7B%0A        List%3CEntry%3E entryList = entryService.getPublicEntries(TEST_USER
 );%0A 
@@ -4346,26 +4346,25 @@
 tistics(
-%22testuser%22
+TEST_USER
 );%0A     

create some unit tests around the new recent entries service