ReportApiClient extends ApiClient { public ApiClientResult sendReports(Reports reports) { Request request = generateReportRequest(reports); Response response = null; try { response = httpClient.newCall(request).execute(); if (response.isSuccessful()) { response.close(); return new ApiClientResult(reports); } } catch (IOException e) { return new ApiClientResult(ApiClientResult.Error.NETWORK_ERROR); } ApiClientResult.Error error = mapError(response); return new ApiClientResult(error); } ReportApiClient(String apiKey, Device device, String scheme, String host, int port,
      boolean debugEnabled);  ReportApiClient(String apiKey, Device device, String scheme, String host, int port,
      boolean forceReportsEnabled, boolean useGzip); ApiClientResult sendReports(Reports reports);  }
@Test public void sendsTheCorrectBodyBasedOnAReportsInstance() throws Exception { enqueueMockResponse(); Reports reports = givenAReportsInstanceBasedOnJustOneReport(); reportApiClient.sendReports(reports); assertRequestBodyEquals("report/simpleReportRequestBody.json"); }
@Test public void returnsSuccessResultIfTheHttpStatusCodeIsOk() throws Exception { enqueueMockResponse(OK_CODE); Reports reports = givenSomeReports(); ApiClientResult result = reportApiClient.sendReports(reports); assertTrue(result.isSuccess()); }
@Test public void returnsErrorIfServerHasAnInternalError() throws Exception { enqueueMockResponse(SERVER_ERROR_CODE); Reports reports = givenSomeReports(); ApiClientResult result = reportApiClient.sendReports(reports); assertFalse(result.isSuccess()); }
@Test public void returnsTheReportsSentAsPartOfTheReportResultIfTheResponseIsOk() throws Exception { enqueueMockResponse(OK_CODE); Reports reports = givenSomeReports(); ApiClientResult result = reportApiClient.sendReports(reports); assertEquals(reports, result.getValue()); }
@Test public void returnsUnauthorizedErrorIfTheServerSideResponseIsA401() throws Exception { enqueueMockResponse(UNAUTHORIZED_ERROR_CODE); Reports reports = givenSomeReports(); ApiClientResult result = reportApiClient.sendReports(reports); assertFalse(result.isSuccess()); assertEquals(ApiClientResult.Error.UNAUTHORIZED, result.getError()); }
@Test public void returnsUnauthorizedErrorIfTheServerSideResponseIsA403() throws Exception { enqueueMockResponse(FORBIDDEN_ERROR_CODE); Reports reports = givenSomeReports(); ApiClientResult result = reportApiClient.sendReports(reports); assertFalse(result.isSuccess()); assertEquals(ApiClientResult.Error.UNAUTHORIZED, result.getError()); }
@Test public void returnsServerErrorIfTheServerSideResponseIsA500() throws Exception { enqueueMockResponse(SERVER_ERROR_CODE); Reports reports = givenSomeReports(); ApiClientResult result = reportApiClient.sendReports(reports); assertFalse(result.isSuccess()); assertEquals(ApiClientResult.Error.SERVER_ERROR, result.getError()); }
@Test public void returnsClientDisabledAserrorIfTheServerSideResponseIsA412() throws Exception { enqueueMockResponse(PRECONDITION_FAILED); Reports reports = givenSomeReports(); ApiClientResult result = reportApiClient.sendReports(reports); assertFalse(result.isSuccess()); assertEquals(ApiClientResult.Error.CLIENT_DISABLED, result.getError()); }
@Test public void sendsAcceptApplicationJsonHeader() throws Exception { enqueueMockResponse(); Reports reports = givenSomeReports(); reportApiClient.sendReports(reports); assertRequestContainsHeader("Accept", "application/json"); }
@Test public void sendsContentTypeJsonHeader() throws Exception { enqueueMockResponse(); Reports reports = givenSomeReports(); reportApiClient.sendReports(reports); assertRequestContainsHeader("Content-Type", "application/json; charset=utf-8"); }
@Test public void sendsGzipSupportHeader() throws Exception { enqueueMockResponse(); Reports reports = givenSomeReports(); givenAnApiClient(false, true).sendReports(reports); assertRequestContainsHeader("Content-Encoding", "gzip"); }
@Test public void sendsApiKeyHeader() throws Exception { enqueueMockResponse(); Reports reports = givenSomeReports(); reportApiClient.sendReports(reports); assertRequestContainsHeader("X-Api-key", ANY_API_KEY); }
@Test public void sendsUUIDHeader() throws Exception { enqueueMockResponse(); Reports reports = givenSomeReports(); reportApiClient.sendReports(reports); assertRequestContainsHeader("X-UUID", device.getInstallationUUID()); }
@Test public void sendsUserAgentHeader() throws Exception { enqueueMockResponse(); Reports reports = givenSomeReports(); reportApiClient.sendReports(reports); assertRequestContainsHeader("User-Agent", "FlowUpAndroidSDK/" + BuildConfig.VERSION_NAME); }
@Test public void sendsUserAgentHeaderIncludingTheDebugInformation() throws Exception { enqueueMockResponse(); reportApiClient = givenAnApiClient(true, false); reportApiClient.sendReports(givenSomeReports()); assertRequestContainsHeader("User-Agent", "FlowUpAndroidSDK/" + BuildConfig.VERSION_NAME + "-DEBUG"); }
@Test public void sendsDebugHeaderUsingTheForceReportInformationIfIsDisabled() throws Exception { enqueueMockResponse(); boolean forceReportsEnabled = false; reportApiClient = givenAnApiClient(forceReportsEnabled, false); reportApiClient.sendReports(givenSomeReports()); assertRequestContainsHeader("X-Debug-Mode", String.valueOf(forceReportsEnabled)); }
@Test public void sendsDebugHeaderUsingTheForceReportInformationIfIsEnabled() throws Exception { enqueueMockResponse(); boolean forceReportsEnabled = true; reportApiClient = givenAnApiClient(forceReportsEnabled, false); reportApiClient.sendReports(givenSomeReports()); assertRequestContainsHeader("X-Debug-Mode", String.valueOf(forceReportsEnabled)); }
@Test public void sendsReportRequestToTheCorrectPath() throws Exception { enqueueMockResponse(); Reports reports = givenSomeReports(); reportApiClient.sendReports(reports); assertRequestSentTo("/report"); }