WorkspaceManager { public WorkspaceImpl updateWorkspace(String id, Workspace update) throws ConflictException, ServerException, NotFoundException, ValidationException { requireNonNull(id, "Required non-null workspace id"); requireNonNull(update, "Required non-null workspace update"); checkArgument( update.getConfig() != null ^ update.getDevfile() != null, "Required non-null workspace configuration or devfile update but not both"); if (update.getConfig() != null) { validator.validateConfig(update.getConfig()); } WorkspaceImpl workspace = workspaceDao.get(id); validator.validateUpdateAttributes(workspace.getAttributes(), update.getAttributes()); if (workspace.getConfig() != null) { workspace.setConfig(new WorkspaceConfigImpl(update.getConfig())); } if (workspace.getDevfile() != null) { workspace.setDevfile(new DevfileImpl(update.getDevfile())); } workspace.setAttributes(update.getAttributes()); workspace.getAttributes().put(UPDATED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis())); workspace.setTemporary(update.isTemporary()); return normalizeState(workspaceDao.update(workspace), true); } @Inject  WorkspaceManager(
      WorkspaceDao workspaceDao,
      WorkspaceRuntimes runtimes,
      EventService eventService,
      AccountManager accountManager,
      PreferenceManager preferenceManager,
      WorkspaceValidator validator,
      DevfileIntegrityValidator devfileIntegrityValidator); @Traced WorkspaceImpl createWorkspace(
      WorkspaceConfig config, String namespace, Map<String, String> attributes); @Traced WorkspaceImpl createWorkspace(
      Devfile devfile,
      String namespace,
      Map<String, String> attributes,
      FileContentProvider contentProvider); WorkspaceImpl getWorkspace(String key); WorkspaceImpl getWorkspace(String name, String namespace); Page<WorkspaceImpl> getWorkspaces(
      String user, boolean includeRuntimes, int maxItems, long skipCount); Page<WorkspaceImpl> getByNamespace(
      String namespace, boolean includeRuntimes, int maxItems, long skipCount); WorkspaceImpl updateWorkspace(String id, Workspace update); @Traced void removeWorkspace(String workspaceId); WorkspaceImpl startWorkspace(
      String workspaceId, @Nullable String envName, @Nullable Map<String, String> options); WorkspaceImpl startWorkspace(
      WorkspaceConfig config, String namespace, boolean isTemporary, Map<String, String> options); void stopWorkspace(String workspaceId, Map<String, String> options); Set<String> getSupportedRecipes(); long getWorkspacesTotalCount();  }
@Test public void updatesWorkspace() throws Exception { final WorkspaceImpl workspace = new WorkspaceImpl(createAndMockWorkspace()); Map<String, String> oldAttributes = new HashMap<>(workspace.getAttributes()); workspace.setTemporary(true); workspace.getAttributes().put("new attribute", "attribute"); when(workspaceDao.update(any())).thenAnswer(inv -> inv.getArguments()[0]); workspaceManager.updateWorkspace(workspace.getId(), workspace); verify(workspaceDao).update(workspace); verify(validator).validateUpdateAttributes(oldAttributes, workspace.getAttributes()); }
@Test public void updatesWorkspaceAndReturnWorkspaceWithStatusEqualToItsRuntimeStatus() throws Exception { final WorkspaceImpl workspace = createAndMockWorkspace(); mockRuntime(workspace, STARTING); final WorkspaceImpl updated = workspaceManager.updateWorkspace(workspace.getId(), workspace); assertEquals(updated.getStatus(), STARTING); }
@Test( expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Required non-null workspace configuration or devfile update but not both") public void shouldThrowExceptionWhenTryingToUpdateWorkspaceWithoutWorkspaceConfigAndDevfile() throws Exception { final WorkspaceImpl workspace = WorkspaceImpl.builder().generateId().build(); workspaceManager.updateWorkspace(workspace.getId(), workspace); }
@Test( expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Required non-null workspace configuration or devfile update but not both") public void shouldThrowExceptionWhenTryingToUpdateWorkspaceWithWorkspaceConfigAndDevfile() throws Exception { final Workspace workspace = newDto(WorkspaceDto.class) .withId("workspace123") .withDevfile(newDto(DevfileDto.class)) .withConfig(newDto(WorkspaceConfigDto.class)); workspaceManager.updateWorkspace(workspace.getId(), workspace); }