1// Copyright (C) 2014 Google Inc. 2// 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 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 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. 14 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> 24 25#include <algorithm> 26#include <sstream> 27#include <string> 28 29#include <gtest/gtest.h> 30 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" 36 37namespace i18n { 38namespace addressinput { 39 40namespace { 41class AddressProblemEqualsString 42 : public std::binary_function<AddressProblem, std::string, bool> { 43 public: 44 bool operator()(const AddressProblem& ap, const std::string& ep) const { 45 std::ostringstream oss; 46 oss << ap; 47 return oss.str() == ep; 48 } 49}; 50 51} // namespace 52 53class ExampleAddressValidatorTest 54 : public testing::Test, public LoadRulesDelegate { 55 public: 56 ExampleAddressValidatorTest() {} 57 58 virtual ~ExampleAddressValidatorTest() {} 59 60 // testing::Test overrides. 61 virtual void SetUp() OVERRIDE { 62 downloader_.reset(new FakeDownloader); 63 64 validator_ = BuildAddressValidatorForTesting( 65 FakeDownloader::kFakeDataUrl, 66 scoped_ptr<Downloader>(new FakeDownloader), 67 scoped_ptr<Storage>(new FakeStorage), 68 this); 69 } 70 71 void OnDownloaded(bool success, 72 const std::string& url, 73 scoped_ptr<std::string> downloaded_data) { 74 EXPECT_TRUE(success); 75 EXPECT_FALSE(downloaded_data->empty()); 76 data_ = downloaded_data.Pass(); 77 } 78 79 protected: 80 scoped_ptr<Downloader> downloader_; 81 scoped_ptr<AddressValidator> validator_; 82 scoped_ptr<std::string> data_; 83 84 void TestCountryType(const scoped_ptr<Json>& json, 85 const std::string& country, 86 const std::string& type) { 87 scoped_ptr<Json> default_json; 88 89 std::string default_key = "examples/" + country + "/" + type + "/_default"; 90 91 ASSERT_TRUE(json->GetJsonValueForKey(default_key, &default_json)); 92 93 scoped_ptr<Json> fields_json; 94 ASSERT_TRUE(default_json->GetJsonValueForKey( 95 "fields", &fields_json)); 96 97 AddressData address; 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)) 103 break; 104 105 address.address_lines.push_back(street_field); 106 } 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); 115 116 AddressProblems problems; 117 EXPECT_EQ(AddressValidator::SUCCESS, validator_->ValidateAddress( 118 address, AddressProblemFilter(), &problems)); 119 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); 125 } 126 if (expected_problems.size() == problems.size()) { 127 EXPECT_TRUE(equal(problems.begin(), problems.end(), 128 expected_problems.begin(), 129 AddressProblemEqualsString())); 130 } else { 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; 135 } 136 } 137 } 138 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); 146 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); 151 152 for (std::vector<std::string>::const_iterator it = types.begin(), 153 itend = types.end(); 154 it != itend; ++it) { 155 TestCountryType(json, country, *it); 156 } 157 } 158 159 std::string DownloadString(const std::string& url) { 160 data_.reset(); 161 downloader_->Download( 162 url, 163 BuildScopedPtrCallback(dynamic_cast<ExampleAddressValidatorTest*>(this), 164 &ExampleAddressValidatorTest::OnDownloaded)); 165 return *data_; 166 } 167 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))); 171 } 172 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)); 178 } 179 180 private: 181 // LoadRulesDelegate implementation. 182 virtual void OnAddressValidationRulesLoaded(const std::string& country_code, 183 bool success) { 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); 190 } 191}; 192 193TEST_F(ExampleAddressValidatorTest, examples) { 194 scoped_ptr<Json> json(Json::Build()); 195 scoped_ptr<Json> json_examples; 196 DownloadJsonValueForKey("examples", &json, &json_examples); 197 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); 202 203 for (std::vector<std::string>::const_iterator it = countries.begin(); 204 it != countries.end(); ++it) { 205 TestCountry(*it); 206 } 207} 208 209} // addressinput 210} // i18n 211