@Override
  public void getArtifact(
      ArtifactApi.GetArtifactRequest request,
      StreamObserver<ArtifactApi.GetArtifactResponse> responseObserver) {
    // Trying out artifact services in order till one succeeds.
    // If all services fail, re-raises the last error.
    // TODO: when all services fail, return an aggregated error with errors from all services.
    RuntimeException lastError = null;
    for (Endpoints.ApiServiceDescriptor endpoint : endpoints) {
      ArtifactResolver artifactResolver =
          this.artifactResolver != null
              ? this.artifactResolver
              : new EndpointBasedArtifactResolver(endpoint.getUrl());
      try {
        Iterator<ArtifactApi.GetArtifactResponse> responseIterator =
            artifactResolver.getArtifact(request);
        while (responseIterator.hasNext()) {
          responseObserver.onNext(responseIterator.next());
        }

        responseObserver.onCompleted();
        return;
      } catch (RuntimeException exn) {
        lastError = exn;
      } finally {
        if (this.artifactResolver == null) {
          artifactResolver.shutdown();
        }
      }
    }

    if (lastError == null) {
      lastError =
          new RuntimeException(
              "Could not successfully get the artifact for the request " + request);
    }
    throw lastError;
  }
@Test
  public void testArtifactGetFirstEndpoint() {
    Path path = Paths.get("dummypath");

    RunnerApi.ArtifactInformation fileArtifact =
        RunnerApi.ArtifactInformation.newBuilder()
            .setTypeUrn(ArtifactRetrievalService.FILE_ARTIFACT_URN)
            .setTypePayload(
                RunnerApi.ArtifactFilePayload.newBuilder()
                    .setPath(path.toString())
                    .build()
                    .toByteString())
            .setRoleUrn("")
            .build();

    ArtifactApi.GetArtifactRequest request =
        ArtifactApi.GetArtifactRequest.newBuilder().setArtifact(fileArtifact).build();
    Mockito.when(artifactResolver.getArtifact(request))
        .thenReturn(
            ImmutableList.of(ArtifactApi.GetArtifactResponse.newBuilder().build()).iterator());

    StreamObserver<GetArtifactResponse> responseObserver = Mockito.mock(StreamObserver.class);
    artifactService.getArtifact(request, responseObserver);

    Mockito.verify(artifactResolver, Mockito.times(1)).getArtifact(request);
  }