HaloNetworkExceptionResolver { @NonNull @Api(2.0) public HaloNetException resolve(@NonNull Exception exception, @Nullable Request request, boolean isNetworkConnected) { HaloNetException exceptionMapped; String url = request != null ? request.url().toString() : null; if (!isNetworkConnected) { exceptionMapped = new HaloConnectionException("The device has not connectivity to reach the server on " + url, exception); } else { exceptionMapped = new HaloUnknownServerException("An unknown exception has been produced when performing the request: " + exception.getMessage(), exception); } return exceptionMapped; }  @NonNull @Api(2.0) HaloNetException resolve(@NonNull Exception exception, @Nullable Request request, boolean isNetworkConnected); @NonNull @Api(2.0) HaloNetException resolve(@NonNull Response response);  }
@Test public void thatHandleNotConnectivity() throws IOException { HaloNetworkApi networkApi = givenAHaloNetWorkApi(mFramework,mEndpointURL); HaloRequest request = givenAGetRequest(networkApi); HaloNetworkExceptionResolver haloNetworkExceptionResolver = new HaloNetworkExceptionResolver(); HaloNetException haloNetException = haloNetworkExceptionResolver.resolve(new HaloConnectionException("No connectivity",new Exception()),request.buildOkRequest(),false); assertThat(haloNetException).isNotNull(); assertThat(haloNetException.getMessage()).contains("not connectivity"); }
@Test public void thatHandleUnknownExceptionWhenConnectivityIsAvailable() throws IOException { HaloNetworkApi networkApi = givenAHaloNetWorkApi(mFramework,mEndpointURL); HaloRequest request = givenAGetRequest(networkApi); HaloNetworkExceptionResolver haloNetworkExceptionResolver = new HaloNetworkExceptionResolver(); HaloNetException haloUnknownServerException = haloNetworkExceptionResolver.resolve(new HaloUnknownServerException("Unknown exception",new Exception()),request.buildOkRequest(),true); assertThat(haloUnknownServerException).isNotNull(); assertThat(haloUnknownServerException.getMessage()).contains("unknown exception"); }
@Test public void thatHandleUnknownExceptionsWhenConnectivityIsAvailable() throws IOException { HaloNetworkApi networkApi = givenAHaloNetWorkApi(mFramework,mEndpointURL); HaloRequest request = givenAGetRequest(networkApi); HaloNetworkExceptionResolver haloNetworkExceptionResolver = new HaloNetworkExceptionResolver(); HaloNetException haloUnknownServerException = haloNetworkExceptionResolver.resolve(new HaloUnknownServerException("Unknown exception"),request.buildOkRequest(),true); assertThat(haloUnknownServerException).isNotNull(); assertThat(haloUnknownServerException.getMessage()).contains("unknown exception"); }
@Test public void thatHandleNotFoundExceptionIsThrown() throws IOException { enqueueServerError(mMockServer,404); HaloNetworkApi networkApi = givenAHaloNetWorkApi(mFramework,mEndpointURL); HaloRequest request = givenAGetRequest(networkApi); HaloNetworkExceptionResolver haloNetworkExceptionResolver = new HaloNetworkExceptionResolver(); try{ haloNetworkExceptionResolver.resolve(request.execute()); } catch (HaloNotFoundException haloNotFoundException){ assertThat(haloNotFoundException).isNotNull(); assertThat(haloNotFoundException.getMessage()).contains("404"); } }
@Test public void thatAuthenticationExceptionIsThrown() throws IOException { enqueueServerError(mMockServer,401); HaloNetworkApi networkApi = givenAHaloNetWorkApi(mFramework,mEndpointURL); HaloRequest request = givenAGetRequest(networkApi); HaloNetworkExceptionResolver haloNetworkExceptionResolver = new HaloNetworkExceptionResolver(); try{ haloNetworkExceptionResolver.resolve(request.execute()); } catch (HaloAuthenticationException haloAuthenticationException){ assertThat(haloAuthenticationException).isNotNull(); assertThat(haloAuthenticationException.getMessage()).contains("401"); } }
@Test public void thatHaloServerExceptionIsThrown() throws IOException { enqueueServerError(mMockServer,500); HaloNetworkApi networkApi = givenAHaloNetWorkApi(mFramework,mEndpointURL); HaloRequest request = givenAGetRequest(networkApi); HaloNetworkExceptionResolver haloNetworkExceptionResolver = new HaloNetworkExceptionResolver(); try{ haloNetworkExceptionResolver.resolve(request.execute()); } catch (HaloServerException haloServerException){ assertThat(haloServerException).isNotNull(); assertThat(haloServerException.getMessage()).contains("500"); assertThat(haloServerException.getErrorCode()).isEqualTo(500); assertThat(haloServerException.toString()).contains("500"); assertThat(haloServerException.getBody()).isEqualTo(""); } }
@Test public void thatHaloUnknownServerExceptionIsThrown() throws IOException { enqueueServerError(mMockServer,301); HaloNetworkApi networkApi = givenAHaloNetWorkApi(mFramework,mEndpointURL); HaloRequest request = givenAGetRequest(networkApi); HaloNetworkExceptionResolver haloNetworkExceptionResolver = new HaloNetworkExceptionResolver(); try{ haloNetworkExceptionResolver.resolve(request.execute()); } catch (HaloUnknownServerException haloUnknownServerException){ assertThat(haloUnknownServerException).isNotNull(); assertThat(haloUnknownServerException.getMessage()).contains("301"); } }