Search in sources :

Example 46 with DevfileException

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

the class KubernetesComponentToWorkspaceApplier method retrieveContent.

private String retrieveContent(Component recipeComponent, FileContentProvider fileContentProvider) throws DevfileException {
    checkArgument(fileContentProvider != null, "Content provider must not be null");
    if (!isNullOrEmpty(recipeComponent.getReferenceContent())) {
        return recipeComponent.getReferenceContent();
    }
    String recipeFileContent;
    try {
        recipeFileContent = fileContentProvider.fetchContent(recipeComponent.getReference());
    } catch (DevfileException e) {
        throw new DevfileException(format("Fetching content of file `%s` specified in `reference` field of component `%s` is not supported. " + "Please provide its content in `referenceContent` field. Cause: %s", recipeComponent.getReference(), getIdentifiableComponentName(recipeComponent), e.getMessage()), e);
    } catch (IOException e) {
        throw new DevfileException(format("Error during recipe content retrieval for component '%s' with type '%s': %s", getIdentifiableComponentName(recipeComponent), recipeComponent.getType(), e.getMessage()), e);
    }
    if (isNullOrEmpty(recipeFileContent)) {
        throw new DevfileException(format("The reference file '%s' defined in component '%s' is empty.", recipeComponent.getReference(), getIdentifiableComponentName(recipeComponent)));
    }
    return recipeFileContent;
}
Also used : IOException(java.io.IOException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)

Example 47 with DevfileException

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

the class URLFactoryBuilderTest method testDevfileV2.

@Test
public void testDevfileV2() 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);
    FactoryMetaDto factory = urlFactoryBuilder.createFactoryFromDevfile(new DefaultFactoryUrl().withDevfileFileLocation(myLocation), s -> myLocation + ".list", emptyMap()).get();
    assertNotNull(factory);
    assertNull(factory.getSource());
    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) FactoryMetaDto(org.eclipse.che.api.factory.shared.dto.FactoryMetaDto) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.testng.annotations.Test)

Example 48 with DevfileException

use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException 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 49 with DevfileException

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

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 50 with DevfileException

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

the class DevfileToApiExceptionMapperTest method shouldReturnServerExceptionWhenCauseIsCommunicationException.

@Test
public void shouldReturnServerExceptionWhenCauseIsCommunicationException() {
    ScmCommunicationException communicationException = new ScmCommunicationException("unknown");
    ApiException exception = DevfileToApiExceptionMapper.toApiException(new DevfileException("text", communicationException));
    assertTrue(exception instanceof ServerException);
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ApiException(org.eclipse.che.api.core.ApiException) Test(org.testng.annotations.Test)

Aggregations

DevfileException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)52 Test (org.testng.annotations.Test)22 IOException (java.io.IOException)20 ApiException (org.eclipse.che.api.core.ApiException)18 FileContentProvider (org.eclipse.che.api.workspace.server.devfile.FileContentProvider)18 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)16 HashMap (java.util.HashMap)14 List (java.util.List)14 DevfileImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl)14 Map (java.util.Map)12 Optional (java.util.Optional)12 ServerException (org.eclipse.che.api.core.ServerException)12 ExtendedError (org.eclipse.che.api.core.rest.shared.dto.ExtendedError)12 ScmCommunicationException (org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException)12 ScmUnauthorizedException (org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException)12 ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)10 Collections (java.util.Collections)10 UnauthorizedException (org.eclipse.che.api.core.UnauthorizedException)10 UnknownScmProviderException (org.eclipse.che.api.factory.server.scm.exception.UnknownScmProviderException)10