@Test public void testBadRequest() throws TimeoutException, InterruptedException { MockResponse malformed = new MockResponse().setResponseCode(400); server.enqueue(malformed); Sender sender = new Sender("foobarbaz", mapper, 10, server.getUrl("/gcm/send").toExternalForm()); CompletableFuture<Result> future = sender.send(Message.newBuilder().withDestination("1").build()); try { future.get(10, TimeUnit.SECONDS); throw new AssertionError(); } catch (ExecutionException e) { assertTrue(e.getCause() instanceof InvalidRequestException); } assertEquals(server.getRequestCount(), 1); }
public CompletableFuture<Result> send(Message message) { try { HttpRequest request = HttpRequest.newBuilder() .uri(uri) .header("Authorization", authorizationHeader) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(message.serialize())) .timeout(Duration.ofSeconds(10)) .build(); return retry.executeCompletionStage(executorService, () -> getClient().sendAsync(request, BodyHandlers.ofByteArray()) .thenApply(response -> { switch (response.statusCode()) { case 400: throw new CompletionException(new InvalidRequestException()); case 401: throw new CompletionException(new AuthenticationFailedException()); case 204: case 200: return response.body(); default: throw new CompletionException(new ServerFailedException("Bad status: " + response.statusCode())); } }) .thenApply(responseBytes -> { try { List<GcmResponseEntity> responseList = mapper.readValue(responseBytes, GcmResponseListEntity.class).getResults(); if (responseList == null || responseList.size() == 0) { throw new CompletionException(new IOException("Empty response list!")); } GcmResponseEntity responseEntity = responseList.get(0); return new Result(responseEntity.getCanonicalRegistrationId(), responseEntity.getMessageId(), responseEntity.getError()); } catch (IOException e) { throw new CompletionException(e); } })).toCompletableFuture(); } catch (JsonProcessingException e) { return CompletableFuture.failedFuture(e); } }
Sender { public CompletableFuture<Result> send(Message message) { try { HttpRequest request = HttpRequest.newBuilder() .uri(uri) .header("Authorization", authorizationHeader) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(message.serialize())) .timeout(Duration.ofSeconds(10)) .build(); return retry.executeCompletionStage(executorService, () -> getClient().sendAsync(request, BodyHandlers.ofByteArray()) .thenApply(response -> { switch (response.statusCode()) { case 400: throw new CompletionException(new InvalidRequestException()); case 401: throw new CompletionException(new AuthenticationFailedException()); case 204: case 200: return response.body(); default: throw new CompletionException(new ServerFailedException("Bad status: " + response.statusCode())); } }) .thenApply(responseBytes -> { try { List<GcmResponseEntity> responseList = mapper.readValue(responseBytes, GcmResponseListEntity.class).getResults(); if (responseList == null || responseList.size() == 0) { throw new CompletionException(new IOException("Empty response list!")); } GcmResponseEntity responseEntity = responseList.get(0); return new Result(responseEntity.getCanonicalRegistrationId(), responseEntity.getMessageId(), responseEntity.getError()); } catch (IOException e) { throw new CompletionException(e); } })).toCompletableFuture(); } catch (JsonProcessingException e) { return CompletableFuture.failedFuture(e); } } }
Sender { public CompletableFuture<Result> send(Message message) { try { HttpRequest request = HttpRequest.newBuilder() .uri(uri) .header("Authorization", authorizationHeader) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(message.serialize())) .timeout(Duration.ofSeconds(10)) .build(); return retry.executeCompletionStage(executorService, () -> getClient().sendAsync(request, BodyHandlers.ofByteArray()) .thenApply(response -> { switch (response.statusCode()) { case 400: throw new CompletionException(new InvalidRequestException()); case 401: throw new CompletionException(new AuthenticationFailedException()); case 204: case 200: return response.body(); default: throw new CompletionException(new ServerFailedException("Bad status: " + response.statusCode())); } }) .thenApply(responseBytes -> { try { List<GcmResponseEntity> responseList = mapper.readValue(responseBytes, GcmResponseListEntity.class).getResults(); if (responseList == null || responseList.size() == 0) { throw new CompletionException(new IOException("Empty response list!")); } GcmResponseEntity responseEntity = responseList.get(0); return new Result(responseEntity.getCanonicalRegistrationId(), responseEntity.getMessageId(), responseEntity.getError()); } catch (IOException e) { throw new CompletionException(e); } })).toCompletableFuture(); } catch (JsonProcessingException e) { return CompletableFuture.failedFuture(e); } } Sender(String apiKey, ObjectMapper mapper);  Sender(String apiKey, ObjectMapper mapper, int retryCount); @VisibleForTesting  Sender(String apiKey, ObjectMapper mapper, int retryCount, String url); }
Sender { public CompletableFuture<Result> send(Message message) { try { HttpRequest request = HttpRequest.newBuilder() .uri(uri) .header("Authorization", authorizationHeader) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(message.serialize())) .timeout(Duration.ofSeconds(10)) .build(); return retry.executeCompletionStage(executorService, () -> getClient().sendAsync(request, BodyHandlers.ofByteArray()) .thenApply(response -> { switch (response.statusCode()) { case 400: throw new CompletionException(new InvalidRequestException()); case 401: throw new CompletionException(new AuthenticationFailedException()); case 204: case 200: return response.body(); default: throw new CompletionException(new ServerFailedException("Bad status: " + response.statusCode())); } }) .thenApply(responseBytes -> { try { List<GcmResponseEntity> responseList = mapper.readValue(responseBytes, GcmResponseListEntity.class).getResults(); if (responseList == null || responseList.size() == 0) { throw new CompletionException(new IOException("Empty response list!")); } GcmResponseEntity responseEntity = responseList.get(0); return new Result(responseEntity.getCanonicalRegistrationId(), responseEntity.getMessageId(), responseEntity.getError()); } catch (IOException e) { throw new CompletionException(e); } })).toCompletableFuture(); } catch (JsonProcessingException e) { return CompletableFuture.failedFuture(e); } } Sender(String apiKey, ObjectMapper mapper);  Sender(String apiKey, ObjectMapper mapper, int retryCount); @VisibleForTesting  Sender(String apiKey, ObjectMapper mapper, int retryCount, String url); CompletableFuture<Result> send(Message message); Retry getRetry(); }
Sender { public CompletableFuture<Result> send(Message message) { try { HttpRequest request = HttpRequest.newBuilder() .uri(uri) .header("Authorization", authorizationHeader) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(message.serialize())) .timeout(Duration.ofSeconds(10)) .build(); return retry.executeCompletionStage(executorService, () -> getClient().sendAsync(request, BodyHandlers.ofByteArray()) .thenApply(response -> { switch (response.statusCode()) { case 400: throw new CompletionException(new InvalidRequestException()); case 401: throw new CompletionException(new AuthenticationFailedException()); case 204: case 200: return response.body(); default: throw new CompletionException(new ServerFailedException("Bad status: " + response.statusCode())); } }) .thenApply(responseBytes -> { try { List<GcmResponseEntity> responseList = mapper.readValue(responseBytes, GcmResponseListEntity.class).getResults(); if (responseList == null || responseList.size() == 0) { throw new CompletionException(new IOException("Empty response list!")); } GcmResponseEntity responseEntity = responseList.get(0); return new Result(responseEntity.getCanonicalRegistrationId(), responseEntity.getMessageId(), responseEntity.getError()); } catch (IOException e) { throw new CompletionException(e); } })).toCompletableFuture(); } catch (JsonProcessingException e) { return CompletableFuture.failedFuture(e); } } Sender(String apiKey, ObjectMapper mapper);  Sender(String apiKey, ObjectMapper mapper, int retryCount); @VisibleForTesting  Sender(String apiKey, ObjectMapper mapper, int retryCount, String url); CompletableFuture<Result> send(Message message); Retry getRetry();  }