hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ReducingHandlerIntergationTest.cpp
Go to the documentation of this file.
1 /*
2  * ReducingHandlerIntergationTest.cpp
3  *
4  * Created on: Jun 5, 2013
5  * Author: igor
6  */
7 #include <gtest/gtest.h>
8 #include "ReducingHandler.hpp"
16 #include "TestUtils.hpp"
18 
19 using namespace HCE::types;
20 using namespace HCE::reduce;
21 using namespace HCE::reduce_types;
22 using namespace HCE::sphinx;
23 using namespace HCE::sphinx::reduce_task;
24 using namespace Poco;
25 using namespace std;
26 
27 class ReducingHandlerIntergationTest: public ::testing::Test{
28 protected:
29  virtual void SetUp(){
30  itemId = 2442352714LLU;
31  ttl = 7;
32 
33  reducingPartsCountersStorage.assign(new ReducingPartsCountersStorage());
34  reduceAdditionsStorage.assign(new ReduceAdditionsStorage());
35  taskReducersManagerFactory.assign(new TaskReducersManagerFactory());
36  reducingExceptionsTranslator.assign(new ReducingExceptionsTranslator());
37  reducingOutputMessageBuilder.assign(new ReducingOutputMessageBuilder(reducingExceptionsTranslator));
38  supportNotifier.assign(new ConsoleSupportNotifier());
39 
40  reducingHandler.assign(new ReducingHandler(reducingPartsCountersStorage,
41  reduceAdditionsStorage,
42  taskReducersManagerFactory,
43  reducingOutputMessageBuilder,
44  supportNotifier));
45  buildSphinxResults();
46  }
47 
48  void buildSphinxResults()
49  {
50  WordInfo word1 = WordInfo("word1", 10, 3);
51  WordInfo word2 = WordInfo("word2", 1, 1);
52 
53  SphinxMatchInfo matchInfo1 = buildSimpleMatchInfo(7LLU, 111LLU);
54  SphinxMatchInfo matchInfo2 = buildSimpleMatchInfo(9LLU, 222LLU);
55  SphinxMatchInfo matchInfo3 = buildSimpleMatchInfo(15LLU, 674387LLU);
56  SphinxMatchInfo matchInfo4 = buildSimpleMatchInfo(9LLU, 674387LLU);
57 
58  AttrInfo attrInfo1 = AttrInfo("first", "value");
59 
60  matchInfo1.addAttrInfo(attrInfo1);
61  matchInfo2.addAttrInfo(attrInfo1);
62  matchInfo3.addAttrInfo(attrInfo1);
63  matchInfo4.addAttrInfo(attrInfo1);
64 
65  sphinxRequestInfo1.setMaxResultsNumber(5);
66  sphinxRequestInfo1.setNodeName("nodeName1");
67  sphinxRequestInfo1.setQuery("query");
68  sphinxRequestInfo1.setTotal(10);
69  sphinxRequestInfo1.setTotalFound(150);
70  sphinxRequestInfo1.setTimeMsec(200);
71  sphinxRequestInfo1.addWordInfo(word1);
72  sphinxRequestInfo1.addWordInfo(word2);
73 
74  sphinxRequestInfo2.setMaxResultsNumber(5);
75  sphinxRequestInfo2.setNodeName("nodeName2");
76  sphinxRequestInfo2.addWordInfo(word1);
77  sphinxRequestInfo2.addWordInfo(word2);
78 
79  sphinxResultData1.addMatchInfo(matchInfo1);
80  sphinxResultData1.addMatchInfo(matchInfo2);
81 
82  sphinxResultData2.addMatchInfo(matchInfo3);
83  sphinxResultData2.addMatchInfo(matchInfo4);
84 
85  expectedSphinxResultData12.addMatchInfo(matchInfo3);
86  expectedSphinxResultData12.addMatchInfo(matchInfo1);
87  expectedSphinxResultData12.addMatchInfo(matchInfo4);
88  //expectedSphinxResultData12.addMatchInfo(matchInfo3);
89  expectedSphinxResultData12.addRequestInfo(sphinxRequestInfo1);
90  expectedSphinxResultData12.addRequestInfo(sphinxRequestInfo2);
91 
92  sphinxRequestInfo1.setMaxResultsNumber(2);
93  sphinxRequestInfo1.setOrderBy(DESC_SORT);
94 
95  sphinxRequestInfo2.setMaxResultsNumber(2);
96  sphinxRequestInfo2.setOrderBy(DESC_SORT);
97 
98  //because of using map, order by id different from input order
99  expectedSphinxResultData12DescSort.addMatchInfo(matchInfo3);
100  expectedSphinxResultData12DescSort.addMatchInfo(matchInfo4);
101  //expectedSphinxResultData12DescSort.addMatchInfo(matchInfo3);
102  expectedSphinxResultData12DescSort.addRequestInfo(sphinxRequestInfo1);
103  expectedSphinxResultData12DescSort.addRequestInfo(sphinxRequestInfo2);
104  }
105 
106  unsigned long long itemId;
107  int ttl;
108  SharedPtr<ReducingPartsCountersStorage> reducingPartsCountersStorage;
109  SharedPtr<ReduceAdditionsStorage> reduceAdditionsStorage;
110  SharedPtr<TaskReducersManagerFactory> taskReducersManagerFactory;
111  SharedPtr<ReducingExceptionsTranslator>reducingExceptionsTranslator;
112  SharedPtr<ReducingOutputMessageBuilder> reducingOutputMessageBuilder;
113  SharedPtr<SupportNotifierIf> supportNotifier;
114 
115  SharedPtr<ReducingHandler>reducingHandler;
116 
117  SharedPtr<ReducingOutputMessage> reducingOutputMessage;
118  SharedPtr<ReducingOutputMessage> etalonOutputMessage;
119  SharedPtr<ReducingInputMessage> reducingInputMessage;
120  SharedPtr<ReducingInputMessage> reducingInputMessage2;
121 
128 };
129 
130 TEST_F(ReducingHandlerIntergationTest, reducingTaskForInvalidKey)
131 {
132  string errMsg = "ReduceAdditionsStorage - not found key " + std::to_string(itemId);
133  NotFoundByKeyException notFoundByKeyException(errMsg);
134 
135  etalonOutputMessage = buildEtalonFrom(notFoundByKeyException);
136 
137  reducingOutputMessage = reducingHandler->makeReducing(itemId);
138  ASSERT_TRUE(isEqualReducingOutputMessage(*reducingOutputMessage.get(),
139  *etalonOutputMessage.get()));
140 }
141 
142 
143 TEST_F(ReducingHandlerIntergationTest, supressThrowsWhileDeleteReducingTaskByInvalidKey)
144 {
145  ASSERT_NO_THROW(reducingHandler->deleteReducingTaskBy(itemId));
146 }
147 
148 
149 TEST_F(ReducingHandlerIntergationTest, throwExceptionWhenEncounterInvalidKeyForPartsNumberCounters)
150 {
151  ASSERT_THROW(reducingHandler->getAccumulateReducingPartsNumberBy(itemId),
153 }
154 
155 
156 TEST_F(ReducingHandlerIntergationTest, reduceTaskWithOnePartsNumber)
157 {
158  sphinxRequestInfo1.setMaxResultsNumber(5);
159  sphinxRequestInfo1.setOrderBy(NULL_SORT);
160  sphinxRequestInfo2.setMaxResultsNumber(5);
161  sphinxRequestInfo2.setOrderBy(NULL_SORT);
162 
163  sphinxResultData1.addRequestInfo(sphinxRequestInfo1);
164  sphinxResultData2.addRequestInfo(sphinxRequestInfo2);
165 
166  string json_input = buildInputJSONFromSphinxResultData(sphinxResultData1);
167  string json_output = buildOutputJSONFromSphinxResultData(sphinxResultData1);
168  reducingInputMessage = buildReducingInputMessage(MessageType::mtSphinx, ttl, json_input);
169  etalonOutputMessage = buildEtalonFrom(MessageType::mtSphinx, reducingInputMessage->getTTL(), json_output);
170 
171  reducingHandler->accumulateReducingData(itemId, reducingInputMessage);
172  reducingOutputMessage = reducingHandler->makeReducing(itemId);
173  ASSERT_TRUE(isEqualReducingOutputMessage(*reducingOutputMessage.get(),
174  *etalonOutputMessage.get()));
175 }
176 
177 
178 TEST_F(ReducingHandlerIntergationTest, reduceTaskWithTwoPartsNumber)
179 {
180  sphinxRequestInfo1.setMaxResultsNumber(5);
181  sphinxRequestInfo1.setOrderBy(NULL_SORT);
182  sphinxRequestInfo2.setMaxResultsNumber(5);
183  sphinxRequestInfo2.setOrderBy(NULL_SORT);
184 
185  sphinxResultData1.addRequestInfo(sphinxRequestInfo1);
186  sphinxResultData2.addRequestInfo(sphinxRequestInfo2);
187 
188  string expectedJSON = buildOutputJSONFromSphinxResultData(expectedSphinxResultData12);
189  string json1 = buildInputJSONFromSphinxResultData(sphinxResultData1);
190  string json2 = buildInputJSONFromSphinxResultData(sphinxResultData2);
191  reducingInputMessage = buildReducingInputMessage(MessageType::mtSphinx, ttl, json1);
192  reducingInputMessage2 = buildReducingInputMessage(MessageType::mtSphinx, ttl, json2);
193  etalonOutputMessage = buildEtalonFrom(MessageType::mtSphinx, reducingInputMessage->getTTL(), expectedJSON);
194 
195  reducingHandler->accumulateReducingData(itemId, reducingInputMessage);
196  reducingHandler->accumulateReducingData(itemId, reducingInputMessage2);
197  reducingOutputMessage = reducingHandler->makeReducing(itemId);
198 
199  ASSERT_TRUE(isEqualReducingOutputMessage(*reducingOutputMessage.get(),
200  *etalonOutputMessage.get()));
201 }
202 
203 
204 TEST_F(ReducingHandlerIntergationTest, reduceTaskWithTwoPartsNumberWithDescSort)
205 {
206  sphinxResultData1.addRequestInfo(sphinxRequestInfo1);
207  sphinxResultData2.addRequestInfo(sphinxRequestInfo2);
208 
209  string expectedJSON = buildOutputJSONFromSphinxResultData(expectedSphinxResultData12DescSort);
210  string json1 = buildInputJSONFromSphinxResultData(sphinxResultData1);
211  string json2 = buildInputJSONFromSphinxResultData(sphinxResultData2);
212  reducingInputMessage = buildReducingInputMessage(MessageType::mtSphinx, ttl, json1);
213  reducingInputMessage2 = buildReducingInputMessage(MessageType::mtSphinx, ttl, json2);
214  etalonOutputMessage = buildEtalonFrom(MessageType::mtSphinx, reducingInputMessage->getTTL(), expectedJSON);
215 
216  reducingHandler->accumulateReducingData(itemId, reducingInputMessage);
217  reducingHandler->accumulateReducingData(itemId, reducingInputMessage2);
218  reducingOutputMessage = reducingHandler->makeReducing(itemId);
219 
220  ASSERT_TRUE(isEqualReducingOutputMessage(*reducingOutputMessage.get(),
221  *etalonOutputMessage.get()));
222 }
223