Search in sources :

Example 1 with FactoryDevfileV2Dto

use of org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto in project che-server by eclipse-che.

the class URLFactoryBuilderTest method testDevfileV2WithFilename.

@Test
public void testDevfileV2WithFilename() throws ApiException, DevfileException {
    String myLocation = "http://foo-location/";
    Map<String, Object> devfileAsMap = Map.of("hello", "there", "how", "are", "you", "?");
    JsonNode devfile = new ObjectNode(JsonNodeFactory.instance);
    when(devfileParser.parseYamlRaw(anyString())).thenReturn(devfile);
    when(devfileParser.convertYamlToMap(devfile)).thenReturn(devfileAsMap);
    when(devfileVersionDetector.devfileMajorVersion(devfile)).thenReturn(2);
    RemoteFactoryUrl githubLikeRemoteUrl = new RemoteFactoryUrl() {

        @Override
        public String getProviderName() {
            return null;
        }

        @Override
        public List<DevfileLocation> devfileFileLocations() {
            return Collections.singletonList(new DevfileLocation() {

                @Override
                public Optional<String> filename() {
                    return Optional.of("devfile.yaml");
                }

                @Override
                public String location() {
                    return myLocation;
                }
            });
        }

        @Override
        public String rawFileLocation(String filename) {
            return null;
        }

        @Override
        public String getHostName() {
            return null;
        }

        @Override
        public String getBranch() {
            return null;
        }

        @Override
        public void setDevfileFilename(String devfileName) {
        }
    };
    FactoryMetaDto factory = urlFactoryBuilder.createFactoryFromDevfile(githubLikeRemoteUrl, s -> myLocation + ".list", emptyMap()).get();
    assertNotNull(factory);
    assertEquals(factory.getSource(), "devfile.yaml");
    assertTrue(factory instanceof FactoryDevfileV2Dto);
    assertEquals(((FactoryDevfileV2Dto) factory).getDevfile(), devfileAsMap);
}
Also used : UnknownScmProviderException(org.eclipse.che.api.factory.server.scm.exception.UnknownScmProviderException) Listeners(org.testng.annotations.Listeners) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) Test(org.testng.annotations.Test) Collections.singletonList(java.util.Collections.singletonList) FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto) DevfileLocation(org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl.DevfileLocation) DevfileVersionDetector(org.eclipse.che.api.workspace.server.devfile.DevfileVersionDetector) FactoryMetaDto(org.eclipse.che.api.factory.shared.dto.FactoryMetaDto) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) WORKSPACE_TOOLING_PLUGINS_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_TOOLING_PLUGINS_ATTRIBUTE) DtoFactory.newDto(org.eclipse.che.dto.server.DtoFactory.newDto) BeforeMethod(org.testng.annotations.BeforeMethod) Assert.assertNotNull(org.testng.Assert.assertNotNull) MetadataImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.MetadataImpl) List(java.util.List) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Optional(java.util.Optional) FactoryDevfileV2Dto(org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) KUBERNETES_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.KUBERNETES_COMPONENT_TYPE) Assert.assertNull(org.testng.Assert.assertNull) DataProvider(org.testng.annotations.DataProvider) URLFetcher(org.eclipse.che.api.workspace.server.devfile.URLFetcher) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) Mock(org.mockito.Mock) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) Assert.assertEquals(org.testng.Assert.assertEquals) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DevfileParser(org.eclipse.che.api.workspace.server.devfile.DevfileParser) ApiException(org.eclipse.che.api.core.ApiException) DevfileImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl) Collections.singletonMap(java.util.Collections.singletonMap) ExtendedError(org.eclipse.che.api.core.rest.shared.dto.ExtendedError) Collections.emptyMap(java.util.Collections.emptyMap) WorkspaceConfigDto(org.eclipse.che.api.workspace.shared.dto.WorkspaceConfigDto) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) WORKSPACE_TOOLING_EDITOR_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_TOOLING_EDITOR_ATTRIBUTE) FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) ServerException(org.eclipse.che.api.core.ServerException) OverrideParameterException(org.eclipse.che.api.workspace.server.devfile.exception.OverrideParameterException) Assert.assertTrue(org.testng.Assert.assertTrue) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) FactoryDevfileV2Dto(org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Optional(java.util.Optional) FactoryMetaDto(org.eclipse.che.api.factory.shared.dto.FactoryMetaDto) 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 2 with FactoryDevfileV2Dto

use of org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto in project che-server by eclipse-che.

the class URLFactoryBuilderTest method testDevfileSpecifyingFilename.

@Test
public void testDevfileSpecifyingFilename() throws ApiException, DevfileException {
    String myLocation = "http://foo-location/";
    Map<String, Object> devfileAsMap = Map.of("hello", "there", "how", "are", "you", "?");
    JsonNode devfile = new ObjectNode(JsonNodeFactory.instance);
    when(devfileParser.parseYamlRaw(anyString())).thenReturn(devfile);
    when(devfileParser.convertYamlToMap(devfile)).thenReturn(devfileAsMap);
    when(devfileVersionDetector.devfileMajorVersion(devfile)).thenReturn(2);
    RemoteFactoryUrl githubLikeRemoteUrl = new RemoteFactoryUrl() {

        private String devfileName = "default-devfile.yaml";

        @Override
        public String getProviderName() {
            return null;
        }

        @Override
        public List<DevfileLocation> devfileFileLocations() {
            return Collections.singletonList(new DevfileLocation() {

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

                @Override
                public String location() {
                    return myLocation;
                }
            });
        }

        @Override
        public String rawFileLocation(String filename) {
            return null;
        }

        @Override
        public String getHostName() {
            return null;
        }

        @Override
        public String getBranch() {
            return null;
        }

        @Override
        public void setDevfileFilename(String devfileName) {
            this.devfileName = devfileName;
        }
    };
    String pathToDevfile = "my-custom-devfile-path.yaml";
    Map<String, String> propertiesMap = singletonMap(URLFactoryBuilder.DEVFILE_FILENAME, pathToDevfile);
    FactoryMetaDto factory = urlFactoryBuilder.createFactoryFromDevfile(githubLikeRemoteUrl, s -> myLocation + ".list", propertiesMap).get();
    assertNotNull(factory);
    // Check that we didn't fetch from default files but from the parameter
    assertEquals(factory.getSource(), pathToDevfile);
    assertTrue(factory instanceof FactoryDevfileV2Dto);
    assertEquals(((FactoryDevfileV2Dto) factory).getDevfile(), devfileAsMap);
}
Also used : UnknownScmProviderException(org.eclipse.che.api.factory.server.scm.exception.UnknownScmProviderException) Listeners(org.testng.annotations.Listeners) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) Test(org.testng.annotations.Test) Collections.singletonList(java.util.Collections.singletonList) FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto) DevfileLocation(org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl.DevfileLocation) DevfileVersionDetector(org.eclipse.che.api.workspace.server.devfile.DevfileVersionDetector) FactoryMetaDto(org.eclipse.che.api.factory.shared.dto.FactoryMetaDto) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) WORKSPACE_TOOLING_PLUGINS_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_TOOLING_PLUGINS_ATTRIBUTE) DtoFactory.newDto(org.eclipse.che.dto.server.DtoFactory.newDto) BeforeMethod(org.testng.annotations.BeforeMethod) Assert.assertNotNull(org.testng.Assert.assertNotNull) MetadataImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.MetadataImpl) List(java.util.List) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Optional(java.util.Optional) FactoryDevfileV2Dto(org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) KUBERNETES_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.KUBERNETES_COMPONENT_TYPE) Assert.assertNull(org.testng.Assert.assertNull) DataProvider(org.testng.annotations.DataProvider) URLFetcher(org.eclipse.che.api.workspace.server.devfile.URLFetcher) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) Mock(org.mockito.Mock) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) Assert.assertEquals(org.testng.Assert.assertEquals) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DevfileParser(org.eclipse.che.api.workspace.server.devfile.DevfileParser) ApiException(org.eclipse.che.api.core.ApiException) DevfileImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl) Collections.singletonMap(java.util.Collections.singletonMap) ExtendedError(org.eclipse.che.api.core.rest.shared.dto.ExtendedError) Collections.emptyMap(java.util.Collections.emptyMap) WorkspaceConfigDto(org.eclipse.che.api.workspace.shared.dto.WorkspaceConfigDto) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) WORKSPACE_TOOLING_EDITOR_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_TOOLING_EDITOR_ATTRIBUTE) FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) ServerException(org.eclipse.che.api.core.ServerException) OverrideParameterException(org.eclipse.che.api.workspace.server.devfile.exception.OverrideParameterException) Assert.assertTrue(org.testng.Assert.assertTrue) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DevfileLocation(org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl.DevfileLocation) FactoryDevfileV2Dto(org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto) FactoryMetaDto(org.eclipse.che.api.factory.shared.dto.FactoryMetaDto) Test(org.testng.annotations.Test)

Example 3 with FactoryDevfileV2Dto

use of org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto in project che-server by eclipse-che.

the class GithubFactoryParametersResolverTest method shouldSetScmInfoIntoDevfileV2.

@Test
public void shouldSetScmInfoIntoDevfileV2() throws Exception {
    String githubUrl = "https://github.com/eclipse/che/tree/foobar";
    FactoryDevfileV2Dto computedFactory = generateDevfileV2Factory();
    when(urlFactoryBuilder.createFactoryFromDevfile(any(RemoteFactoryUrl.class), any(), anyMap())).thenReturn(Optional.of(computedFactory));
    Map<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, githubUrl);
    // when
    FactoryDevfileV2Dto factory = (FactoryDevfileV2Dto) githubFactoryParametersResolver.createFactory(params);
    // then
    ScmInfo scmInfo = factory.getScmInfo();
    assertNotNull(scmInfo);
    assertEquals(scmInfo.getScmProviderName(), "github");
    assertEquals(scmInfo.getRepositoryUrl(), "https://github.com/eclipse/che.git");
    assertEquals(scmInfo.getBranch(), "foobar");
}
Also used : FactoryDevfileV2Dto(org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto) ScmInfo(org.eclipse.che.api.core.model.factory.ScmInfo) RemoteFactoryUrl(org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl) Test(org.testng.annotations.Test)

Example 4 with FactoryDevfileV2Dto

use of org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto in project che-server by eclipse-che.

the class FactoryLinksHelper method createLinks.

/**
 * Creates factory links.
 *
 * @param serviceContext the context to retrieve factory service base URI
 * @return list of factory links
 */
public static List<Link> createLinks(FactoryMetaDto factory, ServiceContext serviceContext, AdditionalFilenamesProvider additionalFilenamesProvider, String userName, String repositoryUrl) {
    final List<Link> links = new LinkedList<>();
    final UriBuilder uriBuilder = serviceContext.getServiceUriBuilder();
    final String factoryId = factory.getId();
    if (factoryId != null) {
        // creation of link to retrieve factory
        links.add(createLink(HttpMethod.GET, uriBuilder.clone().path(FactoryService.class, "getFactory").build(factoryId).toString(), null, APPLICATION_JSON, RETRIEVE_FACTORY_REL_ATT));
        // creation of accept factory link
        final Link createWorkspace = createLink(HttpMethod.GET, uriBuilder.clone().replacePath("f").queryParam("id", factoryId).build().toString(), null, TEXT_HTML, FACTORY_ACCEPTANCE_REL_ATT);
        links.add(createWorkspace);
    }
    if (!isNullOrEmpty(factory.getName()) && !isNullOrEmpty(userName)) {
        // creation of accept factory link by name and creator
        final Link createWorkspaceFromNamedFactory = createLink(HttpMethod.GET, uriBuilder.clone().replacePath("f").queryParam("name", factory.getName()).queryParam("user", userName).build().toString(), null, TEXT_HTML, NAMED_FACTORY_ACCEPTANCE_REL_ATT);
        links.add(createWorkspaceFromNamedFactory);
    }
    if (factory instanceof FactoryDevfileV2Dto) {
        // link to devfile source
        if (!isNullOrEmpty(factory.getSource())) {
            links.add(createLink(HttpMethod.GET, uriBuilder.clone().replacePath("api").path(ScmService.class).path(ScmService.class, "resolveFile").queryParam("repository", repositoryUrl).queryParam("file", factory.getSource()).build(factoryId).toString(), factory.getSource() + " content"));
        }
        if (((FactoryDevfileV2Dto) factory).getScmInfo() != null) {
            // additional files links
            for (String additionalFile : additionalFilenamesProvider.get()) {
                links.add(createLink(HttpMethod.GET, uriBuilder.clone().replacePath("api").path(ScmService.class).path(ScmService.class, "resolveFile").queryParam("repository", repositoryUrl).queryParam("file", additionalFile).build(factoryId).toString(), additionalFile + " content"));
            }
        }
    }
    return links;
}
Also used : FactoryDevfileV2Dto(org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto) UriBuilder(jakarta.ws.rs.core.UriBuilder) Link(org.eclipse.che.api.core.rest.shared.dto.Link) LinksHelper.createLink(org.eclipse.che.api.core.util.LinksHelper.createLink) LinkedList(java.util.LinkedList)

Example 5 with FactoryDevfileV2Dto

use of org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto in project devspaces-images by redhat-developer.

the class GitlabFactoryParametersResolverTest method shouldSetScmInfoIntoDevfileV2.

@Test
public void shouldSetScmInfoIntoDevfileV2() throws Exception {
    String gitlabUrl = "http://gitlab.2mcl.com/eclipse/che/-/tree/foobar";
    FactoryDevfileV2Dto computedFactory = generateDevfileV2Factory();
    when(urlFactoryBuilder.createFactoryFromDevfile(any(RemoteFactoryUrl.class), any(), anyMap())).thenReturn(Optional.of(computedFactory));
    Map<String, String> params = ImmutableMap.of(URL_PARAMETER_NAME, gitlabUrl);
    // when
    FactoryDevfileV2Dto factory = (FactoryDevfileV2Dto) gitlabFactoryParametersResolver.createFactory(params);
    // then
    ScmInfo scmInfo = factory.getScmInfo();
    assertNotNull(scmInfo);
    assertEquals(scmInfo.getScmProviderName(), "gitlab");
    assertEquals(scmInfo.getRepositoryUrl(), "http://gitlab.2mcl.com/eclipse/che.git");
    assertEquals(scmInfo.getBranch(), "foobar");
}
Also used : FactoryDevfileV2Dto(org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto) ScmInfo(org.eclipse.che.api.core.model.factory.ScmInfo) RemoteFactoryUrl(org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl) Test(org.testng.annotations.Test)

Aggregations

FactoryDevfileV2Dto (org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto)16 Test (org.testng.annotations.Test)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)8 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)8 IOException (java.io.IOException)8 Collections (java.util.Collections)8 Collections.emptyMap (java.util.Collections.emptyMap)8 Collections.singletonList (java.util.Collections.singletonList)8 Collections.singletonMap (java.util.Collections.singletonMap)8 HashMap (java.util.HashMap)8 List (java.util.List)8 Map (java.util.Map)8 Optional (java.util.Optional)8 ApiException (org.eclipse.che.api.core.ApiException)8 ServerException (org.eclipse.che.api.core.ServerException)8 UnauthorizedException (org.eclipse.che.api.core.UnauthorizedException)8 ExtendedError (org.eclipse.che.api.core.rest.shared.dto.ExtendedError)8 ScmCommunicationException (org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException)8 ScmUnauthorizedException (org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException)8