hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
main.cpp
Go to the documentation of this file.
1 
14 #include <gtest/gtest.h>
15 #include <sstream>
16 
17 #include "JsonMessageCover.hpp"
18 
19 const std::string data = "Hello world!";
20 HCE::types::MessageType type = HCE::types::MessageType::mtSphinx;
21 unsigned int ttl = 234;
22 
23 const std::string validDecodeJson = "{\"data\":\"Hello world!\",\"ttl\":234,\"type\":1}";
24 const std::string validEncodeJson = "{\"data\":\"SGVsbG8gd29ybGQh\",\"ttl\":234,\"type\":1}";
25 
26 const std::string invalidDecodeJson = "{\"data\":\"Hello world!\",\"ttl\":234,\"typ";
27 const std::string invalidEncodeJson = "{\"data\":\"SGVsbG8gd29ybGQh\",\"ttl\":234,\"typ";
28 
29 
30 TEST(JsonMessageCover, serializeJsonMessageCover)
31 {
32  std::string json;
33  HCE::JsonMessageCover jsonMessageCover;
34  jsonMessageCover.setType(type);
35  jsonMessageCover.setData(data);
36  jsonMessageCover.setTTL(ttl);
37 
38  ASSERT_TRUE(jsonMessageCover.serialize(json));
39  ASSERT_FALSE(jsonMessageCover.isError());
40  ASSERT_EQ(jsonMessageCover.getErrorCode(), HCE::JsonMessageCover::ErrorCode::NO_ERROR);
41  ASSERT_TRUE(jsonMessageCover.getErrorMsg().empty());
42  ASSERT_FALSE(json.empty());
43 }
44 
45 TEST(JsonMessageCover, unserializeJsonMessageCover)
46 {
47  std::string json;
48 
49 #if JSON_USE_BASE64 == 1
50  json = validEncodeJson;
51 #else
52  json = validDecodeJson;
53 #endif
54 
55  HCE::JsonMessageCover jsonMessageCover;
56  ASSERT_TRUE(jsonMessageCover.unserialize(json));
57  ASSERT_FALSE(jsonMessageCover.isError());
58  ASSERT_EQ(jsonMessageCover.getErrorCode(), HCE::JsonMessageCover::ErrorCode::NO_ERROR);
59  ASSERT_TRUE(jsonMessageCover.getErrorMsg().empty());
60 
61  ASSERT_EQ(jsonMessageCover.getType(), HCE::types::MessageType::mtSphinx);
62  ASSERT_EQ(jsonMessageCover.getData(), data);
63  ASSERT_EQ(jsonMessageCover.getTTL(), ttl);
64 }
65 
66 TEST(JsonMessageCover, useOutputStream)
67 {
68  HCE::JsonMessageCover jsonMessageCover;
69  jsonMessageCover.setType(type);
70  jsonMessageCover.setData(data);
71  jsonMessageCover.setTTL(ttl);
72 
73  std::stringstream json;
74  json << jsonMessageCover;
75 
76  ASSERT_FALSE(jsonMessageCover.isError());
77  ASSERT_EQ(jsonMessageCover.getErrorCode(), HCE::JsonMessageCover::ErrorCode::NO_ERROR);
78  ASSERT_TRUE(jsonMessageCover.getErrorMsg().empty());
79  ASSERT_FALSE(json.str().empty());
80 }
81 
82 TEST(JsonMessageCover, useInputStream)
83 {
84  std::stringstream json;
85 
86 #if JSON_USE_BASE64 == 1
87  json << validEncodeJson;
88 #else
89  json << validDecodeJson;
90 #endif
91 
92  HCE::JsonMessageCover jsonMessageCover;
93  json >> jsonMessageCover;
94 
95  ASSERT_FALSE(jsonMessageCover.isError());
96  ASSERT_EQ(jsonMessageCover.getErrorCode(), HCE::JsonMessageCover::ErrorCode::NO_ERROR);
97  ASSERT_TRUE(jsonMessageCover.getErrorMsg().empty());
98 
99  ASSERT_EQ(jsonMessageCover.getType(), HCE::types::MessageType::mtSphinx);
100  ASSERT_EQ(jsonMessageCover.getData(), data);
101  ASSERT_EQ(jsonMessageCover.getTTL(), ttl);
102 }
103 
104 TEST(JsonMessageCover, errorMessageJsonMessageCover)
105 {
106  std::stringstream json;
107 
108 #if JSON_USE_BASE64 == 1
109  json << invalidEncodeJson;
110 #else
111  json << invalidDecodeJson;
112 #endif
113 
114  HCE::JsonMessageCover jsonMessageCover;
115  json >> jsonMessageCover;
116 
117  ASSERT_TRUE(jsonMessageCover.isError());
118  ASSERT_EQ(jsonMessageCover.getErrorCode(), HCE::JsonMessageCover::ErrorCode::ERROR_UNSERIALIZE);
119  ASSERT_FALSE(jsonMessageCover.getErrorMsg().empty());
120 }
121 
122 int main (int argc, char** argv)
123 {
124  testing::InitGoogleTest(&argc, argv);
125  return RUN_ALL_TESTS();
126 }
127