Add serialization IO exception 75/24575/6
authorAleksander Zdyb <a.zdyb@partner.samsung.com>
Wed, 16 Jul 2014 14:09:05 +0000 (16:09 +0200)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Fri, 18 Jul 2014 08:48:41 +0000 (10:48 +0200)
BucketSerializationException is thrown, when
bucket file stream could not be opened.

Change-Id: If8d3ca06f708fbe4a67c82e43c967d56be30e8af

src/common/exceptions/BucketSerializationException.h [new file with mode: 0644]
src/service/storage/StorageSerializer.cpp
test/storage/serializer/serialize.cpp

diff --git a/src/common/exceptions/BucketSerializationException.h b/src/common/exceptions/BucketSerializationException.h
new file mode 100644 (file)
index 0000000..8882ff4
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+/*
+ * @file        BucketSerializationException.h
+ * @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
+ * @version     1.0
+ * @brief       Implementation of BucketSerializationException
+ */
+#ifndef SRC_COMMON_EXCEPTIONS_BUCKETSERIALIZATIONEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_BUCKETSERIALIZATIONEXCEPTION_H_
+
+#include <exceptions/DatabaseException.h>
+#include <types/PolicyBucketId.h>
+
+namespace Cynara {
+
+class BucketSerializationException : public DatabaseException {
+public:
+    BucketSerializationException(const PolicyBucketId &bucketId) : m_bucketId(bucketId) {}
+
+    const std::string message(void) const {
+        if (m_message.empty()) {
+            m_message = "Could not serialize bucket " + m_bucketId;
+        }
+        return m_message;
+    }
+
+    const PolicyBucketId &bucketId(void) const {
+        return m_bucketId;
+    }
+
+private:
+    mutable std::string m_message;
+    PolicyBucketId m_bucketId;
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_EXCEPTIONS_BUCKETSERIALIZATIONEXCEPTION_H_ */
index e7857b6..be8b9d4 100644 (file)
@@ -24,6 +24,7 @@
 #include <ios>
 #include <iostream>
 
+#include <exceptions/BucketSerializationException.h>
 #include "types/PolicyBucket.h"
 #include "types/PolicyCollection.h"
 
@@ -54,7 +55,7 @@ void StorageSerializer::dump(const InMemoryStorageBackend::Buckets &buckets,
         if (bucketSerializer != nullptr) {
             bucketSerializer->dump(bucket);
         } else {
-            // TODO: Throw?
+            throw BucketSerializationException(bucketId);
         }
     }
 }
index dddd30d..06b8cfb 100644 (file)
@@ -29,6 +29,7 @@
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>
 
+#include <exceptions/BucketSerializationException.h>
 #include <storage/InMemoryStorageBackend.h>
 #include <storage/StorageSerializer.h>
 #include <types/PolicyBucketId.h>
@@ -114,3 +115,24 @@ TEST_F(StorageSerializerFixture, dump_buckets) {
 
     ASSERT_THAT(actualRecords, UnorderedElementsAreArray(expectedRecords));
 }
+
+TEST_F(StorageSerializerFixture, dump_buckets_io_error) {
+    using ::testing::_;
+    using ::testing::Return;
+
+    buckets = {
+        { "bucket1", PolicyBucket("bucket1", PredefinedPolicyType::DENY) },
+    };
+
+    std::stringstream outStream;
+    StorageSerializer dbSerializer(outStream);
+
+    // Make sure stream was opened for each bucket
+    EXPECT_CALL(fakeStreamOpener, streamForBucketId(_))
+        .Times(buckets.size()).WillRepeatedly(Return(nullptr));
+
+    ASSERT_THROW(
+        dbSerializer.dump(buckets, fakeStreamOpener.streamOpener()),
+        BucketSerializationException
+    );
+}