ParametersExtractor { public static Offset extractOffset(Request request) { return Offset.from(extractPositiveInteger(request, OFFSET_PARAMETER_NAME)); }  static Limit extractLimit(Request request); static Offset extractOffset(Request request); static Optional<Double> extractPositiveDouble(Request request, String parameterName); static Optional<Integer> extractPositiveInteger(Request request, String parameterName); static final String LIMIT_PARAMETER_NAME; static final String OFFSET_PARAMETER_NAME; }
@Test public void extractOffsetShouldReturnNoneWhenNotInParameters() { Request request = mock(Request.class); when(request.queryParams("offset")) .thenReturn(null); Offset offset = ParametersExtractor.extractOffset(request); assertThat(offset).isEqualTo(Offset.none()); }
@Test public void extractOffsetShouldReturnNoneWhenPresentInParametersButEmpty() { Request request = mock(Request.class); when(request.queryParams("offset")) .thenReturn(""); Offset offset = ParametersExtractor.extractOffset(request); assertThat(offset).isEqualTo(Offset.none()); }
@Test public void extractOffsetShouldReturnTheOffsetWhenPresentInParameters() { Request request = mock(Request.class); when(request.queryParams("offset")) .thenReturn("123"); Offset offset = ParametersExtractor.extractOffset(request); assertThat(offset).isEqualTo(Offset.from(123)); }
@Test public void extractOffsetShouldThrowWhenNegativeOffset() { Request request = mock(Request.class); when(request.queryParams("offset")) .thenReturn("-123"); assertThatThrownBy(() -> ParametersExtractor.extractOffset(request)) .isInstanceOf(HaltException.class); }
@Test public void extractOffsetShouldReturnNoneWhenZeroLimit() { Request request = mock(Request.class); when(request.queryParams("offset")) .thenReturn("0"); Offset offset = ParametersExtractor.extractOffset(request); assertThat(offset).isEqualTo(Offset.none()); }