1*638d1487SAlexander Hansen #include "config-validator.hpp"
2*638d1487SAlexander Hansen #include "grouplayout.hpp"
3*638d1487SAlexander Hansen #include "ledlayout.hpp"
4*638d1487SAlexander Hansen 
5*638d1487SAlexander Hansen #include <gtest/gtest.h>
6*638d1487SAlexander Hansen 
7*638d1487SAlexander Hansen using namespace phosphor::led;
8*638d1487SAlexander Hansen 
assertValidationException(const GroupMap & ledMap,error::ConfigValidationError err)9*638d1487SAlexander Hansen static void assertValidationException(const GroupMap& ledMap,
10*638d1487SAlexander Hansen                                       error::ConfigValidationError err)
11*638d1487SAlexander Hansen {
12*638d1487SAlexander Hansen     try
13*638d1487SAlexander Hansen     {
14*638d1487SAlexander Hansen         validateConfigV1(ledMap);
15*638d1487SAlexander Hansen         ASSERT_FALSE(true);
16*638d1487SAlexander Hansen     }
17*638d1487SAlexander Hansen     catch (ConfigValidationException& e)
18*638d1487SAlexander Hansen     {
19*638d1487SAlexander Hansen         ASSERT_EQ(e.reason, err);
20*638d1487SAlexander Hansen     }
21*638d1487SAlexander Hansen }
22*638d1487SAlexander Hansen 
TEST(validateConfig,testGoodPathLedPriority)23*638d1487SAlexander Hansen TEST(validateConfig, testGoodPathLedPriority)
24*638d1487SAlexander Hansen {
25*638d1487SAlexander Hansen     ActionSet group1ActionSet = {
26*638d1487SAlexander Hansen         {"led1", Layout::Action::On, 0, 0, Layout::Action::On},
27*638d1487SAlexander Hansen         {"led2", Layout::Action::On, 0, 0, Layout::Action::Blink},
28*638d1487SAlexander Hansen     };
29*638d1487SAlexander Hansen     Layout::GroupLayout group1 = {0, group1ActionSet};
30*638d1487SAlexander Hansen     GroupMap ledMap = {{"group1", group1}};
31*638d1487SAlexander Hansen 
32*638d1487SAlexander Hansen     validateConfigV1(ledMap);
33*638d1487SAlexander Hansen }
34*638d1487SAlexander Hansen 
TEST(validateConfig,testGoodPathGroupPriority)35*638d1487SAlexander Hansen TEST(validateConfig, testGoodPathGroupPriority)
36*638d1487SAlexander Hansen {
37*638d1487SAlexander Hansen     ActionSet group1ActionSet = {
38*638d1487SAlexander Hansen         {"led1", Layout::Action::On, 0, 0, std::nullopt},
39*638d1487SAlexander Hansen         {"led2", Layout::Action::On, 0, 0, std::nullopt},
40*638d1487SAlexander Hansen     };
41*638d1487SAlexander Hansen     ActionSet group2ActionSet = {
42*638d1487SAlexander Hansen         {"led1", Layout::Action::On, 0, 0, std::nullopt},
43*638d1487SAlexander Hansen         {"led2", Layout::Action::On, 0, 0, std::nullopt},
44*638d1487SAlexander Hansen     };
45*638d1487SAlexander Hansen     Layout::GroupLayout group1 = {1, group1ActionSet};
46*638d1487SAlexander Hansen     Layout::GroupLayout group2 = {2, group1ActionSet};
47*638d1487SAlexander Hansen     GroupMap ledMap = {
48*638d1487SAlexander Hansen         {"group1", group1},
49*638d1487SAlexander Hansen         {"group2", group2},
50*638d1487SAlexander Hansen     };
51*638d1487SAlexander Hansen 
52*638d1487SAlexander Hansen     validateConfigV1(ledMap);
53*638d1487SAlexander Hansen }
54*638d1487SAlexander Hansen 
TEST(validateConfig,testLedPriorityMismatch)55*638d1487SAlexander Hansen TEST(validateConfig, testLedPriorityMismatch)
56*638d1487SAlexander Hansen {
57*638d1487SAlexander Hansen     ActionSet group1ActionSet = {
58*638d1487SAlexander Hansen         {"led1", Layout::Action::On, 0, 0, Layout::Action::On},
59*638d1487SAlexander Hansen     };
60*638d1487SAlexander Hansen     ActionSet group2ActionSet = {
61*638d1487SAlexander Hansen         {"led1", Layout::Action::On, 0, 0, Layout::Action::Off},
62*638d1487SAlexander Hansen     };
63*638d1487SAlexander Hansen     Layout::GroupLayout group1 = {0, group1ActionSet};
64*638d1487SAlexander Hansen     Layout::GroupLayout group2 = {0, group2ActionSet};
65*638d1487SAlexander Hansen     GroupMap ledMap = {
66*638d1487SAlexander Hansen         {"group1", group1},
67*638d1487SAlexander Hansen         {"group2", group2},
68*638d1487SAlexander Hansen     };
69*638d1487SAlexander Hansen 
70*638d1487SAlexander Hansen     assertValidationException(ledMap, error::LedPriorityMismatch);
71*638d1487SAlexander Hansen }
72*638d1487SAlexander Hansen 
TEST(validateConfig,testMissingLedPriority)73*638d1487SAlexander Hansen TEST(validateConfig, testMissingLedPriority)
74*638d1487SAlexander Hansen {
75*638d1487SAlexander Hansen     ActionSet group1ActionSet = {
76*638d1487SAlexander Hansen         {"led1", Layout::Action::On, 0, 0, Layout::Action::On},
77*638d1487SAlexander Hansen     };
78*638d1487SAlexander Hansen     ActionSet group2ActionSet = {
79*638d1487SAlexander Hansen         {"led1", Layout::Action::On, 0, 0, std::nullopt},
80*638d1487SAlexander Hansen     };
81*638d1487SAlexander Hansen     Layout::GroupLayout group1 = {0, group1ActionSet};
82*638d1487SAlexander Hansen     Layout::GroupLayout group2 = {0, group2ActionSet};
83*638d1487SAlexander Hansen     GroupMap ledMap = {
84*638d1487SAlexander Hansen         {"group1", group1},
85*638d1487SAlexander Hansen         {"group2", group2},
86*638d1487SAlexander Hansen     };
87*638d1487SAlexander Hansen 
88*638d1487SAlexander Hansen     assertValidationException(ledMap, error::MissingLedPriority);
89*638d1487SAlexander Hansen }
90*638d1487SAlexander Hansen 
TEST(validateConfig,testMixedLedAndGroupPriority)91*638d1487SAlexander Hansen TEST(validateConfig, testMixedLedAndGroupPriority)
92*638d1487SAlexander Hansen {
93*638d1487SAlexander Hansen     ActionSet group1ActionSet = {
94*638d1487SAlexander Hansen         {"led1", Layout::Action::On, 0, 0, Layout::Action::On},
95*638d1487SAlexander Hansen     };
96*638d1487SAlexander Hansen     ActionSet group2ActionSet = {
97*638d1487SAlexander Hansen         {"led1", Layout::Action::On, 0, 0, Layout::Action::On},
98*638d1487SAlexander Hansen     };
99*638d1487SAlexander Hansen     Layout::GroupLayout group1 = {0, group1ActionSet};
100*638d1487SAlexander Hansen     Layout::GroupLayout group2 = {1, group2ActionSet};
101*638d1487SAlexander Hansen     GroupMap ledMap = {
102*638d1487SAlexander Hansen         {"group1", group1},
103*638d1487SAlexander Hansen         {"group2", group2},
104*638d1487SAlexander Hansen     };
105*638d1487SAlexander Hansen 
106*638d1487SAlexander Hansen     assertValidationException(ledMap, error::MixedLedAndGroupPriority);
107*638d1487SAlexander Hansen }
108*638d1487SAlexander Hansen 
TEST(validateConfig,testInvalidGroupPriority)109*638d1487SAlexander Hansen TEST(validateConfig, testInvalidGroupPriority)
110*638d1487SAlexander Hansen {
111*638d1487SAlexander Hansen     ActionSet group1ActionSet = {
112*638d1487SAlexander Hansen         {"led1", Layout::Action::On, 0, 0, std::nullopt},
113*638d1487SAlexander Hansen     };
114*638d1487SAlexander Hansen     ActionSet group2ActionSet = {
115*638d1487SAlexander Hansen         {"led1", Layout::Action::On, 0, 0, std::nullopt},
116*638d1487SAlexander Hansen     };
117*638d1487SAlexander Hansen     Layout::GroupLayout group1 = {0, group1ActionSet};
118*638d1487SAlexander Hansen     Layout::GroupLayout group2 = {1, group2ActionSet};
119*638d1487SAlexander Hansen     GroupMap ledMap = {
120*638d1487SAlexander Hansen         {"group1", group1},
121*638d1487SAlexander Hansen         {"group2", group2},
122*638d1487SAlexander Hansen     };
123*638d1487SAlexander Hansen 
124*638d1487SAlexander Hansen     assertValidationException(ledMap, error::InvalidGroupPriority);
125*638d1487SAlexander Hansen }
126*638d1487SAlexander Hansen 
TEST(validateConfig,testDuplicateGroupPriority)127*638d1487SAlexander Hansen TEST(validateConfig, testDuplicateGroupPriority)
128*638d1487SAlexander Hansen {
129*638d1487SAlexander Hansen     ActionSet group1ActionSet = {
130*638d1487SAlexander Hansen         {"led1", Layout::Action::On, 0, 0, std::nullopt},
131*638d1487SAlexander Hansen     };
132*638d1487SAlexander Hansen     ActionSet group2ActionSet = {
133*638d1487SAlexander Hansen         {"led1", Layout::Action::On, 0, 0, std::nullopt},
134*638d1487SAlexander Hansen     };
135*638d1487SAlexander Hansen     Layout::GroupLayout group1 = {1, group1ActionSet};
136*638d1487SAlexander Hansen     Layout::GroupLayout group2 = {1, group2ActionSet};
137*638d1487SAlexander Hansen     GroupMap ledMap = {
138*638d1487SAlexander Hansen         {"group1", group1},
139*638d1487SAlexander Hansen         {"group2", group2},
140*638d1487SAlexander Hansen     };
141*638d1487SAlexander Hansen 
142*638d1487SAlexander Hansen     assertValidationException(ledMap, error::DuplicateGroupPriority);
143*638d1487SAlexander Hansen }
144