Skip to content
Snippets Groups Projects
Verified Commit d287c569 authored by Timm Fitschen's avatar Timm Fitschen
Browse files

WIP: retrieve simple entity

parent d9bb5c39
No related branches found
No related tags found
No related merge requests found
Pipeline #10122 passed
Pipeline: caosdb-cppinttest

#10128

    ......@@ -159,7 +159,8 @@ if(_LINTING)
    message(WARNING "include-what-you-use: Not found")
    else()
    message(STATUS "include-what-you-use: ${iwyu}")
    set(_CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${iwyu} "-Xiwyu" "--cxx17ns")
    set(_CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${iwyu}
    "-Xiwyu" "--cxx17ns" "-Xiwyu" "--no_fwd_decls")
    set_target_properties(caosdb PROPERTIES
    CXX_INCLUDE_WHAT_YOU_USE "${_CMAKE_CXX_INCLUDE_WHAT_YOU_USE}"
    ......
    ......@@ -28,16 +28,15 @@
    * @brief Configuration and setup of the client authentication.
    */
    #include <grpcpp/security/credentials.h> // for MetadataCredentialsPlugin
    #include <map> // for multimap
    #include <memory> // for shared_ptr
    #include <string> // for string
    #include "caosdb/utils.h" // for base64_encode
    #include "grpcpp/impl/codegen/status.h" // for Status
    #include "grpcpp/impl/codegen/string_ref.h" // for string_ref
    namespace grpc {
    class AuthContext;
    }
    #include <grpcpp/security/credentials.h> // for CallCredentials
    #include <map> // for multimap
    #include <memory> // for shared_ptr
    #include <string> // for string
    #include "caosdb/utils.h" // for base64_encode
    #include "grpcpp/impl/codegen/interceptor.h" // for Status
    #include "grpcpp/impl/codegen/security/auth_context.h" // for AuthContext
    #include "grpcpp/impl/codegen/status.h" // for Status
    #include "grpcpp/impl/codegen/string_ref.h" // for string_ref
    namespace caosdb {
    namespace authentication {
    ......
    ......@@ -27,23 +27,20 @@
    * @date 2021-05-18
    * @brief Configuration and setup of the connection.
    */
    #include <iosfwd> // for ostream
    #include <memory> // for shared_ptr, unique_ptr
    #include <string> // for string
    #include "caosdb/transaction.h"
    #include "caosdb/info/v1alpha1/main.grpc.pb.h" // for GeneralInfoService
    #include "caosdb/entity/v1alpha1/main.grpc.pb.h" // for EntityTransactionService
    namespace caosdb {
    namespace authentication {
    #include <iosfwd> // for ostream
    #include <memory> // for shared_ptr, unique_ptr
    #include <string> // for string
    #include "caosdb/entity/v1alpha1/main.grpc.pb.h" // for EntityTransactionSe...
    #include "caosdb/info/v1alpha1/main.grpc.pb.h" // for GeneralInfoService:...
    #include "caosdb/transaction.h" // for Transaction
    #include "grpcpp/impl/codegen/client_callback.h" // for Channel
    namespace caosdb::authentication {
    class Authenticator;
    } // namespace authentication
    namespace info {
    } // namespace caosdb::authentication
    namespace caosdb::info {
    class VersionInfo;
    } // namespace info
    } // namespace caosdb
    } // namespace caosdb::info
    namespace grpc {
    class Channel;
    class ChannelCredentials;
    } // namespace grpc
    ......
    ......@@ -28,10 +28,9 @@
    #ifndef CAOSDB_ENTITY_H
    #define CAOSDB_ENTITY_H
    #include <memory> // for shared_ptr
    #include <string> // for string
    #include <google/protobuf/repeated_field.h>
    #include "caosdb/entity/v1alpha1/main.pb.h" // for Entity
    #include <memory> // for unique_ptr
    #include <string> // for string
    #include "caosdb/entity/v1alpha1/main.pb.h" // for Entity, RepeatedField
    namespace caosdb::entity {
    ......@@ -122,17 +121,29 @@ public:
    parents(
    (::google::protobuf::RepeatedField<caosdb::entity::v1alpha1::Parent> *)
    wrapped->mutable_parents()){};
    [[nodiscard]] auto GetId() const -> const std::string &;
    [[nodiscard]] auto GetVersion() const -> const std::string &;
    [[nodiscard]] auto GetRole() const -> const std::string &;
    [[nodiscard]] auto GetName() const -> const std::string &;
    [[nodiscard]] inline auto GetId() const -> const std::string & {
    return wrapped->id();
    };
    [[nodiscard]] inline auto GetVersion() const -> const std::string & {
    return wrapped->version().id();
    };
    [[nodiscard]] inline auto GetRole() const -> const std::string & {
    return wrapped->role();
    };
    [[nodiscard]] inline auto GetName() const -> const std::string & {
    return wrapped->name();
    };
    [[nodiscard]] inline auto GetDescription() const -> const std::string & {
    return wrapped->description();
    }
    [[nodiscard]] auto GetDatatype() const -> const std::string &;
    [[nodiscard]] auto GetUnit() const -> const std::string &;
    };
    [[nodiscard]] auto GetDatatype() const -> const std::string & {
    return wrapped->datatype();
    };
    [[nodiscard]] auto GetUnit() const -> const std::string & {
    return wrapped->unit();
    };
    [[nodiscard]] auto GetParents() const -> const Parents &;
    [[nodiscard]] auto GetProperties() const -> const Properties &;
    ......
    ......@@ -36,19 +36,43 @@ namespace caosdb::info {
    using ProtoVersionInfo = caosdb::info::v1alpha1::VersionInfo;
    /**
    * @brief Wrapper class for the VersionInfo protobuf.
    * A read-only version object which represents the version of the server.
    *
    * The version info follows semantic versioning (SemVer 2.0). Wrapper class for
    * the VersionInfo protobuf.
    *
    * @brief A read-only version object which represents the version of the server.
    */
    class VersionInfo {
    private:
    ProtoVersionInfo *info;
    public:
    explicit VersionInfo(ProtoVersionInfo *info);
    [[nodiscard]] auto GetMajor() const -> uint32_t;
    [[nodiscard]] auto GetMinor() const -> uint32_t;
    [[nodiscard]] auto GetPatch() const -> uint32_t;
    [[nodiscard]] auto GetPreRelease() const -> const std::string &;
    [[nodiscard]] auto GetBuild() const -> const std::string &;
    /**
    * Wrapp a Protobuf VersionInfo object.
    *
    * Don't instantiate this version info class. The constructor is only public
    * for simpler testing. Create a CaosDBConnection and use
    * CaosDBConnection::GetVersionInfo() instead to get the version of the
    * server behind the given connection.
    */
    explicit inline VersionInfo(ProtoVersionInfo *info) : info(info){};
    [[nodiscard]] inline auto GetMajor() const -> uint32_t {
    return this->info->major();
    }
    [[nodiscard]] inline auto GetMinor() const -> uint32_t {
    return this->info->minor();
    }
    [[nodiscard]] inline auto GetPatch() const -> uint32_t {
    return this->info->patch();
    }
    [[nodiscard]] inline auto GetPreRelease() const -> const std::string & {
    return this->info->pre_release();
    }
    [[nodiscard]] inline auto GetBuild() const -> const std::string & {
    return this->info->build();
    }
    private:
    /// This object is the owner of the Protobuf VersionInfo message.
    std::unique_ptr<ProtoVersionInfo> info;
    };
    } // namespace caosdb::info
    ......
    ......@@ -26,10 +26,11 @@
    #ifndef CAOSDB_TRANSACTION_H
    #define CAOSDB_TRANSACTION_H
    #include <memory>
    #include "caosdb/entity.h"
    #include "caosdb/entity/v1alpha1/main.pb.h"
    #include "caosdb/entity/v1alpha1/main.grpc.pb.h"
    #include <memory> // for shared_ptr, unique_ptr
    #include <string> // for string
    #include "caosdb/entity.h" // for Entity
    #include "caosdb/entity/v1alpha1/main.grpc.pb.h" // for EntityTransactionSe...
    #include "caosdb/entity/v1alpha1/main.pb.h" // for Entity, RetrieveReq...
    namespace caosdb::transaction {
    using caosdb::entity::Entity;
    ......@@ -37,16 +38,20 @@ using ProtoEntity = caosdb::entity::v1alpha1::Entity;
    using caosdb::entity::v1alpha1::EntityTransactionService;
    using caosdb::entity::v1alpha1::RetrieveRequest;
    class ResultSet {};
    class ResultSet {
    public:
    virtual ~ResultSet(){};
    };
    class UniqueResult : public ResultSet {
    private:
    std::unique_ptr<Entity> entity;
    public:
    ~UniqueResult(){};
    explicit inline UniqueResult(ProtoEntity *protoEntity)
    : entity(new Entity(protoEntity)){};
    [[nodiscard]] auto GetEntity() const -> const Entity &;
    private:
    std::unique_ptr<Entity> entity;
    };
    enum TransactionState { INIT = 10, EXECUTING = 20, SUCCESS = 30, ERROR = 40 };
    ......@@ -56,7 +61,7 @@ enum TransactionState { INIT = 10, EXECUTING = 20, SUCCESS = 30, ERROR = 40 };
    */
    class Transaction {
    private:
    std::shared_ptr<ResultSet> result_set;
    std::unique_ptr<ResultSet> result_set;
    TransactionState state = TransactionState::INIT;
    std::shared_ptr<EntityTransactionService::Stub> service_stub;
    RetrieveRequest request; // TODO(tf)
    ......@@ -65,8 +70,10 @@ public:
    Transaction(std::shared_ptr<EntityTransactionService::Stub> service_stub);
    auto RetrieveById(const std::string &id) -> void;
    auto Execute() -> void;
    [[nodiscard]] auto GetResultSet() const -> std::shared_ptr<ResultSet>;
    auto WaitForIt() const -> void;
    [[nodiscard]] inline auto GetResultSet() const -> const ResultSet & {
    const ResultSet *result_set = this->result_set.get();
    return *result_set;
    }
    };
    } // namespace caosdb::transaction
    ......
    ......@@ -23,8 +23,6 @@
    set(libcaosdb_SRC
    ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/authentication.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/connection.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/entity.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/info.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/caosdb/transaction.cpp
    )
    ......
    /*
    * This file is a part of the CaosDB Project.
    *
    * Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
    * Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
    *
    * This program is free software: you can redistribute it and/or modify
    * it under the terms of the GNU Affero General Public License as
    * published by the Free Software Foundation, either version 3 of the
    * License, or (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU Affero General Public License for more details.
    *
    * You should have received a copy of the GNU Affero General Public License
    * along with this program. If not, see <https://www.gnu.org/licenses/>.
    *
    */
    #include "caosdb/entity.h"
    #include <utility>
    namespace caosdb::entity {} // namespace caosdb::entity
    /*
    * This file is a part of the CaosDB Project.
    *
    * Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
    * Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
    *
    * This program is free software: you can redistribute it and/or modify
    * it under the terms of the GNU Affero General Public License as
    * published by the Free Software Foundation, either version 3 of the
    * License, or (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU Affero General Public License for more details.
    *
    * You should have received a copy of the GNU Affero General Public License
    * along with this program. If not, see <https://www.gnu.org/licenses/>.
    *
    */
    #include "caosdb/info.h"
    #include "caosdb/info/v1alpha1/main.pb.h" // for GetVersionInfoResponse
    namespace caosdb::info {
    using ProtoVersionInfo = caosdb::info::v1alpha1::VersionInfo;
    VersionInfo::VersionInfo(ProtoVersionInfo *info) { this->info = info; }
    [[nodiscard]] auto VersionInfo::GetMajor() const -> uint32_t {
    return this->info->major();
    }
    [[nodiscard]] auto VersionInfo::GetMinor() const -> uint32_t {
    return this->info->minor();
    }
    [[nodiscard]] auto VersionInfo::GetPatch() const -> uint32_t {
    return this->info->patch();
    }
    [[nodiscard]] auto VersionInfo::GetPreRelease() const -> const std::string & {
    return this->info->pre_release();
    }
    [[nodiscard]] auto VersionInfo::GetBuild() const -> const std::string & {
    return this->info->build();
    }
    } // namespace caosdb::info
    ......@@ -51,6 +51,7 @@ Transaction::Transaction(
    auto Transaction::RetrieveById(const std::string &id) -> void {
    RetrieveRequest request;
    // this copies the id, so we're safe.
    request.mutable_by_id()->set_id(id);
    this->request = request;
    ......@@ -78,18 +79,8 @@ auto Transaction::Execute() -> void {
    }
    auto *entity = response.release_entity();
    auto result_set = std::make_shared<UniqueResult>(entity);
    this->result_set = result_set;
    }
    auto Transaction::WaitForIt() const -> void {
    // TODO(tf) throw error if still in INIT state
    // TODO(tf)
    }
    [[nodiscard]] auto Transaction::GetResultSet() const
    -> std::shared_ptr<ResultSet> {
    return this->result_set;
    auto result_set = std::make_unique<UniqueResult>(entity);
    this->result_set = std::move(result_set);
    }
    } // namespace caosdb::transaction
    ......@@ -71,12 +71,13 @@ auto main() -> int {
    auto transaction(connection.CreateTransaction());
    transaction->RetrieveById("20");
    transaction->Execute();
    auto result_set(std::static_pointer_cast<caosdb::transaction::UniqueResult>(
    transaction->GetResultSet()));
    const auto &result_set =
    dynamic_cast<const caosdb::transaction::UniqueResult &>(
    transaction->GetResultSet());
    // print description
    std::cout << "Entity Description: "
    << result_set->GetEntity().GetDescription() << std::endl;
    std::cout << "Entity Description: " << result_set.GetEntity().GetDescription()
    << std::endl;
    return 0;
    }
    ......@@ -25,9 +25,6 @@
    #include <memory> // for allocator, operator!=, shared_ptr
    #include "caosdb/connection.h" // for PemCACertProvider, InsecureCaosDB...
    #include "gtest/gtest_pred_impl.h" // for Test, AssertionResult, EXPECT_EQ
    namespace grpc {
    class ChannelCredentials;
    } // namespace grpc
    namespace caosdb::connection {
    ......
    ......@@ -33,15 +33,17 @@ using ProtoVersionInfo = caosdb::info::v1alpha1::VersionInfo;
    TEST(test_info, create_info_from_proto_info) {
    auto *origial = new ProtoVersionInfo();
    origial->set_major(12);
    origial->set_patch(56);
    origial->set_pre_release("SNAPSHOT");
    origial->set_build("1234asdf");
    VersionInfo wrapper(origial);
    EXPECT_EQ(12, wrapper.GetMajor());
    EXPECT_EQ(0, wrapper.GetMinor()); // default value.
    EXPECT_EQ(56, wrapper.GetPatch());
    EXPECT_EQ("SNAPSHOT", wrapper.GetPreRelease());
    // default value.
    EXPECT_EQ(0, wrapper.GetMinor());
    EXPECT_EQ("1234asdf", wrapper.GetBuild());
    }
    } // namespace caosdb::info
    ......@@ -20,20 +20,23 @@
    *
    */
    #include <memory> // for allocator, make_shared, unique_ptr
    #include "caosdb/connection.h" // for InsecureCaosDBConnectionConfig
    #include "caosdb/entity.h" // for EntityID
    #include "caosdb/exceptions.h" // for ConnectionError
    #include "caosdb/transaction.h" // for Transaction, EntityID
    #include "gtest/gtest-message.h" // for Message
    #include "gtest/gtest-test-part.h" // for TestPartResult
    #include "gtest/gtest.h" // for Test, SuiteApiResolver, TestInfo ...
    #include "caosdb_test_utility.h" // for EXPECT_THROW_MESSAGE
    #include <memory> // for allocator, make_shared
    #include "caosdb/connection.h" // for InsecureCaosDBConnection...
    #include "caosdb/entity.h" // for Entity
    #include "caosdb/entity/v1alpha1/main.pb.h" // for Entity
    #include "caosdb/exceptions.h" // for ConnectionError
    #include "caosdb/transaction.h" // for Transaction, UniqueResult
    #include "caosdb_test_utility.h" // for EXPECT_THROW_MESSAGE
    #include "gtest/gtest-message.h" // for Message
    #include "gtest/gtest-test-part.h" // for SuiteApiResolver, TestPa...
    #include "gtest/gtest_pred_impl.h" // for Test, TestInfo, TEST
    namespace caosdb::transaction {
    using caosdb::connection::CaosDBConnection;
    using caosdb::connection::InsecureCaosDBConnectionConfig;
    using caosdb::exceptions::ConnectionError;
    using caosdb::transaction::UniqueResult;
    using ProtoEntity = caosdb::entity::v1alpha1::Entity;
    TEST(test_transaction, create_transaction) {
    const auto *pHost = "localhost";
    ......@@ -41,9 +44,21 @@ TEST(test_transaction, create_transaction) {
    CaosDBConnection connection(config);
    auto transaction = connection.CreateTransaction();
    transaction->Retrieve(EntityID("someid"));
    transaction->RetrieveById("100");
    EXPECT_THROW_MESSAGE(transaction->Execute(), ConnectionError,
    "failed to connect to all addresses");
    }
    TEST(test_transaction, unique_result) {
    auto *entity = new ProtoEntity();
    entity->set_id("test");
    UniqueResult result(entity);
    EXPECT_EQ("test", result.GetEntity().GetId());
    // DON'T DELETE! The caosdb::entity::Entity takes care of that
    // Try it yourself:
    // delete entity;
    }
    } // namespace caosdb::transaction
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment