Search in sources :

Example 26 with FileContentProvider

use of org.eclipse.che.api.workspace.server.devfile.FileContentProvider in project che-server by eclipse-che.

the class EditorComponentToWorkspaceApplier method apply.

/**
 * Applies changes on workspace config according to the specified editor component.
 *
 * @param workspaceConfig workspace config on which changes should be applied
 * @param editorComponent plugin component that should be applied
 * @param contentProvider optional content provider that may be used for external component
 *     resource fetching
 * @throws IllegalArgumentException if specified workspace config or plugin component is null
 * @throws IllegalArgumentException if specified component has type different from cheEditor
 */
@Override
public void apply(WorkspaceConfigImpl workspaceConfig, ComponentImpl editorComponent, FileContentProvider contentProvider) throws DevfileException {
    checkArgument(workspaceConfig != null, "Workspace config must not be null");
    checkArgument(editorComponent != null, "Component must not be null");
    checkArgument(EDITOR_COMPONENT_TYPE.equals(editorComponent.getType()), format("Plugin must have `%s` type", EDITOR_COMPONENT_TYPE));
    final String editorComponentAlias = editorComponent.getAlias();
    final String editorId = editorComponent.getId();
    final String registryUrl = editorComponent.getRegistryUrl();
    final ExtendedPluginFQN fqn = componentFQNParser.evaluateFQN(editorComponent, contentProvider);
    if (!isNullOrEmpty(fqn.getReference())) {
        workspaceConfig.getAttributes().put(WORKSPACE_TOOLING_EDITOR_ATTRIBUTE, fqn.getReference());
    } else {
        workspaceConfig.getAttributes().put(WORKSPACE_TOOLING_EDITOR_ATTRIBUTE, componentFQNParser.getCompositeId(registryUrl, editorId));
    }
    workspaceConfig.getCommands().stream().filter(c -> editorComponentAlias != null && editorComponentAlias.equals(c.getAttributes().get(COMPONENT_ALIAS_COMMAND_ATTRIBUTE))).forEach(c -> c.getAttributes().put(PLUGIN_ATTRIBUTE, fqn.getId()));
    // make sure id is set to be able to match component with plugin broker result
    // when referenceContent is used
    editorComponent.setId(fqn.getId());
}
Also used : PLUGIN_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.PLUGIN_ATTRIBUTE) ExtendedPluginFQN(org.eclipse.che.api.workspace.server.wsplugins.model.ExtendedPluginFQN) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) WORKSPACE_TOOLING_EDITOR_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_TOOLING_EDITOR_ATTRIBUTE) String.format(java.lang.String.format) FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) ComponentToWorkspaceApplier(org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier) Inject(javax.inject.Inject) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) COMPONENT_ALIAS_COMMAND_ATTRIBUTE(org.eclipse.che.api.workspace.server.devfile.Constants.COMPONENT_ALIAS_COMMAND_ATTRIBUTE) EDITOR_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.EDITOR_COMPONENT_TYPE) ComponentFQNParser(org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentFQNParser) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) ExtendedPluginFQN(org.eclipse.che.api.workspace.server.wsplugins.model.ExtendedPluginFQN)

Example 27 with FileContentProvider

use of org.eclipse.che.api.workspace.server.devfile.FileContentProvider in project devspaces-images by redhat-developer.

the class EditorComponentToWorkspaceApplier method apply.

/**
 * Applies changes on workspace config according to the specified editor component.
 *
 * @param workspaceConfig workspace config on which changes should be applied
 * @param editorComponent plugin component that should be applied
 * @param contentProvider optional content provider that may be used for external component
 *     resource fetching
 * @throws IllegalArgumentException if specified workspace config or plugin component is null
 * @throws IllegalArgumentException if specified component has type different from cheEditor
 */
@Override
public void apply(WorkspaceConfigImpl workspaceConfig, ComponentImpl editorComponent, FileContentProvider contentProvider) throws DevfileException {
    checkArgument(workspaceConfig != null, "Workspace config must not be null");
    checkArgument(editorComponent != null, "Component must not be null");
    checkArgument(EDITOR_COMPONENT_TYPE.equals(editorComponent.getType()), format("Plugin must have `%s` type", EDITOR_COMPONENT_TYPE));
    final String editorComponentAlias = editorComponent.getAlias();
    final String editorId = editorComponent.getId();
    final String registryUrl = editorComponent.getRegistryUrl();
    final ExtendedPluginFQN fqn = componentFQNParser.evaluateFQN(editorComponent, contentProvider);
    if (!isNullOrEmpty(fqn.getReference())) {
        workspaceConfig.getAttributes().put(WORKSPACE_TOOLING_EDITOR_ATTRIBUTE, fqn.getReference());
    } else {
        workspaceConfig.getAttributes().put(WORKSPACE_TOOLING_EDITOR_ATTRIBUTE, componentFQNParser.getCompositeId(registryUrl, editorId));
    }
    workspaceConfig.getCommands().stream().filter(c -> editorComponentAlias != null && editorComponentAlias.equals(c.getAttributes().get(COMPONENT_ALIAS_COMMAND_ATTRIBUTE))).forEach(c -> c.getAttributes().put(PLUGIN_ATTRIBUTE, fqn.getId()));
    // make sure id is set to be able to match component with plugin broker result
    // when referenceContent is used
    editorComponent.setId(fqn.getId());
}
Also used : PLUGIN_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.PLUGIN_ATTRIBUTE) ExtendedPluginFQN(org.eclipse.che.api.workspace.server.wsplugins.model.ExtendedPluginFQN) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) WORKSPACE_TOOLING_EDITOR_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_TOOLING_EDITOR_ATTRIBUTE) String.format(java.lang.String.format) FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) ComponentToWorkspaceApplier(org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier) Inject(javax.inject.Inject) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) COMPONENT_ALIAS_COMMAND_ATTRIBUTE(org.eclipse.che.api.workspace.server.devfile.Constants.COMPONENT_ALIAS_COMMAND_ATTRIBUTE) EDITOR_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.EDITOR_COMPONENT_TYPE) ComponentFQNParser(org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentFQNParser) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) ExtendedPluginFQN(org.eclipse.che.api.workspace.server.wsplugins.model.ExtendedPluginFQN)

Example 28 with FileContentProvider

use of org.eclipse.che.api.workspace.server.devfile.FileContentProvider in project devspaces-images by redhat-developer.

the class URLFactoryBuilderTest method checkThatDtoHasCorrectNames.

@Test(dataProvider = "devfiles")
public void checkThatDtoHasCorrectNames(DevfileImpl devfile, String expectedGenerateName) throws ApiException, IOException, OverrideParameterException, DevfileException {
    DefaultFactoryUrl defaultFactoryUrl = mock(DefaultFactoryUrl.class);
    FileContentProvider fileContentProvider = mock(FileContentProvider.class);
    when(defaultFactoryUrl.devfileFileLocations()).thenReturn(singletonList(new DevfileLocation() {

        @Override
        public Optional<String> filename() {
            return Optional.empty();
        }

        @Override
        public String location() {
            return "http://foo.bar/anything";
        }
    }));
    when(fileContentProvider.fetchContent(anyString())).thenReturn("anything");
    when(devfileParser.parseYamlRaw("anything")).thenReturn(new ObjectNode(JsonNodeFactory.instance));
    when(devfileParser.parseJsonNode(any(JsonNode.class), anyMap())).thenReturn(devfile);
    when(devfileVersionDetector.devfileMajorVersion(any(JsonNode.class))).thenReturn(1);
    FactoryDto factory = (FactoryDto) urlFactoryBuilder.createFactoryFromDevfile(defaultFactoryUrl, fileContentProvider, emptyMap()).get();
    assertNull(factory.getDevfile().getMetadata().getName());
    assertEquals(factory.getDevfile().getMetadata().getGenerateName(), expectedGenerateName);
}
Also used : FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DevfileLocation(org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl.DevfileLocation) Test(org.testng.annotations.Test)

Example 29 with FileContentProvider

use of org.eclipse.che.api.workspace.server.devfile.FileContentProvider in project devspaces-images by redhat-developer.

the class URLFactoryBuilderTest method checkCorrectExceptionThrownDependingOnCause.

@Test(dataProvider = "devfileExceptions")
public void checkCorrectExceptionThrownDependingOnCause(Throwable cause, Class expectedClass, String expectedMessage, Map<String, String> expectedAttributes) throws IOException, DevfileException {
    DefaultFactoryUrl defaultFactoryUrl = mock(DefaultFactoryUrl.class);
    FileContentProvider fileContentProvider = mock(FileContentProvider.class);
    when(defaultFactoryUrl.devfileFileLocations()).thenReturn(singletonList(new DevfileLocation() {

        @Override
        public Optional<String> filename() {
            return Optional.empty();
        }

        @Override
        public String location() {
            return "http://foo.bar/anything";
        }
    }));
    when(fileContentProvider.fetchContent(anyString())).thenThrow(new DevfileException("", cause));
    // when
    try {
        urlFactoryBuilder.createFactoryFromDevfile(defaultFactoryUrl, fileContentProvider, emptyMap());
    } catch (ApiException e) {
        assertTrue(e.getClass().isAssignableFrom(expectedClass));
        assertEquals(e.getMessage(), expectedMessage);
        if (e.getServiceError() instanceof ExtendedError)
            assertEquals(((ExtendedError) e.getServiceError()).getAttributes(), expectedAttributes);
    }
}
Also used : FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) ExtendedError(org.eclipse.che.api.core.rest.shared.dto.ExtendedError) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) DevfileLocation(org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl.DevfileLocation) ApiException(org.eclipse.che.api.core.ApiException) Test(org.testng.annotations.Test)

Example 30 with FileContentProvider

use of org.eclipse.che.api.workspace.server.devfile.FileContentProvider in project devspaces-images by redhat-developer.

the class GitlabAuthorizingFileContentProviderTest method shouldExpandRelativePaths.

@Test
public void shouldExpandRelativePaths() throws Exception {
    URLFetcher urlFetcher = Mockito.mock(URLFetcher.class);
    GitlabUrl gitlabUrl = new GitlabUrl().withHostName("https://gitlab.net").withUsername("eclipse").withProject("che");
    FileContentProvider fileContentProvider = new GitlabAuthorizingFileContentProvider(gitlabUrl, urlFetcher, gitCredentialsManager, personalAccessTokenManager);
    fileContentProvider.fetchContent("devfile.yaml");
    verify(urlFetcher).fetch(eq("https://gitlab.net/api/v4/projects/eclipse%2Fche/repository/files/devfile.yaml/raw"));
}
Also used : FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) URLFetcher(org.eclipse.che.api.workspace.server.devfile.URLFetcher) Test(org.testng.annotations.Test)

Aggregations

FileContentProvider (org.eclipse.che.api.workspace.server.devfile.FileContentProvider)38 Test (org.testng.annotations.Test)30 DevfileImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl)18 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)14 URLFetcher (org.eclipse.che.api.workspace.server.devfile.URLFetcher)10 DevfileException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)8 ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)8 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)6 Inject (javax.inject.Inject)6 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Named (javax.inject.Named)4 DevfileLocation (org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl.DevfileLocation)4 EDITOR_COMPONENT_TYPE (org.eclipse.che.api.workspace.server.devfile.Constants.EDITOR_COMPONENT_TYPE)4 ComponentFQNParser (org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentFQNParser)4 ExtendedPluginFQN (org.eclipse.che.api.workspace.server.wsplugins.model.ExtendedPluginFQN)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 String.format (java.lang.String.format)3 ComponentToWorkspaceApplier (org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier)3