Search in sources :

Example 6 with DevfileFormatException

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

the class DevfileSchemaValidator method validate.

public void validate(JsonNode contentNode) throws DevfileFormatException {
    try {
        List<Problem> validationErrors = new ArrayList<>();
        ProblemHandler handler = ProblemHandler.collectingTo(validationErrors);
        String devfileVersion = devfileVersionDetector.devfileVersion(contentNode);
        if (!schemasByVersion.containsKey(devfileVersion)) {
            throw new DevfileFormatException(String.format("Version '%s' of the devfile is not supported. Supported versions are '%s'.", devfileVersion, SUPPORTED_VERSIONS));
        }
        JsonSchema schema = schemasByVersion.get(devfileVersion);
        try (JsonReader reader = service.createReader(new StringReader(jsonMapper.writeValueAsString(contentNode)), schema, handler)) {
            reader.read();
        }
        if (!validationErrors.isEmpty()) {
            String error = errorMessageComposer.extractMessages(validationErrors, new StringBuilder());
            throw new DevfileFormatException(error);
        }
    } catch (DevfileException dfe) {
        throw new DevfileFormatException(format("Devfile schema validation failed. Error: %s", dfe.getMessage()));
    } catch (IOException e) {
        throw new DevfileFormatException("Unable to validate Devfile. Error: " + e.getMessage());
    }
}
Also used : ProblemHandler(org.leadpony.justify.api.ProblemHandler) JsonSchema(org.leadpony.justify.api.JsonSchema) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) JsonReader(jakarta.json.JsonReader) Problem(org.leadpony.justify.api.Problem) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) IOException(java.io.IOException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)

Example 7 with DevfileFormatException

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

the class DevfileIntegrityValidator method validateContentReferences.

/**
 * Validates that various selectors in the devfile reference something in the referenced content.
 *
 * @param devfile the validated devfile
 * @param provider the file content provider to fetch the referenced content with.
 * @throws DevfileFormatException when some selectors don't match any objects in the referenced
 *     content or any other exception thrown during the validation
 */
public void validateContentReferences(Devfile devfile, FileContentProvider provider) throws DevfileFormatException {
    for (Component component : devfile.getComponents()) {
        ComponentIntegrityValidator validator = validators.get(component.getType());
        if (validator == null) {
            throw new DevfileFormatException(format("Unknown component type: %s", component.getType()));
        }
        validator.validateComponent(component, provider);
    }
}
Also used : DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) Component(org.eclipse.che.api.core.model.workspace.devfile.Component)

Example 8 with DevfileFormatException

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

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

the class DevfileParserTest method shouldThrowExceptionWhenExceptionOccurredDuringSchemaValidation.

@Test(expectedExceptions = DevfileFormatException.class, expectedExceptionsMessageRegExp = "non valid")
public void shouldThrowExceptionWhenExceptionOccurredDuringSchemaValidation() throws Exception {
    // given
    doThrow(new DevfileFormatException("non valid")).when(schemaValidator).validate(any());
    // when
    devfileParser.parseYaml(DEVFILE_YAML_CONTENT);
}
Also used : DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) Test(org.testng.annotations.Test)

Example 10 with DevfileFormatException

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

the class UserDevfileEntityProvider method readFrom.

@Override
public UserDevfileDto readFrom(Class<UserDevfileDto> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    try {
        JsonNode wsNode = mapper.readTree(entityStream);
        JsonNode devfileNode = wsNode.path("devfile");
        if (!devfileNode.isNull() && !devfileNode.isMissingNode()) {
            devfileParser.parseJson(devfileNode.toString());
        } else {
            throw new BadRequestException("Mandatory field `devfile` is not defined.");
        }
        return DtoFactory.getInstance().createDtoFromJson(wsNode.toString(), UserDevfileDto.class);
    } catch (DevfileFormatException e) {
        throw new BadRequestException(e.getMessage());
    } catch (IOException e) {
        throw new WebApplicationException(e.getMessage(), e);
    }
}
Also used : WebApplicationException(jakarta.ws.rs.WebApplicationException) BadRequestException(jakarta.ws.rs.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) IOException(java.io.IOException)

Aggregations

DevfileFormatException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException)22 IOException (java.io.IOException)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 BadRequestException (jakarta.ws.rs.BadRequestException)4 WebApplicationException (jakarta.ws.rs.WebApplicationException)4 ValidationException (org.eclipse.che.api.core.ValidationException)4 Component (org.eclipse.che.api.core.model.workspace.devfile.Component)4 Devfile (org.eclipse.che.api.core.model.workspace.devfile.Devfile)4 DevfileException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)4 Test (org.testng.annotations.Test)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Container (io.fabric8.kubernetes.api.model.Container)2 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 JsonReader (jakarta.json.JsonReader)2 StringReader (java.io.StringReader)2 String.format (java.lang.String.format)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2