config: error on invalid configuration

Since the default led priority is no longer 'Blink', the priority now
has to be explicitly defined when using either group priority or led
priority.

If a configuration does not define the priority, the configuration is
invalid and in the yaml case, phosphor-led-manager should fail to build,
in the json case, the process should exit due to the configuration
error.

The config validation has been extracted into it's own file and made
separate from json config parsing.

So every config will go through the same validation even if its been
created via yaml.

Change-Id: Ifda65942b0768d6c0d3b25076f7a1236b46b3d9f
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/test/utest-config-validator.cpp b/test/utest-config-validator.cpp
new file mode 100644
index 0000000..1eda517
--- /dev/null
+++ b/test/utest-config-validator.cpp
@@ -0,0 +1,143 @@
+#include "config-validator.hpp"
+#include "grouplayout.hpp"
+#include "ledlayout.hpp"
+
+#include <gtest/gtest.h>
+
+using namespace phosphor::led;
+
+static void assertValidationException(const GroupMap& ledMap,
+                                      error::ConfigValidationError err)
+{
+    try
+    {
+        validateConfigV1(ledMap);
+        ASSERT_FALSE(true);
+    }
+    catch (ConfigValidationException& e)
+    {
+        ASSERT_EQ(e.reason, err);
+    }
+}
+
+TEST(validateConfig, testGoodPathLedPriority)
+{
+    ActionSet group1ActionSet = {
+        {"led1", Layout::Action::On, 0, 0, Layout::Action::On},
+        {"led2", Layout::Action::On, 0, 0, Layout::Action::Blink},
+    };
+    Layout::GroupLayout group1 = {0, group1ActionSet};
+    GroupMap ledMap = {{"group1", group1}};
+
+    validateConfigV1(ledMap);
+}
+
+TEST(validateConfig, testGoodPathGroupPriority)
+{
+    ActionSet group1ActionSet = {
+        {"led1", Layout::Action::On, 0, 0, std::nullopt},
+        {"led2", Layout::Action::On, 0, 0, std::nullopt},
+    };
+    ActionSet group2ActionSet = {
+        {"led1", Layout::Action::On, 0, 0, std::nullopt},
+        {"led2", Layout::Action::On, 0, 0, std::nullopt},
+    };
+    Layout::GroupLayout group1 = {1, group1ActionSet};
+    Layout::GroupLayout group2 = {2, group1ActionSet};
+    GroupMap ledMap = {
+        {"group1", group1},
+        {"group2", group2},
+    };
+
+    validateConfigV1(ledMap);
+}
+
+TEST(validateConfig, testLedPriorityMismatch)
+{
+    ActionSet group1ActionSet = {
+        {"led1", Layout::Action::On, 0, 0, Layout::Action::On},
+    };
+    ActionSet group2ActionSet = {
+        {"led1", Layout::Action::On, 0, 0, Layout::Action::Off},
+    };
+    Layout::GroupLayout group1 = {0, group1ActionSet};
+    Layout::GroupLayout group2 = {0, group2ActionSet};
+    GroupMap ledMap = {
+        {"group1", group1},
+        {"group2", group2},
+    };
+
+    assertValidationException(ledMap, error::LedPriorityMismatch);
+}
+
+TEST(validateConfig, testMissingLedPriority)
+{
+    ActionSet group1ActionSet = {
+        {"led1", Layout::Action::On, 0, 0, Layout::Action::On},
+    };
+    ActionSet group2ActionSet = {
+        {"led1", Layout::Action::On, 0, 0, std::nullopt},
+    };
+    Layout::GroupLayout group1 = {0, group1ActionSet};
+    Layout::GroupLayout group2 = {0, group2ActionSet};
+    GroupMap ledMap = {
+        {"group1", group1},
+        {"group2", group2},
+    };
+
+    assertValidationException(ledMap, error::MissingLedPriority);
+}
+
+TEST(validateConfig, testMixedLedAndGroupPriority)
+{
+    ActionSet group1ActionSet = {
+        {"led1", Layout::Action::On, 0, 0, Layout::Action::On},
+    };
+    ActionSet group2ActionSet = {
+        {"led1", Layout::Action::On, 0, 0, Layout::Action::On},
+    };
+    Layout::GroupLayout group1 = {0, group1ActionSet};
+    Layout::GroupLayout group2 = {1, group2ActionSet};
+    GroupMap ledMap = {
+        {"group1", group1},
+        {"group2", group2},
+    };
+
+    assertValidationException(ledMap, error::MixedLedAndGroupPriority);
+}
+
+TEST(validateConfig, testInvalidGroupPriority)
+{
+    ActionSet group1ActionSet = {
+        {"led1", Layout::Action::On, 0, 0, std::nullopt},
+    };
+    ActionSet group2ActionSet = {
+        {"led1", Layout::Action::On, 0, 0, std::nullopt},
+    };
+    Layout::GroupLayout group1 = {0, group1ActionSet};
+    Layout::GroupLayout group2 = {1, group2ActionSet};
+    GroupMap ledMap = {
+        {"group1", group1},
+        {"group2", group2},
+    };
+
+    assertValidationException(ledMap, error::InvalidGroupPriority);
+}
+
+TEST(validateConfig, testDuplicateGroupPriority)
+{
+    ActionSet group1ActionSet = {
+        {"led1", Layout::Action::On, 0, 0, std::nullopt},
+    };
+    ActionSet group2ActionSet = {
+        {"led1", Layout::Action::On, 0, 0, std::nullopt},
+    };
+    Layout::GroupLayout group1 = {1, group1ActionSet};
+    Layout::GroupLayout group2 = {1, group2ActionSet};
+    GroupMap ledMap = {
+        {"group1", group1},
+        {"group2", group2},
+    };
+
+    assertValidationException(ledMap, error::DuplicateGroupPriority);
+}