blob: 0f13041a4bf54ff11ddcaf72d81292bd6ac0a1ff [file] [log] [blame]
Austin Schuhcb108412019-10-13 16:09:54 -07001#include "aos/configuration.h"
2
3#include "absl/strings/strip.h"
4#include "aos/json_to_flatbuffer.h"
5#include "aos/testing/test_logging.h"
6#include "aos/util/file.h"
7#include "gtest/gtest.h"
8
9namespace aos {
10namespace configuration {
11namespace testing {
12
13class ConfigurationTest : public ::testing::Test {
14 public:
15 ConfigurationTest() { ::aos::testing::EnableTestLogging(); }
16};
17
18typedef ConfigurationTest ConfigurationDeathTest;
19
20// *the* expected location for all working tests.
21const char *kExpectedLocation =
22 "{ \"name\": \"/foo\", \"type\": \".aos.bar\", \"max_size\": 5 }";
23
24// Tests that we can read and merge a configuration.
25TEST_F(ConfigurationTest, ConfigMerge) {
Austin Schuh40485ed2019-10-26 21:51:44 -070026 FlatbufferDetachedBuffer<Configuration> config =
27 ReadConfig("aos/testdata/config1.json");
Austin Schuhcb108412019-10-13 16:09:54 -070028 printf("Read: %s\n", FlatbufferToJson(config, true).c_str());
29
30 EXPECT_EQ(
31 absl::StripSuffix(
32 util::ReadFileToStringOrDie("aos/testdata/expected.json"), "\n"),
33 FlatbufferToJson(config, true));
34}
35
36// Tests that we die when a file is imported twice.
37TEST_F(ConfigurationDeathTest, DuplicateFile) {
38 EXPECT_DEATH(
39 {
Austin Schuh40485ed2019-10-26 21:51:44 -070040 FlatbufferDetachedBuffer<Configuration> config =
Austin Schuhcb108412019-10-13 16:09:54 -070041 ReadConfig("aos/testdata/config1_bad.json");
42 },
43 "aos/testdata/config1_bad.json");
44}
45
46// Tests that we can lookup a location, complete with maps, from a merged
47// config.
Austin Schuh40485ed2019-10-26 21:51:44 -070048TEST_F(ConfigurationTest, GetChannel) {
49 FlatbufferDetachedBuffer<Configuration> config =
50 ReadConfig("aos/testdata/config1.json");
Austin Schuhcb108412019-10-13 16:09:54 -070051
52 // Test a basic lookup first.
Austin Schuh40485ed2019-10-26 21:51:44 -070053 EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/foo", ".aos.bar", "app1")),
Austin Schuhcb108412019-10-13 16:09:54 -070054 kExpectedLocation);
55
56 // Test that an invalid name results in nullptr back.
Austin Schuh40485ed2019-10-26 21:51:44 -070057 EXPECT_EQ(GetChannel(config, "/invalid_name", ".aos.bar", "app1"), nullptr);
Austin Schuhcb108412019-10-13 16:09:54 -070058
59 // Tests that a root map/rename works. And that they get processed from the
60 // bottom up.
61 EXPECT_EQ(
Austin Schuh40485ed2019-10-26 21:51:44 -070062 FlatbufferToJson(GetChannel(config, "/batman", ".aos.bar", "app1")),
Austin Schuhcb108412019-10-13 16:09:54 -070063 kExpectedLocation);
64
65 // And then test that an application specific map/rename works.
Austin Schuh40485ed2019-10-26 21:51:44 -070066 EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/bar", ".aos.bar", "app1")),
Austin Schuhcb108412019-10-13 16:09:54 -070067 kExpectedLocation);
Austin Schuh40485ed2019-10-26 21:51:44 -070068 EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/baz", ".aos.bar", "app2")),
Austin Schuhcb108412019-10-13 16:09:54 -070069 kExpectedLocation);
70
71 // And then test that an invalid application name gets properly ignored.
Austin Schuh40485ed2019-10-26 21:51:44 -070072 EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/foo", ".aos.bar", "app3")),
Austin Schuhcb108412019-10-13 16:09:54 -070073 kExpectedLocation);
74}
75
76} // namespace testing
77} // namespace configuration
78} // namespace aos