Adjust tests for new storage and serialization API 52/24852/5
authorAleksander Zdyb <a.zdyb@partner.samsung.com>
Fri, 25 Jul 2014 09:00:53 +0000 (11:00 +0200)
committerAleksander Zdyb <a.zdyb@partner.samsung.com>
Mon, 28 Jul 2014 06:38:56 +0000 (08:38 +0200)
Tests need to be adjusted because of changes in
Storage::insertPolicies() and Storage::deletePolicies().
They also make use of pointers on I/O streams instead of references
(in serializers and deserializers).

Change-Id: Ief7e256f0848097caa66365ffd063289c50ec26c

src/common/types/Policy.h
src/service/storage/InMemoryStorageBackend.h
test/storage/inmemorystoragebackend/buckets.cpp
test/storage/inmemorystoragebackend/fakeinmemorystoragebackend.h
test/storage/inmemorystoragebackend/inmemeorystoragebackendfixture.h
test/storage/serializer/bucket_load.cpp
test/storage/serializer/deserialize.cpp
test/storage/serializer/dump.cpp
test/storage/serializer/serialize.cpp
test/storage/storage/fakestoragebackend.h
test/storage/storage/policies.cpp

index 3b4737f..06db2f4 100644 (file)
@@ -32,6 +32,7 @@
 #include "types/PolicyBucketId.h"
 
 #include <memory>
+#include <tuple>
 
 namespace Cynara {
 
@@ -50,6 +51,10 @@ public:
         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;
index be2ba54..68d042e 100644 (file)
@@ -60,6 +60,7 @@ public:
     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);
 
index bc9fcf6..9028d1a 100644 (file)
  * @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;
 
index 52e046f..a53a24a 100644 (file)
 #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&());
 };
 
 
index 21ae4fc..b194006 100644 (file)
 #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;
@@ -46,7 +47,7 @@ protected:
     virtual ~InMemeoryStorageBackendFixture() {}
 
     // TODO: consider defaulting accessor with ON_CALL
-    Cynara::InMemoryStorageBackend::Buckets m_buckets;
+    Cynara::Buckets m_buckets;
 };
 
 
index aa7d290..3c2096e 100644 (file)
  * @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, "") {
@@ -52,11 +51,11 @@ public:
 
     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) {
@@ -69,7 +68,7 @@ public:
 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();
@@ -79,7 +78,7 @@ TEST_F(BucketDeserializerFixture, load_empty) {
 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();
@@ -91,7 +90,7 @@ TEST_F(BucketDeserializerFixture, load_1) {
 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();
@@ -103,7 +102,7 @@ TEST_F(BucketDeserializerFixture, load_1_allow) {
 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();
@@ -115,7 +114,7 @@ TEST_F(BucketDeserializerFixture, load_1_no_meta_sep) {
 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();
@@ -127,8 +126,8 @@ TEST_F(BucketDeserializerFixture, load_1_no_meta_no_sep) {
 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();
@@ -142,10 +141,10 @@ TEST_F(BucketDeserializerFixture, load_mixed) {
     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();
index 1fd6ad3..140242b 100644 (file)
 #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;
@@ -50,15 +45,14 @@ MATCHER_P(PolicyBucketIdPolicyEq, expected, "") {
 
 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 {
@@ -78,10 +72,10 @@ using namespace Cynara;
 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(
@@ -92,12 +86,12 @@ TEST_F(StorageDeserializerFixture, init_default_only) {
 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(
@@ -112,10 +106,10 @@ TEST_F(StorageDeserializerFixture, init_more) {
 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) });
 
@@ -128,20 +122,19 @@ TEST_F(StorageDeserializerFixture, init_overwrite) {
 }
 
 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));
@@ -153,31 +146,25 @@ TEST_F(StorageDeserializerFixture, load_buckets_plus_policies) {
         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(""))
@@ -193,19 +180,16 @@ TEST_F(StorageDeserializerFixture, load_buckets) {
 }
 
 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(""))
index 157a9f2..2a0bfd0 100644 (file)
  * @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) {
@@ -46,13 +48,13 @@ static std::string expectedPolicyType(const PolicyType &type) {
 }
 
 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) {
@@ -62,7 +64,7 @@ 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);
 
@@ -71,10 +73,12 @@ TEST(serializer_dump, 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) {
@@ -87,7 +91,7 @@ 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);
 
@@ -96,9 +100,12 @@ TEST(serializer_dump, dump_bucket_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());
 }
index 06b8cfb..07804f4 100644 (file)
@@ -30,6 +30,7 @@
 #include <gtest/gtest.h>
 
 #include <exceptions/BucketSerializationException.h>
+#include <storage/Buckets.h>
 #include <storage/InMemoryStorageBackend.h>
 #include <storage/StorageSerializer.h>
 #include <types/PolicyBucketId.h>
@@ -47,16 +48,17 @@ public:
 // 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;
 };
 
@@ -65,12 +67,12 @@ using namespace Cynara;
 // 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) {
@@ -89,7 +91,7 @@ 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
@@ -110,7 +112,7 @@ TEST_F(StorageSerializerFixture, dump_buckets) {
     };
 
     // 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));
@@ -124,7 +126,7 @@ TEST_F(StorageSerializerFixture, dump_buckets_io_error) {
         { "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
index bfa212d..b1e8b69 100644 (file)
@@ -27,6 +27,8 @@ using namespace Cynara;
 
 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,
index 5c593b5..4125a6d 100644 (file)
  * @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);
 
@@ -56,15 +51,15 @@ TEST(storage, deletePolicies) {
     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?
@@ -80,29 +75,41 @@ TEST(storage, deleteBucketWithLinkedPolicies) {
     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)));
         }
     }
 
@@ -110,29 +117,42 @@ TEST(storage, insertPolicies) {
 }
 
 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)));
         }
     }