1 // 2 // Copyright (C) 2016 Google, Inc. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #include <base.h> 18 #include <base/logging.h> 19 #include <rapidjson/document.h> 20 #include <rapidjson/writer.h> 21 #include <rapidjson/stringbuffer.h> 22 #include "test_facade.h" 23 #include <tuple> 24 #include <utils/command_receiver.h> 25 #include <utils/common_utils.h> 26 27 std::tuple<bool, int> TestFacade::TestBoolTrueReturn() { 28 return std::make_tuple(true, sl4n_error_codes::kPassInt); 29 } 30 31 std::tuple<bool, int> TestFacade::TestBoolFalseReturn() { 32 return std::make_tuple(false, sl4n_error_codes::kPassInt); 33 } 34 35 std::tuple<bool, int> TestFacade::TestErrorCodeFail() { 36 return std::make_tuple(true, sl4n_error_codes::kFailInt); 37 } 38 39 std::tuple<int, int> TestFacade::TestNullReturn() { 40 return std::make_tuple(NULL, sl4n_error_codes::kPassInt); 41 } 42 43 std::tuple<std::string, int> TestFacade::TestStringEmptyReturn() { 44 return std::make_tuple("", sl4n_error_codes::kPassInt); 45 } 46 47 std::tuple<std::string, int> TestFacade::TestStringMaxReturn( 48 std::string max_string) { 49 50 return std::make_tuple(max_string, sl4n_error_codes::kPassInt); 51 } 52 53 std::tuple<bool, int> TestFacade::TestSpecificParamNaming( 54 std::string test_string, int test_int) { 55 56 return std::make_tuple(true, sl4n_error_codes::kPassInt); 57 } 58 59 ////////////////// 60 // wrappers 61 ////////////////// 62 63 static TestFacade facade; // triggers registration with CommandReceiver 64 65 void test_bool_true_return_wrapper(rapidjson::Document &doc) { 66 int expected_param_size = 0; 67 if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) { 68 return; 69 } 70 bool result; 71 int error_code; 72 std::tie(result, error_code) = facade.TestBoolTrueReturn(); 73 if (error_code == sl4n_error_codes::kFailInt) { 74 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 75 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 76 } else { 77 doc.AddMember(sl4n::kResultStr, result, doc.GetAllocator()); 78 doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator()); 79 } 80 } 81 82 void test_bool_false_return_wrapper(rapidjson::Document &doc) { 83 int expected_param_size = 0; 84 if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) { 85 return; 86 } 87 int result; 88 int error_code; 89 std::tie(result, error_code) = facade.TestBoolFalseReturn(); 90 if (error_code == sl4n_error_codes::kFailInt) { 91 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 92 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 93 } else { 94 doc.AddMember(sl4n::kResultStr, result, doc.GetAllocator()); 95 doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator()); 96 } 97 } 98 99 void test_null_return_wrapper(rapidjson::Document &doc) { 100 int expected_param_size = 0; 101 if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) { 102 return; 103 } 104 int result; 105 int error_code; 106 std::tie(result, error_code) = facade.TestNullReturn(); 107 if (error_code == sl4n_error_codes::kFailInt) { 108 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 109 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 110 } else { 111 doc.AddMember(sl4n::kResultStr, result, doc.GetAllocator()); 112 doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator()); 113 } 114 } 115 116 void test_string_empty_return_wrapper(rapidjson::Document &doc) { 117 int expected_param_size = 0; 118 if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) { 119 return; 120 } 121 std::string result; 122 int error_code; 123 std::tie(result, error_code) = facade.TestStringEmptyReturn(); 124 if (error_code == sl4n_error_codes::kFailInt) { 125 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 126 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 127 } else { 128 rapidjson::Value tmp; 129 tmp.SetString(result.c_str(), doc.GetAllocator()); 130 doc.AddMember(sl4n::kResultStr, tmp, doc.GetAllocator()); 131 doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator()); 132 } 133 } 134 135 void test_string_max_return_wrapper(rapidjson::Document &doc) { 136 int expected_param_size = 1; 137 if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) { 138 return; 139 } 140 std::string max_string; 141 if (!doc[sl4n::kParamsStr][0].IsString()) { 142 LOG(ERROR) << sl4n::kTagStr << ": Expected String input for name"; 143 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 144 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 145 return; 146 } else { 147 max_string = doc[sl4n::kParamsStr][0].GetString(); 148 } 149 std::string result; 150 int error_code; 151 std::tie(result, error_code) = facade.TestStringMaxReturn(max_string); 152 if (error_code == sl4n_error_codes::kFailInt) { 153 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 154 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 155 } else { 156 rapidjson::Value tmp; 157 tmp.SetString(result.c_str(), doc.GetAllocator()); 158 doc.AddMember(sl4n::kResultStr, tmp, doc.GetAllocator()); 159 doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator()); 160 } 161 } 162 163 void test_specific_param_naming_wrapper(rapidjson::Document &doc) { 164 int expected_param_size = 1; 165 if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) { 166 return; 167 } 168 std::string string_test; 169 int int_test; 170 std::string string_member = "string_test"; 171 std::string int_member = "int_test"; 172 if (!doc[sl4n::kParamsStr][0][0].HasMember(string_member.c_str())) { 173 LOG(ERROR) << sl4n::kTagStr << ": Expected member " << string_member; 174 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 175 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 176 return; 177 } else { 178 if (!doc[sl4n::kParamsStr][0][0][string_member.c_str()].IsString()) { 179 LOG(ERROR) << sl4n::kTagStr << ": Expected String input for " 180 << string_member; 181 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 182 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 183 return; 184 } 185 string_test = doc[sl4n::kParamsStr][0][0][string_member.c_str()].GetString(); 186 } 187 if (!doc[sl4n::kParamsStr][0][0].HasMember(int_member.c_str())) { 188 LOG(ERROR) << sl4n::kTagStr << ": Expected member " << int_member; 189 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 190 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 191 return; 192 } else { 193 if (!doc[sl4n::kParamsStr][0][0][int_member.c_str()].IsInt()) { 194 LOG(ERROR) << sl4n::kTagStr << ": Expected Int input for " 195 << int_member; 196 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 197 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 198 return; 199 } 200 int_test = doc[sl4n::kParamsStr][0][0][int_member.c_str()].GetInt(); 201 } 202 bool result; 203 int error_code; 204 std::tie(result, error_code) = facade.TestSpecificParamNaming( 205 string_test, int_test); 206 if (error_code == sl4n_error_codes::kFailInt) { 207 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 208 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 209 } else { 210 doc.AddMember(sl4n::kResultStr, result, doc.GetAllocator()); 211 doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator()); 212 } 213 } 214 215 //////////////// 216 // constructor 217 //////////////// 218 219 TestFacade::TestFacade() { 220 221 CommandReceiver::RegisterCommand("TestBoolTrueReturn", 222 &test_bool_true_return_wrapper); 223 CommandReceiver::RegisterCommand("TestBoolFalseReturn", 224 &test_bool_false_return_wrapper); 225 CommandReceiver::RegisterCommand("TestNullReturn", 226 &test_null_return_wrapper); 227 CommandReceiver::RegisterCommand("TestStringEmptyReturn", 228 &test_string_empty_return_wrapper); 229 CommandReceiver::RegisterCommand("TestStringMaxReturn", 230 &test_string_max_return_wrapper); 231 CommandReceiver::RegisterCommand("TestSpecificParamNaming", 232 &test_specific_param_naming_wrapper); 233 } 234 235