| 1 |
#include <pbcopper/utility/Alarm.h> |
| 2 |
|
| 3 |
#include <sstream> |
| 4 |
#include <stdexcept> |
| 5 |
#include <string> |
| 6 |
#include <vector> |
| 7 |
|
| 8 |
#include <gtest/gtest.h> |
| 9 |
|
| 10 |
namespace AlarmTests { |
| 11 |
|
| 12 |
const std::string TestUuid{"44af40b0-2986-401e-b21a-7051b40189fb"}; |
| 13 |
|
| 14 |
} // namespace AlarmTests |
| 15 |
|
| 16 |
// clang-format off |
| 17 |
TEST(Utility_Alarm, can_write_single_alarm) |
| 18 |
{ |
| 19 |
const std::string expectedText{ |
| 20 |
R"({ |
| 21 |
"exception": "This is an exception field", |
| 22 |
"id": "44af40b0-2986-401e-b21a-7051b40189fb", |
| 23 |
"message": "Hello, world!", |
| 24 |
"name": "test_alarm", |
| 25 |
"owner": "", |
| 26 |
"severity": "ERROR" |
| 27 |
})"}; |
| 28 |
PacBio::Utility::Alarm alarm{"test_alarm", "Hello, world!", "ERROR", "This is an info field", |
| 29 |
"This is an exception field"}; |
| 30 |
alarm.Id(AlarmTests::TestUuid); |
| 31 |
|
| 32 |
std::ostringstream s; |
| 33 |
alarm.Print(s); |
| 34 |
EXPECT_EQ(expectedText, s.str()); |
| 35 |
|
| 36 |
s.str(""); |
| 37 |
s << alarm; |
| 38 |
EXPECT_EQ(expectedText, s.str()); |
| 39 |
} |
| 40 |
// clang-format on |
| 41 |
|
| 42 |
TEST(Utility_Alarm, can_write_multiple_alarms) |
| 43 |
{ |
| 44 |
const PacBio::Utility::Alarm alarm1{"test_alarm1", "Hello world!"}; |
| 45 |
const PacBio::Utility::Alarm alarm2{"test_alarm2", "Hello again!"}; |
| 46 |
|
| 47 |
std::ostringstream s; |
| 48 |
PacBio::Utility::Alarm::WriteAlarms(s, {alarm1, alarm2}); |
| 49 |
|
| 50 |
EXPECT_NE(s.str().find("test_alarm1"), std::string::npos); |
| 51 |
EXPECT_NE(s.str().find("test_alarm2"), std::string::npos); |
| 52 |
} |
| 53 |
|
| 54 |
// clang-format off |
| 55 |
TEST(Utility_Alarm, can_throw_typed_alarm_exceptions) |
| 56 |
{ |
| 57 |
try { |
| 58 |
throw PB_BARCODE_ALARM("invalid barcode label: 'foo'"); |
| 59 |
} catch (const PacBio::Utility::AlarmException& e) { |
| 60 |
const std::string expectedText{ |
| 61 |
R"([ |
| 62 |
{ |
| 63 |
"exception": "", |
| 64 |
"id": "44af40b0-2986-401e-b21a-7051b40189fb", |
| 65 |
"message": "invalid barcode label: 'foo'", |
| 66 |
"name": "BarcodeError", |
| 67 |
"owner": "", |
| 68 |
"severity": "FATAL" |
| 69 |
} |
| 70 |
])"}; |
| 71 |
std::ostringstream s; |
| 72 |
PacBio::Utility::Alarm alarm{e}; |
| 73 |
alarm.Id(AlarmTests::TestUuid); |
| 74 |
|
| 75 |
PacBio::Utility::Alarm::WriteAlarms(s, {alarm}); |
| 76 |
EXPECT_EQ(s.str(), expectedText); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
TEST(Utility_Alarm, can_throw_generic_alarm_exceptions) |
| 81 |
{ |
| 82 |
try { |
| 83 |
throw PB_ALARM("Invalid data", "record_123 is missing 'sn' tag"); |
| 84 |
} catch (const PacBio::Utility::AlarmException& e) { |
| 85 |
const std::string expectedText{ |
| 86 |
R"([ |
| 87 |
{ |
| 88 |
"exception": "", |
| 89 |
"id": "44af40b0-2986-401e-b21a-7051b40189fb", |
| 90 |
"message": "record_123 is missing 'sn' tag", |
| 91 |
"name": "Invalid data", |
| 92 |
"owner": "", |
| 93 |
"severity": "FATAL" |
| 94 |
} |
| 95 |
])"}; |
| 96 |
std::ostringstream s; |
| 97 |
PacBio::Utility::Alarm alarm{e}; |
| 98 |
alarm.Id(AlarmTests::TestUuid); |
| 99 |
|
| 100 |
PacBio::Utility::Alarm::WriteAlarms(s, {alarm}); |
| 101 |
EXPECT_EQ(s.str(), expectedText); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
TEST(Utility_Alarm, throws_on_empty_alarm_filename) |
| 106 |
{ |
| 107 |
const std::vector<PacBio::Utility::Alarm> alarms { |
| 108 |
PacBio::Utility::Alarm{"test_alarm1", "Hello world!"}, |
| 109 |
PacBio::Utility::Alarm{"test_alarm2", "Hello again!"}, |
| 110 |
}; |
| 111 |
|
| 112 |
std::string fn; |
| 113 |
EXPECT_THROW(PacBio::Utility::Alarm::WriteAlarms(fn, alarms), std::runtime_error); |
| 114 |
} |
| 115 |
|
| 116 |
// clang-format on |