CalendarWindows { public static MonthsWindows months(int number) { return new MonthsWindows(number, 1, DEFAULT_START_DATE, DateTimeZone.UTC); }  static DaysWindows days(int number); static DaysWindows weeks(int number, int startDayOfWeek); static MonthsWindows months(int number); static YearsWindows years(int number);  }
@Test public void testMonths() throws Exception { Map<IntervalWindow, Set<String>> expected = new HashMap<>(); final List<Long> timestamps = Arrays.asList( makeTimestamp(2014, 1, 1, 0, 0).getMillis(), makeTimestamp(2014, 1, 31, 5, 5).getMillis(), makeTimestamp(2014, 2, 1, 0, 0).getMillis(), makeTimestamp(2014, 2, 15, 5, 5).getMillis(), makeTimestamp(2015, 1, 1, 0, 0).getMillis(), makeTimestamp(2015, 1, 31, 5, 5).getMillis()); expected.put( new IntervalWindow(makeTimestamp(2014, 1, 1, 0, 0), makeTimestamp(2014, 2, 1, 0, 0)), set(timestamps.get(0), timestamps.get(1))); expected.put( new IntervalWindow(makeTimestamp(2014, 2, 1, 0, 0), makeTimestamp(2014, 3, 1, 0, 0)), set(timestamps.get(2), timestamps.get(3))); expected.put( new IntervalWindow(makeTimestamp(2015, 1, 1, 0, 0), makeTimestamp(2015, 2, 1, 0, 0)), set(timestamps.get(4), timestamps.get(5))); assertEquals(expected, runWindowFn(CalendarWindows.months(1), timestamps)); }
@Test public void testMonthsCompatibility() throws IncompatibleWindowException { CalendarWindows.MonthsWindows monthsWindows = CalendarWindows.months(10).beginningOnDay(15); monthsWindows.verifyCompatibility(CalendarWindows.months(10).beginningOnDay(15)); thrown.expect(IncompatibleWindowException.class); monthsWindows.verifyCompatibility(CalendarWindows.months(10).beginningOnDay(30)); }
@Test public void testMultiMonths() throws Exception { Map<IntervalWindow, Set<String>> expected = new HashMap<>(); final List<Long> timestamps = Arrays.asList( makeTimestamp(2014, 3, 5, 0, 0).getMillis(), makeTimestamp(2014, 10, 4, 23, 59).getMillis(), makeTimestamp(2014, 10, 5, 0, 0).getMillis(), makeTimestamp(2015, 3, 1, 0, 0).getMillis(), makeTimestamp(2016, 1, 5, 0, 0).getMillis(), makeTimestamp(2016, 1, 31, 5, 5).getMillis()); expected.put( new IntervalWindow(makeTimestamp(2014, 3, 5, 0, 0), makeTimestamp(2014, 10, 5, 0, 0)), set(timestamps.get(0), timestamps.get(1))); expected.put( new IntervalWindow(makeTimestamp(2014, 10, 5, 0, 0), makeTimestamp(2015, 5, 5, 0, 0)), set(timestamps.get(2), timestamps.get(3))); expected.put( new IntervalWindow(makeTimestamp(2015, 12, 5, 0, 0), makeTimestamp(2016, 7, 5, 0, 0)), set(timestamps.get(4), timestamps.get(5))); assertEquals( expected, runWindowFn( CalendarWindows.months(7).withStartingMonth(2014, 3).beginningOnDay(5), timestamps)); }
@Test public void testDefaultWindowMappingFn() { MonthsWindows windowFn = CalendarWindows.months(2); WindowMappingFn<?> mapping = windowFn.getDefaultWindowMappingFn(); assertThat( mapping.getSideInputWindow( new BoundedWindow() { @Override public Instant maxTimestamp() { return new Instant(100L); } }), equalTo(windowFn.assignWindow(new Instant(100L)))); assertThat(mapping.maximumLookback(), equalTo(Duration.ZERO)); }
@Test public void testDefaultWindowMappingFnGlobal() { MonthsWindows windowFn = CalendarWindows.months(2); WindowMappingFn<?> mapping = windowFn.getDefaultWindowMappingFn(); thrown.expect(IllegalArgumentException.class); mapping.getSideInputWindow(GlobalWindow.INSTANCE); }