summaryrefslogtreecommitdiff
path: root/sl4n/facades/test/test_facade.cpp
blob: 4fd41add62c0c2fdc130def0539a12a1b15f219e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//
//  Copyright (C) 2016 Google, Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at:
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#include <base.h>
#include <base/logging.h>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include "test_facade.h"
#include <tuple>
#include <utils/command_receiver.h>
#include <utils/common_utils.h>

std::tuple<bool, int> TestFacade::TestBoolTrueReturn() {
  return std::make_tuple(true, sl4n_error_codes::kPassInt);
}

std::tuple<bool, int> TestFacade::TestBoolFalseReturn() {
  return std::make_tuple(false, sl4n_error_codes::kPassInt);
}

std::tuple<bool, int> TestFacade::TestErrorCodeFail() {
  return std::make_tuple(true, sl4n_error_codes::kFailInt);
}

std::tuple<int, int> TestFacade::TestNullReturn() {
  return std::make_tuple(NULL, sl4n_error_codes::kPassInt);
}

std::tuple<std::string, int> TestFacade::TestStringEmptyReturn() {
  return std::make_tuple("", sl4n_error_codes::kPassInt);
}

std::tuple<std::string, int> TestFacade::TestStringMaxReturn(
  std::string max_string) {

  return std::make_tuple(max_string, sl4n_error_codes::kPassInt);
}

std::tuple<bool, int> TestFacade::TestSpecificParamNaming(
  std::string test_string, int test_int) {

  return std::make_tuple(true, sl4n_error_codes::kPassInt);
}

//////////////////
// wrappers
//////////////////

static TestFacade facade;  // triggers registration with CommandReceiver

void test_bool_true_return_wrapper(rapidjson::Document &doc) {
  int expected_param_size = 0;
  if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) {
    return;
  }
  bool result;
  int error_code;
  std::tie(result, error_code) = facade.TestBoolTrueReturn();
  if (error_code == sl4n_error_codes::kFailInt) {
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
  } else {
    doc.AddMember(sl4n::kResultStr, result, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator());
  }
}

void test_bool_false_return_wrapper(rapidjson::Document &doc) {
  int expected_param_size = 0;
  if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) {
    return;
  }
  int result;
  int error_code;
  std::tie(result, error_code) = facade.TestBoolFalseReturn();
  if (error_code == sl4n_error_codes::kFailInt) {
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
  } else {
    doc.AddMember(sl4n::kResultStr, result, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator());
  }
}

void test_null_return_wrapper(rapidjson::Document &doc) {
  int expected_param_size = 0;
  if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) {
    return;
  }
  int result;
  int error_code;
  std::tie(result, error_code) = facade.TestNullReturn();
  if (error_code == sl4n_error_codes::kFailInt) {
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
  } else {
    doc.AddMember(sl4n::kResultStr, result, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator());
  }
}

void test_string_empty_return_wrapper(rapidjson::Document &doc) {
  int expected_param_size = 0;
  if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) {
    return;
  }
  std::string result;
  int error_code;
  std::tie(result, error_code) = facade.TestStringEmptyReturn();
  if (error_code == sl4n_error_codes::kFailInt) {
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
  } else {
    rapidjson::Value tmp;
    tmp.SetString(result.c_str(), doc.GetAllocator());
    doc.AddMember(sl4n::kResultStr, tmp, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator());
  }
}

void test_string_max_return_wrapper(rapidjson::Document &doc) {
  int expected_param_size = 1;
  if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) {
    return;
  }
  std::string max_string;
  if (!doc[sl4n::kParamsStr][0].IsString()) {
    LOG(ERROR) << sl4n::kTagStr << ": Expected String input for name";
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
    return;
  } else {
    max_string = doc[sl4n::kParamsStr][0].GetString();
  }
  std::string result;
  int error_code;
  std::tie(result, error_code) = facade.TestStringMaxReturn(max_string);
  if (error_code == sl4n_error_codes::kFailInt) {
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
  } else {
    rapidjson::Value tmp;
    tmp.SetString(result.c_str(), doc.GetAllocator());
    doc.AddMember(sl4n::kResultStr, tmp, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator());
  }
}

void test_specific_param_naming_wrapper(rapidjson::Document &doc) {
  int expected_param_size = 1;
  if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) {
    return;
  }
  std::string string_test;
  int int_test;
  std::string string_member = "string_test";
  std::string int_member = "int_test";
  if (!doc[sl4n::kParamsStr][0][0].HasMember(string_member.c_str())) {
    LOG(ERROR) << sl4n::kTagStr << ": Expected member " << string_member;
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
    return;
  } else {
    if (!doc[sl4n::kParamsStr][0][0][string_member.c_str()].IsString()) {
      LOG(ERROR) << sl4n::kTagStr << ": Expected String input for "
        << string_member;
      doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
      doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
      return;
    }
    string_test = doc[sl4n::kParamsStr][0][0][string_member.c_str()].GetString();
  }
  if (!doc[sl4n::kParamsStr][0][0].HasMember(int_member.c_str())) {
    LOG(ERROR) << sl4n::kTagStr << ": Expected member " << int_member;
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
    return;
  } else {
    if (!doc[sl4n::kParamsStr][0][0][int_member.c_str()].IsInt()) {
      LOG(ERROR) << sl4n::kTagStr << ": Expected Int input for "
        << int_member;
      doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
      doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
      return;
    }
    int_test = doc[sl4n::kParamsStr][0][0][int_member.c_str()].GetInt();
  }
  bool result;
  int error_code;
  std::tie(result, error_code) = facade.TestSpecificParamNaming(
    string_test, int_test);
  if (error_code == sl4n_error_codes::kFailInt) {
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
  } else {
    doc.AddMember(sl4n::kResultStr, result, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator());
  }
}

////////////////
// constructor
////////////////

TestFacade::TestFacade() {

  CommandReceiver::RegisterCommand("TestBoolTrueReturn",
    &test_bool_true_return_wrapper);
  CommandReceiver::RegisterCommand("TestBoolFalseReturn",
    &test_bool_false_return_wrapper);
  CommandReceiver::RegisterCommand("TestNullReturn",
    &test_null_return_wrapper);
  CommandReceiver::RegisterCommand("TestStringEmptyReturn",
    &test_string_empty_return_wrapper);
  CommandReceiver::RegisterCommand("TestStringMaxReturn",
    &test_string_max_return_wrapper);
  CommandReceiver::RegisterCommand("TestSpecificParamNaming",
    &test_specific_param_naming_wrapper);
}