blob: 6d0c4cc3c2d8f26880ed36ea8a7f7fb2586eb414 [file] [log] [blame]
Shawn McCarney55f496c2020-04-09 16:19:14 -05001/**
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
27using namespace phosphor::power::regulators;
28
29TEST(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
52TEST(ExceptionUtilsTests, Log)
53{
54 try
55 {
56 try
57 {
58 throw std::invalid_argument{"JSON element is not an array"};
59 }
60 catch (...)
61 {
62 std::throw_with_nested(
63 std::logic_error{"Unable to parse config file"});
64 }
65 }
66 catch (const std::exception& e)
67 {
68 journal::clear();
69 exception_utils::log(e);
70 const std::vector<std::string>& messages = journal::getErrMessages();
71 EXPECT_EQ(messages.size(), 2);
72 EXPECT_EQ(messages[0], "JSON element is not an array");
73 EXPECT_EQ(messages[1], "Unable to parse config file");
74 }
75}
76
77// Test for getMessages() function in the internal namespace
78TEST(ExceptionUtilsTests, GetMessagesInternal)
79{
80 // Test where exception is not nested
81 {
82 std::invalid_argument e{"JSON element is not an array"};
83 std::vector<std::string> messages{};
84 exception_utils::internal::getMessages(e, messages);
85 EXPECT_EQ(messages.size(), 1);
86 EXPECT_EQ(messages[0], "JSON element is not an array");
87 }
88
89 // Test where exception is nested
90 try
91 {
92 try
93 {
94 try
95 {
96 throw std::invalid_argument{"JSON element is not an array"};
97 }
98 catch (...)
99 {
100 std::throw_with_nested(
101 std::logic_error{"Unable to parse config file"});
102 }
103 }
104 catch (...)
105 {
106 std::throw_with_nested(
107 std::runtime_error{"Unable to configure regulators"});
108 }
109 }
110 catch (const std::exception& e)
111 {
112 std::vector<std::string> messages{};
113 exception_utils::internal::getMessages(e, messages);
114 EXPECT_EQ(messages.size(), 3);
115 EXPECT_EQ(messages[0], "JSON element is not an array");
116 EXPECT_EQ(messages[1], "Unable to parse config file");
117 EXPECT_EQ(messages[2], "Unable to configure regulators");
118 }
119
120 // Test where nested exception is not a child of std::exception
121 try
122 {
123 try
124 {
125 try
126 {
127 throw "JSON element is not an array";
128 }
129 catch (...)
130 {
131 std::throw_with_nested(
132 std::logic_error{"Unable to parse config file"});
133 }
134 }
135 catch (...)
136 {
137 std::throw_with_nested(
138 std::runtime_error{"Unable to configure regulators"});
139 }
140 }
141 catch (const std::exception& e)
142 {
143 std::vector<std::string> messages{};
144 exception_utils::internal::getMessages(e, messages);
145 EXPECT_EQ(messages.size(), 2);
146 EXPECT_EQ(messages[0], "Unable to parse config file");
147 EXPECT_EQ(messages[1], "Unable to configure regulators");
148 }
149}