1 /**
2  * Copyright © 2020 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "exception_utils.hpp"
17 #include "journal.hpp"
18 #include "mock_journal.hpp"
19 
20 #include <exception>
21 #include <stdexcept>
22 #include <string>
23 #include <vector>
24 
25 #include <gtest/gtest.h>
26 
27 using namespace phosphor::power::regulators;
28 
29 TEST(ExceptionUtilsTests, GetMessages)
30 {
31     try
32     {
33         try
34         {
35             throw std::invalid_argument{"JSON element is not an array"};
36         }
37         catch (...)
38         {
39             std::throw_with_nested(
40                 std::logic_error{"Unable to parse config file"});
41         }
42     }
43     catch (const std::exception& e)
44     {
45         std::vector<std::string> messages = exception_utils::getMessages(e);
46         EXPECT_EQ(messages.size(), 2);
47         EXPECT_EQ(messages[0], "JSON element is not an array");
48         EXPECT_EQ(messages[1], "Unable to parse config file");
49     }
50 }
51 
52 // Test for getMessages() function in the internal namespace
53 TEST(ExceptionUtilsTests, GetMessagesInternal)
54 {
55     // Test where exception is not nested
56     {
57         std::invalid_argument e{"JSON element is not an array"};
58         std::vector<std::string> messages{};
59         exception_utils::internal::getMessages(e, messages);
60         EXPECT_EQ(messages.size(), 1);
61         EXPECT_EQ(messages[0], "JSON element is not an array");
62     }
63 
64     // Test where exception is nested
65     try
66     {
67         try
68         {
69             try
70             {
71                 throw std::invalid_argument{"JSON element is not an array"};
72             }
73             catch (...)
74             {
75                 std::throw_with_nested(
76                     std::logic_error{"Unable to parse config file"});
77             }
78         }
79         catch (...)
80         {
81             std::throw_with_nested(
82                 std::runtime_error{"Unable to configure regulators"});
83         }
84     }
85     catch (const std::exception& e)
86     {
87         std::vector<std::string> messages{};
88         exception_utils::internal::getMessages(e, messages);
89         EXPECT_EQ(messages.size(), 3);
90         EXPECT_EQ(messages[0], "JSON element is not an array");
91         EXPECT_EQ(messages[1], "Unable to parse config file");
92         EXPECT_EQ(messages[2], "Unable to configure regulators");
93     }
94 
95     // Test where nested exception is not a child of std::exception
96     try
97     {
98         try
99         {
100             try
101             {
102                 throw "JSON element is not an array";
103             }
104             catch (...)
105             {
106                 std::throw_with_nested(
107                     std::logic_error{"Unable to parse config file"});
108             }
109         }
110         catch (...)
111         {
112             std::throw_with_nested(
113                 std::runtime_error{"Unable to configure regulators"});
114         }
115     }
116     catch (const std::exception& e)
117     {
118         std::vector<std::string> messages{};
119         exception_utils::internal::getMessages(e, messages);
120         EXPECT_EQ(messages.size(), 2);
121         EXPECT_EQ(messages[0], "Unable to parse config file");
122         EXPECT_EQ(messages[1], "Unable to configure regulators");
123     }
124 }
125