-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.cpp
1770 lines (1558 loc) · 58.6 KB
/
tester.cpp
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Sample unit tests for BasicServer
*/
#include <algorithm>
#include <exception>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <cpprest/http_client.h>
#include <cpprest/json.h>
#include <pplx/pplxtasks.h>
#include <UnitTest++/UnitTest++.h>
#include "ServerUtils.h"
#include "TableCache.h"
#include "make_unique.h"
#include "azure_keys.h"
using std::cerr;
using std::cout;
using std::endl;
using std::make_pair;
using std::pair;
using std::string;
using std::vector;
using web::http::http_headers;
using web::http::http_request;
using web::http::http_response;
using web::http::method;
using web::http::methods;
using web::http::status_code;
using web::http::status_codes;
using web::http::uri_builder;
using web::http::client::http_client;
using web::json::object;
using web::json::value;
using azure::storage::storage_exception;
const string create_table_op {"CreateTableAdmin"};
const string delete_table_op {"DeleteTableAdmin"};
const string read_entity_admin {"ReadEntityAdmin"};
const string update_entity_admin {"UpdateEntityAdmin"};
const string delete_entity_admin {"DeleteEntityAdmin"};
const string read_entity_auth {"ReadEntityAuth"};
const string update_entity_auth {"UpdateEntityAuth"};
const string get_read_token_op {"GetReadToken"};
const string get_update_token_op {"GetUpdateToken"};
const string get_update_data_op {"GetUpdateData"};
const string sign_on {"SignOn"};
const string sign_off {"SignOff"};
const string add_friend {"AddFriend"};
const string unfriend {"UnFriend"};
const string update_status {"UpdateStatus"};
const string read_friend_list {"ReadFriendList"};
// The two optional operations from Assignment 1
const string add_property_admin {"AddPropertyAdmin"};
const string update_property_admin {"UpdatePropertyAdmin"};
/*
Make an HTTP request, returning the status code and any JSON value in the body
method: member of web::http::methods
uri_string: uri of the request
req_body: [optional] a json::value to be passed as the message body
If the response has a body with Content-Type: application/json,
the second part of the result is the json::value of the body.
If the response does not have that Content-Type, the second part
of the result is simply json::value {}.
You're welcome to read this code but bear in mind: It's the single
trickiest part of the sample code. You can just call it without
attending to its internals, if you prefer.
*/
// Version with explicit third argument
pair<status_code,value> do_request (const method& http_method, const string& uri_string, const value& req_body) {
http_request request {http_method};
if (req_body != value {}) {
http_headers& headers (request.headers());
headers.add("Content-Type", "application/json");
request.set_body(req_body);
}
status_code code;
value resp_body;
http_client client {uri_string};
client.request (request)
.then([&code](http_response response)
{
code = response.status_code();
const http_headers& headers {response.headers()};
auto content_type (headers.find("Content-Type"));
if (content_type == headers.end() ||
content_type->second != "application/json")
return pplx::task<value> ([] { return value::object ();});
else
return response.extract_json();
})
.then([&resp_body](value v) -> void
{
resp_body = v;
return;
})
.wait();
return make_pair(code, resp_body);
}
/*pair<status_code,value> do_request (const method& http_method, const string& uri_string, const value& req_body) {
http_request request {http_method};
if (req_body != value {}) {
http_headers& headers (request.headers());
headers.add("Content-Type", "application/json");
request.set_body(req_body);
}
status_code code;
value resp_body;
http_client client {uri_string};
client.request (request)
.then([&code](http_response response)
{
code = response.status_code();
const http_headers& headers {response.headers()};
auto content_type (headers.find("Content-Type"));
if (content_type == headers.end() ||
content_type->second != "application/json")
return pplx::task<value> ([] { return value {};});
else
return response.extract_json();
})
.then([&resp_body](value v) -> void
{
resp_body = v;
return;
})
.wait();
return make_pair(code, resp_body);
}*/
// Version that defaults third argument
pair<status_code,value> do_request (const method& http_method, const string& uri_string) {
return do_request (http_method, uri_string, value {});
}
/*
Utility to create a table
addr: Prefix of the URI (protocol, address, and port)
table: Table in which to insert the entity
*/
int create_table (const string& addr, const string& table) {
pair<status_code,value> result {do_request (methods::POST, addr + create_table_op + "/" + table)};
return result.first;
}
/*
Utility to compare two JSON objects
This is an internal routine---you probably want to call compare_json_values().
*/
bool compare_json_objects (const object& expected_o, const object& actual_o) {
CHECK_EQUAL (expected_o.size (), actual_o.size());
if (expected_o.size() != actual_o.size())
return false;
bool result {true};
for (auto& exp_prop : expected_o) {
object::const_iterator act_prop {actual_o.find (exp_prop.first)};
CHECK (actual_o.end () != act_prop);
if (actual_o.end () == act_prop)
result = false;
else {
CHECK_EQUAL (exp_prop.second, act_prop->second);
if (exp_prop.second != act_prop->second)
result = false;
}
}
return result;
}
/*
Utility to compare two JSON objects represented as values
expected: json::value that was expected---must be an object
actual: json::value that was actually returned---must be an object
*/
bool compare_json_values (const value& expected, const value& actual) {
assert (expected.is_object());
assert (actual.is_object());
object expected_o {expected.as_object()};
object actual_o {actual.as_object()};
return compare_json_objects (expected_o, actual_o);
}
/*
Utility to compre expected JSON array with actual
exp: vector of objects, sorted by Partition/Row property
The routine will throw if exp is not sorted.
actual: JSON array value of JSON objects
The routine will throw if actual is not an array or if
one or more values is not an object.
Note the deliberate asymmetry of the how the two arguments are handled:
exp is set up by the test, so we *require* it to be of the correct
type (vector<object>) and to be sorted and throw if it is not.
actual is returned by the database and may not be an array, may not
be values, and may not be sorted by partition/row, so we have
to check whether it has those characteristics and convert it
to a type comparable to exp.
*/
bool compare_json_arrays(const vector<object>& exp, const value& actual) {
/*
Check that expected argument really is sorted and
that every value has Partion and Row properties.
This is a precondition of this routine, so we throw
if it is not met.
*/
auto comp = [] (const object& a, const object& b) -> bool {
return a.at("Partition").as_string() < b.at("Partition").as_string()
||
(a.at("Partition").as_string() == b.at("Partition").as_string() &&
a.at("Row").as_string() < b.at("Row").as_string());
};
if ( ! std::is_sorted(exp.begin(),
exp.end(),
comp))
throw std::exception();
// Check that actual is an array
CHECK(actual.is_array());
if ( ! actual.is_array())
return false;
web::json::array act_arr {actual.as_array()};
// Check that the two arrays have same size
CHECK_EQUAL(exp.size(), act_arr.size());
if (exp.size() != act_arr.size())
return false;
// Check that all values in actual are objects
bool all_objs {std::all_of(act_arr.begin(),
act_arr.end(),
[] (const value& v) { return v.is_object(); })};
CHECK(all_objs);
if ( ! all_objs)
return false;
// Convert all values in actual to objects
vector<object> act_o {};
auto make_object = [] (const value& v) -> object {
return v.as_object();
};
std::transform (act_arr.begin(), act_arr.end(), std::back_inserter(act_o), make_object);
/*
Ensure that the actual argument is sorted.
Unlike exp, we cannot assume this argument is sorted,
so we sort it.
*/
std::sort(act_o.begin(), act_o.end(), comp);
// Compare the sorted arrays
bool eq {std::equal(exp.begin(), exp.end(), act_o.begin(), &compare_json_objects)};
CHECK (eq);
return eq;
}
/*
Utility to create JSON object value from vector of properties
*/
value build_json_object (const vector<pair<string,string>>& properties) {
value result {value::object ()};
for (auto& prop : properties) {
result[prop.first] = value::string(prop.second);
}
return result;
}
/*
Utility to delete a table
addr: Prefix of the URI (protocol, address, and port)
table: Table in which to insert the entity
*/
int delete_table (const string& addr, const string& table) {
// SIGH--Note that REST SDK uses "methods::DEL", not "methods::DELETE"
pair<status_code,value> result {
do_request (methods::DEL,
addr + delete_table_op + "/" + table)};
return result.first;
}
/*
Utility to put an entity with a single property
addr: Prefix of the URI (protocol, address, and port)
table: Table in which to insert the entity
partition: Partition of the entity
row: Row of the entity
prop: Name of the property
pstring: Value of the property, as a string
*/
int put_entity(const string& addr, const string& table, const string& partition, const string& row, const string& prop, const string& pstring) {
pair<status_code,value> result {
do_request (methods::PUT,
addr + update_entity_admin + "/" + table + "/" + partition + "/" + row,
value::object (vector<pair<string,value>>
{make_pair(prop, value::string(pstring))}))};
return result.first;
}
/*
Utility to put an entity with multiple properties
addr: Prefix of the URI (protocol, address, and port)
table: Table in which to insert the entity
partition: Partition of the entity
row: Row of the entity
props: vector of string/value pairs representing the properties
*/
int put_entity(const string& addr, const string& table, const string& partition, const string& row,
const vector<pair<string,value>>& props) {
pair<status_code,value> result {
do_request (methods::PUT,
addr + update_entity_admin + "/" + table + "/" + partition + "/" + row,
value::object (props))};
return result.first;
}
/*
Utility to delete an entity
addr: Prefix of the URI (protocol, address, and port)
table: Table in which to insert the entity
partition: Partition of the entity
row: Row of the entity
*/
int delete_entity (const string& addr, const string& table, const string& partition, const string& row) {
// SIGH--Note that REST SDK uses "methods::DEL", not "methods::DELETE"
pair<status_code,value> result {
do_request (methods::DEL,
addr + delete_entity_admin + "/" + table + "/" + partition + "/" + row)};
return result.first;
}
/*
Utility to get a token good for updating a specific entry
from a specific table for one day.
*/
pair<status_code,string> get_update_token(const string& addr, const string& userid, const string& password) {
value pwd {build_json_object (vector<pair<string,string>> {make_pair("Password", password)})};
pair<status_code,value> result {do_request (methods::GET,
addr +
get_update_token_op + "/" +
userid,
pwd
)
};
cerr << "token " << result.second << endl;
if (result.first != status_codes::OK) {
return make_pair (result.first, "");
}
else {
string token {result.second.as_string()};
return make_pair (result.first, token);
}
}
pair<status_code,string> get_read_token(const string& addr, const string& userid, const string& password) {
value pwd {build_json_object (vector<pair<string,string>> {make_pair("Password", password)})};
pair<status_code,value> result {do_request (methods::GET,
addr +
get_read_token_op + "/" +
userid,
pwd
)
};
cerr << "token " << result.second << endl;
if (result.first != status_codes::OK) {
return make_pair (result.first, "");
}
else {
string token {result.second.as_string()};
return make_pair (result.first, token);
}
}
/*
A sample fixture that ensures TestTable exists, and
at least has the entity Franklin,Aretha/USA
with the property "Song": "RESPECT".
The entity is deleted when the fixture shuts down
but the table is left. See the comments in the code
for the reason for this design.
*/
class BasicFixture {
public:
static constexpr const char* addr {"http://localhost:34568/"};
static constexpr const char* table {"TestTable"};
static constexpr const char* partition {"USA"};
static constexpr const char* row {"Franklin,Aretha"};
static constexpr const char* property {"Song"};
static constexpr const char* prop_val {"RESPECT"};
public:
BasicFixture() {
int make_result {create_table(addr, table)};
cerr << "create result " << make_result << endl;
if (make_result != status_codes::Created && make_result != status_codes::Accepted) {
throw std::exception();
}
int put_result {put_entity (addr, table, partition, row, property, prop_val)};
cerr << "put result " << put_result << endl;
if (put_result != status_codes::OK) {
throw std::exception();
}
}
~BasicFixture() {
int del_ent_result {delete_entity (addr, table, partition, row)};
if (del_ent_result != status_codes::OK) {
throw std::exception();
}
/*
In traditional unit testing, we might delete the table after every test.
However, in cloud NoSQL environments (Azure Tables, Amazon DynamoDB)
creating and deleting tables are rate-limited operations. So we
leave the table after each test but delete all its entities.
*/
cout << "Skipping table delete" << endl;
/*
int del_result {delete_table(addr, table)};
cerr << "delete result " << del_result << endl;
if (del_result != status_codes::OK) {
throw std::exception();
}
*/
}
};
SUITE(GET) {
/*
A test of GET all table entries
Demonstrates use of new compare_json_arrays() function.
*/
TEST_FIXTURE(BasicFixture, GetAll) {
cout << ">> GetAll (assign2) Test" << endl;
string partition {"CAN"};
string row {"Katherines,The"};
string property {"Home"};
string prop_val {"Vancouver"};
int put_result {put_entity (BasicFixture::addr, BasicFixture::table, partition, row, property, prop_val)};
cerr << "put result " << put_result << endl;
assert (put_result == status_codes::OK);
pair<status_code,value> result {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin + "/"
+ string(BasicFixture::table))
};
CHECK_EQUAL(status_codes::OK, result.first);
value obj1 {
value::object(vector<pair<string,value>> {
make_pair(string("Partition"), value::string(partition)),
make_pair(string("Row"), value::string(row)),
make_pair(property, value::string(prop_val))
})
};
value obj2 {
value::object(vector<pair<string,value>> {
make_pair(string("Partition"), value::string(BasicFixture::partition)),
make_pair(string("Row"), value::string(BasicFixture::row)),
make_pair(string(BasicFixture::property), value::string(BasicFixture::prop_val))
})
};
vector<object> exp {
obj1.as_object(),
obj2.as_object()
};
compare_json_arrays(exp, result.second);
CHECK_EQUAL(status_codes::OK, delete_entity (BasicFixture::addr, BasicFixture::table, partition, row));
}
/*
A test of GET of a single entity
*/
TEST_FIXTURE(BasicFixture, GetSingle) {
cout << ">> GetSingle test" << endl;
pair<status_code,value> result {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin + "/"
+ BasicFixture::table + "/"
+ BasicFixture::partition + "/"
+ BasicFixture::row)
};
//Formatting changed to fit
CHECK_EQUAL(string("{\"")
/* + "Partition" + "\":\""
+ BasicFixture::partition + "\",\""
+ "Row" + "\":\""
+ BasicFixture::row + "\",\"" */
+ BasicFixture::property + "\":\""
+ BasicFixture::prop_val + "\""
+ "}",
result.second.serialize());
CHECK_EQUAL(status_codes::OK, result.first);
}
/*
A test of GET of an entity with table name, partition name, with row name as '*'
*/
TEST_FIXTURE(BasicFixture, GetPartition) {
cout << ">> GetPartition test" << endl;
string partition {"USA"};
string row {"John,Doe"};
string property {"Song"};
string prop_val {"DISRESPECT"};
int put_result {put_entity (BasicFixture::addr, BasicFixture::table, partition, row, property, prop_val)};
cerr << "put result " << put_result << endl;
assert (put_result == status_codes::OK);
string partition2 {"CAN"};
string row2 {"Katherines,The"};
string property2 {"Home"};
string prop_val2 {"Vancouver"};
int put_result2 {put_entity (BasicFixture::addr, BasicFixture::table, partition2, row2, property2, prop_val2)};
cerr << "put result " << put_result2 << endl;
assert (put_result2 == status_codes::OK);
pair<status_code, value> result {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin + "/"
+ BasicFixture::table + "/"
+ BasicFixture::partition + "/"
+ "*")
};
CHECK_EQUAL(string("[{\"")
+ "Partition" + "\":\""
+ BasicFixture::partition + "\",\""
+ "Row" + "\":\""
+ BasicFixture::row + "\",\""
+ BasicFixture::property + "\":\""
+ BasicFixture::prop_val + "\""
+ "},{\""
+ "Partition" + "\":\""
+ BasicFixture::partition + "\",\""
+ "Row" + "\":\""
+ row + "\",\""
+ property + "\":\""
+ prop_val + "\""
+ "}]",
result.second.serialize());
//CHECK_EQUAL(2, result.second.as_array().size());
CHECK_EQUAL(status_codes::OK, result.first);
CHECK_EQUAL(status_codes::OK, delete_entity (BasicFixture::addr, BasicFixture::table, partition, row));
CHECK_EQUAL(status_codes::OK, delete_entity (BasicFixture::addr, BasicFixture::table, partition2, row2));
}
TEST_FIXTURE(BasicFixture, EdgeCases) {
cout << ">> EdgeCases test" << endl;
string partition {"CAN"};
string row {"Franklin,Aretha"};
string property {"Song"};
string prop_val {"DISRESPECT"};
int put_result {put_entity (BasicFixture::addr, BasicFixture::table, partition, row, property, prop_val)};
cerr << "put result " << put_result << endl;
assert (put_result == status_codes::OK);
//Partition does not exist
cout << "Edge Partition 1" << endl;
pair<status_code, value> result {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin + "/"
+ BasicFixture::table + "/"
+ "NotA,Partition" + "/"
+ "*")
};
CHECK_EQUAL(status_codes::BadRequest, result.first);
//Row does not exist/is not '*'
cout << "Edge Partition 2" << endl;
pair<status_code, value> result2 {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin + "/"
+ BasicFixture::table + "/"
+ BasicFixture::partition + "/"
+ "NotA,Row")
};
CHECK_EQUAL(status_codes::NotFound, result2.first);
//Table does not exist
cout << "Edge Partition 3" << endl;
pair<status_code, value> result3 {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin + "/"
+ "NotATable" + "/"
+ BasicFixture::partition + "/"
+ BasicFixture::row)
};
CHECK_EQUAL(status_codes::NotFound, result3.first);
//No paths (Missing table, partition, row)
cout << "Edge Partition 4" << endl;
pair<status_code, value> result4 {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin)
};
CHECK_EQUAL(status_codes::BadRequest, result4.first);
//Missing Partition
cout << "Edge Partition 5" << endl;
pair<status_code, value> result5 {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin + "/"
+ BasicFixture::table + "/"
+ "/" + BasicFixture::row)
};
CHECK_EQUAL(status_codes::BadRequest, result5.first);
//Missing Row
cout << "Edge Partition 6" << endl;
pair<status_code, value> result6 {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin + "/"
+ BasicFixture::table + "/"
+ BasicFixture::partition)
};
CHECK_EQUAL(status_codes::BadRequest, result6.first);
//Missing Row and Partition, with wrong Table name
cout << "Edge Partition 7" << endl;
pair<status_code, value> result7 {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin + "/"
+ "NotATable")
};
CHECK_EQUAL(status_codes::NotFound, result7.first);
//Wrong table, partition, and row
cout << "Edge Partition 8" << endl;
pair<status_code, value> result8 {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin + "/"
+ "NotATable" + "/"
+ "NotA,Partition" + "/"
+ "NotA,Row")
};
CHECK_EQUAL(status_codes::NotFound, result8.first);
CHECK_EQUAL(status_codes::OK, delete_entity (BasicFixture::addr, BasicFixture::table, partition, row));
}
/*
A test of Get JSON properties
*/
TEST_FIXTURE(BasicFixture, GetJSON) {
cout << ">> GetJSON test" << endl;
string partition {"CAN"};
string row {"Franklin,Aretha"};
string property {"Song"};
string prop_val {"DISRESPECT"};
int put_result {put_entity (BasicFixture::addr, BasicFixture::table, partition, row, property, prop_val)};
cerr << "put result " << put_result << endl;
assert (put_result == status_codes::OK);
string partition2 {"CAN"};
string row2 {"Katherines,The"};
string property2 {"Home"};
string prop_val2 {"Vancouver"};
int put_result2 {put_entity (BasicFixture::addr, BasicFixture::table, partition2, row2, property2, prop_val2)};
cerr << "put result " << put_result2 << endl;
assert (put_result2 == status_codes::OK);
pair<status_code, value> result {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin + "/"
+ BasicFixture::table,
value::object (vector<pair<string,value>>
{make_pair("Song", value::string("Respect"))}))
};
CHECK_EQUAL(2, result.second.as_array().size());
CHECK_EQUAL(string("[{\"")
+ "Partition" + "\":\""
+ partition + "\",\""
+ "Row" + "\":\""
+ row + "\",\""
+ property + "\":\""
+ prop_val + "\""
+ "},{\""
+ "Partition" + "\":\""
+ BasicFixture::partition + "\",\""
+ "Row" + "\":\""
+ BasicFixture::row + "\",\""
+ BasicFixture::property + "\":\""
+ BasicFixture::prop_val + "\""
+ "}]",
result.second.serialize());
//Property not found
cout << "Edge JSON 1" << endl;
pair<status_code, value> result2 {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin + "/"
+ BasicFixture::table,
value::object (vector<pair<string,value>>
{make_pair("NotASong", value::string("string"))}))
};
CHECK_EQUAL(status_codes::BadRequest, result2.first);
//No Table value
cout << "Edge JSON 2" << endl;
pair<status_code, value> result3 {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin,
value::object (vector<pair<string,value>>
{make_pair("NotASong", value::string("string"))}))
};
CHECK_EQUAL(status_codes::BadRequest, result3.first);
//Table not found
cout << "Edge JSON 3" << endl;
pair<status_code, value> result4 {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin + "/"
+ "NotA,Table",
value::object (vector<pair<string,value>>
{make_pair("Home", value::string("string"))}))
};
CHECK_EQUAL(status_codes::NotFound, result4.first);
//Random prop_val and different property (Katherine's)
cout << "Edge JSON 4" << endl;
pair<status_code, value> result5 {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin + "/"
+ BasicFixture::table,
value::object (vector<pair<string,value>>
{make_pair("Home", value::string("KAHD872f273f72kauhfsefKAHDA&Y*Y@#*uygQETR"))}))
};
CHECK_EQUAL(status_codes::OK, result5.first);
CHECK_EQUAL(1, result5.second.as_array().size());
CHECK_EQUAL(status_codes::OK, delete_entity (BasicFixture::addr, BasicFixture::table, partition, row));
CHECK_EQUAL(status_codes::OK, delete_entity (BasicFixture::addr, BasicFixture::table, partition2, row2));
}
/*
A test of GET all table entries
*/
TEST_FIXTURE(BasicFixture, GetAllAssign1) {
cout << ">> GetAll (assign1) test" << endl;
string partition {"Katherines,The"};
string row {"Katherines,The"};
string property {"Home"};
string prop_val {"Vancouver"};
int put_result {put_entity (BasicFixture::addr, BasicFixture::table, partition, row, property, prop_val)};
cerr << "put result " << put_result << endl;
assert (put_result == status_codes::OK);
pair<status_code,value> result {
do_request (methods::GET,
string(BasicFixture::addr)
+ read_entity_admin + "/"
+ string(BasicFixture::table))
};
CHECK(result.second.is_array());
//cout << result.second << endl;
//cout << result.second.serialize() << endl;
CHECK_EQUAL(2, result.second.as_array().size());
/*
Checking the body is not well-supported by UnitTest++, as we have to test
independent of the order of returned values.
*/
//CHECK_EQUAL(body.serialize(), string("{\"")+string(BasicFixture::property)+ "\":\""+string(BasicFixture::prop_val)+"\"}");
CHECK_EQUAL(status_codes::OK, result.first);
CHECK_EQUAL(status_codes::OK, delete_entity (BasicFixture::addr, BasicFixture::table, partition, row));
}
}
class AuthFixture {
public:
static constexpr const char* addr {"http://localhost:34568/"};
static constexpr const char* auth_addr {"http://localhost:34570/"};
static constexpr const char* userid {"user"};
static constexpr const char* user_pwd {"user"};
static constexpr const char* auth_table {"AuthTable"};
static constexpr const char* auth_table_partition {"Userid"};
static constexpr const char* auth_pwd_prop {"Password"};
static constexpr const char* table {"DataTable"};
static constexpr const char* partition {"USA"};
static constexpr const char* row {"Franklin,Aretha"};
static constexpr const char* property {"Song"};
static constexpr const char* prop_val {"RESPECT"};
public:
AuthFixture() {
int make_result {create_table(addr, table)};
cerr << "create result " << make_result << endl;
if (make_result != status_codes::Created && make_result != status_codes::Accepted) {
throw std::exception();
}
//Change properties to Friends/Status/Updates in new Fixture
int put_result {put_entity (addr, table, partition, row, property, prop_val)};
cerr << "put result " << put_result << endl;
if (put_result != status_codes::OK) {
throw std::exception();
}
vector<pair<string, value>> v {
make_pair("Password", value::string(user_pwd)),
make_pair("DataPartition", value::string(partition)),
make_pair("DataRow", value::string(row))
};
// Ensure userid and password in system
int user_result {put_entity (addr, auth_table, auth_table_partition, userid, v)};
cerr << "user auth table insertion result " << user_result << endl;
if (user_result != status_codes::OK)
throw std::exception();
}
~AuthFixture() {
int del_ent_result {delete_entity (addr, table, partition, row)};
if (del_ent_result != status_codes::OK) {
throw std::exception();
}
}
};
SUITE(UPDATE_AUTH) {
TEST_FIXTURE(AuthFixture, PutAuth) {
cout << ">> PutAuth Test" << endl;
pair<string,string> added_prop {make_pair(string("born"),string("1942"))};
cout << "Requesting token" << endl;
pair<status_code,string> token_res {
get_update_token(AuthFixture::auth_addr,
AuthFixture::userid,
AuthFixture::user_pwd)
};
cout << "Token response " << token_res.first << endl;
CHECK_EQUAL ( status_codes::OK, token_res.first);
//read_entity_auth is the token for reading
pair<status_code,value> result {
do_request (methods::PUT,
string(AuthFixture::addr)
+ update_entity_auth + "/"
+ AuthFixture::table + "/"
+ token_res.second + "/"
+ AuthFixture::partition + "/"
+ AuthFixture::row,
value::object (vector<pair<string,value>>
{make_pair(added_prop.first,
value::string(added_prop.second))})
)
};
CHECK_EQUAL(status_codes::OK, result.first);
pair<status_code,value> ret_res {
do_request (methods::GET,
string(AuthFixture::addr)
+ read_entity_admin + "/"
+ AuthFixture::table + "/"
+ AuthFixture::partition + "/"
+ AuthFixture::row)
};
CHECK_EQUAL (status_codes::OK, ret_res.first);
value expect1 {
build_json_object (
vector<pair<string,string>> {
added_prop,
make_pair(string(AuthFixture::property),
string(AuthFixture::prop_val))
}
)
};
//cout << ret_res.second.serialize() << endl;
//cout << expect1 << endl;
compare_json_values (expect1, ret_res.second);
//Less than four parameters
cout << "Edge PUT_AUTH 1" << endl;
pair<status_code,value> result2 {
do_request (methods::PUT,
string(AuthFixture::addr)
+ update_entity_auth + "/"
+ AuthFixture::table + "/"
+ token_res.second,
value::object (vector<pair<string,value>>
{make_pair(added_prop.first,
value::string(added_prop.second))})
)
};
CHECK_EQUAL(status_codes::BadRequest, result2.first);
//Token for reading
cout << "Edge PUT_AUTH 2" << endl;
try {
pair<status_code,value> result3 {
do_request (methods::PUT,
string(AuthFixture::addr)
+ read_entity_auth + "/"
+ AuthFixture::table + "/"
+ token_res.second + "/"
+ AuthFixture::partition + "/"
+ AuthFixture::row,
value::object (vector<pair<string,value>>
{make_pair(added_prop.first,
value::string(added_prop.second))})
)
};
//cout << "No exception thrown: Exception expected" << endl;
CHECK_EQUAL(status_codes::Forbidden, result3.first);
}
catch (const storage_exception& e) {
cout << "Azure Table Storage error: " << e.what() << endl;
cout << e.result().extended_error().message() << endl;
if (e.result().http_status_code() == status_codes::Forbidden)
cout << "Exception: " << status_codes::Forbidden << endl;
else
cout << "Exception: " << status_codes::InternalError << endl;
}
//Token does not authorize access
cout << "Edge PUT_AUTH 3" << endl;
pair<status_code,value> result4 {
do_request (methods::PUT,
string(AuthFixture::addr)
+ update_entity_admin + "/"
+ AuthFixture::table + "/"
+ token_res.second + "/"
+ AuthFixture::partition + "/"
+ AuthFixture::row,
value::object (vector<pair<string,value>>