hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SphinxTaskReducersManagerTest.cpp
Go to the documentation of this file.
1 /*
2  * SphinxTaskReducersManager.cpp
3  *
4  * Created on: May 16, 2013
5  * Author: igor
6  */
7 #include <gmock/gmock.h>
8 #include "Exceptions.hpp"
9 #include "SphinxExceptions.hpp"
10 #include "Mocks.hpp"
15 
16 using namespace HCE::sphinx::reduce_task;
17 using namespace HCE::reduce::core;
18 using namespace HCE::reduce;
19 
20 using ::testing::_;
21 using ::testing::Return;
22 using ::testing::ReturnRef;
23 using ::testing::ReturnPointee;
24 using ::testing::Throw;
25 using ::testing::Eq;
26 
27 
28 class SphinxTaskReducersManagerTest: public ::testing::Test{
29  protected:
30  virtual void SetUp(){
31  sphinxTaskReducersStorage.assign(new SphinxTaskReducersStorageMock());
32  sphinxTaskReducerFactory.assign(new SphinxTaskReducerFactoryMock());
33  sphinxResultJSONConvertor.assign(new SphinxResultJSONConvertorMock());
34  sphinxTaskReducer.assign(new SphinxTaskReducerMock());
35  }
36 
37  virtual void TearDown(){
38  sphinxTaskReducersStorage.assign(NULL);
39  sphinxTaskReducerFactory.assign(NULL);
40  sphinxResultJSONConvertor.assign(NULL);
41  sphinxTaskReducer.assign(NULL);
42  }
43 
44  SharedPtr<SphinxTaskReducersStorageMock> sphinxTaskReducersStorage;
45  SharedPtr<SphinxTaskReducerFactoryMock> sphinxTaskReducerFactory;
46  SharedPtr<SphinxResultJSONConvertorMock> sphinxResultJSONConvertor;
47  SharedPtr<SphinxTaskReducerMock> sphinxTaskReducer;
48 };
49 
50 
51 TEST_F(SphinxTaskReducersManagerTest, canFindSphinxTaskReducerByTaskId)
52 {
53  unsigned long long existTaskId = 2;
54 
55  SphinxTaskReducersManager sphinxTaskReducersManager(sphinxTaskReducersStorage,
56  sphinxTaskReducerFactory, sphinxResultJSONConvertor);
57 
58  EXPECT_CALL(*sphinxTaskReducersStorage.get(), findByKey(_))
59  .WillOnce(ReturnPointee(&sphinxTaskReducer));
60  ASSERT_THAT(sphinxTaskReducersManager.isReducerExist(existTaskId), true);
61 }
62 
63 
64 
65 TEST_F(SphinxTaskReducersManagerTest, canntFindSphinxTaskReducerByTaskId)
66 {
67  unsigned long long noExistTaskId = 7;
68  NotFoundByKeyException notFoundSphinxTaskReducerException("No item in the storage");
69 
70  SphinxTaskReducersManager sphinxTaskReducersManager(sphinxTaskReducersStorage,
71  sphinxTaskReducerFactory, sphinxResultJSONConvertor);
72 
73  EXPECT_CALL(*sphinxTaskReducersStorage.get(), findByKey(_))
74  .WillOnce(Throw(notFoundSphinxTaskReducerException));
75  ASSERT_THAT(sphinxTaskReducersManager.isReducerExist(noExistTaskId), false);
76 }
77 
78 
79 TEST_F(SphinxTaskReducersManagerTest, createNewSphinxTaskReducerForTaskId)
80 {
81  unsigned long long newTaskId = 3;
82 
83  SphinxTaskReducersManager sphinxTaskReducersManager(sphinxTaskReducersStorage,
84  sphinxTaskReducerFactory, sphinxResultJSONConvertor);
85 
86  EXPECT_CALL(*sphinxTaskReducerFactory.get(), create())
87  .WillOnce(ReturnPointee(&sphinxTaskReducer));
88  EXPECT_CALL(*sphinxTaskReducersStorage.get(), addWithKey(Eq(newTaskId), _))
89  .Times(1);
90 
91  sphinxTaskReducersManager.createReducer(newTaskId);
92 }
93 
94 
95 TEST_F(SphinxTaskReducersManagerTest, convertJSONtoSphinxResultAndFillItInSphinxTaskReducer)
96 {
97  unsigned long long existTaskId = 7;
98  string json = "fake JSON";
99  SharedPtr<SphinxResultData> buildFromJSONSphinxResult;
100 
101  SphinxTaskReducersManager sphinxTaskReducersManager(sphinxTaskReducersStorage,
102  sphinxTaskReducerFactory, sphinxResultJSONConvertor);
103 
104  EXPECT_CALL(*sphinxResultJSONConvertor.get(), convertToSphinxResultDataFrom(Eq(json)))
105  .WillOnce(ReturnPointee(&buildFromJSONSphinxResult));
106  EXPECT_CALL(*sphinxTaskReducersStorage.get(), findByKey(Eq(existTaskId)))
107  .WillOnce(ReturnPointee(&sphinxTaskReducer));
108  EXPECT_CALL(*sphinxTaskReducer.get(), addData(_))
109  .Times(1);
110 
111  sphinxTaskReducersManager.addDataInReducer(existTaskId, json);
112 }
113 
114 
115 TEST_F(SphinxTaskReducersManagerTest, throwExceptionWhileEncounterJSONStructureError)
116 {
117  unsigned long long existTaskId = 7;
118  string badStructuredJSON = "bad structure";
119  WrongJSONStructureException wrongJSONStructureException("");
120 
121  SphinxTaskReducersManager sphinxTaskReducersManager(sphinxTaskReducersStorage,
122  sphinxTaskReducerFactory, sphinxResultJSONConvertor);
123 
124  EXPECT_CALL(*sphinxResultJSONConvertor.get(), convertToSphinxResultDataFrom(_))
125  .WillOnce(Throw(wrongJSONStructureException));
126  ASSERT_THROW(sphinxTaskReducersManager.addDataInReducer(existTaskId,
127  badStructuredJSON),
129 }
130 
131 
132 TEST_F(SphinxTaskReducersManagerTest, runReduceForSphinxTaskReducerByTaskIdAndConvertResultToJSON)
133 {
134  unsigned long long reducedTaskId = 4;
135  ScheduleStrategySingle scheduleStrategy;
136  SharedPtr<SphinxResultData> reducedSphinxResult;
137  SphinxResultTrancaterMock SphinxResultTrancater;
138 
139  SphinxTaskReducersManager sphinxTaskReducersManager(sphinxTaskReducersStorage,
140  sphinxTaskReducerFactory, sphinxResultJSONConvertor);
141 
142  EXPECT_CALL(*sphinxTaskReducersStorage.get(), findByKey(Eq(reducedTaskId)))
143  .WillOnce(ReturnPointee(&sphinxTaskReducer));
144  EXPECT_CALL(*sphinxTaskReducerFactory.get(), getScheduleStrategy())
145  .WillOnce(ReturnRef(scheduleStrategy));
146  EXPECT_CALL(*sphinxTaskReducer.get(), reduce(_))
147  .Times(1);
148  EXPECT_CALL(*sphinxTaskReducer.get(), getResult())
149  .WillOnce(ReturnPointee(&reducedSphinxResult));
150  EXPECT_CALL(*sphinxTaskReducerFactory.get(), getSphinxResultTrancater())
151  .WillOnce(ReturnRef(SphinxResultTrancater));
152  EXPECT_CALL(SphinxResultTrancater, trancate(Eq(reducedSphinxResult)))
153  .WillOnce(ReturnPointee(&reducedSphinxResult));
154  EXPECT_CALL(*sphinxResultJSONConvertor.get(), convertToJSONFrom(Eq(reducedSphinxResult)))
155  .Times(1);
156 
157  sphinxTaskReducersManager.runReduceTaskForTaskId(reducedTaskId);
158 }
159 
160 
161 TEST_F(SphinxTaskReducersManagerTest, deleteSphinxTaskReducerByTaskId)
162 {
163  unsigned long long taskId = 3;
164 
165  SphinxTaskReducersManager sphinxTaskReducersManager(sphinxTaskReducersStorage,
166  sphinxTaskReducerFactory, sphinxResultJSONConvertor);
167 
168  EXPECT_CALL(*sphinxTaskReducersStorage.get(), deleteByKey(Eq(taskId)))
169  .Times(1);
170 
171  sphinxTaskReducersManager.deleteReducerBy(taskId);
172 }
173 
174