StyxOkHttpClient implements StyxClient { @Override public CompletionStage<Void> triggerWorkflowInstance(String componentId, String workflowId, String parameter) { return triggerWorkflowInstance(componentId, workflowId, parameter, TriggerParameters.zero()); } private  StyxOkHttpClient(String apiHost, FutureOkHttpClient client, GoogleIdTokenAuth auth); static StyxClient create(String apiHost); static StyxClient create(String apiHost, GoogleCredentials credentials); static StyxClient create(String apiHost, OkHttpClient client); static StyxClient create(String apiHost, OkHttpClient client, GoogleCredentials credentials); @Override CompletionStage<RunStateDataPayload> activeStates(Optional<String> componentId); @Override CompletionStage<List<EventInfo>> eventsForWorkflowInstance(String componentId,
                                                                    String workflowId,
                                                                    String parameter); @Override CompletionStage<TestServiceAccountUsageAuthorizationResponse> testServiceAccountUsageAuthorization(
      String serviceAccountEmail, String principalEmail); @Override CompletionStage<Workflow> workflow(String componentId, String workflowId); @Override CompletionStage<List<Workflow>> workflows(String componentId); @Override CompletionStage<List<Workflow>> workflows(); @Override CompletionStage<Workflow> createOrUpdateWorkflow(String componentId, WorkflowConfiguration workflowConfig); @Override CompletionStage<Void> deleteWorkflow(String componentId, String workflowId); @Override CompletionStage<WorkflowState> workflowState(String componentId, String workflowId); @Override CompletionStage<WorkflowWithState> workflowWithState(String componentId, String workflowId); @Override CompletionStage<WorkflowInstanceExecutionData> workflowInstanceExecutions(String componentId,
                                                                                   String workflowId,
                                                                                   String parameter); @Override CompletionStage<WorkflowState> updateWorkflowState(String componentId, String workflowId,
                                                            WorkflowState workflowState); @Override CompletionStage<Void> triggerWorkflowInstance(String componentId, String workflowId,
      String parameter); @Override CompletionStage<Void> triggerWorkflowInstance(String componentId,
                                                       String workflowId,
                                                       String parameter,
                                                       TriggerParameters triggerParameters); @Override CompletionStage<Void> triggerWorkflowInstance(String componentId,
                                                       String workflowId,
                                                       String parameter,
                                                       TriggerParameters triggerParameters,
                                                       boolean allowFuture); @Override CompletionStage<Void> haltWorkflowInstance(String componentId,
                                                    String workflowId,
                                                    String parameter); @Override CompletionStage<Void> retryWorkflowInstance(String componentId,
                                                     String workflowId,
                                                     String parameter); @Override CompletionStage<Resource> resourceCreate(String resourceId, int concurrency); @Override CompletionStage<Resource> resourceEdit(String resourceId, int concurrency); @Override CompletionStage<Resource> resource(String resourceId); @Override CompletionStage<ResourcesPayload> resourceList(); @Override CompletionStage<Backfill> backfillCreate(String componentId, String workflowId,
                                                  String start, String end,
                                                  int concurrency); @Override CompletionStage<Backfill> backfillCreate(String componentId, String workflowId,
                                                  String start, String end,
                                                  int concurrency,
                                                  String description); @Override CompletionStage<Backfill> backfillCreate(BackfillInput backfill); @Override CompletionStage<Backfill> backfillCreate(BackfillInput backfill, boolean allowFuture); @Override CompletionStage<Backfill> backfillEditConcurrency(String backfillId, int concurrency); @Override CompletionStage<Void> backfillHalt(String backfillId); @Override CompletionStage<Void> backfillHalt(String backfillId, boolean graceful); @Override CompletionStage<BackfillPayload> backfill(String backfillId, boolean includeStatus); @Override CompletionStage<BackfillsPayload> backfillList(Optional<String> componentId,
                                                        Optional<String> workflowId,
                                                        boolean showAll,
                                                        boolean includeStatus); @Override void close();  }
@Test public void testTokenFromContext() throws Exception { when(auth.getToken(any())).thenReturn(Optional.empty()); when(client.send(any(Request.class))) .thenReturn(CompletableFuture.completedFuture(response(HTTP_OK))); var r = Context.current().withValue(GrpcContextKey.AUTHORIZATION_KEY, "foobar") .call(() -> styx.triggerWorkflowInstance("foo", "bar", "baz").toCompletableFuture()); verify(client, timeout(30_000)).send(requestCaptor.capture()); assertThat(r.isDone(), is(true)); var request = requestCaptor.getValue(); assertThat(request.header("Authorization"), is("Bearer foobar")); }
@Test public void testTokenSuccess() { when(client.send(any(Request.class))) .thenReturn(CompletableFuture.completedFuture(response(HTTP_OK))); final CompletableFuture<Void> r = styx.triggerWorkflowInstance("foo", "bar", "baz").toCompletableFuture(); verify(client, timeout(30_000)).send(requestCaptor.capture()); assertThat(r.isDone(), is(true)); final Request request = requestCaptor.getValue(); assertThat(request.header("Authorization"), is("Bearer foobar")); }
@Test public void testTokenFailure() throws Exception { final IOException rootCause = new IOException("netsplit!"); when(auth.getToken(any())).thenThrow(rootCause); thrown.expect(ClientErrorException.class); thrown.expectMessage("Authentication failure: " + rootCause.getMessage()); thrown.expectCause(is(rootCause)); styx.triggerWorkflowInstance("foo", "bar", "baz"); }
@Test public void testUnathorizedMissingCredentialsApiError() throws Exception { when(auth.getToken(any())).thenReturn(Optional.empty()); when(client.send(any())) .thenReturn(CompletableFuture.completedFuture(response(HTTP_FORBIDDEN))); try { styx.triggerWorkflowInstance("foo", "bar", "baz").toCompletableFuture().get(); fail(); } catch (ExecutionException e) { assertThat(e.getCause(), instanceOf(ApiErrorException.class)); ApiErrorException apiErrorException = (ApiErrorException) e.getCause(); assertThat(apiErrorException.isAuthenticated(), is(false)); } }
@Test public void testUnauthorizedWithCredentialsApiError() throws Exception { when(client.send(any())) .thenReturn(CompletableFuture.completedFuture(response(HTTP_FORBIDDEN))); try { styx.triggerWorkflowInstance("foo", "bar", "baz").toCompletableFuture().get(); fail(); } catch (ExecutionException e) { assertThat(e.getCause(), instanceOf(ApiErrorException.class)); ApiErrorException apiErrorException = (ApiErrorException) e.getCause(); assertThat(apiErrorException.isAuthenticated(), is(true)); } }
@Test public void testTriggerWorkflowInstance() throws IOException { final TriggerRequest triggerRequest = TriggerRequest.of(WORKFLOW_1.id(), "2017-01-01", TriggerParameters.zero()); when(client.send(any(Request.class))).thenReturn(CompletableFuture.completedFuture( response(HTTP_OK))); final CompletableFuture<Void> r = styx.triggerWorkflowInstance(WORKFLOW_1.componentId(), WORKFLOW_1.workflowId(), "2017-01-01").toCompletableFuture(); verify(client, timeout(30_000)).send(requestCaptor.capture()); assertThat(r.isDone(), is(true)); final Request request = requestCaptor.getValue(); assertThat(request.url().toString(), is(API_URL + "/scheduler/trigger?allowFuture=false")); assertThat(request.method(), is("POST")); assertThat(Json.deserialize(bytesOfRequestBody(request), TriggerRequest.class), equalTo(triggerRequest)); }
@Test public void testTriggerWorkflowInstanceAllowFuture() throws IOException { final TriggerRequest triggerRequest = TriggerRequest.of(WORKFLOW_1.id(), "2017-01-01", TriggerParameters.zero()); when(client.send(any(Request.class))).thenReturn(CompletableFuture.completedFuture( response(HTTP_OK))); final CompletableFuture<Void> r = styx.triggerWorkflowInstance(WORKFLOW_1.componentId(), WORKFLOW_1.workflowId(), "2017-01-01").toCompletableFuture(); verify(client, timeout(30_000)).send(requestCaptor.capture()); assertThat(r.isDone(), is(true)); final Request request = requestCaptor.getValue(); assertThat(request.url().toString(), is(API_URL + "/scheduler/trigger?allowFuture=false")); assertThat(request.method(), is("POST")); assertThat(Json.deserialize(bytesOfRequestBody(request), TriggerRequest.class), equalTo(triggerRequest)); }
@Test public void testTriggerWorkflowInstanceWithTriggerParametersAllowFuture() throws IOException { final TriggerParameters triggerParameters = TriggerParameters.builder() .env("FOO", "BAR", "BAR", "FOO") .build(); final TriggerRequest triggerRequest = TriggerRequest.of(WORKFLOW_1.id(), "2017-01-01", triggerParameters); when(client.send(any(Request.class))) .thenReturn(CompletableFuture.completedFuture(response(HTTP_OK))); final CompletableFuture<Void> r = styx.triggerWorkflowInstance(WORKFLOW_1.componentId(), WORKFLOW_1.workflowId(), "2017-01-01", triggerParameters, true).toCompletableFuture(); verify(client, timeout(30_000)).send(requestCaptor.capture()); assertThat(r.isDone(), is(true)); final Request request = requestCaptor.getValue(); assertThat(request.url().toString(), is(API_URL + "/scheduler/trigger?allowFuture=true")); assertThat(request.method(), is("POST")); assertThat(Json.deserialize(bytesOfRequestBody(request), TriggerRequest.class), equalTo(triggerRequest)); }