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