1 // Copyright (C) 2013 Google Inc.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "retriever.h"
17 #include <libaddressinput/callback.h>
18 #include <libaddressinput/null_storage.h>
19 #include <libaddressinput/storage.h>
20 #include <libaddressinput/util/basictypes.h>
21 #include <libaddressinput/util/scoped_ptr.h>
26 #include <gtest/gtest.h>
28 #include "mock_source.h"
29 #include "testdata_source.h"
31 #define CHECKSUM "dd63dafcbd4d5b28badfcaf86fb6fcdb"
32 #define DATA "{'foo': 'bar'}"
33 #define OLD_TIMESTAMP "0"
37 using i18n::addressinput::BuildCallback;
38 using i18n::addressinput::MockSource;
39 using i18n::addressinput::NullStorage;
40 using i18n::addressinput::Retriever;
41 using i18n::addressinput::scoped_ptr;
42 using i18n::addressinput::Storage;
43 using i18n::addressinput::TestdataSource;
45 const char kKey[] = "data/CA/AB--fr";
47 // Empty data that the source can return.
48 const char kEmptyData[] = "{}";
50 // The value of the data that the stale storage returns.
51 const char kStaleData[] = DATA;
53 // The actual data that the stale storage returns.
54 const char kStaleWrappedData[] = "timestamp=" OLD_TIMESTAMP "\n"
55 "checksum=" CHECKSUM "\n"
58 // Tests for Retriever object.
59 class RetrieverTest : public testing::Test {
62 : retriever_(new TestdataSource(false), new NullStorage),
66 data_ready_(BuildCallback(this, &RetrieverTest::OnDataReady)) {}
68 virtual ~RetrieverTest() {}
74 const scoped_ptr<const Retriever::Callback> data_ready_;
77 void OnDataReady(bool success,
78 const std::string& key,
79 const std::string& data) {
85 DISALLOW_COPY_AND_ASSIGN(RetrieverTest);
88 TEST_F(RetrieverTest, RetrieveData) {
89 retriever_.Retrieve(kKey, *data_ready_);
91 EXPECT_TRUE(success_);
92 EXPECT_EQ(kKey, key_);
93 EXPECT_FALSE(data_.empty());
94 EXPECT_NE(kEmptyData, data_);
97 TEST_F(RetrieverTest, ReadDataFromStorage) {
98 retriever_.Retrieve(kKey, *data_ready_);
99 retriever_.Retrieve(kKey, *data_ready_);
101 EXPECT_TRUE(success_);
102 EXPECT_EQ(kKey, key_);
103 EXPECT_FALSE(data_.empty());
104 EXPECT_NE(kEmptyData, data_);
107 TEST_F(RetrieverTest, MissingKeyReturnsEmptyData) {
108 static const char kMissingKey[] = "junk";
110 retriever_.Retrieve(kMissingKey, *data_ready_);
112 EXPECT_TRUE(success_);
113 EXPECT_EQ(kMissingKey, key_);
114 EXPECT_EQ(kEmptyData, data_);
117 TEST_F(RetrieverTest, FaultySource) {
118 // An empty MockSource will fail for any request.
119 Retriever bad_retriever(new MockSource, new NullStorage);
121 bad_retriever.Retrieve(kKey, *data_ready_);
123 EXPECT_FALSE(success_);
124 EXPECT_EQ(kKey, key_);
125 EXPECT_TRUE(data_.empty());
128 // The storage that always returns stale data.
129 class StaleStorage : public Storage {
131 StaleStorage() : data_updated_(false) {}
132 virtual ~StaleStorage() {}
134 // Storage implementation.
135 virtual void Get(const std::string& key, const Callback& data_ready) const {
136 data_ready(true, key, new std::string(kStaleWrappedData));
139 virtual void Put(const std::string& key, std::string* value) {
140 ASSERT_TRUE(value != NULL);
141 data_updated_ = true;
148 DISALLOW_COPY_AND_ASSIGN(StaleStorage);
151 TEST_F(RetrieverTest, UseStaleDataWhenSourceFails) {
152 // Owned by |resilient_retriver|.
153 StaleStorage* stale_storage = new StaleStorage;
154 // An empty MockSource will fail for any request.
155 Retriever resilient_retriever(new MockSource, stale_storage);
157 resilient_retriever.Retrieve(kKey, *data_ready_);
159 EXPECT_TRUE(success_);
160 EXPECT_EQ(kKey, key_);
161 EXPECT_EQ(kStaleData, data_);
162 EXPECT_FALSE(stale_storage->data_updated_);
165 TEST_F(RetrieverTest, DoNotUseStaleDataWhenSourceSucceeds) {
166 // Owned by |resilient_retriver|.
167 StaleStorage* stale_storage = new StaleStorage;
168 Retriever resilient_retriever(new TestdataSource(false), stale_storage);
170 resilient_retriever.Retrieve(kKey, *data_ready_);
172 EXPECT_TRUE(success_);
173 EXPECT_EQ(kKey, key_);
174 EXPECT_FALSE(data_.empty());
175 EXPECT_NE(kEmptyData, data_);
176 EXPECT_NE(kStaleData, data_);
177 EXPECT_TRUE(stale_storage->data_updated_);