2 * Copyright (c) 2014 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/inmemorystoragebackend/buckets.cpp
18 * @author Aleksander Zdyb <a.zdyb@samsung.com>
20 * @brief Tests of buckets in InMemeoryStorageBackend
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
26 #include <exceptions/BucketNotExistsException.h>
27 #include <types/PolicyBucket.h>
28 #include <types/PolicyResult.h>
30 #include "fakeinmemorystoragebackend.h"
31 #include "inmemeorystoragebackendfixture.h"
33 using namespace Cynara;
35 TEST_F(InMemeoryStorageBackendFixture, createBucket) {
36 using ::testing::ReturnRef;
37 using ::testing::IsEmpty;
39 FakeInMemoryStorageBackend backend;
40 EXPECT_CALL(backend, buckets())
41 .WillRepeatedly(ReturnRef(m_buckets));
43 PolicyResult defaultPolicy(PredefinedPolicyType::ALLOW);
44 PolicyBucketId bucketId = "new-bucket";
45 backend.createBucket(bucketId, defaultPolicy);
47 ASSERT_EQ(1, m_buckets.size());
48 ASSERT_NE(m_buckets.end(), m_buckets.find(bucketId));
49 ASSERT_EQ(defaultPolicy, m_buckets.at(bucketId).defaultPolicy());
50 ASSERT_THAT(m_buckets.at(bucketId), IsEmpty());
53 TEST_F(InMemeoryStorageBackendFixture, updateBucket) {
54 using ::testing::ReturnRef;
56 FakeInMemoryStorageBackend backend;
57 EXPECT_CALL(backend, buckets())
58 .WillRepeatedly(ReturnRef(m_buckets));
60 PolicyBucketId bucketId = "bucket";
61 auto &bucket = this->createBucket(bucketId);
62 bucket.setDefaultPolicy(PredefinedPolicyType::ALLOW);
64 backend.updateBucket(bucketId, PredefinedPolicyType::DENY);
66 ASSERT_EQ(PredefinedPolicyType::DENY, bucket.defaultPolicy());
69 TEST_F(InMemeoryStorageBackendFixture, updateNonexistentBucket) {
70 using ::testing::ReturnRef;
72 FakeInMemoryStorageBackend backend;
73 EXPECT_CALL(backend, buckets())
74 .WillRepeatedly(ReturnRef(m_buckets));
76 EXPECT_THROW(backend.updateBucket("non-existent", PredefinedPolicyType::ALLOW),
77 BucketNotExistsException);
80 TEST_F(InMemeoryStorageBackendFixture, deleteBucket) {
81 using ::testing::ReturnRef;
82 using ::testing::IsEmpty;
84 FakeInMemoryStorageBackend backend;
85 EXPECT_CALL(backend, buckets())
86 .WillRepeatedly(ReturnRef(m_buckets));
88 PolicyBucketId bucketId = "delete-bucket";
89 m_buckets.insert({ bucketId, PolicyBucket(bucketId) });
91 backend.deleteBucket(bucketId);
93 ASSERT_THAT(m_buckets, IsEmpty());
96 TEST_F(InMemeoryStorageBackendFixture, hasBucket) {
97 using ::testing::ReturnRef;
98 using ::testing::IsEmpty;
100 FakeInMemoryStorageBackend backend;
101 EXPECT_CALL(backend, buckets())
102 .WillRepeatedly(ReturnRef(m_buckets));
104 PolicyBucketId bucketId = "bucket";
105 m_buckets.insert({ bucketId, PolicyBucket(bucketId) });
107 ASSERT_TRUE(backend.hasBucket(bucketId));
108 ASSERT_FALSE(backend.hasBucket("non-existent"));
111 TEST_F(InMemeoryStorageBackendFixture, deleteNonexistentBucket) {
112 using ::testing::ReturnRef;
114 FakeInMemoryStorageBackend backend;
115 EXPECT_CALL(backend, buckets())
116 .WillRepeatedly(ReturnRef(m_buckets));
118 EXPECT_THROW(backend.deleteBucket("non-existent"), BucketNotExistsException);