Java Code Examples for org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl#setRuntime()

The following examples show how to use org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl#setRuntime() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestObjects.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
/** Creates runtime workspace object based on the machines RAM. */
public static WorkspaceImpl createRuntime(String... machineRams) throws Exception {
  final WorkspaceImpl workspace = createWorkspace(DEFAULT_USER_NAME, machineRams);
  final String envName = workspace.getConfig().getDefaultEnv();
  final Map<String, MachineImpl> machines = new HashMap<>();
  int i = 0;
  for (String machineRam : machineRams) {
    machines.put(
        "machine" + i++,
        createMachine(machineRam, Collections.emptyMap(), MachineStatus.RUNNING));
  }
  RuntimeImpl runtime = new RuntimeImpl(envName, machines, DEFAULT_USER_NAME);

  workspace.setStatus(RUNNING);
  workspace.setRuntime(runtime);
  return workspace;
}
 
Example 2
Source File: WorkspaceRuntimes.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Injects runtime information such as status and {@link
 * org.eclipse.che.api.core.model.workspace.Runtime} into the workspace object, if the workspace
 * doesn't have runtime sets the status to {@link WorkspaceStatus#STOPPED}.
 *
 * @param workspace the workspace to inject runtime into
 */
public void injectRuntime(WorkspaceImpl workspace) throws ServerException {
  try (Unlocker ignored = lockService.writeLock(workspace.getId())) {
    WorkspaceStatus workspaceStatus = statuses.get(workspace.getId());

    if (workspaceStatus == null) {
      workspace.setStatus(STOPPED);
      return;
    }

    InternalRuntime<?> internalRuntime;
    try {
      internalRuntime = getInternalRuntime(workspace.getId());
    } catch (ServerException | InfrastructureException e) {
      workspace.setStatus(STOPPED);
      return;
    }

    workspace.setRuntime(asRuntime(internalRuntime));
    workspace.setStatus(workspaceStatus);
  }
}
 
Example 3
Source File: WorkspaceServiceTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldReturnWorkspaceWithTokenIfRuntimeExists() throws Exception {
  final WorkspaceImpl workspace = createWorkspace(createConfigDto());
  workspace.setRuntime(new RuntimeImpl("activeEnv", emptyMap(), "user123"));
  when(wsManager.getWorkspace(workspace.getId())).thenReturn(workspace);
  when(machineTokenProvider.getToken(anyString())).thenReturn("superToken");

  final Response response =
      given()
          .auth()
          .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD)
          .when()
          .get(SECURE_PATH + "/workspace/" + workspace.getId());

  assertEquals(response.getStatusCode(), 200);
  WorkspaceDto retrievedWorkspace = unwrapDto(response, WorkspaceDto.class);
  assertEquals(retrievedWorkspace.getRuntime().getMachineToken(), "superToken");
  verify(machineTokenProvider).getToken(workspace.getId());
}
 
Example 4
Source File: WorkspaceServiceTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldUpdateWorkspaceWithRuntime() throws Exception {
  final WorkspaceImpl workspace = createWorkspace(createConfigDto());
  MachineImpl machine = new MachineImpl(emptyMap(), emptyMap(), MachineStatus.STARTING);
  RuntimeImpl runtime = new RuntimeImpl("myenv", singletonMap("machine1", machine), "owner");
  workspace.setRuntime(runtime);
  when(wsManager.updateWorkspace(any(), any())).thenReturn(workspace);
  // when(wsManager.getWorkspace(workspace.getId())).thenReturn(workspace);
  final WorkspaceDto workspaceDto = asDto(workspace);

  final Response response =
      given()
          .auth()
          .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD)
          .contentType("application/json")
          .body(workspaceDto)
          .when()
          .put(SECURE_PATH + "/workspace/" + workspace.getId());

  assertEquals(response.getStatusCode(), 200);
  assertEquals(unwrapDto(response, WorkspaceDto.class).getRuntime(), asDto(runtime));
}
 
Example 5
Source File: WorkspaceServiceTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldGetWorkspaceWithExternalServersByDefault() throws Exception {
  // given
  WorkspaceImpl workspace = createWorkspace(createConfigDto());
  String externalServerKey = "server2";
  ServerImpl externalServer = createExternalServer();
  Map<String, Server> servers =
      ImmutableMap.of("server1", createInternalServer(), externalServerKey, externalServer);
  Map<String, Machine> machines =
      singletonMap("machine1", new MachineImpl(singletonMap("key", "value"), servers, RUNNING));
  workspace.setRuntime(new RuntimeImpl("activeEnv", machines, "user123"));
  when(wsManager.getWorkspace(workspace.getId())).thenReturn(workspace);
  Map<String, MachineDto> expected =
      singletonMap(
          "machine1",
          newDto(MachineDto.class)
              .withAttributes(singletonMap("key", "value"))
              .withStatus(RUNNING)
              .withServers(
                  singletonMap(
                      externalServerKey,
                      newDto(ServerDto.class)
                          .withUrl(externalServer.getUrl())
                          .withStatus(externalServer.getStatus())
                          .withAttributes(externalServer.getAttributes()))));

  // when
  Response response =
      given()
          .auth()
          .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD)
          .when()
          .get(SECURE_PATH + "/workspace/" + workspace.getId());

  // then
  assertEquals(response.getStatusCode(), 200);
  RuntimeDto retrievedRuntime = unwrapDto(response, WorkspaceDto.class).getRuntime();
  assertNotNull(retrievedRuntime);
  assertEquals(expected, retrievedRuntime.getMachines());
}
 
Example 6
Source File: PreviewUrlLinksVariableGeneratorTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldDoNothingWhenRuntimeIsNull() {
  WorkspaceImpl w = new WorkspaceImpl();
  w.setRuntime(null);

  assertTrue(generator.genLinksMapAndUpdateCommands(w, uriBuilder).isEmpty());
}
 
Example 7
Source File: PreviewUrlLinksVariableGeneratorTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private WorkspaceImpl createWorkspaceWithCommands(List<CommandImpl> commands) {
  RuntimeImpl runtime =
      new RuntimeImpl("", Collections.emptyMap(), "", commands, new ArrayList<>());
  WorkspaceImpl w = new WorkspaceImpl();
  w.setRuntime(runtime);

  return w;
}
 
Example 8
Source File: WorkspaceServiceTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void shouldTreatServerWithInternalServerAttributeNotEqualToTrueExternal()
    throws Exception {
  // given
  WorkspaceImpl workspace = createWorkspace(createConfigDto());
  String externalServerKey = "server2";
  ServerImpl externalServer =
      createInternalServer()
          .withAttributes(singletonMap(ServerConfig.INTERNAL_SERVER_ATTRIBUTE, ""));
  Map<String, Server> servers =
      ImmutableMap.of("server1", createInternalServer(), externalServerKey, externalServer);
  Map<String, Machine> machines =
      singletonMap("machine1", new MachineImpl(singletonMap("key", "value"), servers, RUNNING));
  workspace.setRuntime(new RuntimeImpl("activeEnv", machines, "user123"));
  when(wsManager.getWorkspace(workspace.getId())).thenReturn(workspace);
  Map<String, MachineDto> expected =
      singletonMap(
          "machine1",
          newDto(MachineDto.class)
              .withAttributes(singletonMap("key", "value"))
              .withStatus(RUNNING)
              .withServers(
                  singletonMap(
                      externalServerKey,
                      newDto(ServerDto.class)
                          .withUrl(externalServer.getUrl())
                          .withStatus(externalServer.getStatus())
                          .withAttributes(externalServer.getAttributes()))));

  // when
  Response response =
      given()
          .auth()
          .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD)
          .when()
          .get(SECURE_PATH + "/workspace/" + workspace.getId());

  // then
  assertEquals(response.getStatusCode(), 200);
  RuntimeDto retrievedRuntime = unwrapDto(response, WorkspaceDto.class).getRuntime();
  assertNotNull(retrievedRuntime);
  assertEquals(expected, retrievedRuntime.getMachines());
}
 
Example 9
Source File: WorkspaceServiceTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void shouldGetWorkspaceWithInternalServers() throws Exception {
  // given
  WorkspaceImpl workspace = createWorkspace(createConfigDto());
  String externalServerKey = "server2";
  String internalServerKey = "server1";
  ServerImpl externalServer = createExternalServer();
  ServerImpl internalServer = createInternalServer();
  Map<String, Server> servers =
      ImmutableMap.of(
          internalServerKey, createInternalServer(), externalServerKey, externalServer);
  Map<String, Machine> machines =
      singletonMap("machine1", new MachineImpl(singletonMap("key", "value"), servers, RUNNING));
  workspace.setRuntime(new RuntimeImpl("activeEnv", machines, "user123"));
  when(wsManager.getWorkspace(workspace.getId())).thenReturn(workspace);

  Map<String, MachineDto> expected =
      singletonMap(
          "machine1",
          newDto(MachineDto.class)
              .withAttributes(singletonMap("key", "value"))
              .withStatus(RUNNING)
              .withServers(
                  ImmutableMap.of(
                      externalServerKey,
                      newDto(ServerDto.class)
                          .withUrl(externalServer.getUrl())
                          .withStatus(externalServer.getStatus())
                          .withAttributes(externalServer.getAttributes()),
                      internalServerKey,
                      newDto(ServerDto.class)
                          .withUrl(createInternalServer().getUrl())
                          .withStatus(internalServer.getStatus())
                          .withAttributes(internalServer.getAttributes()))));

  // when
  Response response =
      given()
          .auth()
          .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD)
          .queryParameter("includeInternalServers", Boolean.TRUE.toString())
          .when()
          .get(SECURE_PATH + "/workspace/" + workspace.getId());

  // then
  assertEquals(response.getStatusCode(), 200);
  RuntimeDto retrievedRuntime = unwrapDto(response, WorkspaceDto.class).getRuntime();
  assertNotNull(retrievedRuntime);
  assertEquals(expected, retrievedRuntime.getMachines());
}
 
Example 10
Source File: WorkspaceServiceTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void shouldGetWorkspaceWithInternalServersIfCorrespondingQueryParamHasEmptyValue()
    throws Exception {
  // given
  WorkspaceImpl workspace = createWorkspace(createConfigDto());
  String externalServerKey = "server2";
  String internalServerKey = "server1";
  ServerImpl externalServer = createExternalServer();
  ServerImpl internalServer = createInternalServer();
  Map<String, Server> servers =
      ImmutableMap.of(
          internalServerKey, createInternalServer(), externalServerKey, externalServer);
  Map<String, Machine> machines =
      singletonMap("machine1", new MachineImpl(singletonMap("key", "value"), servers, RUNNING));
  workspace.setRuntime(new RuntimeImpl("activeEnv", machines, "user123"));
  when(wsManager.getWorkspace(workspace.getId())).thenReturn(workspace);

  Map<String, MachineDto> expected =
      singletonMap(
          "machine1",
          newDto(MachineDto.class)
              .withAttributes(singletonMap("key", "value"))
              .withStatus(RUNNING)
              .withServers(
                  ImmutableMap.of(
                      externalServerKey,
                      newDto(ServerDto.class)
                          .withUrl(externalServer.getUrl())
                          .withStatus(externalServer.getStatus())
                          .withAttributes(externalServer.getAttributes()),
                      internalServerKey,
                      newDto(ServerDto.class)
                          .withUrl(createInternalServer().getUrl())
                          .withStatus(internalServer.getStatus())
                          .withAttributes(internalServer.getAttributes()))));

  // when
  Response response =
      given()
          .auth()
          .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD)
          .queryParameter("includeInternalServers", "")
          .when()
          .get(SECURE_PATH + "/workspace/" + workspace.getId());

  // then
  assertEquals(response.getStatusCode(), 200);
  RuntimeDto retrievedRuntime = unwrapDto(response, WorkspaceDto.class).getRuntime();
  assertNotNull(retrievedRuntime);
  assertEquals(expected, retrievedRuntime.getMachines());
}
 
Example 11
Source File: WorkspaceServiceTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void shouldGetWorkspaceWithInternalServersIfCorrespondingQueryParamHasNoValue()
    throws Exception {
  // given
  WorkspaceImpl workspace = createWorkspace(createConfigDto());
  String externalServerKey = "server2";
  String internalServerKey = "server1";
  ServerImpl externalServer = createExternalServer();
  ServerImpl internalServer = createInternalServer();
  Map<String, Server> servers =
      ImmutableMap.of(
          internalServerKey, createInternalServer(), externalServerKey, externalServer);
  Map<String, Machine> machines =
      singletonMap("machine1", new MachineImpl(singletonMap("key", "value"), servers, RUNNING));
  workspace.setRuntime(new RuntimeImpl("activeEnv", machines, "user123"));
  when(wsManager.getWorkspace(workspace.getId())).thenReturn(workspace);

  Map<String, MachineDto> expected =
      singletonMap(
          "machine1",
          newDto(MachineDto.class)
              .withAttributes(singletonMap("key", "value"))
              .withStatus(RUNNING)
              .withServers(
                  ImmutableMap.of(
                      externalServerKey,
                      newDto(ServerDto.class)
                          .withUrl(externalServer.getUrl())
                          .withStatus(externalServer.getStatus())
                          .withAttributes(externalServer.getAttributes()),
                      internalServerKey,
                      newDto(ServerDto.class)
                          .withUrl(createInternalServer().getUrl())
                          .withStatus(internalServer.getStatus())
                          .withAttributes(internalServer.getAttributes()))));

  // when
  Response response =
      given()
          .auth()
          .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD)
          .queryParameter("includeInternalServers")
          .when()
          .get(SECURE_PATH + "/workspace/" + workspace.getId());

  // then
  assertEquals(response.getStatusCode(), 200);
  RuntimeDto retrievedRuntime = unwrapDto(response, WorkspaceDto.class).getRuntime();
  assertNotNull(retrievedRuntime);
  assertEquals(expected, retrievedRuntime.getMachines());
}