summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNan Zhou <nanzhoumails@gmail.com>2022-07-04 04:18:14 +0300
committerNan Zhou <nanzhoumails@gmail.com>2022-07-12 21:56:49 +0300
commitb5a10a22db6f44c7a96cc2912e1b57db439e77fc (patch)
tree7f253f33f46622186381acf6d572143b22b59520
parent7ea4643a5226c6b55e3aa3c4a9dcafb424a91dc9 (diff)
downloadbmcweb-b5a10a22db6f44c7a96cc2912e1b57db439e77fc.tar.xz
redfish-core tests: use matcher correctly
This commit corrects the usage of ASSERT, EXPECT, and all the matchers. It also fixes cases where a cleaner matcher can be used. This commit increases readability (with correct and cleaner matcher) and corrects bugs (access iterator before checking validity). Typical incorrect usage is that when a function returns a boolean value to indicated whether the function succeeds or not, unless the function has clear behavior when it fails, we shouldn't continue the test that inspects the output parameters. A typical test codes look like this, ``` ASSERT_TRUE(fooBar(output)); EXPECT_EQ(output, 123); ``` Reference: https://testing.googleblog.com/2008/07/tott-expect-vs-assert.html https://github.com/google/googletest/blob/main/docs/reference/matchers.md Tested: unit test passed. Signed-off-by: Nan Zhou <nanzhoumails@gmail.com> Change-Id: Ia2cf9922bd4cb2fe8b4b3912e9153e9ae4eab134
-rw-r--r--redfish-core/ut/hex_utils_test.cpp11
-rw-r--r--redfish-core/ut/json_utils_test.cpp111
-rw-r--r--redfish-core/ut/lock_test.cpp53
-rw-r--r--redfish-core/ut/privileges_test.cpp21
-rw-r--r--redfish-core/ut/stl_utils_test.cpp11
-rw-r--r--redfish-core/ut/time_utils_test.cpp3
6 files changed, 112 insertions, 98 deletions
diff --git a/redfish-core/ut/hex_utils_test.cpp b/redfish-core/ut/hex_utils_test.cpp
index 619f6eb841..506dc5a131 100644
--- a/redfish-core/ut/hex_utils_test.cpp
+++ b/redfish-core/ut/hex_utils_test.cpp
@@ -1,10 +1,13 @@
#include "utils/hex_utils.hpp"
#include <gmock/gmock.h>
+#include <gtest/gtest.h>
namespace
{
+using ::testing::IsEmpty;
+
TEST(IntToHexString, ReturnsCorrectHexForUint64)
{
EXPECT_EQ(intToHexString(0xFFFFFFFFFFFFFFFFULL, 16), "FFFFFFFFFFFFFFFF");
@@ -59,14 +62,14 @@ TEST(HexStringToBytes, Success)
std::vector<uint8_t> hexBytes = {0x01, 0x23, 0x45, 0x67,
0x89, 0xAB, 0xCD, 0xEF};
EXPECT_EQ(hexStringToBytes("0123456789ABCDEF"), hexBytes);
- EXPECT_TRUE(hexStringToBytes("").empty());
+ EXPECT_THAT(hexStringToBytes(""), IsEmpty());
}
TEST(HexStringToBytes, Failure)
{
- EXPECT_TRUE(hexStringToBytes("Hello").empty());
- EXPECT_TRUE(hexStringToBytes("`").empty());
- EXPECT_TRUE(hexStringToBytes("012").empty());
+ EXPECT_THAT(hexStringToBytes("Hello"), IsEmpty());
+ EXPECT_THAT(hexStringToBytes("`"), IsEmpty());
+ EXPECT_THAT(hexStringToBytes("012"), IsEmpty());
}
} // namespace \ No newline at end of file
diff --git a/redfish-core/ut/json_utils_test.cpp b/redfish-core/ut/json_utils_test.cpp
index 427ed21523..a28442823f 100644
--- a/redfish-core/ut/json_utils_test.cpp
+++ b/redfish-core/ut/json_utils_test.cpp
@@ -4,12 +4,17 @@
#include <vector>
#include <gmock/gmock.h>
+#include <gtest/gtest.h>
namespace redfish::json_util
{
namespace
{
+using ::testing::ElementsAre;
+using ::testing::IsEmpty;
+using ::testing::Not;
+
TEST(ReadJson, ValidElementsReturnsTrueResponseOkValuesUnpackedCorrectly)
{
crow::Response res;
@@ -20,14 +25,14 @@ TEST(ReadJson, ValidElementsReturnsTrueResponseOkValuesUnpackedCorrectly)
int64_t integer = 0;
std::string str;
std::vector<uint64_t> vec;
- EXPECT_TRUE(readJson(jsonRequest, res, "integer", integer, "string", str,
+ ASSERT_TRUE(readJson(jsonRequest, res, "integer", integer, "string", str,
"vector", vec));
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
- EXPECT_TRUE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, IsEmpty());
EXPECT_EQ(integer, 1);
EXPECT_EQ(str, "hello");
- EXPECT_TRUE((vec == std::vector<uint64_t>{1, 2, 3}));
+ EXPECT_THAT(vec, ElementsAre(1, 2, 3));
}
TEST(readJson, ExtraElementsReturnsFalseReponseIsBadRequest)
@@ -38,14 +43,14 @@ TEST(readJson, ExtraElementsReturnsFalseReponseIsBadRequest)
std::optional<int> integer;
std::optional<std::string> str;
- EXPECT_FALSE(readJson(jsonRequest, res, "integer", integer));
+ ASSERT_FALSE(readJson(jsonRequest, res, "integer", integer));
EXPECT_EQ(res.result(), boost::beast::http::status::bad_request);
- EXPECT_FALSE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, Not(IsEmpty()));
EXPECT_EQ(integer, 1);
- EXPECT_FALSE(readJson(jsonRequest, res, "string", str));
+ ASSERT_FALSE(readJson(jsonRequest, res, "string", str));
EXPECT_EQ(res.result(), boost::beast::http::status::bad_request);
- EXPECT_FALSE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, Not(IsEmpty()));
EXPECT_EQ(str, "hello");
}
@@ -56,18 +61,18 @@ TEST(ReadJson, WrongElementTypeReturnsFalseReponseIsBadRequest)
int64_t integer = 0;
std::string str0;
- EXPECT_FALSE(readJson(jsonRequest, res, "integer", str0));
+ ASSERT_FALSE(readJson(jsonRequest, res, "integer", str0));
EXPECT_EQ(res.result(), boost::beast::http::status::bad_request);
- EXPECT_FALSE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, Not(IsEmpty()));
- EXPECT_FALSE(readJson(jsonRequest, res, "string0", integer));
+ ASSERT_FALSE(readJson(jsonRequest, res, "string0", integer));
EXPECT_EQ(res.result(), boost::beast::http::status::bad_request);
- EXPECT_FALSE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, Not(IsEmpty()));
- EXPECT_FALSE(
+ ASSERT_FALSE(
readJson(jsonRequest, res, "integer", str0, "string0", integer));
EXPECT_EQ(res.result(), boost::beast::http::status::bad_request);
- EXPECT_FALSE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, Not(IsEmpty()));
}
TEST(ReadJson, MissingElementReturnsFalseReponseIsBadRequest)
@@ -79,15 +84,15 @@ TEST(ReadJson, MissingElementReturnsFalseReponseIsBadRequest)
std::string str0;
std::string str1;
std::vector<uint8_t> vec;
- EXPECT_FALSE(readJson(jsonRequest, res, "integer", integer, "string0", str0,
+ ASSERT_FALSE(readJson(jsonRequest, res, "integer", integer, "string0", str0,
"vector", vec));
EXPECT_EQ(res.result(), boost::beast::http::status::bad_request);
- EXPECT_FALSE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, Not(IsEmpty()));
- EXPECT_FALSE(readJson(jsonRequest, res, "integer", integer, "string0", str0,
+ ASSERT_FALSE(readJson(jsonRequest, res, "integer", integer, "string0", str0,
"string1", str1));
EXPECT_EQ(res.result(), boost::beast::http::status::bad_request);
- EXPECT_FALSE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, Not(IsEmpty()));
}
TEST(ReadJson, JsonArrayAreUnpackedCorrectly)
@@ -100,9 +105,9 @@ TEST(ReadJson, JsonArrayAreUnpackedCorrectly)
)"_json;
std::vector<nlohmann::json> jsonVec;
- EXPECT_TRUE(readJson(jsonRequest, res, "TestJson", jsonVec));
+ ASSERT_TRUE(readJson(jsonRequest, res, "TestJson", jsonVec));
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
- EXPECT_TRUE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, IsEmpty());
EXPECT_EQ(jsonVec, R"([{"hello": "yes"}, [{"there": "no"}, "nice"]])"_json);
}
@@ -116,10 +121,10 @@ TEST(ReadJson, JsonSubElementValueAreUnpackedCorrectly)
)"_json;
int integer = 0;
- EXPECT_TRUE(readJson(jsonRequest, res, "json/integer", integer));
+ ASSERT_TRUE(readJson(jsonRequest, res, "json/integer", integer));
EXPECT_EQ(integer, 42);
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
- EXPECT_TRUE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, IsEmpty());
}
TEST(ReadJson, JsonDeeperSubElementValueAreUnpackedCorrectly)
@@ -134,10 +139,10 @@ TEST(ReadJson, JsonDeeperSubElementValueAreUnpackedCorrectly)
)"_json;
std::string foobar;
- EXPECT_TRUE(readJson(jsonRequest, res, "json/json2/string", foobar));
+ ASSERT_TRUE(readJson(jsonRequest, res, "json/json2/string", foobar));
EXPECT_EQ(foobar, "foobar");
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
- EXPECT_TRUE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, IsEmpty());
}
TEST(ReadJson, MultipleJsonSubElementValueAreUnpackedCorrectly)
@@ -156,13 +161,13 @@ TEST(ReadJson, MultipleJsonSubElementValueAreUnpackedCorrectly)
int integer = 0;
std::string foobar;
std::string bazbar;
- EXPECT_TRUE(readJson(jsonRequest, res, "json/integer", integer,
+ ASSERT_TRUE(readJson(jsonRequest, res, "json/integer", integer,
"json/string", foobar, "string", bazbar));
EXPECT_EQ(integer, 42);
EXPECT_EQ(foobar, "foobar");
EXPECT_EQ(bazbar, "bazbar");
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
- EXPECT_TRUE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, IsEmpty());
}
TEST(ReadJson, ExtraElement)
@@ -194,30 +199,30 @@ TEST(ReadJson, ValidMissingElementReturnsTrue)
std::optional<std::string> str0;
std::optional<std::string> str1;
std::optional<std::vector<uint8_t>> vec;
- EXPECT_TRUE(readJson(jsonRequest, res, "missing_integer", integer,
+ ASSERT_TRUE(readJson(jsonRequest, res, "missing_integer", integer,
"integer", requiredInteger));
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
EXPECT_TRUE(res.jsonValue.empty());
EXPECT_EQ(integer, std::nullopt);
- EXPECT_TRUE(readJson(jsonRequest, res, "missing_string", str0, "integer",
+ ASSERT_TRUE(readJson(jsonRequest, res, "missing_string", str0, "integer",
requiredInteger));
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
- EXPECT_TRUE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, IsEmpty());
EXPECT_EQ(str0, std::nullopt);
- EXPECT_TRUE(readJson(jsonRequest, res, "integer", integer, "string", str0,
+ ASSERT_TRUE(readJson(jsonRequest, res, "integer", integer, "string", str0,
"vector", vec));
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
- EXPECT_TRUE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, IsEmpty());
EXPECT_EQ(integer, 1);
EXPECT_EQ(str0, std::nullopt);
EXPECT_EQ(vec, std::nullopt);
- EXPECT_TRUE(readJson(jsonRequest, res, "integer", integer, "string0", str0,
+ ASSERT_TRUE(readJson(jsonRequest, res, "integer", integer, "string0", str0,
"missing_string", str1));
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
- EXPECT_TRUE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, IsEmpty());
EXPECT_EQ(str1, std::nullopt);
}
@@ -230,23 +235,23 @@ TEST(ReadJson, InvalidMissingElementReturnsFalse)
std::string str0;
std::string str1;
std::vector<uint8_t> vec;
- EXPECT_FALSE(readJson(jsonRequest, res, "missing_integer", integer));
+ ASSERT_FALSE(readJson(jsonRequest, res, "missing_integer", integer));
EXPECT_EQ(res.result(), boost::beast::http::status::bad_request);
- EXPECT_FALSE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, Not(IsEmpty()));
- EXPECT_FALSE(readJson(jsonRequest, res, "missing_string", str0));
+ ASSERT_FALSE(readJson(jsonRequest, res, "missing_string", str0));
EXPECT_EQ(res.result(), boost::beast::http::status::bad_request);
- EXPECT_FALSE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, Not(IsEmpty()));
- EXPECT_FALSE(readJson(jsonRequest, res, "integer", integer, "string", str0,
+ ASSERT_FALSE(readJson(jsonRequest, res, "integer", integer, "string", str0,
"vector", vec));
EXPECT_EQ(res.result(), boost::beast::http::status::bad_request);
- EXPECT_FALSE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, Not(IsEmpty()));
- EXPECT_FALSE(readJson(jsonRequest, res, "integer", integer, "string0", str0,
+ ASSERT_FALSE(readJson(jsonRequest, res, "integer", integer, "string0", str0,
"missing_string", str1));
EXPECT_EQ(res.result(), boost::beast::http::status::bad_request);
- EXPECT_FALSE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, Not(IsEmpty()));
}
TEST(ReadJsonPatch, ValidElementsReturnsTrueResponseOkValuesUnpackedCorrectly)
@@ -258,9 +263,10 @@ TEST(ReadJsonPatch, ValidElementsReturnsTrueResponseOkValuesUnpackedCorrectly)
req.body = "{\"integer\": 1}";
int64_t integer = 0;
- EXPECT_TRUE(readJsonPatch(req, res, "integer", integer));
+ ASSERT_TRUE(readJsonPatch(req, res, "integer", integer));
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
- EXPECT_TRUE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, IsEmpty());
+ EXPECT_EQ(integer, 1);
}
TEST(ReadJsonPatch, EmptyObjectReturnsFalseResponseBadRequest)
@@ -272,9 +278,9 @@ TEST(ReadJsonPatch, EmptyObjectReturnsFalseResponseBadRequest)
req.body = "{}";
std::optional<int64_t> integer = 0;
- EXPECT_FALSE(readJsonPatch(req, res, "integer", integer));
+ ASSERT_FALSE(readJsonPatch(req, res, "integer", integer));
EXPECT_EQ(res.result(), boost::beast::http::status::bad_request);
- EXPECT_FALSE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, Not(IsEmpty()));
}
TEST(ReadJsonPatch, OdataIgnored)
@@ -286,9 +292,10 @@ TEST(ReadJsonPatch, OdataIgnored)
req.body = R"({"@odata.etag": "etag", "integer": 1})";
std::optional<int64_t> integer = 0;
- EXPECT_TRUE(readJsonPatch(req, res, "integer", integer));
+ ASSERT_TRUE(readJsonPatch(req, res, "integer", integer));
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
- EXPECT_TRUE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, IsEmpty());
+ EXPECT_EQ(integer, 1);
}
TEST(ReadJsonPatch, OnlyOdataGivesNoOperation)
@@ -300,9 +307,9 @@ TEST(ReadJsonPatch, OnlyOdataGivesNoOperation)
req.body = R"({"@odata.etag": "etag"})";
std::optional<int64_t> integer = 0;
- EXPECT_FALSE(readJsonPatch(req, res, "integer", integer));
+ ASSERT_FALSE(readJsonPatch(req, res, "integer", integer));
EXPECT_EQ(res.result(), boost::beast::http::status::bad_request);
- EXPECT_FALSE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, Not(IsEmpty()));
}
TEST(ReadJsonAction, ValidElementsReturnsTrueResponseOkValuesUnpackedCorrectly)
@@ -314,9 +321,9 @@ TEST(ReadJsonAction, ValidElementsReturnsTrueResponseOkValuesUnpackedCorrectly)
req.body = "{\"integer\": 1}";
int64_t integer = 0;
- EXPECT_TRUE(readJsonAction(req, res, "integer", integer));
+ ASSERT_TRUE(readJsonAction(req, res, "integer", integer));
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
- EXPECT_TRUE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, IsEmpty());
EXPECT_EQ(integer, 1);
}
@@ -329,9 +336,9 @@ TEST(ReadJsonAction, EmptyObjectReturnsTrueResponseOk)
req.body = "{}";
std::optional<int64_t> integer = 0;
- EXPECT_TRUE(readJsonAction(req, res, "integer", integer));
+ ASSERT_TRUE(readJsonAction(req, res, "integer", integer));
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
- EXPECT_TRUE(res.jsonValue.empty());
+ EXPECT_THAT(res.jsonValue, IsEmpty());
}
} // namespace
diff --git a/redfish-core/ut/lock_test.cpp b/redfish-core/ut/lock_test.cpp
index 10c51d3c14..9c37da38a5 100644
--- a/redfish-core/ut/lock_test.cpp
+++ b/redfish-core/ut/lock_test.cpp
@@ -2,7 +2,8 @@
#include <string>
-#include "gmock/gmock.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
namespace crow::ibm_mc_lock
{
@@ -22,6 +23,7 @@ using RcAcquireLock = std::pair<bool, std::variant<Rc, std::pair<bool, int>>>;
using RcReleaseLockApi = std::pair<bool, std::variant<bool, RcRelaseLock>>;
using SessionFlags = std::pair<SType, SType>;
using ListOfSessionIds = std::vector<std::string>;
+using ::testing::IsEmpty;
class LockTest : public ::testing::Test
{
@@ -113,7 +115,7 @@ TEST_F(LockTest, ValidationGoodTestCase)
{
MockLock lockManager;
const LockRequest& t = record;
- ASSERT_EQ(1, lockManager.isValidLockRequest(t));
+ EXPECT_TRUE(lockManager.isValidLockRequest(t));
}
TEST_F(LockTest, ValidationBadTestWithLocktype)
@@ -122,7 +124,7 @@ TEST_F(LockTest, ValidationBadTestWithLocktype)
// Corrupt the lock type
std::get<2>(record) = "rwrite";
const LockRequest& t = record;
- ASSERT_EQ(0, lockManager.isValidLockRequest(t));
+ EXPECT_FALSE(lockManager.isValidLockRequest(t));
}
TEST_F(LockTest, ValidationBadTestWithlockFlags)
@@ -131,7 +133,7 @@ TEST_F(LockTest, ValidationBadTestWithlockFlags)
// Corrupt the lockflag
std::get<4>(record)[0].first = "lock";
const LockRequest& t = record;
- ASSERT_EQ(0, lockManager.isValidLockRequest(t));
+ EXPECT_FALSE(lockManager.isValidLockRequest(t));
}
TEST_F(LockTest, ValidationBadTestWithSegmentlength)
@@ -140,14 +142,14 @@ TEST_F(LockTest, ValidationBadTestWithSegmentlength)
// Corrupt the Segment length
std::get<4>(record)[0].second = 7;
const LockRequest& t = record;
- ASSERT_EQ(0, lockManager.isValidLockRequest(t));
+ EXPECT_FALSE(lockManager.isValidLockRequest(t));
}
TEST_F(LockTest, MultiRequestWithoutConflict)
{
MockLock lockManager;
const LockRequests& t = request;
- ASSERT_EQ(0, lockManager.isConflictRequest(t));
+ EXPECT_FALSE(lockManager.isConflictRequest(t));
}
TEST_F(LockTest, MultiRequestWithConflictduetoSameSegmentLength)
@@ -159,7 +161,7 @@ TEST_F(LockTest, MultiRequestWithConflictduetoSameSegmentLength)
// resource
std::get<4>(request[0])[0].first = "LockAll";
const LockRequests& t = request;
- ASSERT_EQ(1, lockManager.isConflictRequest(t));
+ EXPECT_TRUE(lockManager.isConflictRequest(t));
}
TEST_F(LockTest, MultiRequestWithoutConflictduetoDifferentSegmentData)
@@ -177,7 +179,7 @@ TEST_F(LockTest, MultiRequestWithoutConflictduetoDifferentSegmentData)
std::get<3>(request[0]) = 216179379183550464; // HEX 03 00 06 00 00 00 00 00
std::get<3>(request[1]) = 288236973221478400; // HEX 04 00 06 00 00 00 00 00
const LockRequests& t = request;
- ASSERT_EQ(0, lockManager.isConflictRequest(t));
+ EXPECT_FALSE(lockManager.isConflictRequest(t));
}
TEST_F(LockTest, MultiRequestWithConflictduetoSameSegmentData)
@@ -189,12 +191,13 @@ TEST_F(LockTest, MultiRequestWithConflictduetoSameSegmentData)
// resource
std::get<4>(request[0])[0].first = "DontLock";
std::get<4>(request[0])[1].first = "LockAll";
- // Dont Change the resource id(1st & 2nd byte) at all, so that the conflict
- // occurs from the second segment which is trying to lock all the resources.
+ // Dont Change the resource id(1st & 2nd byte) at all, so that the
+ // conflict occurs from the second segment which is trying to lock all
+ // the resources.
std::get<3>(request[0]) = 216173882346831872; // 03 00 01 00 2B 00 00 00
std::get<3>(request[1]) = 216173882346831872; // 03 00 01 00 2B 00 00 00
const LockRequests& t = request;
- ASSERT_EQ(1, lockManager.isConflictRequest(t));
+ EXPECT_TRUE(lockManager.isConflictRequest(t));
}
TEST_F(LockTest, MultiRequestWithoutConflictduetoDifferentSegmentLength)
@@ -210,7 +213,7 @@ TEST_F(LockTest, MultiRequestWithoutConflictduetoDifferentSegmentLength)
std::get<4>(request[0])[0].second = 3;
const LockRequests& t = request;
// Return No Conflict
- ASSERT_EQ(0, lockManager.isConflictRequest(t));
+ EXPECT_FALSE(lockManager.isConflictRequest(t));
}
TEST_F(LockTest, MultiRequestWithoutConflictduetoReadLocktype)
@@ -221,7 +224,7 @@ TEST_F(LockTest, MultiRequestWithoutConflictduetoReadLocktype)
std::get<4>(request[0])[0].first = "LockAll";
const LockRequests& t = request;
// Return No Conflict
- ASSERT_EQ(0, lockManager.isConflictRequest(t));
+ EXPECT_FALSE(lockManager.isConflictRequest(t));
}
TEST_F(LockTest, MultiRequestWithoutConflictduetoReadLocktypeAndLockall)
@@ -233,7 +236,7 @@ TEST_F(LockTest, MultiRequestWithoutConflictduetoReadLocktypeAndLockall)
std::get<4>(request[0])[1].first = "LockAll";
const LockRequests& t = request;
// Return No Conflict
- ASSERT_EQ(0, lockManager.isConflictRequest(t));
+ EXPECT_FALSE(lockManager.isConflictRequest(t));
}
TEST_F(LockTest, RequestConflictedWithLockTableEntries)
@@ -248,7 +251,7 @@ TEST_F(LockTest, RequestConflictedWithLockTableEntries)
const LockRequests& p = request;
auto rc2 = lockManager.isConflictWithTable(p);
// Return a Conflict
- ASSERT_EQ(1, rc2.first);
+ EXPECT_TRUE(rc2.first);
}
TEST_F(LockTest, RequestNotConflictedWithLockTableEntries)
@@ -264,15 +267,15 @@ TEST_F(LockTest, RequestNotConflictedWithLockTableEntries)
const LockRequests& p = request;
auto rc2 = lockManager.isConflictWithTable(p);
// Return No Conflict
- ASSERT_EQ(0, rc2.first);
+ EXPECT_FALSE(rc2.first);
}
TEST_F(LockTest, TestGenerateTransactionIDFunction)
{
MockLock lockManager;
- uint32_t transactionid1 = lockManager.generateTransactionId();
- uint32_t transactionid2 = lockManager.generateTransactionId();
- EXPECT_TRUE(transactionid2 == ++transactionid1);
+ uint32_t transactionId1 = lockManager.generateTransactionId();
+ uint32_t transactionId2 = lockManager.generateTransactionId();
+ EXPECT_EQ(transactionId2, ++transactionId1);
}
TEST_F(LockTest, ValidateTransactionIDsGoodTestCase)
@@ -283,7 +286,7 @@ TEST_F(LockTest, ValidateTransactionIDsGoodTestCase)
auto rc1 = lockManager.isConflictWithTable(t);
std::vector<uint32_t> tids = {1};
const std::vector<uint32_t>& p = tids;
- ASSERT_EQ(1, lockManager.validateRids(p));
+ EXPECT_TRUE(lockManager.validateRids(p));
}
TEST_F(LockTest, ValidateTransactionIDsBadTestCase)
@@ -294,7 +297,7 @@ TEST_F(LockTest, ValidateTransactionIDsBadTestCase)
auto rc1 = lockManager.isConflictWithTable(t);
std::vector<uint32_t> tids = {10};
const std::vector<uint32_t>& p = tids;
- ASSERT_EQ(0, lockManager.validateRids(p));
+ EXPECT_FALSE(lockManager.validateRids(p));
}
TEST_F(LockTest, ValidateisItMyLockGoodTestCase)
@@ -309,7 +312,7 @@ TEST_F(LockTest, ValidateisItMyLockGoodTestCase)
std::string sessionid = "xxxxx";
std::pair<SType, SType> ids = std::make_pair(hmcid, sessionid);
auto rc = lockManager.isItMyLock(p, ids);
- ASSERT_EQ(1, rc.first);
+ EXPECT_TRUE(rc.first);
}
TEST_F(LockTest, ValidateisItMyLockBadTestCase)
@@ -326,7 +329,7 @@ TEST_F(LockTest, ValidateisItMyLockBadTestCase)
std::string sessionid = "random";
std::pair<SType, SType> ids = std::make_pair(hmcid, sessionid);
auto rc = lockManager.isItMyLock(p, ids);
- ASSERT_EQ(0, rc.first);
+ EXPECT_FALSE(rc.first);
}
TEST_F(LockTest, ValidateSessionIDForGetlocklistBadTestCase)
@@ -339,7 +342,7 @@ TEST_F(LockTest, ValidateSessionIDForGetlocklistBadTestCase)
auto status = lockManager.getLockList(sessionid);
auto result =
std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status);
- ASSERT_EQ(0, result.size());
+ EXPECT_THAT(result, IsEmpty());
}
TEST_F(LockTest, ValidateSessionIDForGetlocklistGoodTestCase)
@@ -352,7 +355,7 @@ TEST_F(LockTest, ValidateSessionIDForGetlocklistGoodTestCase)
auto status = lockManager.getLockList(sessionid);
auto result =
std::get<std::vector<std::pair<uint32_t, LockRequests>>>(status);
- ASSERT_EQ(1, result.size());
+ EXPECT_EQ(result.size(), 1);
}
} // namespace
diff --git a/redfish-core/ut/privileges_test.cpp b/redfish-core/ut/privileges_test.cpp
index 3813b54fa2..c5a7a87964 100644
--- a/redfish-core/ut/privileges_test.cpp
+++ b/redfish-core/ut/privileges_test.cpp
@@ -11,12 +11,15 @@ namespace redfish
namespace
{
+using ::testing::IsEmpty;
+using ::testing::UnorderedElementsAre;
+
TEST(PrivilegeTest, PrivilegeConstructor)
{
Privileges privileges{"Login", "ConfigureManager"};
EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
- ::testing::UnorderedElementsAre("Login", "ConfigureManager"));
+ UnorderedElementsAre("Login", "ConfigureManager"));
}
TEST(PrivilegeTest, PrivilegeCheckForNoPrivilegesRequired)
@@ -96,10 +99,10 @@ TEST(PrivilegeTest, DefaultPrivilegeBitsetsAreEmpty)
Privileges privileges;
EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
- ::testing::IsEmpty());
+ IsEmpty());
EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::OEM),
- ::testing::IsEmpty());
+ IsEmpty());
}
TEST(PrivilegeTest, GetActivePrivilegeNames)
@@ -107,7 +110,7 @@ TEST(PrivilegeTest, GetActivePrivilegeNames)
Privileges privileges;
EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
- ::testing::IsEmpty());
+ IsEmpty());
std::array<const char*, 5> expectedPrivileges{
"Login", "ConfigureManager", "ConfigureUsers", "ConfigureComponents",
@@ -118,11 +121,11 @@ TEST(PrivilegeTest, GetActivePrivilegeNames)
EXPECT_TRUE(privileges.setSinglePrivilege(privilege));
}
- EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
- ::testing::UnorderedElementsAre(
- expectedPrivileges[0], expectedPrivileges[1],
- expectedPrivileges[2], expectedPrivileges[3],
- expectedPrivileges[4]));
+ EXPECT_THAT(
+ privileges.getActivePrivilegeNames(PrivilegeType::BASE),
+ UnorderedElementsAre(expectedPrivileges[0], expectedPrivileges[1],
+ expectedPrivileges[2], expectedPrivileges[3],
+ expectedPrivileges[4]));
}
} // namespace
} // namespace redfish \ No newline at end of file
diff --git a/redfish-core/ut/stl_utils_test.cpp b/redfish-core/ut/stl_utils_test.cpp
index 7242200a62..0c8e8c1bba 100644
--- a/redfish-core/ut/stl_utils_test.cpp
+++ b/redfish-core/ut/stl_utils_test.cpp
@@ -2,16 +2,20 @@
#include <string>
+#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace redfish::stl_utils
{
namespace
{
+using ::testing::ElementsAre;
+
TEST(FirstDuplicate, ReturnsIteratorToFirstDuplicate)
{
std::vector<std::string> strVec = {"s1", "s4", "s1", "s2", "", "s3", "s3"};
auto iter = firstDuplicate(strVec.begin(), strVec.end());
+ ASSERT_NE(iter, strVec.end());
EXPECT_EQ(*iter, "s3");
}
@@ -20,12 +24,7 @@ TEST(RemoveDuplicates, AllDuplicatesAreRempvedInplace)
std::vector<std::string> strVec = {"s1", "s4", "s1", "s2", "", "s3", "s3"};
removeDuplicate(strVec);
- EXPECT_EQ(strVec.size(), 5);
- EXPECT_EQ(strVec[0], "s1");
- EXPECT_EQ(strVec[1], "s4");
- EXPECT_EQ(strVec[2], "s2");
- EXPECT_EQ(strVec[3], "");
- EXPECT_EQ(strVec[4], "s3");
+ EXPECT_THAT(strVec, ElementsAre("s1", "s4", "s2", "", "s3"));
}
} // namespace
} // namespace redfish::stl_utils
diff --git a/redfish-core/ut/time_utils_test.cpp b/redfish-core/ut/time_utils_test.cpp
index ee2e037e6c..e97e9a7bce 100644
--- a/redfish-core/ut/time_utils_test.cpp
+++ b/redfish-core/ut/time_utils_test.cpp
@@ -59,8 +59,7 @@ TEST(ToDurationStringFromUintTest, PositiveTests)
uint64_t maxAcceptedTimeMs =
static_cast<uint64_t>(std::chrono::milliseconds::max().count());
- EXPECT_THAT(toDurationStringFromUint(maxAcceptedTimeMs),
- ::testing::Ne(std::nullopt));
+ EXPECT_NE(toDurationStringFromUint(maxAcceptedTimeMs), std::nullopt);
EXPECT_EQ(toDurationStringFromUint(0), "PT");
EXPECT_EQ(toDurationStringFromUint(250), "PT0.250S");
EXPECT_EQ(toDurationStringFromUint(5000), "PT5.000S");