2 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file test/storage/serializer/serialize.cpp
18 * @author Aleksander Zdyb <a.zdyb@samsung.com>
20 * @brief Tests for dumping feature of Cynara::StorageSerializer
29 #include <gmock/gmock.h>
30 #include <gtest/gtest.h>
32 #include <exceptions/BucketSerializationException.h>
33 #include <storage/Buckets.h>
34 #include <storage/InMemoryStorageBackend.h>
35 #include <storage/StorageSerializer.h>
36 #include <types/PolicyBucketId.h>
38 class FakeStreamForBucketId {
40 typedef std::shared_ptr<Cynara::StorageSerializer<std::stringstream> >
41 StringstreamStorageSerializerPtr;
43 MOCK_METHOD1(streamForBucketId,
44 StringstreamStorageSerializerPtr(const Cynara::PolicyBucketId &));
46 Cynara::StorageSerializer<std::stringstream>::BucketStreamOpener streamOpener() {
47 return std::bind(&FakeStreamForBucketId::streamForBucketId, this, std::placeholders::_1);
51 // Fake StorageSerializer for Cynara::PolicyBucket
52 class FakeStorageSerializer : public Cynara::StorageSerializer<std::stringstream> {
54 FakeStorageSerializer(std::shared_ptr<std::stringstream> o)
55 : Cynara::StorageSerializer<std::stringstream>(o), outStream(o) {}
56 MOCK_METHOD1(dump, void(const Cynara::PolicyBucket &bucket));
57 std::shared_ptr<std::stringstream> outStream;
60 class StorageSerializerFixture : public ::testing::Test {
62 virtual ~StorageSerializerFixture() {};
64 Cynara::Buckets buckets;
65 FakeStreamForBucketId fakeStreamOpener;
68 using namespace Cynara;
70 // Be sure no calls to streamForBucketId() are made
71 // and output stream is not touched
72 TEST_F(StorageSerializerFixture, dump_buckets_empty) {
73 auto outStream = std::make_shared<std::stringstream>();
74 StorageSerializer<std::stringstream> serializer(outStream);
75 serializer.dump(Buckets(), fakeStreamOpener.streamOpener());
77 // Stream should be empty
78 ASSERT_EQ(0, outStream->tellp());
81 TEST_F(StorageSerializerFixture, dump_buckets) {
83 using ::testing::Property;
84 using ::testing::Return;
85 using ::testing::UnorderedElementsAreArray;
87 // Will be returned as serializer for buckets
88 auto fakeBucketSerializer = std::make_shared<FakeStorageSerializer>(
89 std::make_shared<std::stringstream>());
92 { "bucket1", PolicyBucket("bucket1", PredefinedPolicyType::DENY) },
93 { "bucket2", PolicyBucket("bucket2", PredefinedPolicyType::DENY) },
95 PolicyBucket("bucket3", PolicyResult(PredefinedPolicyType::BUCKET, "bucket2")) }
98 auto outStream = std::make_shared<std::stringstream>();
99 StorageSerializer<std::stringstream> dbSerializer(outStream);
101 // Make sure stream was opened for each bucket
102 EXPECT_CALL(fakeStreamOpener, streamForBucketId(_))
103 .Times(buckets.size()).WillRepeatedly(Return(fakeBucketSerializer));
105 // Make sure every bucket was dumped
106 for (const auto &bucket : buckets) {
107 EXPECT_CALL(*fakeBucketSerializer, dump(Property(&PolicyBucket::id, bucket.first)));
110 dbSerializer.dump(buckets, fakeStreamOpener.streamOpener());
112 std::vector<std::string> expectedRecords = {
115 "bucket3;0xFFFE;bucket2"
118 // Split stream into records
119 auto actualRecords = std::vector<std::string>(std::istream_iterator<std::string>(*outStream),
120 std::istream_iterator<std::string>());
122 ASSERT_THAT(actualRecords, UnorderedElementsAreArray(expectedRecords));
125 TEST_F(StorageSerializerFixture, dump_buckets_io_error) {
127 using ::testing::Return;
130 { "bucket1", PolicyBucket("bucket1", PredefinedPolicyType::DENY) },
133 auto outStream = std::make_shared<std::stringstream>();
134 StorageSerializer<std::stringstream> dbSerializer(outStream);
136 // Make sure stream was opened for each bucket
137 EXPECT_CALL(fakeStreamOpener, streamForBucketId(_))
138 .Times(buckets.size()).WillRepeatedly(Return(nullptr));
141 dbSerializer.dump(buckets, fakeStreamOpener.streamOpener()),
142 BucketSerializationException