Fawkes API  Fawkes Development Version
robot_memory_test.h
1 /***************************************************************************
2  * robot_memory_test.h - Test for the RobotMemory and their test class
3  *
4  *
5  * Created: 3:11:53 PM 2016
6  * Copyright 2016 Frederik Zwilling
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #ifndef _PLUGINS_ROBOT_MEMORY_TEST_H_
23 #define _PLUGINS_ROBOT_MEMORY_TEST_H_
24 
25 #include "plugins/robot-memory/robot_memory.h"
26 
27 #include <blackboard/blackboard.h>
28 #include <gtest/gtest.h>
29 
30 #include <bsoncxx/builder/basic/document.hpp>
31 #include <bsoncxx/document/value.hpp>
32 #include <bsoncxx/document/view.hpp>
33 #include <stdio.h>
34 
35 /** Environment for running Tests of the RobotMemory
36  * Necessary for making object such as the robot memory available in tests.
37  */
38 class RobotMemoryTestEnvironment : public ::testing::Environment
39 {
40 public:
41  /**
42  * Constructor with objects of the thread
43  * @param robot_memory Robot Memory
44  * @param blackboard Blackboard
45  */
47  {
48  this->robot_memory = robot_memory;
49  this->blackboard = blackboard;
50  }
52  {
53  }
54  /// Setup the environment
55  void
56  SetUp()
57  {
58  }
59  /// TearDown the environment
60  virtual void
61  TearDown()
62  {
63  }
64 
65 public:
66  /// Access to Robot Memory
67  static RobotMemory *robot_memory;
68  /// Access to blackboard
70 };
71 
72 /** Class for Tests of the RobotMemory
73  */
74 class RobotMemoryTest : public ::testing::Test
75 {
76 protected:
77  virtual void SetUp();
78  /// Access to Robot Memory
80  /// Access to blackboard
82 
83 protected:
84  ::testing::AssertionResult contains_pairs(const bsoncxx::document::view_or_value &obj,
85  const bsoncxx::document::view_or_value &exp);
86  ::testing::AssertionResult contains_pairs(mongocxx::cursor & cursor,
87  const bsoncxx::document::view_or_value &exp);
88 };
89 
90 /**
91  * Class to register callbacks independent of how many tests are using them at the moment
92  */
94 {
95 public:
97  {
98  callback_counter = 0;
99  };
100  ~RobotMemoryCallback(){};
101  /// Counter for how often the callback was called
102  int callback_counter;
103  /**
104  * Test callback function
105  * @param update Trigger update
106  */
107  void
108  callback_test(const bsoncxx::document::view &update)
109  {
111  }
112 };
113 
114 /**
115  * Class providing a computable function
116  */
117 class TestComputable
118 {
119 public:
120  TestComputable(){};
121  ~TestComputable(){};
122  //Different functions for computables:
123  /**
124  * Computable function for static document
125  * @param query Input query
126  * @param collection Corresponding collection
127  * @return Computed docs
128  */
129  std::list<bsoncxx::document::value>
130  compute(const bsoncxx::document::view &query, const std::string &collection)
131  {
132  std::list<bsoncxx::document::value> res;
133  using namespace bsoncxx::builder;
134  basic::document doc;
135  doc.append(basic::kvp("computed", true));
136  doc.append(basic::kvp("result", "this is computed"));
137  res.push_back(doc.extract());
138  return res;
139  }
140  /**
141  * Computable function for addition
142  * @param query Input query
143  * @param collection Corresponding collection
144  * @return Computed docs
145  */
146  std::list<bsoncxx::document::value>
147  compute_sum(const bsoncxx::document::view &query, const std::string &collection)
148  {
149  std::list<bsoncxx::document::value> res;
150  int x = query["x"].get_int64();
151  int y = query["y"].get_int64();
152  int sum = x + y;
153  using namespace bsoncxx::builder;
154  basic::document b;
155  b.append(basic::kvp("compute", "sum"));
156  b.append(basic::kvp("x", x));
157  b.append(basic::kvp("y", y));
158  b.append(basic::kvp("sum", sum));
159  res.push_back(b.extract());
160  return res;
161  }
162  /**
163  * Computable function for multiple static document
164  * @param query Input query
165  * @param collection Corresponding collection
166  * @return Computed docs
167  */
168  std::list<bsoncxx::document::value>
169  compute_multiple(const bsoncxx::document::view &query, const std::string &collection)
170  {
171  std::list<bsoncxx::document::value> res;
172  using namespace bsoncxx::builder;
173  for (auto i : {1, 2, 3}) {
174  basic::document doc;
175  doc.append(basic::kvp("compute", "multiple"));
176  doc.append(basic::kvp("count", i));
177  res.push_back(doc.extract());
178  }
179  return res;
180  }
181 };
182 
183 #endif
RobotMemoryTest::contains_pairs
::testing::AssertionResult contains_pairs(const bsoncxx::document::view_or_value &obj, const bsoncxx::document::view_or_value &exp)
Function for testing if a document contains all key-value pairs of another document.
Definition: robot_memory_test.cpp:257
RobotMemoryTest::SetUp
virtual void SetUp()
Setup for each test.
Definition: robot_memory_test.cpp:43
RobotMemoryTest
Class for Tests of the RobotMemory.
Definition: robot_memory_test.h:73
RobotMemoryCallback
Class to register callbacks independent of how many tests are using them at the moment.
Definition: robot_memory_test.h:92
RobotMemory
Definition: robot_memory.h:45
RobotMemoryTestEnvironment::robot_memory
static RobotMemory * robot_memory
Access to Robot Memory.
Definition: robot_memory_test.h:71
fawkes::BlackBoard
Definition: blackboard.h:48
TestComputable
Class providing a computable function.
Definition: robot_memory_test.h:116
RobotMemoryTestEnvironment
Environment for running Tests of the RobotMemory Necessary for making object such as the robot memory...
Definition: robot_memory_test.h:37
RobotMemoryTest::robot_memory
RobotMemory * robot_memory
Access to Robot Memory.
Definition: robot_memory_test.h:78
TestComputable::compute
std::list< bsoncxx::document::value > compute(const bsoncxx::document::view &query, const std::string &collection)
Computable function for static document.
Definition: robot_memory_test.h:129
TestComputable::compute_multiple
std::list< bsoncxx::document::value > compute_multiple(const bsoncxx::document::view &query, const std::string &collection)
Computable function for multiple static document.
Definition: robot_memory_test.h:168
RobotMemoryCallback::callback_counter
int callback_counter
Counter for how often the callback was called.
Definition: robot_memory_test.h:99
RobotMemoryCallback::callback_test
void callback_test(const bsoncxx::document::view &update)
Test callback function.
Definition: robot_memory_test.h:107
TestComputable::compute_sum
std::list< bsoncxx::document::value > compute_sum(const bsoncxx::document::view &query, const std::string &collection)
Computable function for addition.
Definition: robot_memory_test.h:146
RobotMemoryTestEnvironment::SetUp
void SetUp()
Setup the environment.
Definition: robot_memory_test.h:60
RobotMemoryTest::blackboard
fawkes::BlackBoard * blackboard
Access to blackboard.
Definition: robot_memory_test.h:80
RobotMemoryTestEnvironment::blackboard
static fawkes::BlackBoard * blackboard
Access to blackboard.
Definition: robot_memory_test.h:73
RobotMemoryTestEnvironment::RobotMemoryTestEnvironment
RobotMemoryTestEnvironment(RobotMemory *robot_memory, fawkes::BlackBoard *blackboard)
Constructor with objects of the thread.
Definition: robot_memory_test.h:50
RobotMemoryTestEnvironment::TearDown
virtual void TearDown()
TearDown the environment.
Definition: robot_memory_test.h:65