Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • eclipse/openpass/mantle-api
  • adascri/scenario_api
  • jtschea/scenario_api
  • mstump/mantle-api
  • xiaopan/scenario_api
  • AndreasB/scenario_api
  • kcantero/scenario_api
  • dweiwg6/scenario_api
  • shankarpatali/mantle-api
  • etiennep/mantle-api
  • nutario/mantle-api
  • rbiegel/mantle-api
  • nmraghu/mantle-api
  • rparisha2/mantle-api
  • naidagoro/mantle-api
  • kim10101/mantle-api
  • mbehrischv52/mantle-api
  • khbner/mantle-api
  • lappino/mantle-api
  • anastasiiavolkova/mantle-api
  • daniilnikulin/mantle-api
  • mkellerer/mantle-api
  • ansssardesa/mantle-api
  • tonweenink/mantle-api
24 results
Show changes
Commits on Source (2)
/*******************************************************************************
* Copyright (c) 2024, Mercedes-Benz Tech Innovation GmbH
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
//-----------------------------------------------------------------------------
/** @file entity_properties.h */
//-----------------------------------------------------------------------------
#ifndef MANTLEAPI_TRAFFIC_ENTITY_H
#define MANTLEAPI_TRAFFIC_ENTITY_H
#include <memory>
#include <stdexcept>
namespace mantle_api
{
class Entity
{
public:
template <typename T>
explicit Entity(std::shared_ptr<T> entity)
: entity_(std::make_shared<Model<T>>(entity))
{
}
virtual ~Entity() = default;
virtual void Update()
{
entity_->Update();
}
class Concept
{
public:
virtual ~Concept() = default;
virtual void Update() = 0;
};
template <typename T>
class Model : public Concept
{
public:
explicit Model(std::weak_ptr<T> entity)
: entity_(entity) {}
void Update() override
{
if (auto entity = entity_.lock())
{
entity->Update();
}
else
{
throw std::runtime_error("Entity is no longer available");
}
}
private:
std::weak_ptr<T> entity_;
};
private:
std::shared_ptr<Concept> entity_;
};
} // namespace mantle_api
#endif // MANTLEAPI_TRAFFIC_ENTITY_H
/*******************************************************************************
* Copyright (c) 2024, Mercedes-Benz Tech Innovation GmbH
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
//-----------------------------------------------------------------------------
/** @file entity_repository.h */
//-----------------------------------------------------------------------------
#ifndef MANTLEAPI_TRAFFIC_ENTITY_REPOSITORY_H
#define MANTLEAPI_TRAFFIC_ENTITY_REPOSITORY_H
#include <MantleAPI/Traffic/entity.h>
#include <memory>
#include <stdexcept>
namespace mantle_api
{
class EntityRepository
{
public:
template <typename T>
explicit EntityRepository(std::shared_ptr<T> entity_repository)
: entity_repository_(std::make_shared<Model<T>>(entity_repository))
{
}
auto Create() -> Entity
{
return entity_repository_->Create();
}
void Delete()
{
return entity_repository_->Delete();
}
class Concept
{
public:
virtual ~Concept() = default;
virtual auto Create() -> Entity = 0;
virtual void Delete() = 0;
};
template <typename T>
class Model : public Concept
{
public:
explicit Model(std::weak_ptr<T> entity_repository)
: entity_repository_(entity_repository) {}
auto Create() -> Entity override
{
if (auto entity_repository = entity_repository_.lock())
{
return entity_repository->Create();
}
throw std::runtime_error("EntityRepository is no longer available");
}
void Delete() override
{
if (auto entity_repository = entity_repository_.lock())
{
return entity_repository->Delete();
}
throw std::runtime_error("EntityRepository is no longer available");
}
private:
std::weak_ptr<T> entity_repository_;
};
private:
std::shared_ptr<Concept> entity_repository_;
};
} // namespace mantle_api
#endif // MANTLEAPI_TRAFFIC_ENTITY_REPOSITORY_H
...@@ -10,3 +10,4 @@ ...@@ -10,3 +10,4 @@
add_subdirectory(Common) add_subdirectory(Common)
add_subdirectory(Execution) add_subdirectory(Execution)
add_subdirectory(Traffic)
################################################################################
# Copyright (c) 2024, Mercedes-Benz Tech Innovation GmbH
#
# This program and the accompanying materials are made available under the terms
# of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
################################################################################
add_executable(TrafficTest)
target_sources(TrafficTest PUBLIC entity_test.cc entity_repository_test.cc)
target_include_directories(TrafficTest PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/test>)
target_link_libraries(TrafficTest PUBLIC MantleAPI::MantleAPI GTest::gmock_main)
include(GoogleTest)
gtest_discover_tests(TrafficTest)
/*******************************************************************************
* Copyright (c) 2024, Mercedes-Benz Tech Innovation GmbH
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
#include "MantleAPI/Traffic/entity_repository.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory>
#include <stdexcept>
#include "MantleAPI/Traffic/entity.h"
namespace
{
class MockEntity
{
public:
MOCK_METHOD(void, Update, (), ());
};
class MockEntityRepository
{
public:
MOCK_METHOD(mantle_api::Entity, Create, (), ());
MOCK_METHOD(void, Delete, (), ());
void DelegateToFake()
{
ON_CALL(*this, Create).WillByDefault([this]()
{ return mantle_api::Entity{mock_entity}; });
ON_CALL(*this, Delete).WillByDefault([this]()
{ mock_entity.reset(); });
}
std::shared_ptr<MockEntity> mock_entity = std::make_shared<MockEntity>();
};
TEST(EntityRepositoryTest, Get)
{
// Create a mock entity repository
auto mock_entity_repository = std::make_shared<MockEntityRepository>();
ASSERT_EQ(1, mock_entity_repository.use_count());
mock_entity_repository->DelegateToFake();
// Create an entity
auto entity_repository = mantle_api::EntityRepository{mock_entity_repository};
EXPECT_CALL(*mock_entity_repository, Create()).Times(1);
auto entity = entity_repository.Create();
// Delete the entity
EXPECT_CALL(*mock_entity_repository, Delete()).Times(1);
entity_repository.Delete();
// Check that the entity can no longer be updated
ASSERT_THROW(entity.Update(), std::runtime_error);
// Check that the entity repository is no longer available
mock_entity_repository.reset();
ASSERT_THROW(entity_repository.Create(), std::runtime_error);
}
} // namespace
/*******************************************************************************
* Copyright (c) 2024, Mercedes-Benz Tech Innovation GmbH
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
#include "MantleAPI/Traffic/entity.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory>
#include <stdexcept>
namespace
{
class MockEntity
{
public:
MOCK_METHOD(void, Update, (), ());
};
TEST(EntityTest, Update)
{
// Create a mock entity and check that it is only referenced by the entity
auto mock_entity = std::make_shared<MockEntity>();
ASSERT_EQ(1, mock_entity.use_count());
// Create an entity from the mock entity and check that the reference count is not increased
auto entity = mantle_api::Entity{mock_entity};
ASSERT_EQ(1, mock_entity.use_count());
// Check that the mock entity is called when the entity is updated
EXPECT_CALL(*mock_entity, Update()).Times(1);
entity.Update();
// Check that the mock entity is called when the copy of the entity is updated
auto entity_copy = entity;
ASSERT_EQ(1, mock_entity.use_count());
EXPECT_CALL(*mock_entity, Update()).Times(1);
entity_copy.Update();
// Check that the mock entity is not called when the entity is updated after the copy is destroyed
mock_entity.reset();
ASSERT_EQ(0, mock_entity.use_count());
ASSERT_THROW(entity.Update(), std::runtime_error);
ASSERT_THROW(entity_copy.Update(), std::runtime_error);
}
} // namespace