#include "types/PolicyBucketId.h"
#include <memory>
+#include <tuple>
namespace Cynara {
return std::make_shared<Policy>(key, result);
}
+ bool operator==(const Policy &other) const {
+ return std::tie(m_key, m_result) == std::tie(other.m_key, other.m_result);
+ }
+
private:
PolicyKey m_key;
PolicyResult m_result;
virtual void deleteLinking(const PolicyBucketId &bucketId);
protected:
+ InMemoryStorageBackend() {}
void openFileStream(std::shared_ptr<std::ifstream> stream, const std::string &filename);
std::shared_ptr<BucketDeserializer> bucketStreamOpener(const PolicyBucketId &bucketId);
* @brief Tests of buckets in InMemeoryStorageBackend
*/
-#include "gmock/gmock.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
-#include "inmemeorystoragebackendfixture.h"
-#include "fakeinmemorystoragebackend.h"
+#include <exceptions/BucketNotExistsException.h>
+#include <types/PolicyBucket.h>
+#include <types/PolicyResult.h>
-#include "types/PolicyResult.h"
-#include "types/PolicyBucket.h"
+#include "fakeinmemorystoragebackend.h"
+#include "inmemeorystoragebackendfixture.h"
using namespace Cynara;
#ifndef FAKEINMEMORYSTORAGEBACKEND_H_
#define FAKEINMEMORYSTORAGEBACKEND_H_
-#include "storage/InMemoryStorageBackend.h"
+#include <storage/Buckets.h>
+#include <storage/InMemoryStorageBackend.h>
class FakeInMemoryStorageBackend : public Cynara::InMemoryStorageBackend {
public:
- MOCK_METHOD0(buckets, Cynara::InMemoryStorageBackend::Buckets&());
+ using Cynara::InMemoryStorageBackend::InMemoryStorageBackend;
+ MOCK_METHOD0(buckets, Cynara::Buckets&());
};
#ifndef INMEMEORYSTORAGEBACKENDFIXTURE_H_
#define INMEMEORYSTORAGEBACKENDFIXTURE_H_
-#include "gmock/gmock.h"
+#include <gmock/gmock.h>
-#include "types/PolicyBucket.h"
-#include "types/PolicyCollection.h"
-#include "storage/InMemoryStorageBackend.h"
+#include <types/PolicyBucket.h>
+#include <types/PolicyCollection.h>
+#include <storage/Buckets.h>
+#include <storage/InMemoryStorageBackend.h>
class InMemeoryStorageBackendFixture : public ::testing::Test {
protected:
- Cynara::InMemoryStorageBackend::Buckets::mapped_type &
+ Cynara::Buckets::mapped_type &
createBucket(const Cynara::PolicyBucketId &bucketId) {
auto bucket = Cynara::PolicyBucket();
return m_buckets.insert({ bucketId, bucket }).first->second;
virtual ~InMemeoryStorageBackendFixture() {}
// TODO: consider defaulting accessor with ON_CALL
- Cynara::InMemoryStorageBackend::Buckets m_buckets;
+ Cynara::Buckets m_buckets;
};
* @brief Tests for Cynara::BucketDeserializer
*/
+#include <memory>
+#include <sstream>
+#include <tuple>
+#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "../../helpers.h"
-#include <memory>
-#include <sstream>
-#include <tuple>
-#include <vector>
-
using namespace Cynara;
MATCHER_P(PolicyAtPtrEq, policy, "") {
void checkCorruptedData(const std::string &data, const std::string &corruptedLine,
size_t corruptedLineNumber) {
- std::istringstream bucketStream(data);
+ auto bucketStream = std::make_shared<std::istringstream>(data);
BucketDeserializer deserializer(bucketStream);
EXPECT_THROW(deserializer.loadPolicies(), BucketRecordCorruptedException);
- bucketStream.seekg(0);
+ bucketStream->seekg(0);
try {
deserializer.loadPolicies();
} catch (const BucketRecordCorruptedException &ex) {
TEST_F(BucketDeserializerFixture, load_empty) {
using ::testing::IsEmpty;
- std::istringstream bucketStream;
+ auto bucketStream = std::make_shared<std::istringstream>();
BucketDeserializer deserializer(bucketStream);
auto policies = deserializer.loadPolicies();
TEST_F(BucketDeserializerFixture, load_1) {
using ::testing::UnorderedElementsAre;
- std::istringstream bucketStream("c;u;p;0;meta");
+ auto bucketStream = std::make_shared<std::istringstream>("c;u;p;0;meta");
BucketDeserializer deserializer(bucketStream);
auto policies = deserializer.loadPolicies();
TEST_F(BucketDeserializerFixture, load_1_allow) {
using ::testing::UnorderedElementsAre;
- std::istringstream bucketStream("c;u;p;0xFFFF;meta");
+ auto bucketStream = std::make_shared<std::istringstream>("c;u;p;0xFFFF;meta");
BucketDeserializer deserializer(bucketStream);
auto policies = deserializer.loadPolicies();
TEST_F(BucketDeserializerFixture, load_1_no_meta_sep) {
using ::testing::UnorderedElementsAre;
- std::istringstream bucketStream("c;u;p;0;");
+ auto bucketStream = std::make_shared<std::istringstream>("c;u;p;0;");
BucketDeserializer deserializer(bucketStream);
auto policies = deserializer.loadPolicies();
TEST_F(BucketDeserializerFixture, load_1_no_meta_no_sep) {
using ::testing::UnorderedElementsAre;
- std::istringstream bucketStream("c;u;p;0");
+ auto bucketStream = std::make_shared<std::istringstream>("c;u;p;0");
BucketDeserializer deserializer(bucketStream);
auto policies = deserializer.loadPolicies();
TEST_F(BucketDeserializerFixture, load_2) {
using ::testing::UnorderedElementsAre;
- std::istringstream bucketStream("c;u;p;0;meta\n"
- "c;u;p;0;meta\n");
+ auto bucketStream = std::make_shared<std::istringstream>("c;u;p;0;meta\n"
+ "c;u;p;0;meta\n");
BucketDeserializer deserializer(bucketStream);
auto policies = deserializer.loadPolicies();
using ::testing::UnorderedElementsAre;
using ::testing::UnorderedElementsAreArray;
- std::istringstream bucketStream("c1;u1;p1;0;meta\n"
- "c2;u2;p2;0xFFFF;meta2\n"
- "c2;u2;p2;0xFFFF;\n"
- "c1;u1;p3;0xFFFE;bucket\n");
+ auto bucketStream = std::make_shared<std::istringstream>("c1;u1;p1;0;meta\n"
+ "c2;u2;p2;0xFFFF;meta2\n"
+ "c2;u2;p2;0xFFFF;\n"
+ "c1;u1;p3;0xFFFE;bucket\n");
BucketDeserializer deserializer(bucketStream);
auto policies = deserializer.loadPolicies();
#include <gtest/gtest.h>
#include <exceptions/BucketDeserializationException.h>
-#include <types/PolicyBucket.h>
+#include <storage/Buckets.h>
#include <storage/StorageDeserializer.h>
-
-// TODO: Move to .h, because it's used also in bucket_load.cpp
-MATCHER_P(PolicyPtrEq, policy, "") {
- return std::tie(policy->key(), policy->result())
- == std::tie(arg->key(), arg->result());
-}
+#include <types/PolicyBucket.h>
MATCHER_P(PolicyBucketIdPolicyEq, expected, "") {
auto bucket1 = expected.second;
class FakeStreamForBucketId {
public:
- MOCK_METHOD1(streamForBucketId, std::shared_ptr<Cynara::BucketDeserializer>(const std::string &));
+ MOCK_METHOD1(streamForBucketId,
+ std::shared_ptr<Cynara::BucketDeserializer>(const std::string &));
};
class EmptyBucketDeserializer : public Cynara::BucketDeserializer {
public:
- EmptyBucketDeserializer() : Cynara::BucketDeserializer(m_emptyStream),
- m_emptyStream("") {}
-private:
- std::istringstream m_emptyStream;
+ EmptyBucketDeserializer()
+ : Cynara::BucketDeserializer(std::make_shared<std::istringstream>("")) {}
};
class StorageDeserializerFixture : public ::testing::Test {
TEST_F(StorageDeserializerFixture, init_default_only) {
using ::testing::UnorderedElementsAre;
- std::istringstream ss(";0");
+ auto ss = std::make_shared<std::istringstream>(";0");
StorageDeserializer deserializer(ss, nullStreamOpener);
- InMemoryStorageBackend::Buckets buckets;
+ Buckets buckets;
deserializer.initBuckets(buckets);
ASSERT_THAT(buckets, UnorderedElementsAre(
TEST_F(StorageDeserializerFixture, init_more) {
using ::testing::UnorderedElementsAre;
- std::istringstream ss(";0\n"
- "bucket2;0\n"
- "bucket3;0xFFFE;bucket2\n");
+ auto ss = std::make_shared<std::istringstream>(";0\n"
+ "bucket2;0\n"
+ "bucket3;0xFFFE;bucket2\n");
StorageDeserializer deserializer(ss, nullStreamOpener);
- InMemoryStorageBackend::Buckets buckets;
+ Buckets buckets;
deserializer.initBuckets(buckets);
ASSERT_THAT(buckets, UnorderedElementsAre(
TEST_F(StorageDeserializerFixture, init_overwrite) {
using ::testing::UnorderedElementsAre;
- std::istringstream ss(";0x0");
+ auto ss = std::make_shared<std::istringstream>(";0x0");
StorageDeserializer deserializer(ss, nullStreamOpener);
- InMemoryStorageBackend::Buckets buckets;
+ Buckets buckets;
// Default bucket has ALLOW policy as default
buckets.insert({ "", PolicyBucket("fakeId", PredefinedPolicyType::ALLOW) });
}
TEST_F(StorageDeserializerFixture, load_buckets_plus_policies) {
- using ::testing::_;
+ using ::testing::Pointee;
using ::testing::Return;
using ::testing::UnorderedElementsAre;
- InMemoryStorageBackend::Buckets buckets;
+ Buckets buckets;
buckets.insert({ "", PolicyBucket("", PredefinedPolicyType::DENY) });
- std::istringstream bucketsStream; // Won't be used; buckets are pre-inserted above
FakeStreamForBucketId streamOpener;
auto streamOpenerFunc = std::bind(&FakeStreamForBucketId::streamForBucketId, &streamOpener,
std::placeholders::_1);
- StorageDeserializer deserializer(bucketsStream, streamOpenerFunc);
+ StorageDeserializer deserializer(nullptr, streamOpenerFunc);
- std::istringstream defaultBucketStream("c;u;p;0;meta");
+ auto defaultBucketStream = std::make_shared<std::istringstream>("c;u;p;0;meta");
auto bucketDeserializer = std::make_shared<BucketDeserializer>(defaultBucketStream);
EXPECT_CALL(streamOpener, streamForBucketId(""))
.WillOnce(Return(bucketDeserializer));
COMPARE_BUCKETS("", PolicyBucket("", PredefinedPolicyType::DENY))
));
- auto expectedPolicy = std::make_shared<Policy>(PolicyKey("c", "u", "p"),
- PolicyResult(PredefinedPolicyType::DENY, "meta"));
-
// Check policy was inserted into bucket
ASSERT_THAT(buckets.at("").policyCollection(), UnorderedElementsAre(
- PolicyPtrEq(expectedPolicy)
+ Pointee(Policy(PolicyKey("c", "u", "p"), PolicyResult(PredefinedPolicyType::DENY, "meta")))
));
}
TEST_F(StorageDeserializerFixture, load_buckets) {
- using ::testing::_;
using ::testing::Return;
- using ::testing::UnorderedElementsAre;
// Pre-insert some buckets
- InMemoryStorageBackend::Buckets buckets;
+ Buckets buckets;
buckets.insert({ "", PolicyBucket("", PredefinedPolicyType::DENY) });
buckets.insert({ "bucket1", PolicyBucket("bucket1", PredefinedPolicyType::DENY) });
buckets.insert({ "bucket2", PolicyBucket("bucket2", PredefinedPolicyType::DENY) });
- std::istringstream bucketsStream; // Won't be used; buckets are pre-inserted above
FakeStreamForBucketId streamOpener;
auto streamOpenerFunc = std::bind(&FakeStreamForBucketId::streamForBucketId, &streamOpener,
std::placeholders::_1);
- StorageDeserializer deserializer(bucketsStream, streamOpenerFunc);
+ StorageDeserializer deserializer(nullptr, streamOpenerFunc);
// Check, if streamOpener was called for each bucket
EXPECT_CALL(streamOpener, streamForBucketId(""))
}
TEST_F(StorageDeserializerFixture, load_buckets_io_error) {
- using ::testing::_;
using ::testing::Return;
- using ::testing::UnorderedElementsAre;
// Pre-insert some buckets
- InMemoryStorageBackend::Buckets buckets;
+ Buckets buckets;
buckets.insert({ "", PolicyBucket("", PredefinedPolicyType::DENY) });
- std::istringstream bucketsStream; // Won't be used; buckets are pre-inserted above
FakeStreamForBucketId streamOpener;
auto streamOpenerFunc = std::bind(&FakeStreamForBucketId::streamForBucketId, &streamOpener,
std::placeholders::_1);
- StorageDeserializer deserializer(bucketsStream, streamOpenerFunc);
+ StorageDeserializer deserializer(nullptr, streamOpenerFunc);
// Check, if streamOpener was called for each bucket
EXPECT_CALL(streamOpener, streamForBucketId(""))
* @brief Tests for dumping feature of Cynara::StorageSerializer
*/
-#include <gtest/gtest.h>
+
+#include <sstream>
+#include <memory>
+
#include <gmock/gmock.h>
+#include <gtest/gtest.h>
-#include "storage/StorageSerializer.h"
-#include "types/PolicyBucket.h"
-#include "types/PolicyType.h"
-#include "types/PolicyKey.h"
-#include "types/Policy.h"
+#include <storage/StorageSerializer.h>
+#include <types/Policy.h>
+#include <types/PolicyBucket.h>
+#include <types/PolicyKey.h>
+#include <types/PolicyType.h>
#include "../../helpers.h"
-#include <sstream>
-
using namespace Cynara;
static std::string expectedPolicyKey(const PolicyKey &key) {
}
TEST(serializer_dump, dump_empty_bucket) {
- std::ostringstream oss;
+ auto oss = std::make_shared<std::ostringstream>();
PolicyBucket bucket;
StorageSerializer serializer(oss);
serializer.dump(bucket);
- ASSERT_EQ("", oss.str());
+ ASSERT_EQ("", oss->str());
}
TEST(serializer_dump, dump_bucket) {
PolicyBucket bucket = {{ Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW),
Policy::simpleWithKey(pk2, PredefinedPolicyType::DENY) }};
- std::ostringstream outputStream;
+ auto outputStream = std::make_shared<std::ostringstream>();
StorageSerializer serializer(outputStream);
serializer.dump(bucket);
// See: StorageSerializerFixture::dump_buckets in serialize.cpp
std::stringstream expected;
expected
- << expectedPolicyKey(pk1) << ";" << expectedPolicyType(PredefinedPolicyType::ALLOW) << ";" << "\n"
- << expectedPolicyKey(pk2) << ";" << expectedPolicyType(PredefinedPolicyType::DENY) << ";" << "\n";
+ << expectedPolicyKey(pk1) << ";" << expectedPolicyType(PredefinedPolicyType::ALLOW)<< ";"
+ << std::endl
+ << expectedPolicyKey(pk2) << ";" << expectedPolicyType(PredefinedPolicyType::DENY) << ";"
+ << std::endl;
- ASSERT_EQ(expected.str(), outputStream.str());
+ ASSERT_EQ(expected.str(), outputStream->str());
}
TEST(serializer_dump, dump_bucket_bucket) {
Policy::simpleWithKey(pk2, PredefinedPolicyType::DENY),
Policy::bucketWithKey(pk3, bucketId) }};
- std::ostringstream outputStream;
+ auto outputStream = std::make_shared<std::ostringstream>();
StorageSerializer serializer(outputStream);
serializer.dump(bucket);
// See: StorageSerializerFixture::dump_buckets in serialize.cpp
std::stringstream expected;
expected
- << expectedPolicyKey(pk1) << ";" << expectedPolicyType(PredefinedPolicyType::BUCKET) << ";" << bucketId << "\n"
- << expectedPolicyKey(pk2) << ";" << expectedPolicyType(PredefinedPolicyType::DENY) << ";" << "\n"
- << expectedPolicyKey(pk3) << ";" << expectedPolicyType(PredefinedPolicyType::BUCKET) << ";" << bucketId << "\n";
-
- ASSERT_EQ(expected.str(), outputStream.str());
+ << expectedPolicyKey(pk1) << ";" << expectedPolicyType(PredefinedPolicyType::BUCKET) << ";"
+ << bucketId << std::endl
+ << expectedPolicyKey(pk2) << ";" << expectedPolicyType(PredefinedPolicyType::DENY) << ";"
+ << std::endl
+ << expectedPolicyKey(pk3) << ";" << expectedPolicyType(PredefinedPolicyType::BUCKET) << ";"
+ << bucketId << std::endl;
+
+ ASSERT_EQ(expected.str(), outputStream->str());
}
#include <gtest/gtest.h>
#include <exceptions/BucketSerializationException.h>
+#include <storage/Buckets.h>
#include <storage/InMemoryStorageBackend.h>
#include <storage/StorageSerializer.h>
#include <types/PolicyBucketId.h>
// Fake StorageSerializer for Cynara::PolicyBucket
class FakeStorageSerializer : public Cynara::StorageSerializer {
public:
- FakeStorageSerializer() : Cynara::StorageSerializer(outStream) {}
+ FakeStorageSerializer() : Cynara::StorageSerializer(outStream),
+ outStream(new std::ostringstream()) {}
MOCK_METHOD1(dump, void(const Cynara::PolicyBucket &bucket));
- std::ostringstream outStream;
+ std::shared_ptr<std::ostringstream> outStream;
};
class StorageSerializerFixture : public ::testing::Test {
public:
virtual ~StorageSerializerFixture() = default;
- Cynara::InMemoryStorageBackend::Buckets buckets;
+ Cynara::Buckets buckets;
FakeStreamForBucketId fakeStreamOpener;
};
// Be sure no calls to streamForBucketId() are made
// and output stream is not touched
TEST_F(StorageSerializerFixture, dump_buckets_empty) {
- std::ostringstream outStream;
+ auto outStream = std::make_shared<std::ostringstream>();
StorageSerializer serializer(outStream);
- serializer.dump(InMemoryStorageBackend::Buckets(), fakeStreamOpener.streamOpener());
+ serializer.dump(Buckets(), fakeStreamOpener.streamOpener());
// Stream should be empty
- ASSERT_EQ(0, outStream.tellp());
+ ASSERT_EQ(0, outStream->tellp());
}
TEST_F(StorageSerializerFixture, dump_buckets) {
PolicyBucket("bucket3", PolicyResult(PredefinedPolicyType::BUCKET, "bucket2")) }
};
- std::stringstream outStream;
+ auto outStream = std::make_shared<std::stringstream>();
StorageSerializer dbSerializer(outStream);
// Make sure stream was opened for each bucket
};
// Split stream into records
- auto actualRecords = std::vector<std::string>(std::istream_iterator<std::string>(outStream),
+ auto actualRecords = std::vector<std::string>(std::istream_iterator<std::string>(*outStream),
std::istream_iterator<std::string>());
ASSERT_THAT(actualRecords, UnorderedElementsAreArray(expectedRecords));
{ "bucket1", PolicyBucket("bucket1", PredefinedPolicyType::DENY) },
};
- std::stringstream outStream;
+ auto outStream = std::make_shared<std::stringstream>();
StorageSerializer dbSerializer(outStream);
// Make sure stream was opened for each bucket
class FakeStorageBackend : public StorageBackend {
public:
+ MOCK_METHOD0(load, void(void));
+ MOCK_METHOD0(save, void(void));
MOCK_METHOD1(searchDefaultBucket, PolicyBucket(const PolicyKey &key));
MOCK_METHOD2(searchBucket, PolicyBucket(const PolicyBucketId &bucket, const PolicyKey &key));
MOCK_METHOD2(createBucket, void(const PolicyBucketId &bucketId,
* @brief Tests of policies in Storage
*/
-#include <gtest/gtest.h>
-#include <gmock/gmock.h>
-
-#include "types/PolicyType.h"
-#include "types/PolicyKey.h"
-#include "types/PolicyResult.h"
-#include "types/PolicyCollection.h"
-#include "types/pointers.h"
-#include "exceptions/DefaultBucketDeletionException.h"
-#include "storage/Storage.h"
-#include "storage/StorageBackend.h"
-
-
-#include "fakestoragebackend.h"
-#include "../../helpers.h"
-
#include <map>
#include <memory>
#include <tuple>
#include <vector>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include <storage/Storage.h>
+#include <storage/StorageBackend.h>
+#include <types/pointers.h>
+#include <types/PolicyCollection.h>
+#include <types/PolicyKey.h>
+#include <types/PolicyResult.h>
+#include <types/PolicyType.h>
+
+#include "../../helpers.h"
+#include "fakestoragebackend.h"
+
using namespace Cynara;
TEST(storage, deletePolicies) {
- using ::testing::_;
- using ::testing::Eq;
- using ::testing::TypedEq;
FakeStorageBackend backend;
Cynara::Storage storage(backend);
PolicyBucketId bucketId2 = "bucket-2";
EXPECT_CALL(backend, deletePolicy(bucketId1, pk1));
- EXPECT_CALL(backend, deletePolicy(bucketId2, pk1));
EXPECT_CALL(backend, deletePolicy(bucketId1, pk2));
+ EXPECT_CALL(backend, deletePolicy(bucketId2, pk1));
- std::map<PolicyBucketId, std::vector<PolicyKey>> policies;
- policies[bucketId1].push_back(pk1);
- policies[bucketId2].push_back(pk1);
- policies[bucketId1].push_back(pk2);
+ typedef std::pair<PolicyBucketId, std::vector<PolicyKey>> BucketPoliciesPair;
- storage.deletePolicies(policies);
+ storage.deletePolicies({
+ BucketPoliciesPair(bucketId1, { pk1, pk2 }),
+ BucketPoliciesPair(bucketId2, { pk1 }),
+ });
}
// TODO: isn't it the same test as storage.deleteBucket?
storage.deleteBucket(bucketId);
}
-
TEST(storage, insertPolicies) {
+ using ::testing::Pointee;
using ::testing::Return;
- using ::testing::_;
FakeStorageBackend backend;
- Cynara::Storage storage(backend);
+ Storage storage(backend);
PolicyBucketId testBucket1 = "test-bucket-1";
PolicyBucketId testBucket2 = "test-bucket-2";
- std::map<PolicyBucketId, std::vector<Policy>> policiesToInsert;
- policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("1"), PredefinedPolicyType::ALLOW));
- policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("2"), PredefinedPolicyType::DENY));
- policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("3"), PredefinedPolicyType::DENY));
- policiesToInsert[testBucket2].push_back(Policy(Helpers::generatePolicyKey("4"), PredefinedPolicyType::ALLOW));
- policiesToInsert[testBucket2].push_back(Policy(Helpers::generatePolicyKey("5"), PredefinedPolicyType::ALLOW));
-
- for (const auto &bucket : policiesToInsert) {
- const PolicyBucketId &bucketId = bucket.first;
- for (const auto &policy : bucket.second) {
+ typedef std::pair<PolicyBucketId, std::vector<Policy>> BucketPolicyPair;
+
+ auto createPolicy = [] (const std::string &keySuffix, const PolicyType &type) -> Policy {
+ return Policy(Helpers::generatePolicyKey(keySuffix), type);
+ };
+
+ std::map<PolicyBucketId, std::vector<Policy>> policiesToInsert = {
+ BucketPolicyPair(testBucket1, {
+ createPolicy("1", PredefinedPolicyType::ALLOW),
+ createPolicy("2", PredefinedPolicyType::DENY),
+ createPolicy("3", PredefinedPolicyType::DENY)
+ }),
+ BucketPolicyPair(testBucket2, {
+ createPolicy("4", PredefinedPolicyType::ALLOW),
+ createPolicy("5", PredefinedPolicyType::ALLOW)
+ })
+ };
+
+ for (const auto &group : policiesToInsert) {
+ const auto &bucketId = group.first;
+ const auto &policies = group.second;
+
+ for (const auto &policy : policies) {
EXPECT_CALL(backend, searchBucket(bucketId, policy.key()))
.WillOnce(Return(PolicyBucket()));
- EXPECT_CALL(backend, insertPolicy(bucketId, _));
+ EXPECT_CALL(backend, insertPolicy(bucketId, Pointee(policy)));
}
}
}
TEST(storage, updatePolicies) {
+ using ::testing::Pointee;
using ::testing::Return;
- using ::testing::_;
FakeStorageBackend backend;
- Cynara::Storage storage(backend);
+ Storage storage(backend);
PolicyBucketId testBucket1 = "test-bucket-1";
PolicyBucketId testBucket2 = "test-bucket-2";
- std::map<PolicyBucketId, std::vector<Policy>> policiesToInsert;
- policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("1"), PredefinedPolicyType::ALLOW));
- policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("2"), PredefinedPolicyType::DENY));
- policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("3"), PredefinedPolicyType::DENY));
- policiesToInsert[testBucket2].push_back(Policy(Helpers::generatePolicyKey("4"), PredefinedPolicyType::ALLOW));
- policiesToInsert[testBucket2].push_back(Policy(Helpers::generatePolicyKey("5"), PredefinedPolicyType::ALLOW));
-
- storage.insertPolicies(policiesToInsert);
-
- for (const auto &bucket : policiesToInsert) {
- const PolicyBucketId &bucketId = bucket.first;
- for (const auto &policy : bucket.second) {
- EXPECT_CALL(backend, searchBucket(bucketId, policy.key()));
+ typedef std::pair<PolicyBucketId, std::vector<Policy>> BucketPolicyPair;
+
+ auto createPolicy = [] (const std::string &keySuffix, const PolicyType &type) -> Policy {
+ return Policy(Helpers::generatePolicyKey(keySuffix), type);
+ };
+
+ std::map<PolicyBucketId, std::vector<Policy>> policiesToInsert = {
+ BucketPolicyPair(testBucket1, {
+ createPolicy("1", PredefinedPolicyType::ALLOW),
+ createPolicy("2", PredefinedPolicyType::DENY),
+ createPolicy("3", PredefinedPolicyType::DENY)
+ }),
+ BucketPolicyPair(testBucket2, {
+ createPolicy("4", PredefinedPolicyType::ALLOW),
+ createPolicy("5", PredefinedPolicyType::ALLOW)
+ })
+ };
+
+ for (const auto &group : policiesToInsert) {
+ const auto &bucketId = group.first;
+ const auto &policies = group.second;
+
+ for (const auto &policy : policies) {
+ PolicyBucket searchResult(PolicyCollection { std::make_shared<Policy>(policy) });
+ EXPECT_CALL(backend, searchBucket(bucketId, policy.key()))
+ .WillOnce(Return(searchResult));
EXPECT_CALL(backend, deletePolicy(bucketId, policy.key()));
- EXPECT_CALL(backend, insertPolicy(bucketId, _));
+ EXPECT_CALL(backend, insertPolicy(bucketId, Pointee(policy)));
}
}