### Question:
AuthenticationGateway { public void signIn(String username, String password, Callbacks responseCallbacks) { apiGateway.makeRequest( new TrackerAuthenticationRequest(username, password), new AuthenticationApiResponseCallbacks(responseCallbacks, sharedPreferences)); } AuthenticationGateway(ApiGateway apiGateway, Context context); void signIn(String username, String password, Callbacks responseCallbacks); boolean isAuthenticated(); void signOut(); String getToken(); public ApiGateway apiGateway; }### Answer:
@Test public void shouldMakeARemoteCallWhenSigningIn() throws Exception { authenticationGateway.signIn("spongebob", "squidward", callbacks); String urlString = apiGateway.getLatestRequest().getUrlString(); assertThat(urlString, equalTo("https: }
@Test public void shouldSendUsernameAndPassword() throws Exception { authenticationGateway.signIn("spongebob", "squidward", callbacks); TrackerAuthenticationRequest request = (TrackerAuthenticationRequest) apiGateway.getLatestRequest(); assertThat(request, equalTo(new TrackerAuthenticationRequest("spongebob", "squidward"))); }
@Test public void shouldCallSuccessWhenAuthenticationSucceeds() throws Exception { authenticationGateway.signIn("spongebob", "squidward", callbacks); apiGateway.simulateResponse(200, AUTH_SUCCESS); assertThat(callbacks.successWasCalled, equalTo(true)); assertThat(callbacks.failureWasCalled, equalTo(false)); assertThat(callbacks.completeWasCalled, equalTo(true)); }
@Test public void shouldCallFailureWhenAuthenticationFails() throws Exception { authenticationGateway.signIn("spongebob", "squidward", callbacks); apiGateway.simulateResponse(401, "Access Denied"); assertThat(callbacks.failureWasCalled, equalTo(true)); assertThat(callbacks.successWasCalled, equalTo(false)); assertThat(callbacks.completeWasCalled, equalTo(true)); }
@Test public void shouldCallFailureWhenServerReturnsError() throws Exception { authenticationGateway.signIn("spongebob", "squidward", callbacks); apiGateway.simulateResponse(500, "ERROR"); assertThat(callbacks.failureWasCalled, equalTo(true)); assertThat(callbacks.successWasCalled, equalTo(false)); assertThat(callbacks.completeWasCalled, equalTo(true)); }
@Test public void shouldStoreApiTokenInPrefs() throws Exception { authenticationGateway.signIn("spongebob", "squidward", callbacks); apiGateway.simulateResponse(200, AUTH_SUCCESS); assertThat(getStoredGuid(), equalTo("c93f12c")); }