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());
}
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;
}
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());
}
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
}
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");
}
Aggregations