summaryrefslogtreecommitdiff
path: root/test/redfish-core/include/utils/telemetry_utils_test.cpp
blob: d910a1f8a0f70c33f965b5e8c0a4cca4cbcb3624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#include "generated/enums/metric_report_definition.hpp"
#include "utils/telemetry_utils.hpp"

#include <nlohmann/json.hpp>

#include <optional>
#include <stdexcept>
#include <string>
#include <vector>

#include <gtest/gtest.h>

namespace redfish::telemetry
{
namespace
{

TEST(GetDbusReportPath, AppendsIdToReportsCollection)
{
    EXPECT_EQ(getDbusReportPath("TestReport"),
              "/xyz/openbmc_project/Telemetry/Reports/TelemetryService/"
              "TestReport");
    EXPECT_EQ(getDbusReportPath("Test_Report"),
              "/xyz/openbmc_project/Telemetry/Reports/TelemetryService/"
              "Test_Report");
    EXPECT_EQ(getDbusReportPath("1Report"),
              "/xyz/openbmc_project/Telemetry/Reports/TelemetryService/"
              "1Report");
}

TEST(GetDbusReportPath, EmptyIdThrows)
{
    EXPECT_THROW(getDbusReportPath(""), std::invalid_argument);
}

TEST(GetDbusReportPath, EscapesIdStartingWithUnderscore)
{
    EXPECT_EQ(getDbusReportPath("_Report"),
              "/xyz/openbmc_project/Telemetry/Reports/TelemetryService/"
              "_5fReport");
}

TEST(GetDbusTriggerPath, AppendsIdToTriggersCollection)
{
    EXPECT_EQ(getDbusTriggerPath("TestTrigger"),
              "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService/"
              "TestTrigger");
}

TEST(GetDbusTriggerPath, EmptyIdThrows)
{
    EXPECT_THROW(getDbusTriggerPath(""), std::invalid_argument);
}

TEST(GetDbusTriggerPath, EscapesIdStartingWithUnderscore)
{
    EXPECT_EQ(getDbusTriggerPath("_Trigger"),
              "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService/"
              "_5fTrigger");
}

TEST(GetTriggerIdFromDbusPath, ReturnsLastSegment)
{
    EXPECT_EQ(getTriggerIdFromDbusPath(
                  "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService/"
                  "TestTrigger"),
              std::optional<std::string>("TestTrigger"));
    EXPECT_EQ(getTriggerIdFromDbusPath(
                  "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService/"
                  "Test_Trigger"),
              std::optional<std::string>("Test_Trigger"));
}

TEST(GetTriggerIdFromDbusPath, DecodesEscapedSegment)
{
    EXPECT_EQ(getTriggerIdFromDbusPath(
                  "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService/"
                  "_5fTrigger"),
              std::optional<std::string>("_Trigger"));
}

TEST(GetTriggerIdFromDbusPath, RoundTripsGetDbusTriggerPath)
{
    EXPECT_EQ(getTriggerIdFromDbusPath(getDbusTriggerPath("TestTrigger")),
              std::optional<std::string>("TestTrigger"));
    EXPECT_EQ(getTriggerIdFromDbusPath(getDbusTriggerPath("Test_Trigger")),
              std::optional<std::string>("Test_Trigger"));
    EXPECT_EQ(getTriggerIdFromDbusPath(getDbusTriggerPath("_Trigger")),
              std::optional<std::string>("_Trigger"));
    EXPECT_EQ(getTriggerIdFromDbusPath(getDbusTriggerPath("a.b")),
              std::optional<std::string>("a.b"));
}

TEST(GetTriggerIdFromDbusPath, RejectsEmptyId)
{
    EXPECT_EQ(getTriggerIdFromDbusPath(
                  "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService/"),
              std::nullopt);
    EXPECT_EQ(getTriggerIdFromDbusPath(
                  "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService/"
                  "_zz"),
              std::nullopt);
}

TEST(GetTriggerIdFromDbusPath, RejectsPathOutsideTriggersCollection)
{
    EXPECT_EQ(getTriggerIdFromDbusPath(
                  "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService"),
              std::nullopt);
    EXPECT_EQ(getTriggerIdFromDbusPath(
                  "/xyz/openbmc_project/Telemetry/Reports/TelemetryService/"
                  "TestReport"),
              std::nullopt);
    EXPECT_EQ(getTriggerIdFromDbusPath(
                  "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService/"
                  "a/b"),
              std::nullopt);
    EXPECT_EQ(getTriggerIdFromDbusPath(""), std::nullopt);
    EXPECT_EQ(getTriggerIdFromDbusPath("NotAPath"), std::nullopt);
}

TEST(ToRedfishCollectionFunction, ConvertsKnownOperationType)
{
    EXPECT_EQ(toRedfishCollectionFunction(
                  "xyz.openbmc_project.Telemetry.Report.OperationType.Maximum"),
              metric_report_definition::CalculationAlgorithmEnum::Maximum);
    EXPECT_EQ(toRedfishCollectionFunction(
                  "xyz.openbmc_project.Telemetry.Report.OperationType.Minimum"),
              metric_report_definition::CalculationAlgorithmEnum::Minimum);
    EXPECT_EQ(toRedfishCollectionFunction(
                  "xyz.openbmc_project.Telemetry.Report.OperationType.Average"),
              metric_report_definition::CalculationAlgorithmEnum::Average);
    EXPECT_EQ(
        toRedfishCollectionFunction(
            "xyz.openbmc_project.Telemetry.Report.OperationType.Summation"),
        metric_report_definition::CalculationAlgorithmEnum::Summation);
}

TEST(ToRedfishCollectionFunction, UnknownOperationTypeIsInvalid)
{
    EXPECT_EQ(toRedfishCollectionFunction(""),
              metric_report_definition::CalculationAlgorithmEnum::Invalid);
    EXPECT_EQ(toRedfishCollectionFunction(
                  "xyz.openbmc_project.Telemetry.Report.OperationType.Invalid"),
              metric_report_definition::CalculationAlgorithmEnum::Invalid);
    EXPECT_EQ(toRedfishCollectionFunction("Maximum"),
              metric_report_definition::CalculationAlgorithmEnum::Invalid);
    EXPECT_EQ(toRedfishCollectionFunction(
                  "xyz.openbmc_project.Telemetry.Report.OperationType.maximum"),
              metric_report_definition::CalculationAlgorithmEnum::Invalid);
}

TEST(ToDbusCollectionFunction, ConvertsKnownCalculationAlgorithm)
{
    EXPECT_EQ(toDbusCollectionFunction("Maximum"),
              "xyz.openbmc_project.Telemetry.Report.OperationType.Maximum");
    EXPECT_EQ(toDbusCollectionFunction("Minimum"),
              "xyz.openbmc_project.Telemetry.Report.OperationType.Minimum");
    EXPECT_EQ(toDbusCollectionFunction("Average"),
              "xyz.openbmc_project.Telemetry.Report.OperationType.Average");
    EXPECT_EQ(toDbusCollectionFunction("Summation"),
              "xyz.openbmc_project.Telemetry.Report.OperationType.Summation");
}

TEST(ToDbusCollectionFunction, UnknownCalculationAlgorithmIsEmpty)
{
    EXPECT_EQ(toDbusCollectionFunction(""), "");
    EXPECT_EQ(toDbusCollectionFunction("Invalid"), "");
    EXPECT_EQ(toDbusCollectionFunction("maximum"), "");
    EXPECT_EQ(toDbusCollectionFunction(
                  "xyz.openbmc_project.Telemetry.Report.OperationType.Maximum"),
              "");
}

TEST(ToRedfishCollectionFunctions, EmptyInputGivesEmptyArray)
{
    const std::vector<std::string> dbusEnums;

    std::optional<nlohmann::json::array_t> result =
        toRedfishCollectionFunctions(dbusEnums);
    const std::optional<nlohmann::json::array_t> expected =
        nlohmann::json::array_t{};
    EXPECT_EQ(result, expected);
}

TEST(ToRedfishCollectionFunctions, ConvertsEachKnownOperationType)
{
    const std::vector<std::string> dbusEnums{
        "xyz.openbmc_project.Telemetry.Report.OperationType.Maximum",
        "xyz.openbmc_project.Telemetry.Report.OperationType.Minimum",
        "xyz.openbmc_project.Telemetry.Report.OperationType.Average",
        "xyz.openbmc_project.Telemetry.Report.OperationType.Summation"};

    std::optional<nlohmann::json::array_t> result =
        toRedfishCollectionFunctions(dbusEnums);
    const std::optional<nlohmann::json::array_t> expected =
        nlohmann::json::array_t{"Maximum", "Minimum", "Average", "Summation"};
    EXPECT_EQ(result, expected);
}

TEST(ToRedfishCollectionFunctions, SingleUnknownValueDiscardsWholeArray)
{
    const std::vector<std::string> withBogus{
        "xyz.openbmc_project.Telemetry.Report.OperationType.Maximum", "bogus"};
    const std::vector<std::string> redfishSpelling{"Maximum"};
    const std::vector<std::string> emptyString{""};

    EXPECT_EQ(toRedfishCollectionFunctions(withBogus), std::nullopt);
    EXPECT_EQ(toRedfishCollectionFunctions(redfishSpelling), std::nullopt);
    EXPECT_EQ(toRedfishCollectionFunctions(emptyString), std::nullopt);
}

} // namespace
} // namespace redfish::telemetry