DefaultEditorProvisioner { public void apply(DevfileImpl devfile, FileContentProvider contentProvider) throws DevfileException { if (defaultEditorRef == null) { return; } if ("true".equals(devfile.getAttributes().get(EDITOR_FREE_DEVFILE_ATTRIBUTE))) { return; } List<ComponentImpl> components = devfile.getComponents(); Optional<ComponentImpl> editorOpt = components.stream().filter(t -> EDITOR_COMPONENT_TYPE.equals(t.getType())).findFirst(); boolean isDefaultEditorUsed; if (!editorOpt.isPresent()) { components.add(new ComponentImpl(EDITOR_COMPONENT_TYPE, defaultEditorRef)); isDefaultEditorUsed = true; } else { Component editor = editorOpt.get(); String editorPublisherAndName = getPluginPublisherAndName(editor, contentProvider); isDefaultEditorUsed = defaultEditor.equals(editorPublisherAndName); } if (isDefaultEditorUsed) { provisionDefaultPlugins(components, contentProvider); } if ("false".equals(devfile.getAttributes().get(PERSIST_VOLUMES_ATTRIBUTE)) && "true".equals(devfile.getAttributes().get(ASYNC_PERSIST_ATTRIBUTE))) { provisionAsyncStoragePlugin(components, contentProvider); } } @Inject  DefaultEditorProvisioner(
      @Named("che.workspace.devfile.default_editor") String defaultEditorRef,
      @Named("che.workspace.devfile.default_editor.plugins") String[] defaultPluginsRefs,
      @Named("che.workspace.devfile.async.storage.plugin") String asyncStoragePluginRef,
      ComponentFQNParser componentFQNParser,
      PluginFQNParser pluginFQNParser); void apply(DevfileImpl devfile, FileContentProvider contentProvider);  }
@Test public void shouldNotProvisionDefaultEditorIfItIsNotConfigured() throws Exception { provisioner = new DefaultEditorProvisioner( null, new String[] {}, ASYNC_STORAGE_PLUGIN_REF, fqnParser, pluginFQNParser); DevfileImpl devfile = new DevfileImpl(); provisioner.apply(devfile, fileContentProvider); assertTrue(devfile.getComponents().isEmpty()); }
@Test public void shouldProvisionDefaultEditorWithPluginsWhenDevfileDoNotHaveAny() throws Exception { provisioner = new DefaultEditorProvisioner( EDITOR_REF, new String[] {TERMINAL_PLUGIN_REF, COMMAND_PLUGIN_REF}, "", fqnParser, pluginFQNParser); DevfileImpl devfile = new DevfileImpl(); provisioner.apply(devfile, fileContentProvider); List<ComponentImpl> components = devfile.getComponents(); assertEquals(components.size(), 3); assertTrue(components.contains(new ComponentImpl(EDITOR_COMPONENT_TYPE, EDITOR_REF))); assertTrue(components.contains(new ComponentImpl(PLUGIN_COMPONENT_TYPE, COMMAND_PLUGIN_REF))); assertTrue(components.contains(new ComponentImpl(PLUGIN_COMPONENT_TYPE, TERMINAL_PLUGIN_REF))); }
@Test public void shouldProvisionDefaultPluginsIfTheyAreNotSpecifiedAndDefaultEditorIsConfigured() throws Exception { provisioner = new DefaultEditorProvisioner( EDITOR_REF, new String[] {TERMINAL_PLUGIN_REF}, "", fqnParser, pluginFQNParser); DevfileImpl devfile = new DevfileImpl(); ComponentImpl defaultEditorWithDifferentVersion = new ComponentImpl(EDITOR_COMPONENT_TYPE, EDITOR_PUBLISHER + "/" + EDITOR_NAME + "/latest"); devfile.getComponents().add(defaultEditorWithDifferentVersion); provisioner.apply(devfile, fileContentProvider); List<ComponentImpl> components = devfile.getComponents(); assertEquals(components.size(), 2); assertTrue(components.contains(defaultEditorWithDifferentVersion)); assertTrue(components.contains(new ComponentImpl(PLUGIN_COMPONENT_TYPE, TERMINAL_PLUGIN_REF))); }
@Test public void shouldProvisionDefaultPluginsIfTheyAreNotSpecifiedAndDefaultEditorFromCustomRegistryIsConfigured() throws Exception { provisioner = new DefaultEditorProvisioner( EDITOR_REF, new String[] {TERMINAL_PLUGIN_REF}, "", fqnParser, pluginFQNParser); DevfileImpl devfile = new DevfileImpl(); ComponentImpl defaultEditorWithDifferentVersion = new ComponentImpl( EDITOR_COMPONENT_TYPE, "https: devfile.getComponents().add(defaultEditorWithDifferentVersion); provisioner.apply(devfile, fileContentProvider); List<ComponentImpl> components = devfile.getComponents(); assertEquals(components.size(), 2); assertTrue(components.contains(defaultEditorWithDifferentVersion)); assertTrue(components.contains(new ComponentImpl(PLUGIN_COMPONENT_TYPE, TERMINAL_PLUGIN_REF))); }
@Test public void shouldNotProvisionDefaultPluginsIfCustomEditorIsConfiguredWhichStartWithDefaultId() throws Exception { provisioner = new DefaultEditorProvisioner( EDITOR_REF, new String[] {TERMINAL_PLUGIN_REF}, "", fqnParser, pluginFQNParser); DevfileImpl devfile = new DevfileImpl(); ComponentImpl editorWithNameSimilarToDefault = new ComponentImpl( EDITOR_COMPONENT_TYPE, EDITOR_PUBLISHER + "/" + EDITOR_NAME + "-dev/dev-version"); devfile.getComponents().add(editorWithNameSimilarToDefault); provisioner.apply(devfile, fileContentProvider); List<ComponentImpl> components = devfile.getComponents(); assertEquals(components.size(), 1); assertTrue(components.contains(editorWithNameSimilarToDefault)); assertNull(findById(components, EDITOR_NAME)); }
@Test public void shouldNotProvisionDefaultPluginsIfDevfileContainsEditorFreeAttribute() throws Exception { provisioner = new DefaultEditorProvisioner( EDITOR_REF, new String[] {TERMINAL_PLUGIN_REF}, "", fqnParser, pluginFQNParser); DevfileImpl devfile = new DevfileImpl(); devfile.getAttributes().put(EDITOR_FREE_DEVFILE_ATTRIBUTE, "true"); provisioner.apply(devfile, fileContentProvider); List<ComponentImpl> components = devfile.getComponents(); assertTrue(components.isEmpty()); }
@Test public void shouldProvisionDefaultPluginIfDevfileAlreadyContainPluginWithNameWhichStartWithDefaultOne() throws Exception { provisioner = new DefaultEditorProvisioner( EDITOR_REF, new String[] {TERMINAL_PLUGIN_REF}, "", fqnParser, pluginFQNParser); DevfileImpl devfile = new DevfileImpl(); ComponentImpl pluginWithNameSimilarToDefault = new ComponentImpl( PLUGIN_COMPONENT_TYPE, EDITOR_PUBLISHER + "/" + TERMINAL_PLUGIN_NAME + "-dummy/latest"); devfile.getComponents().add(pluginWithNameSimilarToDefault); provisioner.apply(devfile, fileContentProvider); List<ComponentImpl> components = devfile.getComponents(); assertEquals(components.size(), 3); assertTrue(components.contains(new ComponentImpl(EDITOR_COMPONENT_TYPE, EDITOR_REF))); assertTrue(components.contains(new ComponentImpl(PLUGIN_COMPONENT_TYPE, TERMINAL_PLUGIN_REF))); assertTrue(components.contains(pluginWithNameSimilarToDefault)); }
@Test public void shouldNotProvisionDefaultEditorOrDefaultPluginsIfDevfileAlreadyHasNonDefaultEditor() throws Exception { provisioner = new DefaultEditorProvisioner(EDITOR_REF, new String[] {}, "", fqnParser, pluginFQNParser); DevfileImpl devfile = new DevfileImpl(); ComponentImpl nonDefaultEditor = new ComponentImpl(EDITOR_COMPONENT_TYPE, "anypublisher/anyname/v" + EDITOR_VERSION); devfile.getComponents().add(nonDefaultEditor); provisioner.apply(devfile, fileContentProvider); List<ComponentImpl> components = devfile.getComponents(); assertEquals(components.size(), 1); assertTrue(components.contains(nonDefaultEditor)); }
@Test public void shouldNotProvisionDefaultEditorIfDevfileAlreadyContainsSuchButWithDifferentVersion() throws Exception { provisioner = new DefaultEditorProvisioner(EDITOR_REF, new String[] {}, "", fqnParser, pluginFQNParser); DevfileImpl devfile = new DevfileImpl(); ComponentImpl myTheiaEditor = new ComponentImpl( EDITOR_COMPONENT_TYPE, EDITOR_PUBLISHER + "/" + EDITOR_NAME + "/my-custom"); devfile.getComponents().add(myTheiaEditor); provisioner.apply(devfile, fileContentProvider); List<ComponentImpl> components = devfile.getComponents(); assertEquals(components.size(), 1); assertTrue(components.contains(myTheiaEditor)); }
@Test public void shouldNotProvisionDefaultEditorIfDevfileAlreadyContainsSuchByReference() throws Exception { provisioner = new DefaultEditorProvisioner(EDITOR_REF, new String[] {}, "", fqnParser, pluginFQNParser); DevfileImpl devfile = new DevfileImpl(); ComponentImpl myTheiaEditor = new ComponentImpl( EDITOR_COMPONENT_TYPE, null, "https: String meta = "apiVersion: v2\n" + "publisher: " + EDITOR_PUBLISHER + "\n" + "name: " + EDITOR_NAME + "\n" + "version: " + EDITOR_VERSION + "\n" + "type: Che Editor"; devfile.getComponents().add(myTheiaEditor); when(fileContentProvider.fetchContent(anyString())).thenReturn(meta); provisioner.apply(devfile, fileContentProvider); List<ComponentImpl> components = devfile.getComponents(); assertEquals(components.size(), 1); assertTrue(components.contains(myTheiaEditor)); }
@Test public void shouldNotProvisionDefaultPluginIfDevfileAlreadyContainsSuchButWithDifferentVersion() throws Exception { provisioner = new DefaultEditorProvisioner( EDITOR_REF, new String[] {TERMINAL_PLUGIN_REF}, "", fqnParser, pluginFQNParser); DevfileImpl devfile = new DevfileImpl(); ComponentImpl myTerminal = new ComponentImpl( PLUGIN_COMPONENT_TYPE, EDITOR_PUBLISHER + "/" + TERMINAL_PLUGIN_NAME + "/my-custom"); devfile.getComponents().add(myTerminal); provisioner.apply(devfile, fileContentProvider); List<ComponentImpl> components = devfile.getComponents(); assertEquals(components.size(), 2); assertTrue(components.contains(new ComponentImpl(EDITOR_COMPONENT_TYPE, EDITOR_REF))); assertTrue(components.contains(myTerminal)); }
@Test public void shouldNotProvisionDefaultPluginIfDevfileAlreadyContainsSuchByReference() throws Exception { provisioner = new DefaultEditorProvisioner( EDITOR_REF, new String[] {TERMINAL_PLUGIN_REF}, "", fqnParser, pluginFQNParser); DevfileImpl devfile = new DevfileImpl(); String meta = "apiVersion: v2\n" + "publisher: " + EDITOR_PUBLISHER + "\n" + "name: " + TERMINAL_PLUGIN_NAME + "\n" + "version: " + TERMINAL_PLUGIN_VERSION + "\n" + "type: Che Plugin"; ComponentImpl myTerminal = new ComponentImpl( PLUGIN_COMPONENT_TYPE, null, "https: when(fileContentProvider.fetchContent(anyString())).thenReturn(meta); devfile.getComponents().add(myTerminal); provisioner.apply(devfile, fileContentProvider); List<ComponentImpl> components = devfile.getComponents(); assertEquals(components.size(), 2); assertTrue(components.contains(new ComponentImpl(EDITOR_COMPONENT_TYPE, EDITOR_REF))); assertTrue(components.contains(myTerminal)); }
@Test public void shouldGenerateDefaultPluginNameIfIdIsNotUnique() throws Exception { provisioner = new DefaultEditorProvisioner( EDITOR_REF, new String[] {EDITOR_PUBLISHER + "/" + "my-plugin/v2.0"}, "", fqnParser, pluginFQNParser); DevfileImpl devfile = new DevfileImpl(); ComponentImpl myPlugin = new ComponentImpl( PLUGIN_COMPONENT_TYPE, EDITOR_PUBLISHER + "/" + "my-custom-plugin/v0.0.3"); devfile.getComponents().add(myPlugin); provisioner.apply(devfile, fileContentProvider); List<ComponentImpl> components = devfile.getComponents(); assertEquals(components.size(), 3); assertTrue(components.contains(new ComponentImpl(EDITOR_COMPONENT_TYPE, EDITOR_REF))); assertTrue(components.contains(myPlugin)); ComponentImpl defaultPlugin = findByRef(components, EDITOR_PUBLISHER + "/" + "my-plugin/v2.0"); assertNotNull(defaultPlugin); assertNull(defaultPlugin.getAlias()); }
@Test public void shouldResolveDefaultReferencePlugins() throws Exception { String referencePluginRef = "https: provisioner = new DefaultEditorProvisioner( EDITOR_REF, new String[] {EDITOR_PUBLISHER + "/" + "my-plugin/v2.0", referencePluginRef}, "", fqnParser, pluginFQNParser); String meta = "apiVersion: v2\n" + "publisher: " + EDITOR_PUBLISHER + "\n" + "name: " + COMMAND_PLUGIN_NAME + "\n" + "version: v1.0.0" + "\n" + "type: Che Plugin"; DevfileImpl devfile = new DevfileImpl(); when(fileContentProvider.fetchContent(anyString())).thenReturn(meta); ComponentImpl myPlugin = new ComponentImpl(PLUGIN_COMPONENT_TYPE, COMMAND_PLUGIN_REF); myPlugin.setReference(referencePluginRef); myPlugin.setId(COMMAND_PLUGIN_REF); provisioner.apply(devfile, fileContentProvider); List<ComponentImpl> components = devfile.getComponents(); assertEquals(components.size(), 3); assertTrue(components.contains(myPlugin)); }
@Test public void shouldProvisionAsyncStoragePluginsIfWorkspaceHasOnlyOneAttribute() throws Exception { provisioner = new DefaultEditorProvisioner( EDITOR_REF, new String[] {TERMINAL_PLUGIN_REF}, ASYNC_STORAGE_PLUGIN_REF, fqnParser, pluginFQNParser); DevfileImpl devfile = new DevfileImpl(); devfile.setAttributes(ImmutableMap.of(Constants.ASYNC_PERSIST_ATTRIBUTE, "true")); provisioner.apply(devfile, fileContentProvider); List<ComponentImpl> components = devfile.getComponents(); assertEquals(components.size(), 2); assertFalse( components.contains(new ComponentImpl(PLUGIN_COMPONENT_TYPE, ASYNC_STORAGE_PLUGIN_REF))); }
@Test public void shouldProvisionAsyncStoragePluginsIfWorkspaceHasBothAttributes() throws Exception { provisioner = new DefaultEditorProvisioner( EDITOR_REF, new String[] {TERMINAL_PLUGIN_REF}, ASYNC_STORAGE_PLUGIN_REF, fqnParser, pluginFQNParser); DevfileImpl devfile = new DevfileImpl(); devfile.setAttributes( ImmutableMap.of( Constants.ASYNC_PERSIST_ATTRIBUTE, "true", Constants.PERSIST_VOLUMES_ATTRIBUTE, "false")); provisioner.apply(devfile, fileContentProvider); List<ComponentImpl> components = devfile.getComponents(); assertEquals(components.size(), 3); assertTrue( components.contains(new ComponentImpl(PLUGIN_COMPONENT_TYPE, ASYNC_STORAGE_PLUGIN_REF))); }