Search through config for schema of specified flatbuffer type

Allow user to call function that returns a schema reflection in
the config for flatbuffer type instead of having to find a
matching schema manually.

Signed-off-by: Nathan Leong <100028864@mvla.net>
Change-Id: I40e836e8f379ac48f32633d49d8c01a1530697df
Signed-off-by: Nathan Leong <100028864@mvla.net>
diff --git a/aos/configuration_test.cc b/aos/configuration_test.cc
index fa74e20..fc45d88 100644
--- a/aos/configuration_test.cc
+++ b/aos/configuration_test.cc
@@ -1,6 +1,7 @@
 #include "aos/configuration.h"
 
 #include "absl/strings/strip.h"
+#include "aos/events/ping_generated.h"
 #include "aos/json_to_flatbuffer.h"
 #include "aos/testing/flatbuffer_eq.h"
 #include "aos/testing/path.h"
@@ -1007,10 +1008,47 @@
       JsonToFlatbuffer<Channel>(
           "{ \"name\": \"/foo\", \"type\": \".aos.bar\", \"num_readers\": 5, "
           "\"num_senders\": 10 }");
-
   EXPECT_EQ(QueueScratchBufferSize(&channel.message()), 15);
 }
 
+// Tests that GetSchema returns schema of specified type
+TEST_F(ConfigurationTest, GetSchema) {
+  FlatbufferDetachedBuffer<Configuration> config =
+      ReadConfig(ArtifactPath("aos/events/pingpong_config.json"));
+  FlatbufferVector<reflection::Schema> expected_schema =
+      FileToFlatbuffer<reflection::Schema>(
+          ArtifactPath("aos/events/ping.bfbs"));
+  EXPECT_EQ(FlatbufferToJson(GetSchema(&config.message(), "aos.examples.Ping")),
+            FlatbufferToJson(expected_schema));
+  EXPECT_EQ(GetSchema(&config.message(), "invalid_name"), nullptr);
+}
+
+// Tests that GetSchema template returns schema of specified type
+TEST_F(ConfigurationTest, GetSchemaTemplate) {
+  FlatbufferDetachedBuffer<Configuration> config =
+      ReadConfig(ArtifactPath("aos/events/pingpong_config.json"));
+  FlatbufferVector<reflection::Schema> expected_schema =
+      FileToFlatbuffer<reflection::Schema>(
+          ArtifactPath("aos/events/ping.bfbs"));
+  EXPECT_EQ(FlatbufferToJson(GetSchema<aos::examples::Ping>(&config.message())),
+            FlatbufferToJson(expected_schema));
+}
+
+// Tests that GetSchemaDetachedBuffer returns detached buffer of specified type
+TEST_F(ConfigurationTest, GetSchemaDetachedBuffer) {
+  FlatbufferDetachedBuffer<Configuration> config =
+      ReadConfig(ArtifactPath("aos/events/pingpong_config.json"));
+  FlatbufferVector<reflection::Schema> expected_schema =
+      FileToFlatbuffer<reflection::Schema>(
+          ArtifactPath("aos/events/ping.bfbs"));
+  EXPECT_EQ(FlatbufferToJson(
+                GetSchemaDetachedBuffer(&config.message(), "aos.examples.Ping")
+                    .value()),
+            FlatbufferToJson(expected_schema));
+  EXPECT_EQ(GetSchemaDetachedBuffer(&config.message(), "invalid_name"),
+            std::nullopt);
+}
+
 }  // namespace testing
 }  // namespace configuration
 }  // namespace aos