LimitsCheckingWorkspaceManager extends WorkspaceManager { @VisibleForTesting void checkWorkspaceResourceAvailability(String accountId) throws NotFoundException, ServerException { try { resourceManager.checkResourcesAvailability( accountId, singletonList(new ResourceImpl(WorkspaceResourceType.ID, 1, WorkspaceResourceType.UNIT))); } catch (NoEnoughResourcesException e) { throw new LimitExceededException("You are not allowed to create more workspaces."); } } @Inject  LimitsCheckingWorkspaceManager(
      WorkspaceDao workspaceDao,
      WorkspaceRuntimes runtimes,
      EventService eventService,
      AccountManager accountManager,
      PreferenceManager preferenceManager,
      WorkspaceValidator workspaceValidator,
      // own injects
      @Named("che.limits.workspace.env.ram") String maxRamPerEnv,
      EnvironmentRamCalculator environmentRamCalculator,
      ResourceManager resourceManager,
      ResourcesLocks resourcesLocks,
      DevfileIntegrityValidator devfileIntegrityValidator); @Override @Traced WorkspaceImpl createWorkspace(
      WorkspaceConfig config, String namespace, @Nullable Map<String, String> attributes); @Override WorkspaceImpl startWorkspace(
      String workspaceId, @Nullable String envName, @Nullable Map<String, String> options); @Override WorkspaceImpl startWorkspace(
      WorkspaceConfig config, String namespace, boolean isTemporary, Map<String, String> options); @Override WorkspaceImpl updateWorkspace(String id, Workspace update);  }
@Test public void shouldNotThrowLimitExceedExceptionIfAccountHasEnoughAvailableWorkspaceResource() throws Exception { LimitsCheckingWorkspaceManager manager = managerBuilder().setResourceManager(resourceManager).build(); manager.checkWorkspaceResourceAvailability(ACCOUNT_ID); verify(resourceManager) .checkResourcesAvailability( ACCOUNT_ID, singletonList( new ResourceImpl(WorkspaceResourceType.ID, 1, WorkspaceResourceType.UNIT))); }
@Test( expectedExceptions = LimitExceededException.class, expectedExceptionsMessageRegExp = "You are not allowed to create more workspaces\\.") public void shouldThrowLimitExceedExceptionIfAccountDoesNotHaveEnoughAvailableWorkspaceResource() throws Exception { doThrow(new NoEnoughResourcesException(emptyList(), emptyList(), emptyList())) .when(resourceManager) .checkResourcesAvailability(any(), any()); doReturn( singletonList( new ResourceImpl(WorkspaceResourceType.ID, 5, WorkspaceResourceType.UNIT))) .when(resourceManager) .getTotalResources(anyString()); LimitsCheckingWorkspaceManager manager = managerBuilder().setResourceManager(resourceManager).build(); manager.checkWorkspaceResourceAvailability(ACCOUNT_ID); }