StyxOkHttpClient implements StyxClient { @Override public CompletionStage<List<Workflow>> workflows(String componentId) { return execute(forUri(urlBuilder("workflows", componentId)), Workflow[].class) .thenApply(Arrays::asList); } 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 shouldGetAllWorkflowsOfAComponent() throws Exception { final List<Workflow> workflows = Arrays.asList(WORKFLOW_1, WORKFLOW_2); when(client.send(any(Request.class))) .thenReturn(CompletableFuture.completedFuture(response(HTTP_OK, workflows))); final String componentId = WORKFLOW_1.componentId(); final CompletableFuture<List<Workflow>> r = styx.workflows(componentId).toCompletableFuture(); verify(client, timeout(30_000)).send(requestCaptor.capture()); assertThat(r.isDone(), is(true)); final Request request = requestCaptor.getValue(); final HttpUrl url = API_URL.newBuilder().addPathSegment("workflows") .addPathSegment(componentId).build(); assertThat(request.url().toString(), is(url.uri().toString())); assertThat(request.method(), is("GET")); assertThat(r.join(), is(workflows)); }
@Test public void testSendsRequestId() throws Exception { when(client.send(requestCaptor.capture())).thenReturn(CompletableFuture.completedFuture( response(HTTP_OK, Collections.emptyList()))); styx.workflows(); final Request request = requestCaptor.getValue(); assertThat(request.header("X-Request-Id"), isValidUuid()); }
@Test public void testUsesServerRequestIdOnMismatch() throws Exception { final String responseRequestId = "foobar"; when(client.send(any())).thenReturn(CompletableFuture.completedFuture( responseBuilder(HTTP_INTERNAL_ERROR, Collections.emptyList()) .addHeader("X-Request-Id", responseRequestId).build())); try { styx.workflows().toCompletableFuture().get(); fail(); } catch (ExecutionException e) { assertThat(e.getCause(), instanceOf(ApiErrorException.class)); final ApiErrorException apiError = (ApiErrorException) e.getCause(); assertThat(apiError.getRequestId(), is(responseRequestId)); } }
@Test public void testUsesClientRequestIdOnNoResponseRequestId() throws Exception { when(client.send(requestCaptor.capture())) .thenReturn(CompletableFuture.completedFuture(response(HTTP_INTERNAL_ERROR, Collections.emptyList()))); try { styx.workflows().toCompletableFuture().get(); fail(); } catch (ExecutionException e) { assertThat(e.getCause(), instanceOf(ApiErrorException.class)); final ApiErrorException apiError = (ApiErrorException) e.getCause(); final Request request = requestCaptor.getValue(); assertThat(apiError.getRequestId(), is(request.header("X-Request-Id"))); } }