WorkspaceRuntimes { public Set<String> getActive() { return ImmutableSet.copyOf(statuses.asMap().keySet()); } @VisibleForTesting  WorkspaceRuntimes(
      ConcurrentMap<String, InternalRuntime<?>> runtimes,
      EventService eventService,
      Map<String, InternalEnvironmentFactory> envFactories,
      RuntimeInfrastructure infra,
      WorkspaceSharedPool sharedPool,
      WorkspaceDao workspaceDao,
      @SuppressWarnings("unused") DBInitializer ignored,
      ProbeScheduler probeScheduler,
      WorkspaceStatusCache statuses,
      WorkspaceLockService lockService,
      DevfileConverter devfileConverter); @Inject  WorkspaceRuntimes(
      EventService eventService,
      Map<String, InternalEnvironmentFactory> envFactories,
      RuntimeInfrastructure infra,
      WorkspaceSharedPool sharedPool,
      WorkspaceDao workspaceDao,
      @SuppressWarnings("unused") DBInitializer ignored,
      ProbeScheduler probeScheduler,
      WorkspaceStatusCache statuses,
      WorkspaceLockService lockService,
      DevfileConverter devfileConverter); void validate(WorkspaceImpl workspace, @Nullable String envName); String evalInfrastructureNamespace(NamespaceResolutionContext resolutionCtx); String evalLegacyInfrastructureNamespace(NamespaceResolutionContext resolutionContext); boolean isInfrastructureNamespaceValid(String namespaceName); void injectRuntime(WorkspaceImpl workspace); boolean hasRuntime(String workspaceId); InternalRuntime<?> getInternalRuntime(String workspaceId); WorkspaceStatus getStatus(String workspaceId); @Traced CompletableFuture<Void> startAsync(
      WorkspaceImpl workspace, @Nullable String envName, Map<String, String> options); @Traced CompletableFuture<Void> stopAsync(WorkspaceImpl workspace, Map<String, String> options); boolean refuseStart(); Set<String> getRunning(); Set<String> getActive(); Set<String> getActive(String owner); boolean isAnyActive(); Set<String> getInProgress(); Set<String> getInProgress(String owner); boolean isAnyInProgress(); Optional<RuntimeContext> getRuntimeContext(String workspaceId); Set<String> getSupportedRecipes();  }
@Test public void shouldReturnRuntimesIdsOfActiveWorkspaces() { when(statuses.asMap()) .thenReturn( ImmutableMap.of( "ws1", WorkspaceStatus.STARTING, "ws2", WorkspaceStatus.RUNNING, "ws3", WorkspaceStatus.STOPPING)); Set<String> active = runtimes.getActive(); assertEquals(active.size(), 3); assertTrue(active.containsAll(asList("ws1", "ws2", "ws3"))); }
@Test public void shouldReturnRuntimesIdsOfActiveWorkspacesForGivenOwner() throws Exception { String ws1 = generate("workspace", 6); String ws2 = generate("workspace", 6); String ws3 = generate("workspace", 6); String owner = generate("user", 6); when(statuses.asMap()) .thenReturn( ImmutableMap.of( ws1, WorkspaceStatus.STARTING, ws2, WorkspaceStatus.RUNNING, ws3, WorkspaceStatus.STOPPING)); RuntimeIdentityImpl runtimeIdentity1 = new RuntimeIdentityImpl(ws1, generate("env", 6), owner, generate("infraNamespace", 6)); RuntimeIdentityImpl runtimeIdentity2 = new RuntimeIdentityImpl( ws2, generate("env", 6), generate("user", 6), generate("infraNamespace", 6)); RuntimeIdentityImpl runtimeIdentity3 = new RuntimeIdentityImpl( ws3, generate("env", 6), generate("user", 6), generate("infraNamespace", 6)); mockWorkspaceWithConfig(runtimeIdentity1); mockWorkspaceWithConfig(runtimeIdentity2); mockWorkspaceWithConfig(runtimeIdentity3); RuntimeContext context1 = mockContext(runtimeIdentity1); RuntimeContext context2 = mockContext(runtimeIdentity2); RuntimeContext context3 = mockContext(runtimeIdentity3); when(context1.getRuntime()) .thenReturn(new TestInternalRuntime(context1, emptyMap(), WorkspaceStatus.STARTING)); when(context2.getRuntime()) .thenReturn(new TestInternalRuntime(context2, emptyMap(), WorkspaceStatus.RUNNING)); when(context3.getRuntime()) .thenReturn(new TestInternalRuntime(context3, emptyMap(), WorkspaceStatus.STOPPING)); doReturn(context1).when(infrastructure).prepare(eq(runtimeIdentity1), any()); doReturn(context2).when(infrastructure).prepare(eq(runtimeIdentity2), any()); doReturn(context3).when(infrastructure).prepare(eq(runtimeIdentity3), any()); Set<RuntimeIdentity> identities = ImmutableSet.of(runtimeIdentity1, runtimeIdentity2, runtimeIdentity3); doReturn(identities).when(infrastructure).getIdentities(); InternalEnvironment internalEnvironment = mock(InternalEnvironment.class); doReturn(internalEnvironment).when(testEnvFactory).create(any(Environment.class)); Set<String> active = runtimes.getActive(owner); assertEquals(active.size(), 1); assertTrue(active.containsAll(asList(ws1))); }