Ejemplo n.º 1
0
  @Test
  public void contentShouldBeReturned() throws Exception {
    Link link1 = mock(Link.class);
    Link link2 = mock(Link.class);

    when(data.getLinks()).thenReturn(Arrays.asList(link2, link1));
    when(link1.getRel()).thenReturn(DockerFile.GET_CONTENT);
    when(link1.getHref()).thenReturn(SOME_TEXT);
    when(link2.getRel()).thenReturn(SOME_TEXT);

    dockerFile.getContent(callback);

    verify(link1).getHref();
    verify(link2, never()).getHref();
  }
Ejemplo n.º 2
0
 /**
  * Find first link in the specified list by its relation.
  *
  * @param rel link's relation
  * @param links list of links
  * @return found link or {@code null}
  */
 public static Link getLink(String rel, List<Link> links) {
   for (Link link : links) {
     if (rel.equals(link.getRel())) {
       return link;
     }
   }
   return null;
 }
Ejemplo n.º 3
0
 /**
  * Find all links in the specified list by its relation.
  *
  * @param rel link's relation
  * @param links list of links
  * @return found link or {@code null}
  */
 public static List<Link> getLinks(String rel, List<Link> links) {
   final List<Link> result = new LinkedList<>();
   for (Link link : links) {
     if (rel.equals(link.getRel())) {
       result.add(link);
     }
   }
   return result;
 }
Ejemplo n.º 4
0
 private Link getLink(String rel) throws Exception {
   List<Link> links = getDescriptor().getLinks();
   for (Link link : links) {
     if (link.getRel().equals(rel)) {
       return link;
     }
   }
   return null;
 }
Ejemplo n.º 5
0
  private ApplicationProcessDescriptor getDescriptor(
      RunnerProcess process, ServiceContext restfulRequestContext) throws RunnerException {
    final ApplicationStatus status =
        process.getError() == null
            ? (process.isCancelled()
                ? ApplicationStatus.CANCELLED
                : (process.isStopped()
                    ? ApplicationStatus.STOPPED
                    : (process.isStarted() ? ApplicationStatus.RUNNING : ApplicationStatus.NEW)))
            : ApplicationStatus.FAILED;
    final List<Link> links = new LinkedList<>();
    final UriBuilder servicePathBuilder = restfulRequestContext.getServiceUriBuilder();
    final DtoFactory dtoFactory = DtoFactory.getInstance();
    links.add(
        dtoFactory
            .createDto(Link.class)
            .withRel(Constants.LINK_REL_GET_STATUS)
            .withHref(
                servicePathBuilder
                    .clone()
                    .path(getClass(), "getStatus")
                    .build(process.getRunner(), process.getId())
                    .toString())
            .withMethod("GET")
            .withProduces(MediaType.APPLICATION_JSON));
    links.add(
        dtoFactory
            .createDto(Link.class)
            .withRel(Constants.LINK_REL_VIEW_LOG)
            .withHref(
                servicePathBuilder
                    .clone()
                    .path(getClass(), "getLogs")
                    .build(process.getRunner(), process.getId())
                    .toString())
            .withMethod("GET"));
    switch (status) {
      case NEW:
      case RUNNING:
        links.add(
            dtoFactory
                .createDto(Link.class)
                .withRel(Constants.LINK_REL_STOP)
                .withHref(
                    servicePathBuilder
                        .clone()
                        .path(getClass(), "stop")
                        .build(process.getRunner(), process.getId())
                        .toString())
                .withMethod("POST")
                .withProduces(MediaType.APPLICATION_JSON));
        break;
    }
    final RunnerConfiguration configuration = process.getConfiguration();
    final RunRequest request = configuration.getRequest();
    final java.io.File recipeFile = configuration.getRecipeFile();
    if (recipeFile != null) {
      links.add(
          dtoFactory
              .createDto(Link.class)
              .withRel(Constants.LINK_REL_RUNNER_RECIPE)
              .withHref(
                  servicePathBuilder
                      .clone()
                      .path(getClass(), "getRecipeFile")
                      .build(process.getRunner(), process.getId())
                      .toString())
              .withMethod("GET")
              .withProduces(MediaType.TEXT_PLAIN));
    }
    final List<Link> additionalLinks = new LinkedList<>();
    PortMapping portMapping = null;
    switch (status) {
      case NEW:
      case RUNNING:
        for (Link link : configuration.getLinks()) {
          additionalLinks.add(dtoFactory.clone(link));
        }
        final Map<String, String> ports = configuration.getPortMapping();
        if (!ports.isEmpty()) {
          portMapping =
              dtoFactory
                  .createDto(PortMapping.class)
                  .withHost(configuration.getHost())
                  .withPorts(new HashMap<>(ports));
        }
        break;
      default:
        for (Link link : configuration.getLinks()) {
          if ("web url".equals(link.getRel()) || "shell url".equals(link.getRel())) {
            // Hide web and shell links if application is not running.
            continue;
          }
          additionalLinks.add(dtoFactory.clone(link));
        }
        break;
    }

    links.addAll(additionalLinks);
    return dtoFactory
        .createDto(ApplicationProcessDescriptor.class)
        .withProcessId(process.getId())
        .withStatus(status)
        .withStartTime(process.getStartTime())
        .withStopTime(process.getStopTime())
        .withLinks(links)
        .withWorkspace(request.getWorkspace())
        .withProject(request.getProject())
        .withUserId(request.getUserId())
        .withDebugHost(configuration.getDebugHost())
        .withDebugPort(configuration.getDebugPort())
        .withPortMapping(portMapping);
  }