WorkspaceRuntimes { @VisibleForTesting InternalEnvironment createInternalEnvironment( @Nullable Environment environment, Map<String, String> workspaceConfigAttributes, List<? extends Command> commands, DevfileImpl devfile) throws InfrastructureException, ValidationException, NotFoundException { String recipeType; if (environment == null) { recipeType = Constants.NO_ENVIRONMENT_RECIPE_TYPE; } else { recipeType = environment.getRecipe().getType(); } InternalEnvironmentFactory factory = environmentFactories.get(recipeType); if (factory == null) { throw new NotFoundException( format("InternalEnvironmentFactory is not configured for recipe type: '%s'", recipeType)); } InternalEnvironment internalEnvironment = factory.create(environment); internalEnvironment.setAttributes(new HashMap<>(workspaceConfigAttributes)); internalEnvironment.setCommands(commands.stream().map(CommandImpl::new).collect(toList())); internalEnvironment.setDevfile(devfile); return internalEnvironment; } @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 internalEnvironmentCreationShouldRespectNoEnvironmentCase() throws Exception { InternalEnvironmentFactory noEnvFactory = mock(InternalEnvironmentFactory.class); runtimes = new WorkspaceRuntimes( eventService, ImmutableMap.of( TEST_ENVIRONMENT_TYPE, testEnvFactory, NO_ENVIRONMENT_RECIPE_TYPE, noEnvFactory), infrastructure, sharedPool, workspaceDao, dbInitializer, probeScheduler, statuses, lockService, devfileConverter); InternalEnvironment expectedEnvironment = mock(InternalEnvironment.class); when(noEnvFactory.create(eq(null))).thenReturn(expectedEnvironment); InternalEnvironment actualEnvironment = runtimes.createInternalEnvironment(null, emptyMap(), emptyList(), null); assertEquals(actualEnvironment, expectedEnvironment); }
@Test( expectedExceptions = NotFoundException.class, expectedExceptionsMessageRegExp = "InternalEnvironmentFactory is not configured for recipe type: 'not-supported-type'") public void internalEnvironmentShouldThrowExceptionWhenNoEnvironmentFactoryFoundForRecipeType() throws Exception { EnvironmentImpl environment = new EnvironmentImpl(); environment.setRecipe(new RecipeImpl("not-supported-type", "", "", null)); runtimes.createInternalEnvironment(environment, emptyMap(), emptyList(), null); }
@Test( expectedExceptions = NotFoundException.class, expectedExceptionsMessageRegExp = "InternalEnvironmentFactory is not configured for recipe type: '" + NO_ENVIRONMENT_RECIPE_TYPE + "'") public void internalEnvironmentShouldThrowExceptionWhenNoEnvironmentFactoryFoundForNoEnvironmentWorkspaceCase() throws Exception { runtimes.createInternalEnvironment(null, emptyMap(), emptyList(), null); }