DevfileConverter { public WorkspaceConfigImpl devFileToWorkspaceConfig( DevfileImpl devfile, FileContentProvider contentProvider) throws DevfileException { checkArgument(devfile != null, "Devfile must not be null"); checkArgument(contentProvider != null, "Content provider must not be null"); devfile = new DevfileImpl(devfile); validateCurrentVersion(devfile); defaultEditorProvisioner.apply(devfile, contentProvider); WorkspaceConfigImpl config = new WorkspaceConfigImpl(); config.setName(devfile.getName()); for (Command command : devfile.getCommands()) { CommandImpl com = commandConverter.toWorkspaceCommand(command, contentProvider); if (com != null) { config.getCommands().add(com); } } for (ComponentImpl component : devfile.getComponents()) { ComponentToWorkspaceApplier applier = componentTypeToApplier.get(component.getType()); if (applier == null) { throw new DevfileException( String.format( "Devfile contains component `%s` with type `%s` that can not be converted to workspace", getIdentifiableComponentName(component), component.getType())); } applier.apply(config, component, contentProvider); } for (ProjectImpl project : devfile.getProjects()) { ProjectConfigImpl projectConfig = projectConverter.toWorkspaceProject(project); config.getProjects().add(projectConfig); } config.getAttributes().putAll(devfile.getAttributes()); config.setDevfile(devfile); return config; } @Inject  DevfileConverter(
      ProjectConverter projectConverter,
      CommandConverter commandConverter,
      Map<String, ComponentToWorkspaceApplier> componentTypeToApplier,
      DefaultEditorProvisioner defaultEditorProvisioner,
      URLFetcher urlFetcher); WorkspaceConfigImpl convert(Devfile devfile); WorkspaceConfigImpl devFileToWorkspaceConfig(
      DevfileImpl devfile, FileContentProvider contentProvider);  }
@Test public void shouldUseDevfileNameForWorkspaceNameDuringConvertingDevfileToWorkspaceConfig() throws Exception { FileContentProvider fileContentProvider = mock(FileContentProvider.class); DevfileImpl devfile = newDevfile("petclinic"); WorkspaceConfigImpl workspaceConfig = devfileConverter.devFileToWorkspaceConfig(devfile, fileContentProvider); assertEquals(workspaceConfig.getName(), "petclinic"); }
@Test public void shouldInvokeDefaultEditorProvisionerDuringConvertingDevfileToWorkrspaceConfig() throws Exception { FileContentProvider fileContentProvider = mock(FileContentProvider.class); DevfileImpl devfile = newDevfile("petclinic"); devfileConverter.devFileToWorkspaceConfig(devfile, fileContentProvider); verify(defaultEditorToolApplier).apply(devfile, fileContentProvider); }
@Test public void shouldProvisionDevfileAttributesAsConfigAttributesDuringConvertingDevfileToWorkspaceConfig() throws Exception { FileContentProvider fileContentProvider = mock(FileContentProvider.class); Map<String, String> devfileAttributes = new HashMap<>(); devfileAttributes.put(PERSIST_VOLUMES_ATTRIBUTE, "false"); devfileAttributes.put("anotherAttribute", "value"); DevfileImpl devfile = newDevfile("petclinic"); devfile.getAttributes().putAll(devfileAttributes); WorkspaceConfigImpl config = devfileConverter.devFileToWorkspaceConfig(devfile, fileContentProvider); assertEquals(config.getAttributes(), devfileAttributes); }
@Test public void shouldConvertCommandsDuringConvertingDevfileToWorkspaceConfig() throws Exception { FileContentProvider fileContentProvider = mock(FileContentProvider.class); DevfileImpl devfile = newDevfile("petclinic"); CommandImpl devfileCommand = mock(CommandImpl.class); devfile.getCommands().add(devfileCommand); org.eclipse.che.api.workspace.server.model.impl.CommandImpl workspaceCommand = mock(org.eclipse.che.api.workspace.server.model.impl.CommandImpl.class); when(commandConverter.toWorkspaceCommand(any(), any())).thenReturn(workspaceCommand); WorkspaceConfigImpl workspaceConfig = devfileConverter.devFileToWorkspaceConfig(devfile, fileContentProvider); assertEquals(workspaceConfig.getCommands().size(), 1); assertSame(workspaceConfig.getCommands().get(0), workspaceCommand); }
@Test public void shouldConvertProjectsDuringConvertingDevfileToWorkspaceConfig() throws Exception { FileContentProvider fileContentProvider = mock(FileContentProvider.class); DevfileImpl devfile = newDevfile("petclinic"); ProjectImpl devfileProject = mock(ProjectImpl.class); devfile.getProjects().add(devfileProject); ProjectConfigImpl workspaceProject = mock(ProjectConfigImpl.class); when(projectConverter.toWorkspaceProject(any())).thenReturn(workspaceProject); WorkspaceConfigImpl workspaceConfig = devfileConverter.devFileToWorkspaceConfig(devfile, fileContentProvider); assertEquals(workspaceConfig.getProjects().size(), 1); assertSame(workspaceConfig.getProjects().get(0), workspaceProject); }
@Test public void shouldConvertComponentsDuringConvertingDevfileToWorkspaceConfig() throws Exception { DevfileImpl devfile = newDevfile("petclinic"); ComponentImpl component = new ComponentImpl(); component.setType(COMPONENT_TYPE); devfile.getComponents().add(component); FileContentProvider fileContentProvider = mock(FileContentProvider.class); WorkspaceConfigImpl workspaceConfig = devfileConverter.devFileToWorkspaceConfig(devfile, fileContentProvider); verify(componentToWorkspaceApplier).apply(workspaceConfig, component, fileContentProvider); }
@Test( expectedExceptions = DevfileFormatException.class, expectedExceptionsMessageRegExp = "Provided Devfile has no API version specified") public void shouldThrowAnExceptionIfDevfileApiVersionIsMissingDuringConvertingDevfileToWorkspaceConfig() throws Exception { FileContentProvider fileContentProvider = mock(FileContentProvider.class); DevfileImpl devfile = new DevfileImpl(); devfile.setName("petclinic"); devfileConverter.devFileToWorkspaceConfig(devfile, fileContentProvider); }
@Test( expectedExceptions = DevfileFormatException.class, expectedExceptionsMessageRegExp = "Provided Devfile has unsupported version '1\\.0\\.0-non-supported'. The following versions are supported: .*") public void shouldThrowAnExceptionIfDevfileApiVersionIsNotSupportedDuringConvertingDevfileToWorkspaceConfig() throws Exception { FileContentProvider fileContentProvider = mock(FileContentProvider.class); DevfileImpl devfile = new DevfileImpl(); devfile.setApiVersion("1.0.0-non-supported"); devfile.setName("petclinic"); devfileConverter.devFileToWorkspaceConfig(devfile, fileContentProvider); }