### Question:
CredentialFlowStateHelper { static Set<CredentialFlowState> restoreFrom(final Restorer restore, final HttpServletRequest request) { final Cookie[] servletCookies = request.getCookies(); if (ArrayUtils.isEmpty(servletCookies)) { return Collections.emptySet(); } final List<javax.ws.rs.core.Cookie> credentialCookies = Arrays.stream(servletCookies) .filter(c -> c.getName().startsWith(CredentialFlowState.CREDENTIAL_PREFIX)) .map(CredentialFlowStateHelper::toJaxRsCookie).collect(Collectors.toList()); try { return restore.apply(credentialCookies, CredentialFlowState.class); } catch (final IllegalArgumentException e) { return Collections.emptySet(); } } private  CredentialFlowStateHelper();   }### Answer:
@Test public void ifCookieNameDoesNotMatchShouldReturnEmptySet() { final Cookie cookie = new Cookie("notMatching", "anyValue"); final HttpServletRequest request = mock(HttpServletRequest.class); when(request.getCookies()).thenReturn(new Cookie[] {cookie}); final Set<CredentialFlowState> got = CredentialFlowStateHelper.restoreFrom((cookies, cls) -> { assertThat(cookies).isEmpty(); return Collections.emptySet(); }, request); assertThat(got).isEmpty(); }
@Test public void ifRestoreFailsShouldReturnEmptySet() { final Cookie cookie = new Cookie(CredentialFlowState.CREDENTIAL_PREFIX + "key", "anyValue"); final HttpServletRequest request = mock(HttpServletRequest.class); when(request.getCookies()).thenReturn(new Cookie[] {cookie}); final Set<CredentialFlowState> got = CredentialFlowStateHelper.restoreFrom((cookies, cls) -> { assertThat(cookies.get(0).getValue()).isEqualTo("anyValue"); throw new IllegalArgumentException(); }, request); assertThat(got).isEmpty(); }
@Test public void shouldRestoreCookiesToStreamOfState() { final CredentialFlowState expected1 = new OAuth2CredentialFlowState.Builder().connectorId("connectorId") .key("key1").build(); final CredentialFlowState expected2 = new OAuth2CredentialFlowState.Builder().connectorId("connectorId") .key("key2").build(); final Cookie cookie1 = new Cookie(CredentialFlowState.CREDENTIAL_PREFIX + "key1", "anyValue"); final Cookie cookie2 = new Cookie(CredentialFlowState.CREDENTIAL_PREFIX + "key2", "anyValue"); final HttpServletRequest request = mock(HttpServletRequest.class); when(request.getCookies()).thenReturn(new Cookie[] {cookie1, cookie2}); when(request.getCookies()).thenReturn(new Cookie[] {cookie1, cookie2}); final Set<CredentialFlowState> states = CredentialFlowStateHelper.restoreFrom((cookies, cls) -> { assertThat(cookies).allSatisfy(cookie -> assertThat(cookie.getValue()).isEqualTo("anyValue")); return cookies.stream() .map(cookie -> new OAuth2CredentialFlowState.Builder().connectorId("connectorId") .key(cookie.getName().substring(CredentialFlowState.CREDENTIAL_PREFIX.length())).build()) .collect(Collectors.toSet()); }, request); assertThat(states).containsOnly(expected1, expected2); }
@Test public void shouldReturnEmptyStreamIfNoCookiesPresent() { final HttpServletRequest request = mock(HttpServletRequest.class); final Set<CredentialFlowState> streamFromNullCookies = CredentialFlowStateHelper.restoreFrom((c, cls) -> null, request); assertThat(streamFromNullCookies).isEmpty(); when(request.getCookies()).thenReturn(new Cookie[0]); final Set<CredentialFlowState> streamFromEmptyCookies = CredentialFlowStateHelper.restoreFrom((c, cls) -> null, request); assertThat(streamFromEmptyCookies).isEmpty(); }