Skip to content

Commit 3c16100

Browse files
committed
Add tests for the router YAML validator and JSON schema
Signed-off-by: David Laseca Perez <davidlaseca@eprosima.com>
1 parent 7dc4357 commit 3c16100

17 files changed

Lines changed: 687 additions & 0 deletions

ddsrouter_yaml/test/unittest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
# TODO uncomment when new API applied
2020
add_subdirectory(configuration)
2121
add_subdirectory(participants)
22+
add_subdirectory(ddsrouter_yaml_validator)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright 2026 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
#################################
16+
# DdsRouter Yaml Validator Test #
17+
#################################
18+
19+
set(TEST_NAME YamlValidatorDdsRouterTest)
20+
21+
22+
set(TEST_SOURCES
23+
YamlValidatorDdsRouterTest.cpp
24+
)
25+
26+
set(TEST_LIST
27+
validation_passed
28+
validation_failed
29+
)
30+
31+
set(TEST_EXTRA_LIBRARIES
32+
yaml-cpp
33+
fastcdr
34+
fastdds
35+
cpp_utils
36+
ddspipe_core
37+
ddspipe_participants
38+
ddspipe_yaml
39+
)
40+
41+
configure_file(
42+
"${PROJECT_SOURCE_DIR}/../resources/configurations/ddsrouter_config_schema.json"
43+
"${CMAKE_CURRENT_BINARY_DIR}/ddsrouter_config_schema.json"
44+
COPYONLY
45+
)
46+
47+
file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/valid_config_files_router/")
48+
file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/invalid_config_files_router/")
49+
50+
file(GLOB_RECURSE TEST_NEEDED_SOURCES
51+
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
52+
"invalid_config_files_router/*.yaml"
53+
"valid_config_files_router/*.yaml"
54+
)
55+
56+
file(GLOB GETTING_STARTED_YAMLS
57+
"${PROJECT_SOURCE_DIR}/../docs/resources/getting_started/*.yaml"
58+
)
59+
file(COPY ${GETTING_STARTED_YAMLS}
60+
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/valid_config_files_router/"
61+
)
62+
63+
file(GLOB EXAMPLES_YAMLS
64+
"${PROJECT_SOURCE_DIR}/../resources/configurations/examples/*.yaml"
65+
)
66+
file(COPY ${EXAMPLES_YAMLS}
67+
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/valid_config_files_router/"
68+
)
69+
70+
add_unittest_executable(
71+
"${TEST_NAME}"
72+
"${TEST_SOURCES}"
73+
"${TEST_LIST}"
74+
"${TEST_EXTRA_LIBRARIES}"
75+
"${TEST_NEEDED_SOURCES}"
76+
)
77+
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright 2026 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <filesystem>
16+
#include <fstream>
17+
#include <iostream>
18+
19+
#include <cpp_utils/testing/gtest_aux.hpp>
20+
#include <gtest/gtest.h>
21+
22+
#include <ddspipe_yaml/YamlManager.hpp>
23+
#include <ddspipe_yaml/YamlValidator.hpp>
24+
25+
using namespace eprosima;
26+
using namespace eprosima::ddspipe::yaml;
27+
28+
namespace test {
29+
// Paths and files for the tests
30+
std::string schema_path = "./ddsrouter_config_schema.json";
31+
32+
// Vectors with the valid and invalid YAML files
33+
std::vector<std::string> valid_files = []()
34+
{
35+
std::vector<std::string> files;
36+
for (const auto& entry : std::filesystem::directory_iterator("./valid_config_files_router/"))
37+
{
38+
if (entry.path().extension() == ".yaml")
39+
{
40+
files.push_back(entry.path().generic_string());
41+
}
42+
}
43+
return files;
44+
}();
45+
46+
std::vector<std::string> invalid_files = []()
47+
{
48+
std::vector<std::string> files;
49+
for (const auto& entry : std::filesystem::directory_iterator("./invalid_config_files_router/"))
50+
{
51+
if (entry.path().extension() == ".yaml")
52+
{
53+
files.push_back(entry.path().generic_string());
54+
}
55+
}
56+
return files;
57+
}();
58+
} // namespace test
59+
60+
/**
61+
* Test that a set of valid YAML configurations pass the validation
62+
*/
63+
TEST(YamlValidatorDdsRouterTest, validation_passed)
64+
{
65+
YamlValidator validator = YamlValidator(YamlValidator::from_file(test::schema_path));
66+
67+
// valid files
68+
{
69+
for (std::string st : test::valid_files)
70+
{
71+
Yaml yml = YamlManager::load_file(st);
72+
ASSERT_TRUE(validator.validate_YAML(yml)) << "Failed for file: " << st;
73+
}
74+
}
75+
76+
}
77+
78+
/**
79+
* Test that a set of invalid YAML configurations don't pass the validation
80+
*/
81+
TEST(YamlValidatorDdsRouterTest, validation_failed)
82+
{
83+
YamlValidator validator = YamlValidator(YamlValidator::from_file(test::schema_path));
84+
85+
// invalid files
86+
{
87+
for (std::string st : test::invalid_files)
88+
{
89+
Yaml yml = YamlManager::load_file(st);
90+
// Validate is called with false to prevent filling the output with the specific errors
91+
ASSERT_FALSE(validator.validate_YAML(yml, false)) << "Failed for file: " << st;
92+
}
93+
}
94+
}
95+
96+
int main(
97+
int argc,
98+
char** argv)
99+
{
100+
::testing::InitGoogleTest(&argc, argv);
101+
return RUN_ALL_TESTS();
102+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
version: v5.0
2+
3+
allowlist:
4+
- name: HelloWorldTopic
5+
type: HelloWorld
6+
- name: rt/chatter
7+
type: std_msgs::msg::dds_::String_
8+
9+
10+
participants:
11+
12+
- name: EchoParticipant
13+
kind: echo
14+
15+
- name: discovery_participant
16+
kind: discovery-server
17+
discovery-server-guid:
18+
guid: 01.0f.00.00.00.00.00.00.00.00.ca.fe
19+
id: 0
20+
ros-discovery-server: true
21+
listening-addresses:
22+
- port: 11667
23+
transport: udp
24+
ip-version: v4
25+
connection-addresses:
26+
- discovery-server-guid:
27+
guid: 01.0f.00.00.00.00.00.00.00.00.ca.fe
28+
id: 0
29+
ros-discovery-server: true
30+
addresses:
31+
- ip: 127.0.0.1
32+
domain: localhost
33+
port: 11667
34+
transport: udp
35+
ip-version: v4
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
version: v5.0
2+
3+
allowlist:
4+
- name: HelloWorldTopic
5+
type: HelloWorld
6+
- name: rt/chatter
7+
type: std_msgs::msg::dds_::String_
8+
9+
10+
participants:
11+
12+
- name: EchoParticipant
13+
kind: echo
14+
15+
- name: discovery_participant
16+
kind: discovery-server
17+
discovery-server-guid:
18+
guid: 01.0f.00.00.00.00.00.00.00.00.ca.fe
19+
id: 0
20+
ros-discovery-server: true
21+
listening-addresses:
22+
- ip: 127.0.0.1
23+
domain: localhost
24+
transport: udp
25+
ip-version: v4
26+
connection-addresses:
27+
- discovery-server-guid:
28+
guid: 01.0f.00.00.00.00.00.00.00.00.ca.fe
29+
id: 0
30+
ros-discovery-server: true
31+
addresses:
32+
- ip: 127.0.0.1
33+
domain: localhost
34+
port: 11667
35+
transport: udp
36+
ip-version: v4
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: v5.0
2+
3+
builtin-topics:
4+
- type: HelloWorld
5+
6+
7+
participants:
8+
9+
- name: SimpleParticipant
10+
kind: local
11+
domain: 0
12+
13+
- name: EchoParticipant
14+
kind: echo
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: v5.0
2+
3+
builtin-topics:
4+
- name: HelloWorldTopic
5+
6+
7+
participants:
8+
9+
- name: SimpleParticipant
10+
kind: local
11+
domain: 0
12+
13+
- name: EchoParticipant
14+
kind: echo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
version: v5.0
2+
3+
allowlist:
4+
- name: HelloWorldTopic
5+
type: HelloWorld
6+
- name: rt/chatter
7+
type: std_msgs::msg::dds_::String_
8+
9+
10+
participants:
11+
12+
- name: EchoParticipant
13+
kind: echo
14+
15+
- name: discovery_participant
16+
kind: discovery-server
17+
listening-addresses:
18+
- ip: 127.0.0.1
19+
domain: localhost
20+
port: 11667
21+
transport: udp
22+
ip-version: v4
23+
connection-addresses:
24+
- discovery-server-guid:
25+
guid: 01.0f.00.00.00.00.00.00.00.00.ca.fe
26+
id: 0
27+
ros-discovery-server: true
28+
addresses:
29+
- ip: 127.0.0.1
30+
domain: localhost
31+
port: 11667
32+
transport: udp
33+
ip-version: v4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: v5.0
2+
3+
allowlist:
4+
- name: HelloWorldTopic
5+
type: HelloWorld
6+
- name: rt/chatter
7+
type: std_msgs::msg::dds_::String_
8+
9+
10+
participants:
11+
12+
- name: EchoParticipant
13+
kind: echo
14+
15+
- name: discovery_participant
16+
kind: discovery-server
17+
discovery-server-guid:
18+
guid: 01.0f.00.00.00.00.00.00.00.00.ca.fe
19+
id: 0
20+
ros-discovery-server: true
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: v5.0
2+
3+
allowlist:
4+
- type: HelloWorld
5+
6+
7+
participants:
8+
9+
- name: SimpleParticipant
10+
kind: local
11+
domain: 0
12+
13+
- name: EchoParticipant
14+
kind: echo

0 commit comments

Comments
 (0)