Implement listPolicies() in Storage and InMemoryStorageBackend.
UnitTests for Storage::listPolicies() were added.
Change-Id: I113c3c0f9b5c1d1d5cbed44e3d23d5d7e489a227
}
}
+PolicyBucket::Policies PolicyBucket::listPolicies(const PolicyKey &filter) const {
+ PolicyBucket::Policies policies;
+ for (auto iter = m_policyCollection.begin(); iter != m_policyCollection.end(); ++iter) {
+ auto &policyPtr = iter->second;
+ if (policyPtr->key().matchFilter(filter))
+ policies.push_back(*policyPtr);
+ }
+ return policies;
+}
+
PolicyMap PolicyBucket::makePolicyMap(const PolicyCollection &policies) {
PolicyMap result;
for (const auto &policy : policies) {
#include <algorithm>
#include <memory>
#include <string>
+#include <vector>
#include <exceptions/NotImplementedException.h>
#include <types/pointers.h>
typedef PolicyCollection::value_type value_type;
typedef const_policy_iterator const_iterator;
+ typedef std::vector<Policy> Policies;
// TODO: Review usefulness of ctors
//delete default constructor in order to prevent creation of buckets with no id
PolicyBucket filtered(const PolicyKey &key) const;
void insertPolicy(PolicyPtr policy);
void deletePolicy(const PolicyKey &key);
+ Policies listPolicies(const PolicyKey &filter) const;
// TODO: Try to change interface, so this method is not needed
void deletePolicy(std::function<bool(PolicyPtr)> predicate);
}
}
+PolicyBucket::Policies InMemoryStorageBackend::listPolicies(const PolicyBucketId &bucketId,
+ const PolicyKey &filter) const {
+ try {
+ auto &bucket = buckets().at(bucketId);
+ return bucket.listPolicies(filter);
+ } catch (const std::out_of_range &) {
+ throw BucketNotExistsException(bucketId);
+ }
+}
+
void InMemoryStorageBackend::openFileStream(std::shared_ptr<std::ifstream> stream,
const std::string &filename) {
// TODO: Consider adding exceptions to streams and handling them:
virtual bool hasBucket(const PolicyBucketId &bucketId);
virtual void deletePolicy(const PolicyBucketId &bucketId, const PolicyKey &key);
virtual void deleteLinking(const PolicyBucketId &bucketId);
+ virtual PolicyBucket::Policies listPolicies(const PolicyBucketId &bucketId,
+ const PolicyKey &filter) const;
protected:
InMemoryStorageBackend() {}
virtual Buckets &buckets(void) {
return m_buckets;
}
+ virtual const Buckets &buckets(void) const {
+ return m_buckets;
+ }
};
} /* namespace Cynara */
}
}
+PolicyBucket::Policies Storage::listPolicies(const PolicyBucketId &bucketId,
+ const PolicyKey &filter) const {
+ return m_backend.listPolicies(bucketId, filter);
+}
+
void Storage::load(void) {
m_backend.load();
}
void addOrUpdateBucket(const PolicyBucketId &bucketId, const PolicyResult &defaultBucketPolicy);
void deleteBucket(const PolicyBucketId &bucketId);
+ PolicyBucket::Policies listPolicies(const PolicyBucketId &bucketId,
+ const PolicyKey &filter) const;
+
void load(void);
void save(void);
virtual void deletePolicy(const PolicyBucketId &bucketId, const PolicyKey &key) = 0;
virtual void deleteLinking(const PolicyBucketId &bucket) = 0;
-
+ virtual PolicyBucket::Policies listPolicies(const PolicyBucketId &bucketId,
+ const PolicyKey &filter) const = 0;
virtual void load(void) = 0;
virtual void save(void) = 0;
};
MOCK_METHOD2(deletePolicy, void(const PolicyBucketId &bucketId, const PolicyKey &key));
MOCK_METHOD1(deleteLinking, void(const PolicyBucketId &bucket));
MOCK_METHOD2(insertPolicy, void(const PolicyBucketId &bucketId, PolicyPtr policy));
+ MOCK_CONST_METHOD2(listPolicies, PolicyBucket::Policies(const PolicyBucketId &bucketId,
+ const PolicyKey &filter));
};
-
-
#endif /* FAKESTORAGEBACKEND_H_ */
ASSERT_THROW(storage.insertPolicies(policiesToInsert), BucketNotExistsException);
}
+
+TEST(storage, listPolicies) {
+ using ::testing::Return;
+ using PredefinedPolicyType::DENY;
+
+ FakeStorageBackend backend;
+ Storage storage(backend);
+
+ PolicyBucketId testBucket("test-bucket");
+ PolicyResult defaultPolicy(PredefinedPolicyType::DENY);
+
+ EXPECT_CALL(backend, hasBucket(testBucket)).WillOnce(Return(false));
+ EXPECT_CALL(backend, createBucket(testBucket, defaultPolicy)).WillOnce(Return());
+ storage.addOrUpdateBucket(testBucket, defaultPolicy);
+
+ PolicyKey filter = Helpers::generatePolicyKey("1");
+ EXPECT_CALL(backend, listPolicies(testBucket, filter))
+ .WillOnce(Return(PolicyBucket::Policies()));
+
+ storage.listPolicies(testBucket, filter);
+}
+
+TEST(storage, listPoliciesFromNonexistentBucket) {
+ using ::testing::Return;
+ using PredefinedPolicyType::DENY;
+
+ FakeStorageBackend backend;
+ Storage storage(backend);
+
+ PolicyBucketId testBucket("test-bucket");
+ PolicyResult defaultPolicy(PredefinedPolicyType::DENY);
+ PolicyKey filter = Helpers::generatePolicyKey("1");
+ EXPECT_CALL(backend, listPolicies(testBucket, filter))
+ .WillOnce(Return(PolicyBucket::Policies()));
+
+ storage.listPolicies(testBucket, filter);
+}