RestApiHandler implements HttpRequestHandler { static Optional<Duration> parseTimeout(String timeoutOption) throws InvalidContentException { if (timeoutOption == null) { return Optional.empty(); } float timeoutSeconds; try { timeoutSeconds = Float.parseFloat(timeoutOption); } catch (NumberFormatException e) { throw new InvalidContentException("value of timeout->" + timeoutOption + " is not a float"); } if (timeoutSeconds <= 0.0) { return Optional.of(Duration.ZERO); } else if (timeoutSeconds <= MAX_TIMEOUT.getSeconds()) { return Optional.of(Duration.ofMillis(Math.round(timeoutSeconds * 1000))); } else { throw new InvalidContentException("value of timeout->" + timeoutOption + " exceeds max timeout " + MAX_TIMEOUT); } } RestApiHandler(StateRestAPI restApi); RestApiHandler setDefaultPathPrefix(String defaultPathPrefix); @Override HttpResult handleRequest(HttpRequest request); static final Duration MAX_TIMEOUT; }
@Test public void testParsingOfTimeout() throws InvalidContentException { assertEquals(Optional.empty(), RestApiHandler.parseTimeout(null)); assertEquals(Optional.of(Duration.ofMillis(12500)), RestApiHandler.parseTimeout("12.5")); assertEquals(Optional.of(Duration.ofMillis(0)), RestApiHandler.parseTimeout("-1")); assertEquals(Optional.of(Duration.ofMillis(0)), RestApiHandler.parseTimeout("0.0001")); }