1 // Copyright (C) 2014 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 <libaddressinput/address_data.h>
16 #include <libaddressinput/address_field.h>
17 #include <libaddressinput/address_problem.h>
18 #include <libaddressinput/address_validator.h>
19 #include <libaddressinput/callback.h>
20 #include <libaddressinput/downloader.h>
21 #include <libaddressinput/load_rules_delegate.h>
22 #include <libaddressinput/storage.h>
23 #include <libaddressinput/util/scoped_ptr.h>
29 #include <gtest/gtest.h>
31 #include "address_validator_test.h"
32 #include "fake_downloader.h"
33 #include "fake_storage.h"
34 #include "util/json.h"
35 #include "util/string_util.h"
38 namespace addressinput {
41 class AddressProblemEqualsString
42 : public std::binary_function<AddressProblem, std::string, bool> {
44 bool operator()(const AddressProblem& ap, const std::string& ep) const {
45 std::ostringstream oss;
47 return oss.str() == ep;
53 class ExampleAddressValidatorTest
54 : public testing::Test, public LoadRulesDelegate {
56 ExampleAddressValidatorTest() {}
58 virtual ~ExampleAddressValidatorTest() {}
60 // testing::Test overrides.
61 virtual void SetUp() OVERRIDE {
62 downloader_.reset(new FakeDownloader);
64 validator_ = BuildAddressValidatorForTesting(
65 FakeDownloader::kFakeDataUrl,
66 scoped_ptr<Downloader>(new FakeDownloader),
67 scoped_ptr<Storage>(new FakeStorage),
71 void OnDownloaded(bool success,
72 const std::string& url,
73 scoped_ptr<std::string> downloaded_data) {
75 EXPECT_FALSE(downloaded_data->empty());
76 data_ = downloaded_data.Pass();
80 scoped_ptr<Downloader> downloader_;
81 scoped_ptr<AddressValidator> validator_;
82 scoped_ptr<std::string> data_;
84 void TestCountryType(const scoped_ptr<Json>& json,
85 const std::string& country,
86 const std::string& type) {
87 scoped_ptr<Json> default_json;
89 std::string default_key = "examples/" + country + "/" + type + "/_default";
91 ASSERT_TRUE(json->GetJsonValueForKey(default_key, &default_json));
93 scoped_ptr<Json> fields_json;
94 ASSERT_TRUE(default_json->GetJsonValueForKey(
95 "fields", &fields_json));
98 for (int i = 1; i < 10; ++i) {
99 std::string street_key = "street";
100 street_key.append(1, static_cast<char>('0' + i));
101 std::string street_field;
102 if (!fields_json->GetStringValueForKey(street_key, &street_field))
105 address.address_lines.push_back(street_field);
107 address.country_code = country;
108 fields_json->GetStringValueForKey("state", &address.administrative_area);
109 fields_json->GetStringValueForKey("city", &address.locality);
110 fields_json->GetStringValueForKey("locality", &address.dependent_locality);
111 fields_json->GetStringValueForKey("sorting_code", &address.sorting_code);
112 fields_json->GetStringValueForKey("zip", &address.postal_code);
113 fields_json->GetStringValueForKey("organization", &address.organization);
114 fields_json->GetStringValueForKey("name", &address.recipient);
116 AddressProblems problems;
117 EXPECT_EQ(AddressValidator::SUCCESS, validator_->ValidateAddress(
118 address, AddressProblemFilter(), &problems));
120 std::string expected_problems_str;
121 std::vector<std::string> expected_problems;
122 if (default_json->GetStringValueForKey(
123 "problems", &expected_problems_str)) {
124 SplitString(expected_problems_str, '~', &expected_problems);
126 if (expected_problems.size() == problems.size()) {
127 EXPECT_TRUE(equal(problems.begin(), problems.end(),
128 expected_problems.begin(),
129 AddressProblemEqualsString()));
131 EXPECT_EQ(expected_problems.size(), problems.size());
132 for (AddressProblems::const_iterator it = problems.begin();
133 it != problems.end(); ++it) {
134 ADD_FAILURE() << "problem for " << default_key << ':' << *it;
139 void TestCountry(const std::string& country) {
140 validator_->LoadRules(country);
141 std::string key = "examples/" + country;
142 std::string url = FakeDownloader::kFakeDataUrl + key;
143 scoped_ptr<Json> json(Json::Build());
144 scoped_ptr<Json> json_country;
145 DownloadJsonValueForKey(key, &json, &json_country);
147 std::string types_str;
148 ASSERT_TRUE(json_country->GetStringValueForKey("types", &types_str));
149 std::vector<std::string> types;
150 SplitString(types_str, '~', &types);
152 for (std::vector<std::string>::const_iterator it = types.begin(),
155 TestCountryType(json, country, *it);
159 std::string DownloadString(const std::string& url) {
161 downloader_->Download(
163 BuildScopedPtrCallback(dynamic_cast<ExampleAddressValidatorTest*>(this),
164 &ExampleAddressValidatorTest::OnDownloaded));
168 void DownloadJson(const std::string& key, scoped_ptr<Json>* json) {
169 std::string url = FakeDownloader::kFakeDataUrl + key;
170 ASSERT_TRUE((*json)->ParseObject(DownloadString(url)));
173 void DownloadJsonValueForKey(const std::string& key,
174 scoped_ptr<Json>* json,
175 scoped_ptr<Json>* newjson) {
176 DownloadJson(key, json);
177 ASSERT_TRUE((*json)->GetJsonValueForKey(key, newjson));
181 // LoadRulesDelegate implementation.
182 virtual void OnAddressValidationRulesLoaded(const std::string& country_code,
184 AddressData address_data;
185 address_data.country_code = country_code;
186 AddressValidator::Status status =
187 validator_->ValidateAddress(address_data, AddressProblemFilter(), NULL);
188 EXPECT_TRUE(success);
189 EXPECT_EQ(AddressValidator::SUCCESS, status);
193 TEST_F(ExampleAddressValidatorTest, examples) {
194 scoped_ptr<Json> json(Json::Build());
195 scoped_ptr<Json> json_examples;
196 DownloadJsonValueForKey("examples", &json, &json_examples);
198 std::string countries_str;
199 ASSERT_TRUE(json_examples->GetStringValueForKey("countries", &countries_str));
200 std::vector<std::string> countries;
201 SplitString(countries_str, '~', &countries);
203 for (std::vector<std::string>::const_iterator it = countries.begin();
204 it != countries.end(); ++it) {