// run this before each test starts
  protected void onSetUpBeforeTransaction() throws Exception {
    // Time
    /*long oneMonthAgoMs = new Date().getTime() - (30 * 24 * 60 * 60 * 1000);
    long twoMonthAgoMs = new Date().getTime() - (2 * 30 * 24 * 60 * 60 * 1000);
    Time timeA = createMock(Time.class);
    expect(timeA.getTime()).andReturn(oneMonthAgoMs).anyTimes();
    replay(timeA);
    Time timeB = createMock(Time.class);
    expect(timeB.getTime()).andReturn(twoMonthAgoMs).anyTimes();
    replay(timeB);*/

    // Site Service
    M_ss = createMock(SiteService.class);

    // null site
    expect(M_ss.getSite(null)).andThrow(new IdUnusedException("null")).anyTimes();
    expect(M_ss.getSite("non_existent_site"))
        .andThrow(new IdUnusedException("non_existent_site"))
        .anyTimes();

    // My Workspace - user sites
    FakeSite userSiteA = new FakeSite("~" + FakeData.USER_A_ID);
    FakeSite userSiteB = new FakeSite("~" + FakeData.USER_B_ID);
    expect(M_ss.getSiteUserId(FakeData.USER_A_ID)).andStubReturn("~" + FakeData.USER_A_ID);
    expect(M_ss.getSiteUserId(FakeData.USER_B_ID)).andStubReturn("~" + FakeData.USER_B_ID);
    expect(M_ss.getSiteUserId("no_user")).andStubReturn(null);
    expect(M_ss.getSite("~" + FakeData.USER_A_ID)).andStubReturn(userSiteA);
    expect(M_ss.getSite("~" + FakeData.USER_B_ID)).andStubReturn(userSiteB);

    // Site A has tools {SiteStats, Chat, Resources}, has {user-a,user-b}, created 1 month ago
    Site siteA =
        new FakeSite(
            FakeData.SITE_A_ID,
            Arrays.asList(
                StatsManager.SITESTATS_TOOLID, FakeData.TOOL_CHAT, StatsManager.RESOURCES_TOOLID));
    ((FakeSite) siteA)
        .setUsers(new HashSet<String>(Arrays.asList(FakeData.USER_A_ID, FakeData.USER_B_ID)));
    ((FakeSite) siteA)
        .setMembers(new HashSet<String>(Arrays.asList(FakeData.USER_A_ID, FakeData.USER_B_ID)));
    expect(M_ss.getSite(FakeData.SITE_A_ID)).andStubReturn(siteA);
    expect(M_ss.isUserSite(FakeData.SITE_A_ID)).andStubReturn(false);
    expect(M_ss.isSpecialSite(FakeData.SITE_A_ID)).andStubReturn(false);

    // Site B has tools {TOOL_CHAT}, has {user-a}, created 2 months ago
    FakeSite siteB = new FakeSite(FakeData.SITE_B_ID, FakeData.TOOL_CHAT);
    ((FakeSite) siteB).setUsers(new HashSet<String>(Arrays.asList(FakeData.USER_A_ID)));
    ((FakeSite) siteB).setMembers(new HashSet<String>(Arrays.asList(FakeData.USER_A_ID)));
    expect(M_ss.getSite(FakeData.SITE_B_ID)).andStubReturn(siteB);
    expect(M_ss.isUserSite(FakeData.SITE_B_ID)).andStubReturn(false);
    expect(M_ss.isSpecialSite(FakeData.SITE_B_ID)).andStubReturn(false);

    // Site 'non_existent_site' doesn't exist
    expect(M_ss.isUserSite("non_existent_site")).andStubReturn(false);
    expect(M_ss.isSpecialSite("non_existent_site")).andStubReturn(false);

    // Content Hosting Service
    //		M_chs = createMock(ContentHostingService.class);
    //		M_chs.checkCollection("/group/site-a-id/folder/");
    //		expectLastCall().anyTimes();
    //		M_chs.checkResource("/group-user/site-a-id/user-a/resource1");
    //		expectLastCall().anyTimes();

    ResourceLoader msgs = createMock(ResourceLoader.class);
    expect(msgs.getString((String) anyObject()))
        .andStubAnswer(
            new IAnswer<String>() {
              public String answer() throws Throwable {
                return (String) getCurrentArguments()[0];
              }
            });

    // apply
    replay(M_ss);
    // replay(M_chs);
    replay(msgs);
    ((FakeEventRegistryService) M_ers).setSiteService(M_ss);
    ((FakeEventRegistryService) M_ers).setToolManager(M_tm);
    ((FakeEventRegistryService) M_ers).setStatsManager(M_sm);
    ((ReportManagerImpl) M_rm).setEventRegistryService(M_ers);
    ((StatsManagerImpl) M_sm).setSiteService(M_ss);
    // ((StatsManagerImpl)M_sm).setContentHostingService(M_chs);
    ((StatsManagerImpl) M_sm).setResourceLoader(msgs);
    ((StatsManagerImpl) M_sm).setEnableSitePresences(true);
    ((ReportManagerImpl) M_rm).setResourceLoader(msgs);
    ((StatsUpdateManagerImpl) M_sum).setSiteService(M_ss);
    ((StatsUpdateManagerImpl) M_sum).setStatsManager(M_sm);
  }
  public void testReportsFromOverviewPage() {
    M_sum.collectEvents(getSampleData2());

    // MiniStatsVisits & MiniStatUniqueVisits
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      rp.setWhat(ReportManager.WHAT_VISITS_TOTALS);
      rp.setWhen(ReportManager.WHEN_ALL);
      rp.setWho(ReportManager.WHO_ALL);
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_DATE);
      totalsBy.add(StatsManager.T_VISITS);
      totalsBy.add(StatsManager.T_UNIQUEVISITS);
      rp.setHowTotalsBy(totalsBy);
      rp.setHowSort(true);
      rp.setHowSortBy(StatsManager.T_DATE);
      rp.setHowSortAscending(false);
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_BOTH);
      rp.setHowChartType(StatsManager.CHARTTYPE_TIMESERIESBAR);
      rp.setHowChartSource(StatsManager.T_DATE);
      rp.setHowChartSeriesSource(StatsManager.T_NONE);
      rp.setHowChartSeriesPeriod(StatsManager.CHARTTIMESERIES_DAY);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(5, rep.getReportData().size());
    }

    // MiniStatEnrolledUsersWithVisits
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      rp.setWhat(ReportManager.WHAT_VISITS);
      rp.setWhen(ReportManager.WHEN_ALL);
      rp.setWho(ReportManager.WHO_ALL);
      // grouping
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_USER);
      rp.setHowTotalsBy(totalsBy);
      // sorting
      rp.setHowSort(false);
      // chart
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(2, rep.getReportData().size());
    }

    // MiniStatEnrolledUsersWithoutVisits
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      rp.setWhat(ReportManager.WHAT_VISITS);
      rp.setWhen(ReportManager.WHEN_ALL);
      rp.setWho(ReportManager.WHO_NONE);
      // grouping
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_USER);
      rp.setHowTotalsBy(totalsBy);
      // sorting
      rp.setHowSort(false);
      // chart
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(0, rep.getReportData().size());
    }

    // MiniStatActivityEvents
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      // what
      rp.setWhat(ReportManager.WHAT_EVENTS);
      rp.setWhatEventSelType(ReportManager.WHAT_EVENTS_BYEVENTS);
      rp.setWhatEventIds(new ArrayList<String>(M_ers.getEventIds()));
      // when
      rp.setWhen(ReportManager.WHEN_ALL);
      // who
      rp.setWho(ReportManager.WHO_ALL);
      // grouping
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_EVENT);
      rp.setHowTotalsBy(totalsBy);
      // sorting
      rp.setHowSort(true);
      rp.setHowSortBy(StatsManager.T_EVENT);
      rp.setHowSortAscending(true);
      // chart
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(6, rep.getReportData().size());
    }

    // MiniStatMostActiveUser
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      // what
      rp.setWhat(ReportManager.WHAT_EVENTS);
      rp.setWhatEventSelType(ReportManager.WHAT_EVENTS_BYEVENTS);
      rp.setWhatEventIds(new ArrayList<String>(M_ers.getEventIds()));
      // when
      rp.setWhen(ReportManager.WHEN_ALL);
      // who
      rp.setWho(ReportManager.WHO_ALL);
      // grouping
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_USER);
      rp.setHowTotalsBy(totalsBy);
      // sorting
      rp.setHowSort(true);
      rp.setHowSortBy(StatsManager.T_TOTAL);
      rp.setHowSortAscending(false);
      // chart
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(3, rep.getReportData().size());
    }

    // MiniStatFiles (files with new event)
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      // what
      rp.setWhat(ReportManager.WHAT_RESOURCES);
      rp.setWhatLimitedAction(true);
      rp.setWhatResourceAction(ReportManager.WHAT_RESOURCES_ACTION_NEW);
      rp.setWhatLimitedResourceIds(true);
      rp.setWhatResourceIds(Arrays.asList(StatsManager.RESOURCES_DIR + FakeData.SITE_A_ID + "/"));
      // when
      rp.setWhen(ReportManager.WHEN_ALL);
      // who
      rp.setWho(ReportManager.WHO_ALL);
      // grouping
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_RESOURCE);
      rp.setHowTotalsBy(totalsBy);
      // sorting
      rp.setHowSort(true);
      rp.setHowSortBy(StatsManager.T_RESOURCE);
      rp.setHowSortAscending(true);
      // chart
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(2, rep.getReportData().size());
    }

    // MiniStatOpenedFiles (files with read event)
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      // what
      rp.setWhat(ReportManager.WHAT_RESOURCES);
      rp.setWhatLimitedAction(true);
      rp.setWhatResourceAction(ReportManager.WHAT_RESOURCES_ACTION_READ);
      rp.setWhatLimitedResourceIds(true);
      rp.setWhatResourceIds(Arrays.asList(StatsManager.RESOURCES_DIR + FakeData.SITE_A_ID + "/"));
      // when
      rp.setWhen(ReportManager.WHEN_ALL);
      // who
      rp.setWho(ReportManager.WHO_ALL);
      // grouping
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_RESOURCE);
      rp.setHowTotalsBy(totalsBy);
      // sorting
      rp.setHowSort(true);
      rp.setHowSortBy(StatsManager.T_TOTAL);
      rp.setHowSortAscending(false);
      // chart
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(2, rep.getReportData().size());
    }

    // MiniStatUserThatOpenedMoreFiles
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      // what
      rp.setWhat(ReportManager.WHAT_RESOURCES);
      rp.setWhatLimitedAction(true);
      rp.setWhatResourceAction(ReportManager.WHAT_RESOURCES_ACTION_READ);
      rp.setWhatLimitedResourceIds(true);
      rp.setWhatResourceIds(Arrays.asList(StatsManager.RESOURCES_DIR + FakeData.SITE_A_ID + "/"));
      // when
      rp.setWhen(ReportManager.WHEN_ALL);
      // who
      rp.setWho(ReportManager.WHO_ALL);
      // grouping
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_USER);
      rp.setHowTotalsBy(totalsBy);
      // sorting
      rp.setHowSort(true);
      rp.setHowSortBy(StatsManager.T_TOTAL);
      rp.setHowSortAscending(false);
      // chart
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(2, rep.getReportData().size());
    }
  }