Leosac  0.8.0
Open Source Access Control
TestHelper.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include "FakeGPIO.hpp"
23 #include "core/CoreUtils.hpp"
24 #include "core/MessageBus.hpp"
26 #include "gtest/gtest.h"
27 #include <boost/property_tree/ptree.hpp>
28 #include <zmqpp/zmqpp.hpp>
29 
30 namespace Leosac
31 {
32 namespace Test
33 {
34 namespace Helper
35 {
41 template <typename ModuleType>
42 bool test_run_module(zmqpp::context *ctx, zmqpp::socket *pipe,
43  const boost::property_tree::ptree &cfg)
44 {
45  {
46  CoreUtilsPtr u = std::make_shared<CoreUtils>(
47  nullptr, nullptr, std::make_shared<ConfigChecker>(), false);
48  ModuleType module(*ctx, pipe, cfg, u);
49 
50  pipe->send(zmqpp::signal::ok);
51  module.run();
52  }
53  return true;
54 }
55 
59 bool bus_read_extract(zmqpp::message *)
60 {
61  return true;
62 }
63 
64 template <typename... Content>
65 bool bus_read_extract(zmqpp::message *m, const char *first_arg, Content... content);
66 
72 template <typename T, typename... Content>
73 typename std::enable_if<!std::is_same<const char *, T>::value, bool>::type
74 bus_read_extract(zmqpp::message *m, T first_arg, Content... content)
75 {
76  T value;
77  *m >> value;
78  if (value != first_arg)
79  return false;
80 
81  return bus_read_extract(m, content...);
82 }
83 
87 template <typename... Content>
88 bool bus_read_extract(zmqpp::message *m, const char *first_arg, Content... content)
89 {
90  std::string value;
91  *m >> value;
92 
93  if (strcmp(value.c_str(), first_arg) != 0)
94  return false;
95  return bus_read_extract(m, content...);
96 }
97 
102 template <typename... Content>
103 bool bus_read(zmqpp::socket &sub, Content... content)
104 {
105  zmqpp::message msg;
106 
107  if (!sub.receive(msg))
108  return false;
109  return bus_read_extract(&msg, content...);
110 }
111 
116 class TestHelper : public ::testing::Test
117 {
118  private:
124  virtual bool run_module(zmqpp::socket *pipe) = 0;
125 
126  public:
128  : ctx_()
129  , bus_(ctx_)
130  , bus_sub_(ctx_, zmqpp::socket_type::sub)
131  , bus_push_(ctx_, zmqpp::socket_type::push)
132  , module_actor_(nullptr)
133  {
134  bus_sub_.connect("inproc://zmq-bus-pub");
135  bus_push_.connect("inproc://zmq-bus-pull");
136  }
137 
138  virtual ~TestHelper()
139  {
140  }
141 
146  virtual void SetUp() override final
147  {
148  module_actor_ = std::unique_ptr<zmqpp::actor>(new zmqpp::actor(
149  std::bind(&TestHelper::run_module, this, std::placeholders::_1)));
150  }
151 
155  zmqpp::context ctx_;
156 
161 
165  zmqpp::socket bus_sub_;
166 
170  zmqpp::socket bus_push_;
171 
176  std::unique_ptr<zmqpp::actor> module_actor_;
177 };
178 }
179 }
180 }
Leosac::Test::Helper::TestHelper::module_actor_
std::unique_ptr< zmqpp::actor > module_actor_
An actor, to run the module code the same way it would be run by the core.
Definition: TestHelper.hpp:176
zmqpp
Definition: CoreUtils.hpp:27
FakeGPIO.hpp
Leosac::Test::Helper::TestHelper::bus_sub_
zmqpp::socket bus_sub_
A SUB socket connected to the previous bus.
Definition: TestHelper.hpp:165
Leosac::Test::Helper::TestHelper::TestHelper
TestHelper()
Definition: TestHelper.hpp:127
MessageBus.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Test::Helper::TestHelper::bus_push_
zmqpp::socket bus_push_
A PUSH socket to write on the bus.
Definition: TestHelper.hpp:170
Leosac::Test::Helper::TestHelper::ctx_
zmqpp::context ctx_
A context for the test case.
Definition: TestHelper.hpp:155
Leosac::Test::Helper::test_run_module
bool test_run_module(zmqpp::context *ctx, zmqpp::socket *pipe, const boost::property_tree::ptree &cfg)
Helper function that create an object of type ModuleType (using conventional parameter) and run it.
Definition: TestHelper.hpp:42
Leosac::Test::Helper::TestHelper::run_module
virtual bool run_module(zmqpp::socket *pipe)=0
Called in the module's actor's thread.
Leosac::Test::Helper::bus_read_extract
bool bus_read_extract(zmqpp::message *)
Part of the bus_read() stuff.
Definition: TestHelper.hpp:59
MessageBus
Implements a message bus (running in its own thread) that use 2 sockets.
Definition: MessageBus.hpp:29
Leosac::Test::Helper::bus_read
bool bus_read(zmqpp::socket &sub, Content... content)
Make a blocking read on the bus, return true if content match the message.
Definition: TestHelper.hpp:103
CoreUtils.hpp
Leosac::Test::Helper::TestHelper::~TestHelper
virtual ~TestHelper()
Definition: TestHelper.hpp:138
Leosac::Test::Helper::TestHelper
Base class for test fixtures, it defines a ZMQ context, a BUS and a sub socket connect to the bus.
Definition: TestHelper.hpp:116
ConfigChecker.hpp
Leosac::CoreUtilsPtr
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35
Leosac::Test::Helper::TestHelper::bus_
MessageBus bus_
A BUS identical to the one spawned by core.
Definition: TestHelper.hpp:160
Leosac::Test::Helper::TestHelper::SetUp
virtual void SetUp() override final
We need this 2-step initialization to prevent calling virtual method (run_module) in constructor.
Definition: TestHelper.hpp:146