Commit a16d2be9bb501ae7e40f180e243dbd8b03df04fd

Authored by Moritz W
Committed by Moritz Wirger
1 parent a675e68a

Fix unittests for HueCommandAPI

hueplusplus/test/test_HueCommandAPI.cpp
@@ -30,35 +30,35 @@ TEST(HueCommandAPI, PUTRequest) @@ -30,35 +30,35 @@ TEST(HueCommandAPI, PUTRequest)
30 using namespace ::testing; 30 using namespace ::testing;
31 std::shared_ptr<MockHttpHandler> httpHandler = std::make_shared<MockHttpHandler>(); 31 std::shared_ptr<MockHttpHandler> httpHandler = std::make_shared<MockHttpHandler>();
32 32
33 - HueCommandAPI api(bridge_ip, bridge_username, httpHandler); 33 + HueCommandAPI api(getBridgeIp(), getBridgeUsername(), httpHandler);
34 Json::Value request; 34 Json::Value request;
35 Json::Value result = Json::objectValue; 35 Json::Value result = Json::objectValue;
36 result["ok"] = true; 36 result["ok"] = true;
37 37
38 //empty path 38 //empty path
39 { 39 {
40 - EXPECT_CALL(*httpHandler, PUTJson("/api/" + bridge_username, request, bridge_ip, 80)).WillOnce(Return(result)); 40 + EXPECT_CALL(*httpHandler, PUTJson("/api/" + getBridgeUsername(), request, getBridgeIp(), 80)).WillOnce(Return(result));
41 EXPECT_EQ(result, api.PUTRequest("", request)); 41 EXPECT_EQ(result, api.PUTRequest("", request));
42 Mock::VerifyAndClearExpectations(httpHandler.get()); 42 Mock::VerifyAndClearExpectations(httpHandler.get());
43 } 43 }
44 //not empty path, starting with slash 44 //not empty path, starting with slash
45 { 45 {
46 const std::string path = "/test"; 46 const std::string path = "/test";
47 - EXPECT_CALL(*httpHandler, PUTJson("/api/" + bridge_username + path, request, bridge_ip, 80)).WillOnce(Return(result)); 47 + EXPECT_CALL(*httpHandler, PUTJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80)).WillOnce(Return(result));
48 EXPECT_EQ(result, api.PUTRequest(path, request)); 48 EXPECT_EQ(result, api.PUTRequest(path, request));
49 Mock::VerifyAndClearExpectations(httpHandler.get()); 49 Mock::VerifyAndClearExpectations(httpHandler.get());
50 } 50 }
51 //not empty path, not starting with slash 51 //not empty path, not starting with slash
52 { 52 {
53 const std::string path = "test"; 53 const std::string path = "test";
54 - EXPECT_CALL(*httpHandler, PUTJson("/api/" + bridge_username + '/' + path, request, bridge_ip, 80)).WillOnce(Return(result)); 54 + EXPECT_CALL(*httpHandler, PUTJson("/api/" + getBridgeUsername() + '/' + path, request, getBridgeIp(), 80)).WillOnce(Return(result));
55 EXPECT_EQ(result, api.PUTRequest(path, request)); 55 EXPECT_EQ(result, api.PUTRequest(path, request));
56 Mock::VerifyAndClearExpectations(httpHandler.get()); 56 Mock::VerifyAndClearExpectations(httpHandler.get());
57 } 57 }
58 //recoverable error 58 //recoverable error
59 { 59 {
60 const std::string path = "/test"; 60 const std::string path = "/test";
61 - EXPECT_CALL(*httpHandler, PUTJson("/api/" + bridge_username + path, request, bridge_ip, 80)) 61 + EXPECT_CALL(*httpHandler, PUTJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80))
62 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset)))) 62 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset))))
63 .WillOnce(Return(result)); 63 .WillOnce(Return(result));
64 EXPECT_EQ(result, api.PUTRequest(path, request)); 64 EXPECT_EQ(result, api.PUTRequest(path, request));
@@ -67,7 +67,7 @@ TEST(HueCommandAPI, PUTRequest) @@ -67,7 +67,7 @@ TEST(HueCommandAPI, PUTRequest)
67 //recoverable error x2 67 //recoverable error x2
68 { 68 {
69 const std::string path = "/test"; 69 const std::string path = "/test";
70 - EXPECT_CALL(*httpHandler, PUTJson("/api/" + bridge_username + path, request, bridge_ip, 80)) 70 + EXPECT_CALL(*httpHandler, PUTJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80))
71 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset)))) 71 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset))))
72 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset)))); 72 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset))));
73 EXPECT_THROW(api.PUTRequest(path, request), std::system_error); 73 EXPECT_THROW(api.PUTRequest(path, request), std::system_error);
@@ -76,7 +76,7 @@ TEST(HueCommandAPI, PUTRequest) @@ -76,7 +76,7 @@ TEST(HueCommandAPI, PUTRequest)
76 //unrecoverable error 76 //unrecoverable error
77 { 77 {
78 const std::string path = "/test"; 78 const std::string path = "/test";
79 - EXPECT_CALL(*httpHandler, PUTJson("/api/" + bridge_username + path, request, bridge_ip, 80)) 79 + EXPECT_CALL(*httpHandler, PUTJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80))
80 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::not_enough_memory)))); 80 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::not_enough_memory))));
81 EXPECT_THROW(api.PUTRequest(path, request), std::system_error); 81 EXPECT_THROW(api.PUTRequest(path, request), std::system_error);
82 Mock::VerifyAndClearExpectations(httpHandler.get()); 82 Mock::VerifyAndClearExpectations(httpHandler.get());
@@ -88,35 +88,35 @@ TEST(HueCommandAPI, GETRequest) @@ -88,35 +88,35 @@ TEST(HueCommandAPI, GETRequest)
88 using namespace ::testing; 88 using namespace ::testing;
89 std::shared_ptr<MockHttpHandler> httpHandler = std::make_shared<MockHttpHandler>(); 89 std::shared_ptr<MockHttpHandler> httpHandler = std::make_shared<MockHttpHandler>();
90 90
91 - HueCommandAPI api(bridge_ip, bridge_username, httpHandler); 91 + HueCommandAPI api(getBridgeIp(), getBridgeUsername(), httpHandler);
92 Json::Value request; 92 Json::Value request;
93 Json::Value result = Json::objectValue; 93 Json::Value result = Json::objectValue;
94 result["ok"] = true; 94 result["ok"] = true;
95 95
96 //empty path 96 //empty path
97 { 97 {
98 - EXPECT_CALL(*httpHandler, GETJson("/api/" + bridge_username, request, bridge_ip, 80)).WillOnce(Return(result)); 98 + EXPECT_CALL(*httpHandler, GETJson("/api/" + getBridgeUsername(), request, getBridgeIp(), 80)).WillOnce(Return(result));
99 EXPECT_EQ(result, api.GETRequest("", request)); 99 EXPECT_EQ(result, api.GETRequest("", request));
100 Mock::VerifyAndClearExpectations(httpHandler.get()); 100 Mock::VerifyAndClearExpectations(httpHandler.get());
101 } 101 }
102 //not empty path, starting with slash 102 //not empty path, starting with slash
103 { 103 {
104 const std::string path = "/test"; 104 const std::string path = "/test";
105 - EXPECT_CALL(*httpHandler, GETJson("/api/" + bridge_username + path, request, bridge_ip, 80)).WillOnce(Return(result)); 105 + EXPECT_CALL(*httpHandler, GETJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80)).WillOnce(Return(result));
106 EXPECT_EQ(result, api.GETRequest(path, request)); 106 EXPECT_EQ(result, api.GETRequest(path, request));
107 Mock::VerifyAndClearExpectations(httpHandler.get()); 107 Mock::VerifyAndClearExpectations(httpHandler.get());
108 } 108 }
109 //not empty path, not starting with slash 109 //not empty path, not starting with slash
110 { 110 {
111 const std::string path = "test"; 111 const std::string path = "test";
112 - EXPECT_CALL(*httpHandler, GETJson("/api/" + bridge_username + '/' + path, request, bridge_ip, 80)).WillOnce(Return(result)); 112 + EXPECT_CALL(*httpHandler, GETJson("/api/" + getBridgeUsername() + '/' + path, request, getBridgeIp(), 80)).WillOnce(Return(result));
113 EXPECT_EQ(result, api.GETRequest(path, request)); 113 EXPECT_EQ(result, api.GETRequest(path, request));
114 Mock::VerifyAndClearExpectations(httpHandler.get()); 114 Mock::VerifyAndClearExpectations(httpHandler.get());
115 } 115 }
116 //recoverable error 116 //recoverable error
117 { 117 {
118 const std::string path = "/test"; 118 const std::string path = "/test";
119 - EXPECT_CALL(*httpHandler, GETJson("/api/" + bridge_username + path, request, bridge_ip, 80)) 119 + EXPECT_CALL(*httpHandler, GETJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80))
120 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset)))) 120 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset))))
121 .WillOnce(Return(result)); 121 .WillOnce(Return(result));
122 EXPECT_EQ(result, api.GETRequest(path, request)); 122 EXPECT_EQ(result, api.GETRequest(path, request));
@@ -125,7 +125,7 @@ TEST(HueCommandAPI, GETRequest) @@ -125,7 +125,7 @@ TEST(HueCommandAPI, GETRequest)
125 //recoverable error x2 125 //recoverable error x2
126 { 126 {
127 const std::string path = "/test"; 127 const std::string path = "/test";
128 - EXPECT_CALL(*httpHandler, GETJson("/api/" + bridge_username + path, request, bridge_ip, 80)) 128 + EXPECT_CALL(*httpHandler, GETJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80))
129 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset)))) 129 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset))))
130 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset)))); 130 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset))));
131 EXPECT_THROW(api.GETRequest(path, request), std::system_error); 131 EXPECT_THROW(api.GETRequest(path, request), std::system_error);
@@ -134,7 +134,7 @@ TEST(HueCommandAPI, GETRequest) @@ -134,7 +134,7 @@ TEST(HueCommandAPI, GETRequest)
134 //unrecoverable error 134 //unrecoverable error
135 { 135 {
136 const std::string path = "/test"; 136 const std::string path = "/test";
137 - EXPECT_CALL(*httpHandler, GETJson("/api/" + bridge_username + path, request, bridge_ip, 80)) 137 + EXPECT_CALL(*httpHandler, GETJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80))
138 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::not_enough_memory)))); 138 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::not_enough_memory))));
139 EXPECT_THROW(api.GETRequest(path, request), std::system_error); 139 EXPECT_THROW(api.GETRequest(path, request), std::system_error);
140 Mock::VerifyAndClearExpectations(httpHandler.get()); 140 Mock::VerifyAndClearExpectations(httpHandler.get());
@@ -146,35 +146,35 @@ TEST(HueCommandAPI, DELETERequest) @@ -146,35 +146,35 @@ TEST(HueCommandAPI, DELETERequest)
146 using namespace ::testing; 146 using namespace ::testing;
147 std::shared_ptr<MockHttpHandler> httpHandler = std::make_shared<MockHttpHandler>(); 147 std::shared_ptr<MockHttpHandler> httpHandler = std::make_shared<MockHttpHandler>();
148 148
149 - HueCommandAPI api(bridge_ip, bridge_username, httpHandler); 149 + HueCommandAPI api(getBridgeIp(), getBridgeUsername(), httpHandler);
150 Json::Value request; 150 Json::Value request;
151 Json::Value result = Json::objectValue; 151 Json::Value result = Json::objectValue;
152 result["ok"] = true; 152 result["ok"] = true;
153 153
154 //empty path 154 //empty path
155 { 155 {
156 - EXPECT_CALL(*httpHandler, DELETEJson("/api/" + bridge_username, request, bridge_ip, 80)).WillOnce(Return(result)); 156 + EXPECT_CALL(*httpHandler, DELETEJson("/api/" + getBridgeUsername(), request, getBridgeIp(), 80)).WillOnce(Return(result));
157 EXPECT_EQ(result, api.DELETERequest("", request)); 157 EXPECT_EQ(result, api.DELETERequest("", request));
158 Mock::VerifyAndClearExpectations(httpHandler.get()); 158 Mock::VerifyAndClearExpectations(httpHandler.get());
159 } 159 }
160 //not empty path, starting with slash 160 //not empty path, starting with slash
161 { 161 {
162 const std::string path = "/test"; 162 const std::string path = "/test";
163 - EXPECT_CALL(*httpHandler, DELETEJson("/api/" + bridge_username + path, request, bridge_ip, 80)).WillOnce(Return(result)); 163 + EXPECT_CALL(*httpHandler, DELETEJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80)).WillOnce(Return(result));
164 EXPECT_EQ(result, api.DELETERequest(path, request)); 164 EXPECT_EQ(result, api.DELETERequest(path, request));
165 Mock::VerifyAndClearExpectations(httpHandler.get()); 165 Mock::VerifyAndClearExpectations(httpHandler.get());
166 } 166 }
167 //not empty path, not starting with slash 167 //not empty path, not starting with slash
168 { 168 {
169 const std::string path = "test"; 169 const std::string path = "test";
170 - EXPECT_CALL(*httpHandler, DELETEJson("/api/" + bridge_username + '/' + path, request, bridge_ip, 80)).WillOnce(Return(result)); 170 + EXPECT_CALL(*httpHandler, DELETEJson("/api/" + getBridgeUsername() + '/' + path, request, getBridgeIp(), 80)).WillOnce(Return(result));
171 EXPECT_EQ(result, api.DELETERequest(path, request)); 171 EXPECT_EQ(result, api.DELETERequest(path, request));
172 Mock::VerifyAndClearExpectations(httpHandler.get()); 172 Mock::VerifyAndClearExpectations(httpHandler.get());
173 } 173 }
174 //recoverable error 174 //recoverable error
175 { 175 {
176 const std::string path = "/test"; 176 const std::string path = "/test";
177 - EXPECT_CALL(*httpHandler, DELETEJson("/api/" + bridge_username + path, request, bridge_ip, 80)) 177 + EXPECT_CALL(*httpHandler, DELETEJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80))
178 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset)))) 178 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset))))
179 .WillOnce(Return(result)); 179 .WillOnce(Return(result));
180 EXPECT_EQ(result, api.DELETERequest(path, request)); 180 EXPECT_EQ(result, api.DELETERequest(path, request));
@@ -183,7 +183,7 @@ TEST(HueCommandAPI, DELETERequest) @@ -183,7 +183,7 @@ TEST(HueCommandAPI, DELETERequest)
183 //recoverable error x2 183 //recoverable error x2
184 { 184 {
185 const std::string path = "/test"; 185 const std::string path = "/test";
186 - EXPECT_CALL(*httpHandler, DELETEJson("/api/" + bridge_username + path, request, bridge_ip, 80)) 186 + EXPECT_CALL(*httpHandler, DELETEJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80))
187 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset)))) 187 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset))))
188 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset)))); 188 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::connection_reset))));
189 EXPECT_THROW(api.DELETERequest(path, request), std::system_error); 189 EXPECT_THROW(api.DELETERequest(path, request), std::system_error);
@@ -192,9 +192,9 @@ TEST(HueCommandAPI, DELETERequest) @@ -192,9 +192,9 @@ TEST(HueCommandAPI, DELETERequest)
192 //unrecoverable error 192 //unrecoverable error
193 { 193 {
194 const std::string path = "/test"; 194 const std::string path = "/test";
195 - EXPECT_CALL(*httpHandler, GETJson("/api/" + bridge_username + path, request, bridge_ip, 80)) 195 + EXPECT_CALL(*httpHandler, GETJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80))
196 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::not_enough_memory)))); 196 .WillOnce(Throw(std::system_error(std::make_error_code(std::errc::not_enough_memory))));
197 EXPECT_THROW(api.GETRequest(path, request), std::system_error); 197 EXPECT_THROW(api.GETRequest(path, request), std::system_error);
198 Mock::VerifyAndClearExpectations(httpHandler.get()); 198 Mock::VerifyAndClearExpectations(httpHandler.get());
199 } 199 }
200 -}  
201 \ No newline at end of file 200 \ No newline at end of file
  201 +}