Introduce AdminPolicyParser::parse() 77/32177/6
authorAleksander Zdyb <a.zdyb@samsung.com>
Tue, 9 Dec 2014 13:17:49 +0000 (14:17 +0100)
committerPawel Wieczorek <p.wieczorek2@samsung.com>
Mon, 29 Dec 2014 14:26:21 +0000 (15:26 +0100)
This function parses input stream and returns
a CynaraAdminPolicies collection.

It uses parsing functions from cynara-storage library.

Change-Id: I3615563a2a8184a5c39b88952af795abd8399748

src/cyad/AdminPolicyParser.cpp [new file with mode: 0644]
src/cyad/AdminPolicyParser.h [new file with mode: 0644]
src/cyad/CMakeLists.txt
test/CMakeLists.txt
test/cyad/helpers.h
test/cyad/policy_parser.cpp [new file with mode: 0644]

diff --git a/src/cyad/AdminPolicyParser.cpp b/src/cyad/AdminPolicyParser.cpp
new file mode 100644 (file)
index 0000000..517dc04
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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        src/cyad/AdminPolicyParser.cpp
+ * @author      Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       Parses policies from input stream
+ */
+
+#include <exceptions/BucketRecordCorruptedException.h>
+#include <storage/StorageDeserializer.h>
+#include <storage/StorageSerializer.h>
+
+#include "AdminPolicyParser.h"
+
+namespace Cynara {
+
+namespace AdminPolicyParser {
+
+CynaraAdminPolicies parse(const std::shared_ptr<std::istream> &input) {
+    CynaraAdminPolicies policies;
+
+    for (std::size_t lineNum = 1; !input->eof(); ++lineNum) {
+        std::string line;
+        std::getline(*input, line, StorageSerializer::recordSeparator());
+
+        if (line.empty())
+            break;
+
+        try {
+            std::size_t beginToken = 0;
+            auto bucketId = StorageDeserializer::parseBucketId(line, beginToken);
+            auto policyKey = BucketDeserializer::parseKey(line, beginToken);
+            auto policyType = StorageDeserializer::parsePolicyType(line, beginToken);
+            auto metadata = StorageDeserializer::parseMetadata(line, beginToken);
+
+            policies.add(bucketId, PolicyResult(policyType, metadata), policyKey);
+        } catch (const BucketRecordCorruptedException &ex) {
+            throw ex.withLineNumber(lineNum);
+        }
+    }
+
+    policies.seal();
+    return policies;
+}
+
+} /* namespace AdminPolicyParser */
+
+} /* namespace Cynara */
diff --git a/src/cyad/AdminPolicyParser.h b/src/cyad/AdminPolicyParser.h
new file mode 100644 (file)
index 0000000..f448408
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * 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        src/cyad/AdminPolicyParser.h
+ * @author      Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       Parses policies from input stream
+ */
+
+#ifndef SRC_CYAD_ADMINPOLICYPARSER_H_
+#define SRC_CYAD_ADMINPOLICYPARSER_H_
+
+#include <istream>
+#include <memory>
+
+#include <cyad/CynaraAdminPolicies.h>
+
+namespace Cynara {
+
+namespace AdminPolicyParser {
+
+CynaraAdminPolicies parse(const std::shared_ptr<std::istream> &input);
+
+} /* namespace AdminPolicyParser */
+
+} /* namespace Cynara */
+
+#endif /* SRC_CYAD_ADMINPOLICYPARSER_H_ */
index d5f220c..788e8f2 100644 (file)
@@ -20,6 +20,7 @@ SET(CYAD_PATH ${CYNARA_PATH}/cyad)
 
 SET(CYAD_SOURCES
     ${CYAD_PATH}/AdminApiWrapper.cpp
+    ${CYAD_PATH}/AdminPolicyParser.cpp
     ${CYAD_PATH}/CynaraAdminPolicies.cpp
     ${CYAD_PATH}/DispatcherIO.cpp
     ${CYAD_PATH}/main.cpp
index a60837c..6e6d12c 100644 (file)
@@ -48,6 +48,7 @@ SET(CYNARA_SOURCES_FOR_TESTS
     ${CYNARA_SRC}/common/types/PolicyDescription.cpp
     ${CYNARA_SRC}/common/types/PolicyResult.cpp
     ${CYNARA_SRC}/common/types/PolicyType.cpp
+    ${CYNARA_SRC}/cyad/AdminPolicyParser.cpp
     ${CYNARA_SRC}/cyad/CynaraAdminPolicies.cpp
     ${CYNARA_SRC}/helpers/creds-commons/CredsCommonsInner.cpp
     ${CYNARA_SRC}/helpers/creds-commons/creds-commons.cpp
@@ -73,6 +74,7 @@ SET(CYNARA_TESTS_SOURCES
     credsCommons/parser/Parser.cpp
     cyad/helpers.cpp
     cyad/policy_collection.cpp
+    cyad/policy_parser.cpp
     helpers.cpp
     storage/performance/bucket.cpp
     storage/storage/policies.cpp
index b4fd02c..eb29f9a 100644 (file)
 #ifndef TEST_CYAD_HELPERS_H_
 #define TEST_CYAD_HELPERS_H_
 
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
 struct cynara_admin_policy;
 
 bool operator==(const cynara_admin_policy &lhs, const cynara_admin_policy &rhs);
 bool operator!=(const cynara_admin_policy &lhs, const cynara_admin_policy &rhs);
 
+
+MATCHER_P(AdmPolicyListEq, policies, "") {
+    unsigned i = 0;
+    while (policies[i] != nullptr && arg[i] != nullptr) {
+        if (*policies[i] != *arg[i])
+            return false;
+        ++i;
+    }
+    return policies[i] == nullptr && arg[i] == nullptr;
+}
+
 namespace Cynara {
 
 namespace Helpers {
diff --git a/test/cyad/policy_parser.cpp b/test/cyad/policy_parser.cpp
new file mode 100644 (file)
index 0000000..ab95987
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * 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        test/cyad/commandline.cpp
+ * @author      Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       Tests for AdminPolicyParser
+ */
+
+#include <memory>
+#include <sstream>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include <common/exceptions/BucketRecordCorruptedException.h>
+#include <common/types/PolicyKey.h>
+#include <cyad/AdminPolicyParser.h>
+
+#include "helpers.h"
+
+TEST(AdminPolicyParser, parseInvalid) {
+    auto input = std::make_shared<std::stringstream>();
+
+    *input << "invalid input" << std::endl;
+
+    ASSERT_THROW(Cynara::AdminPolicyParser::parse(input), Cynara::BucketRecordCorruptedException);
+}
+
+TEST(AdminPolicyParser, parse0) {
+    auto input = std::make_shared<std::stringstream>();
+
+    Cynara::CynaraAdminPolicies expectedPolicies;
+    expectedPolicies.seal();
+
+    auto policies = Cynara::AdminPolicyParser::parse(input);
+
+    ASSERT_TRUE(policies.sealed());
+    ASSERT_THAT(policies.data(), AdmPolicyListEq(expectedPolicies.data()));
+}
+
+TEST(AdminPolicyParser, parse1) {
+    auto input = std::make_shared<std::stringstream>();
+
+    *input << "b;c;u;p;0;m" << std::endl;
+
+    Cynara::CynaraAdminPolicies expectedPolicies;
+    expectedPolicies.add("b", { 0, "m" }, { "c", "u", "p" });
+    expectedPolicies.seal();
+
+    auto policies = Cynara::AdminPolicyParser::parse(input);
+
+    ASSERT_TRUE(policies.sealed());
+    ASSERT_THAT(policies.data(), AdmPolicyListEq(expectedPolicies.data()));
+}
+
+TEST(AdminPolicyParser, parse2) {
+    auto input = std::make_shared<std::stringstream>();
+
+    *input << "b1;c1;u1;p1;0;m1" << std::endl;
+    *input << "b2;c2;u2;p2;0;m2" << std::endl;
+
+    Cynara::CynaraAdminPolicies expectedPolicies;
+    expectedPolicies.add("b1", { 0, "m1" }, { "c1", "u1", "p1" });
+    expectedPolicies.add("b2", { 0, "m2" }, { "c2", "u2", "p2" });
+    expectedPolicies.seal();
+
+    auto policies = Cynara::AdminPolicyParser::parse(input);
+
+    ASSERT_TRUE(policies.sealed());
+    ASSERT_THAT(policies.data(), AdmPolicyListEq(expectedPolicies.data()));
+}