PageRenderTime 47ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/test/storage/serializer/serialize.cpp

https://gitlab.com/admin-github-cloud/cynara
C++ | 144 lines | 87 code | 27 blank | 30 comment | 1 complexity | b809f6557e6f122395fac79b533ba845 MD5 | raw file
  1. /*
  2. * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file test/storage/serializer/serialize.cpp
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @version 1.0
  20. * @brief Tests for dumping feature of Cynara::StorageSerializer
  21. */
  22. #include <iterator>
  23. #include <memory>
  24. #include <sstream>
  25. #include <string>
  26. #include <vector>
  27. #include <gmock/gmock.h>
  28. #include <gtest/gtest.h>
  29. #include <exceptions/BucketSerializationException.h>
  30. #include <storage/Buckets.h>
  31. #include <storage/InMemoryStorageBackend.h>
  32. #include <storage/StorageSerializer.h>
  33. #include <types/PolicyBucketId.h>
  34. class FakeStreamForBucketId {
  35. public:
  36. typedef std::shared_ptr<Cynara::StorageSerializer<std::stringstream> >
  37. StringstreamStorageSerializerPtr;
  38. MOCK_METHOD1(streamForBucketId,
  39. StringstreamStorageSerializerPtr(const Cynara::PolicyBucketId &));
  40. Cynara::StorageSerializer<std::stringstream>::BucketStreamOpener streamOpener() {
  41. return std::bind(&FakeStreamForBucketId::streamForBucketId, this, std::placeholders::_1);
  42. }
  43. };
  44. // Fake StorageSerializer for Cynara::PolicyBucket
  45. class FakeStorageSerializer : public Cynara::StorageSerializer<std::stringstream> {
  46. public:
  47. FakeStorageSerializer(std::shared_ptr<std::stringstream> o)
  48. : Cynara::StorageSerializer<std::stringstream>(o), outStream(o) {}
  49. MOCK_METHOD1(dump, void(const Cynara::PolicyBucket &bucket));
  50. std::shared_ptr<std::stringstream> outStream;
  51. };
  52. class StorageSerializerFixture : public ::testing::Test {
  53. public:
  54. virtual ~StorageSerializerFixture() {};
  55. Cynara::Buckets buckets;
  56. FakeStreamForBucketId fakeStreamOpener;
  57. };
  58. using namespace Cynara;
  59. // Be sure no calls to streamForBucketId() are made
  60. // and output stream is not touched
  61. TEST_F(StorageSerializerFixture, dump_buckets_empty) {
  62. auto outStream = std::make_shared<std::stringstream>();
  63. StorageSerializer<std::stringstream> serializer(outStream);
  64. serializer.dump(Buckets(), fakeStreamOpener.streamOpener());
  65. // Stream should be empty
  66. ASSERT_EQ(0, outStream->tellp());
  67. }
  68. TEST_F(StorageSerializerFixture, dump_buckets) {
  69. using ::testing::_;
  70. using ::testing::Property;
  71. using ::testing::Return;
  72. using ::testing::UnorderedElementsAreArray;
  73. // Will be returned as serializer for buckets
  74. auto fakeBucketSerializer = std::make_shared<FakeStorageSerializer>(
  75. std::make_shared<std::stringstream>());
  76. buckets = {
  77. { "bucket1", PolicyBucket("bucket1", PredefinedPolicyType::DENY) },
  78. { "bucket2", PolicyBucket("bucket2", PredefinedPolicyType::DENY) },
  79. { "bucket3",
  80. PolicyBucket("bucket3", PolicyResult(PredefinedPolicyType::BUCKET, "bucket2")) }
  81. };
  82. auto outStream = std::make_shared<std::stringstream>();
  83. StorageSerializer<std::stringstream> dbSerializer(outStream);
  84. // Make sure stream was opened for each bucket
  85. EXPECT_CALL(fakeStreamOpener, streamForBucketId(_))
  86. .Times(buckets.size()).WillRepeatedly(Return(fakeBucketSerializer));
  87. // Make sure every bucket was dumped
  88. for (const auto &bucket : buckets) {
  89. EXPECT_CALL(*fakeBucketSerializer, dump(Property(&PolicyBucket::id, bucket.first)));
  90. }
  91. dbSerializer.dump(buckets, fakeStreamOpener.streamOpener());
  92. std::vector<std::string> expectedRecords = {
  93. "bucket1;0x0;",
  94. "bucket2;0x0;",
  95. "bucket3;0xFFFE;bucket2"
  96. };
  97. // Split stream into records
  98. auto actualRecords = std::vector<std::string>(std::istream_iterator<std::string>(*outStream),
  99. std::istream_iterator<std::string>());
  100. ASSERT_THAT(actualRecords, UnorderedElementsAreArray(expectedRecords));
  101. }
  102. TEST_F(StorageSerializerFixture, dump_buckets_io_error) {
  103. using ::testing::_;
  104. using ::testing::Return;
  105. buckets = {
  106. { "bucket1", PolicyBucket("bucket1", PredefinedPolicyType::DENY) },
  107. };
  108. auto outStream = std::make_shared<std::stringstream>();
  109. StorageSerializer<std::stringstream> dbSerializer(outStream);
  110. // Make sure stream was opened for each bucket
  111. EXPECT_CALL(fakeStreamOpener, streamForBucketId(_))
  112. .Times(buckets.size()).WillRepeatedly(Return(nullptr));
  113. ASSERT_THROW(
  114. dbSerializer.dump(buckets, fakeStreamOpener.streamOpener()),
  115. BucketSerializationException
  116. );
  117. }