ProtocolAwareExceptionMapper implements ExceptionMapper<E> { @VisibleForTesting static Optional<String> parseProtocolVersionFromUserAgentHeader(@Nonnull List<String> userAgentHeader) { return userAgentHeader .stream() .map(UserAgents::tryParse) .map(UserAgent::informational) .flatMap(List::stream) .filter(agent -> agent.name().equals(AtlasDbRemotingConstants.ATLASDB_HTTP_CLIENT)) .findFirst() .map(UserAgent.Agent::version); }  @Override Response toResponse(E exception);  }
@Test public void detectsAtlasDbHttpClientAgent() { assertThat(ProtocolAwareExceptionMapper.parseProtocolVersionFromUserAgentHeader( ImmutableList.of(FORMATTED_USER_AGENT_WITH_HTTP_CLIENT))) .contains(HTTP_PROTOCOL_VERSION); }
@Test public void returnsEmptyIfNoRelevantAgentsFound() { assertThat(ProtocolAwareExceptionMapper.parseProtocolVersionFromUserAgentHeader( ImmutableList.of(UserAgents.format(USER_AGENT_1.addAgent(AGENT_1))))) .isEmpty(); }
@Test public void returnsEmptyIfNoUserAgentsArePresent() { assertThat(ProtocolAwareExceptionMapper.parseProtocolVersionFromUserAgentHeader(ImmutableList.of())) .isEmpty(); }
@Test public void returnsEmptyIfUserAgentStringCannotBeParsed() { assertThat(ProtocolAwareExceptionMapper.parseProtocolVersionFromUserAgentHeader( ImmutableList.of("I don't know what a user agent is!"))) .isEmpty(); }
@Test public void detectsAtlasDbHttpClientAgentInPresenceOfUnparseableHeaders() { assertThat(ProtocolAwareExceptionMapper.parseProtocolVersionFromUserAgentHeader( ImmutableList.of("Ich weiß nicht, was ein 'User-Agent' bedeutet", FORMATTED_USER_AGENT_WITH_HTTP_CLIENT))) .contains(HTTP_PROTOCOL_VERSION); }
@Test public void detectsAtlasDbHttpClientAgentOnAnyPresentUserAgentHeader() { assertThat(ProtocolAwareExceptionMapper.parseProtocolVersionFromUserAgentHeader( ImmutableList.of(UserAgents.format(USER_AGENT_2.addAgent(AGENT_1)), FORMATTED_USER_AGENT_WITH_HTTP_CLIENT))) .contains(HTTP_PROTOCOL_VERSION); }