Search in sources :

Example 1 with Devfile

use of org.eclipse.che.api.core.model.workspace.devfile.Devfile in project che-server by eclipse-che.

the class WorkspaceServiceTest method shouldCreateWorkspaceFromDevfile.

@Test
public void shouldCreateWorkspaceFromDevfile() throws Exception {
    final DevfileDto devfileDto = createDevfileDto();
    final WorkspaceImpl workspace = createWorkspace(devfileDto);
    when(wsManager.createWorkspace(any(Devfile.class), anyString(), any(), any())).thenReturn(workspace);
    final Response response = given().auth().basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD).contentType("application/json").body(devfileDto).when().post(SECURE_PATH + "/workspace/devfile" + "?namespace=test" + "&attribute=factoryId:factory123" + "&attribute=custom:custom:value");
    assertEquals(response.getStatusCode(), 201);
    assertEquals(new WorkspaceImpl(unwrapDto(response, WorkspaceDto.class), TEST_ACCOUNT), workspace);
    verify(wsManager).createWorkspace(any(Devfile.class), eq("test"), eq(ImmutableMap.of("factoryId", "factory123", "custom", "custom:value")), any());
}
Also used : Response(io.restassured.response.Response) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) DevfileDto(org.eclipse.che.api.workspace.shared.dto.devfile.DevfileDto) Devfile(org.eclipse.che.api.core.model.workspace.devfile.Devfile) Test(org.testng.annotations.Test)

Example 2 with Devfile

use of org.eclipse.che.api.core.model.workspace.devfile.Devfile in project che-server by eclipse-che.

the class DevfileIntegrityValidator method validateComponents.

private Set<String> validateComponents(Devfile devfile) throws DevfileFormatException {
    Set<String> definedAliases = new HashSet<>();
    Component editorComponent = null;
    Map<String, Set<String>> idsPerComponentType = new HashMap<>();
    for (Component component : devfile.getComponents()) {
        if (component.getAlias() != null && !definedAliases.add(component.getAlias())) {
            throw new DevfileFormatException(format("Duplicate component alias found:'%s'", component.getAlias()));
        }
        Optional<Map.Entry<String, Long>> duplicatedEndpoint = component.getEndpoints().stream().map(Endpoint::getName).collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().stream().filter(e -> e.getValue() > 1L).findFirst();
        if (duplicatedEndpoint.isPresent()) {
            throw new DevfileFormatException(format("Duplicated endpoint name '%s' found in '%s' component", duplicatedEndpoint.get().getKey(), getIdentifiableComponentName(component)));
        }
        Set<String> tempSet = new HashSet<>();
        for (Env env : component.getEnv()) {
            if (!tempSet.add(env.getName())) {
                throw new DevfileFormatException(format("Duplicate environment variable '%s' found in component '%s'", env.getName(), getIdentifiableComponentName(component)));
            }
        }
        if (!idsPerComponentType.computeIfAbsent(component.getType(), __ -> new HashSet<>()).add(getIdentifiableComponentName(component))) {
            throw new DevfileFormatException(format("There are multiple components '%s' of type '%s' that cannot be uniquely" + " identified. Please add aliases that would distinguish the components.", getIdentifiableComponentName(component), component.getType()));
        }
        if (component.getAutomountWorkspaceSecrets() != null && component.getAlias() == null) {
            throw new DevfileFormatException(format("The 'automountWorkspaceSecrets' property cannot be used in component which doesn't have alias. " + "Please add alias to component '%s' that would allow to distinguish its containers.", getIdentifiableComponentName(component)));
        }
        switch(component.getType()) {
            case EDITOR_COMPONENT_TYPE:
                if (editorComponent != null) {
                    throw new DevfileFormatException(format("Multiple editor components found: '%s', '%s'", getIdentifiableComponentName(editorComponent), getIdentifiableComponentName(component)));
                }
                editorComponent = component;
                break;
            case PLUGIN_COMPONENT_TYPE:
            case KUBERNETES_COMPONENT_TYPE:
            case OPENSHIFT_COMPONENT_TYPE:
            case DOCKERIMAGE_COMPONENT_TYPE:
                // do nothing
                break;
            default:
                throw new DevfileFormatException(format("One of the components has unsupported component type: '%s'", component.getType()));
        }
    }
    return definedAliases;
}
Also used : KUBERNETES_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.KUBERNETES_COMPONENT_TYPE) Env(org.eclipse.che.api.core.model.workspace.devfile.Env) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) PLUGIN_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.PLUGIN_COMPONENT_TYPE) Function(java.util.function.Function) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Map(java.util.Map) Components.getIdentifiableComponentName(org.eclipse.che.api.workspace.server.devfile.Components.getIdentifiableComponentName) EDITOR_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.EDITOR_COMPONENT_TYPE) DOCKERIMAGE_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.DOCKERIMAGE_COMPONENT_TYPE) Component(org.eclipse.che.api.core.model.workspace.devfile.Component) Devfile(org.eclipse.che.api.core.model.workspace.devfile.Devfile) OPENSHIFT_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.OPENSHIFT_COMPONENT_TYPE) Set(java.util.Set) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) Optional(java.util.Optional) Command(org.eclipse.che.api.core.model.workspace.devfile.Command) Project(org.eclipse.che.api.core.model.workspace.devfile.Project) Pattern(java.util.regex.Pattern) Action(org.eclipse.che.api.core.model.workspace.devfile.Action) Endpoint(org.eclipse.che.api.core.model.workspace.devfile.Endpoint) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Env(org.eclipse.che.api.core.model.workspace.devfile.Env) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) Component(org.eclipse.che.api.core.model.workspace.devfile.Component) HashSet(java.util.HashSet)

Example 3 with Devfile

use of org.eclipse.che.api.core.model.workspace.devfile.Devfile in project devspaces-images by redhat-developer.

the class WorkspaceServiceTest method shouldCreateWorkspaceFromDevfile.

@Test
public void shouldCreateWorkspaceFromDevfile() throws Exception {
    final DevfileDto devfileDto = createDevfileDto();
    final WorkspaceImpl workspace = createWorkspace(devfileDto);
    when(wsManager.createWorkspace(any(Devfile.class), anyString(), any(), any())).thenReturn(workspace);
    final Response response = given().auth().basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD).contentType("application/json").body(devfileDto).when().post(SECURE_PATH + "/workspace/devfile" + "?namespace=test" + "&attribute=factoryId:factory123" + "&attribute=custom:custom:value");
    assertEquals(response.getStatusCode(), 201);
    assertEquals(new WorkspaceImpl(unwrapDto(response, WorkspaceDto.class), TEST_ACCOUNT), workspace);
    verify(wsManager).createWorkspace(any(Devfile.class), eq("test"), eq(ImmutableMap.of("factoryId", "factory123", "custom", "custom:value")), any());
}
Also used : Response(io.restassured.response.Response) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) DevfileDto(org.eclipse.che.api.workspace.shared.dto.devfile.DevfileDto) Devfile(org.eclipse.che.api.core.model.workspace.devfile.Devfile) Test(org.testng.annotations.Test)

Example 4 with Devfile

use of org.eclipse.che.api.core.model.workspace.devfile.Devfile in project che-server by eclipse-che.

the class WorkspaceManagerTest method shouldFailTocreateWorkspaceUsingInconsistentDevfile.

@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "boom")
public void shouldFailTocreateWorkspaceUsingInconsistentDevfile() throws Exception {
    // given
    doThrow(new DevfileFormatException("boom")).when(devfileIntegrityValidator).validateContentReferences(any(), any());
    Devfile devfile = mock(Devfile.class);
    // when
    workspaceManager.createWorkspace(devfile, "ns", emptyMap(), null);
// then exception is thrown
}
Also used : DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) Devfile(org.eclipse.che.api.core.model.workspace.devfile.Devfile) Test(org.testng.annotations.Test)

Example 5 with Devfile

use of org.eclipse.che.api.core.model.workspace.devfile.Devfile in project che-server by eclipse-che.

the class WorkspaceServiceTest method shouldReturnBadRequestOnInvalidDevfile.

@Test
public void shouldReturnBadRequestOnInvalidDevfile() throws Exception {
    final DevfileDto devfileDto = createDevfileDto();
    final WorkspaceImpl workspace = createWorkspace(devfileDto);
    when(wsManager.createWorkspace(any(Devfile.class), anyString(), any(), any())).thenThrow(new ValidationException("boom"));
    final Response response = given().auth().basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD).contentType("application/json").body(devfileDto).when().post(SECURE_PATH + "/workspace/devfile" + "?namespace=test" + "&attribute=factoryId:factory123" + "&attribute=custom:custom:value");
    assertEquals(response.getStatusCode(), 400);
    String error = unwrapError(response);
    assertEquals(error, "boom");
}
Also used : Response(io.restassured.response.Response) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) ValidationException(org.eclipse.che.api.core.ValidationException) DevfileDto(org.eclipse.che.api.workspace.shared.dto.devfile.DevfileDto) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Devfile(org.eclipse.che.api.core.model.workspace.devfile.Devfile) Test(org.testng.annotations.Test)

Aggregations

Devfile (org.eclipse.che.api.core.model.workspace.devfile.Devfile)8 Test (org.testng.annotations.Test)6 Response (io.restassured.response.Response)4 DevfileFormatException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException)4 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)4 DevfileDto (org.eclipse.che.api.workspace.shared.dto.devfile.DevfileDto)4 String.format (java.lang.String.format)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Function (java.util.function.Function)2 Pattern (java.util.regex.Pattern)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 Singleton (javax.inject.Singleton)2 ValidationException (org.eclipse.che.api.core.ValidationException)2 Action (org.eclipse.che.api.core.model.workspace.devfile.Action)2 Command (org.eclipse.che.api.core.model.workspace.devfile.Command)2