Commit b55065bc601b60a5dd4af425516dc6d2e82c6fa6

Authored by Jojo-1000
Committed by Moritz Wirger
1 parent b43598ce

Add tests for APICache with base cache.

Showing 1 changed file with 141 additions and 6 deletions
test/test_APICache.cpp
... ... @@ -48,6 +48,13 @@ TEST(APICache, getRefreshDuration)
48 48 APICache cache("", commands, refresh);
49 49 EXPECT_EQ(refresh, cache.getRefreshDuration());
50 50 }
  51 + // With base cache, still independent duration
  52 + {
  53 + auto duration = std::chrono::seconds(5);
  54 + auto baseCache = std::make_shared<APICache>("/test", commands, std::chrono::seconds(0));
  55 + APICache c(baseCache, "api", duration);
  56 + EXPECT_EQ(duration, c.getRefreshDuration());
  57 + }
51 58 }
52 59  
53 60 TEST(APICache, refresh)
... ... @@ -69,8 +76,48 @@ TEST(APICache, refresh)
69 76 std::string path = "";
70 77 APICache cache(path, commands, std::chrono::seconds(10));
71 78 EXPECT_CALL(*handler,
  79 + GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
  80 + .Times(2)
  81 + .WillRepeatedly(Return(nlohmann::json::object()));
  82 + cache.refresh();
  83 + cache.refresh();
  84 + Mock::VerifyAndClearExpectations(handler.get());
  85 + }
  86 +}
  87 +
  88 +TEST(APICache, refreshBase)
  89 +{
  90 + using namespace ::testing;
  91 + auto handler = std::make_shared<MockHttpHandler>();
  92 + HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
  93 + const std::string basePath = "/test";
  94 + const std::string childPath = "/test/abc";
  95 + // Base cache with max duration
  96 + {
  97 + auto baseCache = std::make_shared<APICache>(basePath, commands, std::chrono::steady_clock::duration::max());
  98 + APICache cache(baseCache, "abc", std::chrono::seconds(0));
  99 +
  100 + // First call refreshes base, second call only child
  101 + InSequence s;
  102 + EXPECT_CALL(*handler,
  103 + GETJson("/api/" + getBridgeUsername() + basePath, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
  104 + .WillOnce(Return(nlohmann::json::object()));
  105 + EXPECT_CALL(*handler,
72 106 GETJson(
73   - "/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
  107 + "/api/" + getBridgeUsername() + childPath, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
  108 + .WillOnce(Return(nlohmann::json::object()));
  109 + cache.refresh();
  110 + cache.refresh();
  111 + Mock::VerifyAndClearExpectations(handler.get());
  112 + }
  113 + // Base cache with min duration
  114 + {
  115 + auto baseCache = std::make_shared<APICache>(basePath, commands, std::chrono::seconds(0));
  116 + APICache cache(baseCache, "abc", std::chrono::seconds(0));
  117 +
  118 + // Both calls refresh base
  119 + EXPECT_CALL(*handler,
  120 + GETJson("/api/" + getBridgeUsername() + basePath, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
74 121 .Times(2)
75 122 .WillRepeatedly(Return(nlohmann::json::object()));
76 123 cache.refresh();
... ... @@ -89,7 +136,7 @@ TEST(APICache, getValue)
89 136 {
90 137 std::string path = "/test/abc";
91 138 APICache cache(path, commands, std::chrono::seconds(0));
92   - nlohmann::json value = { {"a", "b"} };
  139 + nlohmann::json value = {{"a", "b"}};
93 140 EXPECT_CALL(*handler,
94 141 GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
95 142 .Times(2)
... ... @@ -102,7 +149,7 @@ TEST(APICache, getValue)
102 149 {
103 150 std::string path = "/test/abc";
104 151 APICache cache(path, commands, std::chrono::steady_clock::duration::max());
105   - nlohmann::json value = { {"a", "b"} };
  152 + nlohmann::json value = {{"a", "b"}};
106 153 EXPECT_CALL(*handler,
107 154 GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
108 155 .WillOnce(Return(value));
... ... @@ -114,7 +161,7 @@ TEST(APICache, getValue)
114 161 {
115 162 std::string path = "/test/abc";
116 163 APICache cache(path, commands, std::chrono::seconds(0));
117   - nlohmann::json value = { {"a", "b"} };
  164 + nlohmann::json value = {{"a", "b"}};
118 165 EXPECT_CALL(*handler,
119 166 GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
120 167 .WillOnce(Return(value));
... ... @@ -126,11 +173,99 @@ TEST(APICache, getValue)
126 173 {
127 174 std::string path = "/test/abc";
128 175 const APICache cache(path, commands, std::chrono::steady_clock::duration::max());
129   - nlohmann::json value = { {"a", "b"} };
  176 + nlohmann::json value = {{"a", "b"}};
130 177 EXPECT_CALL(*handler,
131 178 GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
132 179 .Times(0);
133 180 EXPECT_THROW(cache.getValue(), HueException);
134 181 Mock::VerifyAndClearExpectations(handler.get());
135 182 }
136   -}
137 183 \ No newline at end of file
  184 +}
  185 +
  186 +TEST(APICache, getValueBase)
  187 +{
  188 + using namespace ::testing;
  189 + auto handler = std::make_shared<MockHttpHandler>();
  190 + HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
  191 +
  192 + const std::string basePath = "/test";
  193 + const std::string childPath = "/test/abc";
  194 + const nlohmann::json childValue = {{"test", "value"}};
  195 + const nlohmann::json baseValue = {{"abc", childValue}};
  196 + // Always refresh base
  197 + {
  198 + auto baseCache = std::make_shared<APICache>(basePath, commands, std::chrono::seconds(0));
  199 + APICache cache(baseCache, "abc", std::chrono::seconds(0));
  200 + EXPECT_CALL(*handler,
  201 + GETJson("/api/" + getBridgeUsername() + basePath, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
  202 + .Times(2)
  203 + .WillRepeatedly(Return(baseValue));
  204 + EXPECT_EQ(childValue, cache.getValue());
  205 + EXPECT_EQ(childValue, cache.getValue());
  206 + Mock::VerifyAndClearExpectations(handler.get());
  207 + }
  208 + // Child duration > base duration
  209 + {
  210 + auto baseCache = std::make_shared<APICache>(basePath, commands, std::chrono::seconds(0));
  211 + APICache cache(baseCache, "abc", std::chrono::steady_clock::duration::max());
  212 + EXPECT_CALL(*handler,
  213 + GETJson("/api/" + getBridgeUsername() + basePath, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
  214 + .Times(1)
  215 + .WillRepeatedly(Return(baseValue));
  216 + EXPECT_EQ(childValue, cache.getValue());
  217 + EXPECT_EQ(childValue, cache.getValue());
  218 + Mock::VerifyAndClearExpectations(handler.get());
  219 + }
  220 + // Child duration < base duration
  221 + {
  222 + auto baseCache = std::make_shared<APICache>(basePath, commands, std::chrono::steady_clock::duration::max());
  223 + APICache cache(baseCache, "abc", std::chrono::seconds(0));
  224 + const nlohmann::json updateChildValue = { {"test", "updated"} };
  225 + InSequence s;
  226 + EXPECT_CALL(*handler,
  227 + GETJson("/api/" + getBridgeUsername() + basePath, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
  228 + .Times(1)
  229 + .WillRepeatedly(Return(baseValue));
  230 + EXPECT_CALL(*handler,
  231 + GETJson("/api/" + getBridgeUsername() + childPath, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
  232 + .Times(1)
  233 + .WillRepeatedly(Return(updateChildValue));
  234 + EXPECT_EQ(childValue, cache.getValue());
  235 + EXPECT_EQ(updateChildValue, cache.getValue());
  236 + // Base cache is updated
  237 + EXPECT_EQ(updateChildValue, baseCache->getValue()["abc"]);
  238 + Mock::VerifyAndClearExpectations(handler.get());
  239 + }
  240 + // Only refresh once
  241 + {
  242 + auto baseCache = std::make_shared<APICache>(basePath, commands, std::chrono::steady_clock::duration::max());
  243 + APICache cache(baseCache, "abc", std::chrono::steady_clock::duration::max());
  244 + EXPECT_CALL(*handler,
  245 + GETJson("/api/" + getBridgeUsername() + basePath, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
  246 + .Times(1)
  247 + .WillRepeatedly(Return(baseValue));
  248 + EXPECT_EQ(childValue, cache.getValue());
  249 + EXPECT_EQ(childValue, cache.getValue());
  250 + Mock::VerifyAndClearExpectations(handler.get());
  251 + }
  252 +}
  253 +
  254 +TEST(APICache, getRequestPath)
  255 +{
  256 + using namespace ::testing;
  257 + auto handler = std::make_shared<MockHttpHandler>();
  258 + HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
  259 +
  260 + // No base cache
  261 + {
  262 + std::string path = "/test/api";
  263 + APICache c(path, commands, std::chrono::seconds(0));
  264 + EXPECT_EQ(path, c.getRequestPath());
  265 + }
  266 + // With base cache
  267 + {
  268 + auto baseCache = std::make_shared<APICache>("/test", commands, std::chrono::seconds(0));
  269 + APICache c(baseCache, "api", std::chrono::seconds(0));
  270 + EXPECT_EQ("/test/api", c.getRequestPath());
  271 + }
  272 +}
... ...