Skip to content

Commit c079b4a

Browse files
committed
CAMEL-22931: to only check if can be yaml parsered
1 parent 8b22e94 commit c079b4a

4 files changed

Lines changed: 121 additions & 1 deletion

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.dsl.yaml.validator;
18+
19+
import java.io.File;
20+
import java.util.Collections;
21+
import java.util.List;
22+
23+
import com.fasterxml.jackson.databind.ObjectMapper;
24+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
25+
import com.networknt.schema.ValidationMessage;
26+
27+
/**
28+
* YAML DSL parser that tooling can use to parse Camel source files to check if they can be YAML parsed.
29+
*
30+
* Notice that this parser can only check if the source can be parsed as YAML, but cannot check if any of the content is
31+
* correct Camel DSL, such as if EIPs have typos etc.
32+
*/
33+
public class YamlParser {
34+
35+
private final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
36+
37+
public List<ValidationMessage> parse(File file) throws Exception {
38+
try {
39+
mapper.readTree(file);
40+
return Collections.emptyList();
41+
} catch (Exception e) {
42+
ValidationMessage vm = ValidationMessage.builder().type("parser")
43+
.messageSupplier(() -> e.getClass().getName() + ": " + e.getMessage()).build();
44+
return List.of(vm);
45+
}
46+
}
47+
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.dsl.yaml.validator;
18+
19+
import java.io.File;
20+
21+
import org.junit.jupiter.api.Assertions;
22+
import org.junit.jupiter.api.BeforeAll;
23+
import org.junit.jupiter.api.Test;
24+
25+
public class YamlParserTest {
26+
27+
private static YamlParser parser;
28+
29+
@BeforeAll
30+
public static void setup() throws Exception {
31+
parser = new YamlParser();
32+
}
33+
34+
@Test
35+
public void testParseOk() throws Exception {
36+
Assertions.assertTrue(parser.parse(new File("src/test/resources/foo.yaml")).isEmpty());
37+
Assertions.assertTrue(parser.parse(new File("src/test/resources/bad.yaml")).isEmpty());
38+
}
39+
40+
@Test
41+
public void testParseBad() throws Exception {
42+
var report = parser.parse(new File("src/test/resources/parse-bad.yaml"));
43+
Assertions.assertFalse(report.isEmpty());
44+
Assertions.assertEquals(1, report.size());
45+
Assertions.assertTrue(report.get(0).getMessage().contains("while parsing a block mapping"));
46+
Assertions.assertTrue(report.get(0).getMessage().contains("- setBody:"));
47+
}
48+
}

dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/test/java/org/apache/camel/dsl/yaml/validator/YamlValidatorTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,5 @@ public void testValidateBad() throws Exception {
4343
Assertions.assertFalse(report.isEmpty());
4444
Assertions.assertEquals(1, report.size());
4545
Assertions.assertTrue(report.get(0).getMessage().contains("setCheese"));
46-
System.out.println(report);
4746
}
4847
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
- route:
19+
from:
20+
uri: timer:yaml
21+
parameters:
22+
period: "1000"
23+
- setBody:
24+
simple: Hello Camel from ${routeId}
25+
- log: ${body}

0 commit comments

Comments
 (0)