hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ReducingHandlerTest.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 #include "HandlerMocks.hpp"
3 #include "ReducingHandler.hpp"
6 #include "Exceptions.hpp"
8 
9 using namespace HCE::reduce_types;
10 using namespace HCE::sphinx::reduce_task;
11 
12 using ::testing::_;
13 using ::testing::Eq;
14 using ::testing::Return;
15 using ::testing::NiceMock;
16 using ::testing::Throw;
17 using ::testing::ReturnPointee;
18 using ::testing::Matcher;
19 using ::testing::InSequence;
20 
21 
22 class ReducingHandlerTest: public ::testing::Test{
23 protected:
24  virtual void SetUp(){
25  reducingPartsCountersStorage.assign(new ReducingPartsCountersStorageMock());
26  reduceAdditionsStorage.assign(new ReduceAdditionsStorageMock());
27  taskReducersManagerFactory.assign(new TaskReducersManagerFactoryMock());
28  taskReducersManager.assign(new TaskReducersManagerMock());
29  reducingOutputMessageBuilder.assign(new ReducingOutputMessageBuilderMock());
30  supportNotifier.assign(new SupportNotifierMock());
31  reducingPartsCountersStorageNice.assign(new NiceMock<ReducingPartsCountersStorageMock>());
32 
33  reducingHandler.assign(new ReducingHandler(reducingPartsCountersStorage,
34  reduceAdditionsStorage,
35  taskReducersManagerFactory,
36  reducingOutputMessageBuilder,
37  supportNotifier));
38  itemId = 2;
39  HCE::types::MessageType reduceType = HCE::types::MessageType::mtMysql; //need use const - must failure
40  int ttl = 7;
41  string body = "some body";
42 
43  reducingInputMessage.assign(new ReducingInputMessage(reduceType, ttl, body));
44  }
45 
46  unsigned long long itemId;
47  SharedPtr<ReducingPartsCountersStorageMock> reducingPartsCountersStorage;
48  SharedPtr<ReduceAdditionsStorageMock> reduceAdditionsStorage;
49  SharedPtr<TaskReducersManagerMock>taskReducersManager;
50  SharedPtr<TaskReducersManagerFactoryMock> taskReducersManagerFactory;
51  SharedPtr<ReducingOutputMessageBuilderMock> reducingOutputMessageBuilder;
52  SharedPtr<SupportNotifierMock> supportNotifier;
53  SharedPtr<ReducingInputMessage> reducingInputMessage;
54  SharedPtr<NiceMock<ReducingPartsCountersStorageMock> >reducingPartsCountersStorageNice;
55  SharedPtr<ReducingOutputMessage> reducingOutputMessage;
56  SharedPtr<ReducingHandler>reducingHandler;
57 };
58 
59 
60 TEST_F(ReducingHandlerTest, createInitStructuresForAccumulating)
61 {
62  NotFoundByKeyException notFoundByKeyException("");
63  SharedPtr<TaskReducersManagerMock>taskReducersManagerN(new NiceMock<TaskReducersManagerMock>());
64 
65  InSequence s;
66 
67  EXPECT_CALL(*reducingPartsCountersStorage.get(), findBy(Eq(itemId)))
68  .WillRepeatedly(Return(false));
69  //init new structures
70  EXPECT_CALL(*reduceAdditionsStorage.get(), findBy(Matcher<HCE::types::MessageType>(_)))
71  .WillRepeatedly(Throw(notFoundByKeyException));
72  EXPECT_CALL(*taskReducersManagerFactory.get(), build(_))
73  .WillOnce(ReturnPointee(&taskReducersManagerN));
74  EXPECT_CALL(*reduceAdditionsStorage.get(), addWith(Matcher<HCE::types::MessageType>(_), _))
75  .Times(1);
76  EXPECT_CALL(*reduceAdditionsStorage.get(), findBy(Matcher<HCE::types::MessageType>(_)))
77  .WillRepeatedly(ReturnPointee(&taskReducersManagerN));
78  EXPECT_CALL(*reduceAdditionsStorage.get(), addWith(Matcher<unsigned long long>(_), _))
79  .Times(1);
80  EXPECT_CALL(*reducingPartsCountersStorage.get(), addReducingPartsCounterWithKey(Eq(itemId)))
81  .Times(1);
82  EXPECT_CALL(*reduceAdditionsStorage.get(), findBy(Matcher<HCE::types::MessageType>(_)))
83  .WillRepeatedly(ReturnPointee(&taskReducersManagerN));
84  //usual flow
85  EXPECT_CALL(*reducingPartsCountersStorage.get(), incrementBy(Eq(itemId)))
86  .Times(1);
87  EXPECT_CALL(*reduceAdditionsStorage.get(), findBy(Matcher<unsigned long long>(Eq(itemId))))
88  .WillRepeatedly(ReturnPointee(&taskReducersManagerN));
89 
90  reducingHandler->accumulateReducingData(itemId, reducingInputMessage);
91 }
92 
93 
94 TEST_F(ReducingHandlerTest, translateJSONStructureThrowsToSupportNotifier)
95 {
96  WrongJSONStructureException wrongJSONStructureException("");
97 
98  EXPECT_CALL(*reducingPartsCountersStorage.get(), findBy(_))
99  .WillOnce(Return(true));
100 
101  EXPECT_CALL(*reducingPartsCountersStorage.get(), incrementBy(_))
102  .Times(1);
103 
104  EXPECT_CALL(*reduceAdditionsStorage.get(), findBy(Matcher<unsigned long long>(Eq(itemId))))
105  .WillRepeatedly(ReturnPointee(&taskReducersManager));
106 
107  EXPECT_CALL(*taskReducersManager.get(), addDataInReducer(_, _))
108  .WillOnce(Throw(wrongJSONStructureException));
109 
110  EXPECT_CALL(*supportNotifier.get(), notifyWrongJSONStructure(_, _))
111  .Times(1);
112 
113  reducingHandler->accumulateReducingData(itemId, reducingInputMessage);
114 }
115 
116 
117 TEST_F(ReducingHandlerTest, translateManagerFactoryNotFoundByKeyExceptionToSupportNotifier)
118 {
119  NotFoundByKeyException notFoundByKeyException("");
120 
121  EXPECT_CALL(*reducingPartsCountersStorage.get(), findBy(_))
122  .WillOnce(Return(false));
123 
124  EXPECT_CALL(*reduceAdditionsStorage.get(), findBy(Matcher<HCE::types::MessageType>(_)))
125  .WillOnce(Throw(notFoundByKeyException));
126 
127  EXPECT_CALL(*taskReducersManagerFactory.get(), build(_))
128  .WillOnce(Throw(notFoundByKeyException));
129 
130  EXPECT_CALL(*supportNotifier.get(), notifyNotFoundItemException(_))
131  .Times(1);
132 
133  reducingHandler->accumulateReducingData(itemId, reducingInputMessage);
134 }
135 
136 
138 {
139  string reducingJSON;
140  types::MessageType msgType = types::MessageType::mtSphinx;
141 
142  EXPECT_CALL(*reduceAdditionsStorage.get(), findBy(Matcher<unsigned long long>(Eq(itemId))))
143  .WillRepeatedly(ReturnPointee(&taskReducersManager));
144  EXPECT_CALL(*taskReducersManager.get(), getType())
145  .WillOnce(Return(msgType));
146 
147  EXPECT_CALL(*reduceAdditionsStorage.get(), findBy(Matcher<unsigned long long>(Eq(itemId))))
148  .WillRepeatedly(ReturnPointee(&taskReducersManager));
149 
150  EXPECT_CALL(*taskReducersManager.get(), runReduceTaskForTaskId(Eq(itemId)))
151  .WillOnce(Return(reducingJSON));
152  EXPECT_CALL(*reducingOutputMessageBuilder.get(), build(_, _, Matcher<const string&>(Eq(reducingJSON))))
153  .WillOnce(ReturnPointee(&reducingOutputMessage));
154 
155  reducingHandler->makeReducing(itemId);
156 }
157 
158 
159 TEST_F(ReducingHandlerTest, translateNotFoundSphinxTaskReducerExceptionToSupportNotifier)
160 {
161  NotFoundByKeyException notFoundSphinxTaskReducerException("");
162  types::MessageType msgType = types::MessageType::mtSphinx;
163 
164  EXPECT_CALL(*reduceAdditionsStorage.get(), findBy(Matcher<unsigned long long>(Eq(itemId))))
165  .WillRepeatedly(ReturnPointee(&taskReducersManager));
166 
167  EXPECT_CALL(*taskReducersManager.get(), getType())
168  .WillOnce(Return(msgType));
169 
170  EXPECT_CALL(*reduceAdditionsStorage.get(), findBy(Matcher<unsigned long long>(Eq(itemId))))
171  .WillRepeatedly(ReturnPointee(&taskReducersManager));
172 
173  EXPECT_CALL(*taskReducersManager.get(), runReduceTaskForTaskId(Eq(itemId)))
174  .WillOnce(Throw(notFoundSphinxTaskReducerException));
175  EXPECT_CALL(*supportNotifier.get(), notifyNotFoundItemException(_))
176  .Times(1);
177  EXPECT_CALL(*reducingOutputMessageBuilder.get(),
178  build(Matcher<const NotFoundByKeyException&>(_)))
179  .WillOnce(ReturnPointee(&reducingOutputMessage));
180 
181  reducingHandler->makeReducing(itemId);
182 }
183 
184 
185 TEST_F(ReducingHandlerTest, cleanUpResourcesAfterDeleteItemId)
186 {
187  EXPECT_CALL(*reducingPartsCountersStorage.get(), deleteBy(Eq(itemId)))
188  .Times(1);
189  EXPECT_CALL(*reduceAdditionsStorage.get(), findBy(Matcher<unsigned long long>(Eq(itemId))))
190  .WillRepeatedly(ReturnPointee(&taskReducersManager));
191  EXPECT_CALL(*taskReducersManager.get(), deleteReducerBy(Eq(itemId)))
192  .Times(1);
193  EXPECT_CALL(*reduceAdditionsStorage.get(), deleteBy(Matcher<unsigned long long>(Eq(itemId))))
194  .Times(1);
195 
196  reducingHandler->deleteReducingTaskBy(itemId);
197 }
198 
199 
200 TEST_F(ReducingHandlerTest, translateNotFoundSphinxTaskReducerExceptionToSupportNotifierDuringDelete)
201 {
202  NotFoundByKeyException notFoundSphinxTaskReducerException("");
203 
204  EXPECT_CALL(*reducingPartsCountersStorage.get(), deleteBy(Eq(itemId)))
205  .WillOnce(Throw(notFoundSphinxTaskReducerException));
206  EXPECT_CALL(*supportNotifier.get(), notifyNotFoundItemException(_))
207  .Times(1);
208 
209  reducingHandler->deleteReducingTaskBy(itemId);
210 }
211 
212 
213 TEST_F(ReducingHandlerTest, getAccumulateReducingParts)
214 {
215  EXPECT_CALL(*reducingPartsCountersStorage.get(), getAccumulateReducingParts(Eq(itemId)))
216  .WillOnce(Return(1));
217 
218  reducingHandler->getAccumulateReducingPartsNumberBy(itemId);
219 }
220 
221 
222 TEST_F(ReducingHandlerTest, throwExceptionWhilegetAccumulateReducingPartsbyInvalidKey)
223 {
224  NotFoundByKeyException notFoundSphinxTaskReducerException("");
225 
226  EXPECT_CALL(*reducingPartsCountersStorage.get(), getAccumulateReducingParts(Eq(itemId)))
227  .WillOnce(Throw(notFoundSphinxTaskReducerException));
228 
229  ASSERT_THROW(reducingHandler->getAccumulateReducingPartsNumberBy(itemId),
231 }