aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/tests/func_tests/features/steps/data.py
blob: 67d743c2d448c5754a5deb99f8b5edf79e690bbe (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
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
# Software Name: MOON

# Version: 5.4

# SPDX-FileCopyrightText: Copyright (c) 2018-2020 Orange and its contributors
# SPDX-License-Identifier: Apache-2.0

# This software is distributed under the 'Apache License 2.0',
# the text of which is available at 'http://www.apache.org/licenses/LICENSE-2.0.txt'
# or see the "LICENSE" file for more details.


from behave import *
from Static_Variables import GeneralVariables
from astropy.table import Table, Column
from common_functions import *
import requests
import json
import logging

apis_urls = GeneralVariables()
commonfunctions = commonfunctions()

logger = logging.getLogger(__name__)

# Step Definition Implementation:
# 1) Get all the existing subject meta data in the system by getting the policies then their models then the model attached meta rules and then the categories
# 2) Get subject data using both the policy id & the category id
# 3) Loop by data id and delete it
@Given('the system has no subject data')
def step_impl(context):
    logger.info("Given the system has no subject data")
    headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

    response_policies = requests.get(apis_urls.serverURL + apis_urls.policyAPI,headers=apis_urls.auth_headers)
    if len(response_policies.json()[apis_urls.policyAPI]) != 0:
        for policies_ids in dict(response_policies.json()[apis_urls.policyAPI]).keys():
            subjectcategoryidslist = []
            modelid = response_policies.json()[apis_urls.policyAPI][policies_ids]['model_id']
            if (modelid != None and modelid != ""):
                metaruleslist = \
                    requests.get(apis_urls.serverURL + apis_urls.modelAPI,headers=apis_urls.auth_headers).json()[apis_urls.modelAPI][modelid][
                        'meta_rules']
                for metarule_ids in metaruleslist:
                    categorieslist = \
                        requests.get(apis_urls.serverURL + apis_urls.metarulesAPI,headers=apis_urls.auth_headers).json()[apis_urls.metarulesAPI][
                            metarule_ids]['subject_categories']
                    for categoryid in categorieslist:
                        if (categoryid not in subjectcategoryidslist):
                            subjectcategoryidslist.append(categoryid)

                for categoryid in subjectcategoryidslist:
                    response_data = requests.get(
                        apis_urls.serverURL + apis_urls.policyAPI + "/" + policies_ids + "/" + apis_urls.datasubjectAPI + "/" + categoryid,headers=apis_urls.auth_headers)
                    for ids in response_data.json()[apis_urls.datasubjectAPI][0]['data']:
                        data_id = response_data.json()[apis_urls.datasubjectAPI][0]['data'][str(ids)]['id']
                        requests.delete(
                            apis_urls.serverURL + apis_urls.policyAPI + "/" + policies_ids + "/" + apis_urls.datasubjectAPI + "/" + categoryid + "/" + data_id,
                            headers=headers)

# Step Definition Implementation:
# 1) Post subject data using the policy id & the category id
@Given('the following subject data exists')
def step_impl(context):
    logger.info("Given the following subject data exists")
    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "subject data name: '" + row["subjectdataname"] + "' subject data description: '" + row[
                "subjectdatadescription"] + "' and subject category: '" + row[
                "subjectcategory"] + "' and policies: '" + row['policyname'] + "'")

        policies_id = ""
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        if (len(row['policyname']) > 25):
            policies_id = row['policyname']
        else:
            policies_id = commonfunctions.get_policyid(row['policyname'])

        if (len(row['subjectcategory']) > 25):
            categories_id = row['subjectcategory']
        else:
            categories_id = commonfunctions.get_subjectcategoryid(row['subjectcategory'])

        data = {
            'name': row["subjectdataname"],
            'description': row["subjectdatadescription"],

        }
        response = requests.post(
            apis_urls.serverURL + "policies/" + str(policies_id) + "/" + apis_urls.datasubjectAPI + "/" + str(
                categories_id), headers=headers, data=json.dumps(data))

# Step Definition Implementation:
# 1) Get all the existing object meta data in the system by getting the policies then their models then the model attached meta rules and then the categories
# 2) Get object data using both the policy id & the category id
# 3) Loop by data id and delete it
@Given('the system has no object data')
def step_impl(context):
    logger.info("Given the system has no object data")
    headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

    response_policies = requests.get(apis_urls.serverURL + apis_urls.policyAPI,headers=apis_urls.auth_headers)
    if len(response_policies.json()[apis_urls.policyAPI]) != 0:
        for policies_ids in dict(response_policies.json()[apis_urls.policyAPI]).keys():
            objectcategoryidslist = []
            modelid = response_policies.json()[apis_urls.policyAPI][policies_ids]['model_id']
            if (modelid != None and modelid != ""):
                metaruleslist = \
                    requests.get(apis_urls.serverURL + apis_urls.modelAPI,headers=apis_urls.auth_headers).json()[apis_urls.modelAPI][modelid][
                        'meta_rules']
                for metarule_ids in metaruleslist:
                    for categoryid in \
                            (requests.get(apis_urls.serverURL + apis_urls.metarulesAPI,headers=apis_urls.auth_headers)).json()[apis_urls.metarulesAPI][
                                metarule_ids][
                                'object_categories']:
                        if (categoryid not in objectcategoryidslist):
                            objectcategoryidslist.append(categoryid)

                for categoryid in objectcategoryidslist:
                    response_data = requests.get(
                        apis_urls.serverURL + apis_urls.policyAPI + "/" + policies_ids + "/" + apis_urls.dataobjectAPI + "/" + categoryid,headers=apis_urls.auth_headers)
                    for ids in response_data.json()[apis_urls.dataobjectAPI][0]['data']:
                        data_id = response_data.json()[apis_urls.dataobjectAPI][0]['data'][str(ids)]['id']
                        requests.delete(
                            apis_urls.serverURL + apis_urls.policyAPI + "/" + policies_ids + "/" + apis_urls.dataobjectAPI + "/" + categoryid + "/" + data_id,
                            headers=headers)

# Step Definition Implementation:
# 1) Post object data using the policy id & the category id
@Given('the following object data exists')
def step_impl(context):
    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "subject data name: '" + row["objectdataname"] + "' object data description: '" + row[
                "objectdatadescription"] + "' and object category: '" + row[
                "objectcategory"] + "' and policies: '" + row['policyname'] + "'")

        policies_id = ""
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        if (len(row['policyname']) > 25):
            policies_id = row['policyname']
        else:
            policies_id = commonfunctions.get_policyid(row['policyname'])

        if (len(row['objectcategory']) > 25):
            categories_id = row['objectcategory']
        else:
            categories_id = commonfunctions.get_objectcategoryid(row['objectcategory'])

        data = {
            'name': row["objectdataname"],
            'description': row["objectdatadescription"],

        }
        response = requests.post(
            apis_urls.serverURL + "policies/" + str(policies_id) + "/" + apis_urls.dataobjectAPI + "/" + str(
                categories_id), headers=headers, data=json.dumps(data))

# Step Definition Implementation:
# 1) Get all the existing action meta data in the system by getting the policies then their models then the model attached meta rules and then the categories
# 2) Get action data using both the policy id & the category id
# 3) Loop by data id and delete it
@Given('the system has no action data')
def step_impl(context):
    logger.info("Given the system has no action data")
    headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}
    actioncategoryidslist = []
    response_policies = requests.get(apis_urls.serverURL + apis_urls.policyAPI,headers=apis_urls.auth_headers)
    if len(response_policies.json()[apis_urls.policyAPI]) != 0:
        for policies_ids in dict(response_policies.json()[apis_urls.policyAPI]).keys():
            actioncategoryidslist = []
            modelid = response_policies.json()[apis_urls.policyAPI][policies_ids]['model_id']
            if (modelid != None and modelid != ""):
                metaruleslist = \
                    requests.get(apis_urls.serverURL + apis_urls.modelAPI,headers=apis_urls.auth_headers).json()[apis_urls.modelAPI][modelid][
                        'meta_rules']
                for metarule_ids in metaruleslist:
                    for categoryid in \
                            (requests.get(apis_urls.serverURL + apis_urls.metarulesAPI,headers=apis_urls.auth_headers)).json()[apis_urls.metarulesAPI][
                                metarule_ids][
                                'action_categories']:
                        if (categoryid not in actioncategoryidslist):
                            actioncategoryidslist.append(categoryid)

                for categoryid in actioncategoryidslist:
                    response_data = requests.get(
                        apis_urls.serverURL + apis_urls.policyAPI + "/" + policies_ids + "/" + apis_urls.dataactionAPI + "/" + categoryid,headers=apis_urls.auth_headers)
                    for ids in response_data.json()[apis_urls.dataactionAPI][0]['data']:
                        data_id = response_data.json()[apis_urls.dataactionAPI][0]['data'][str(ids)]['id']
                        requests.delete(
                            apis_urls.serverURL + apis_urls.policyAPI + "/" + policies_ids + "/" + apis_urls.dataactionAPI + "/" + categoryid + "/" + data_id,
                            headers=headers)

# Step Definition Implementation:
# 1) Post action data using the policy id & the category id
@Given('the following action data exists')
def step_impl(context):
    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "subject data name: '" + row["actiondataname"] + "' action data description: '" + row[
                "actiondatadescription"] + "' and action category: '" + row[
                "actioncategory"] + "' and policies: '" + row['policyname'] + "'")

        policies_id = ""
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        if (len(row['policyname']) > 25):
            policies_id = row['policyname']
        else:
            policies_id = commonfunctions.get_policyid(row['policyname'])

        if (len(row['actioncategory']) > 25):
            categories_id = row['actioncategory']
        else:
            categories_id = commonfunctions.get_actioncategoryid(row['actioncategory'])

        data = {
            'name': row["actiondataname"],
            'description': row["actiondatadescription"],

        }
        response = requests.post(
            apis_urls.serverURL + "policies/" + str(policies_id) + "/" + apis_urls.dataactionAPI + "/" + str(
                categories_id), headers=headers, data=json.dumps(data))

# Step Definition Implementation:
# 1) Add subject data using the post request
# 2) If the request code was 200 set the api response flag to true else false
@When('the user sets to add the following subject data')
def step_impl(context):
    logger.info("When the user sets to add the following subject data")
    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "subject data name: '" + row["subjectdataname"] + "' subject data description: '" + row[
                "subjectdatadescription"] + "' and subject category: '" + row[
                "subjectcategory"] + "' and policies: '" + row['policyname'] + "'")

        policies_id = ""
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        if (len(row['policyname']) > 25):
            policies_id = row['policyname']
        else:
            policies_id = commonfunctions.get_policyid(row['policyname'])

        if (len(row['subjectcategory']) > 25):
            categories_id = row['subjectcategory']
        else:
            categories_id = commonfunctions.get_subjectcategoryid(row['subjectcategory'])

        data = {
            'name': row["subjectdataname"],
            'description': row["subjectdatadescription"],

        }
        response = requests.post(
            apis_urls.serverURL + "policies/" + str(policies_id) + "/" + apis_urls.datasubjectAPI + "/" + str(
                categories_id), headers=headers, data=json.dumps(data))

    if response.status_code == 200:
        GeneralVariables.api_responseflag['value'] = 'True'
    else:
        GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Delete subject data by policy id, subject data id, subject category id
# 2) If the request code was 200 set the api response flag to true else false
@When('the user sets to delete the following subject data')
def step_impl(context):
    logging.info("When the user sets to delete the following subject data")

    model = getattr(context, "model", None)
    for row in context.table:

        logger.info("subject data name:'" + row["subjectdataname"] + "' and subject category name:'" + row[
            "subjectcategory"] + "' and policy name:'" + row["policyname"] + "'")

        policies_id = []
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        response_data = requests.delete(
            apis_urls.serverURL + apis_urls.policyAPI + "/" + commonfunctions.get_policyid(row[
                                                                                               "policyname"]) + "/" + apis_urls.datasubjectAPI + "/" + commonfunctions.get_subjectcategoryid(
                row["subjectcategory"]) + "/" + commonfunctions.get_subjectdataid(row["subjectdataname"],
                                                                                  commonfunctions.get_subjectcategoryid(
                                                                                      row["subjectcategory"]),
                                                                                  commonfunctions.get_policyid(
                                                                                      row["policyname"])),
            headers=headers)

        if response_data.status_code == 200:
            GeneralVariables.api_responseflag['value'] = 'True'
        else:
            GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Add object data using the post request
# 2) If the request code was 200 set the api response flag to true else false
@When('the user sets to add the following object data')
def step_impl(context):
    logger.info("When the user sets to add the following object data")
    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "object data name: '" + row["objectdataname"] + "' object data description: '" + row[
                "objectdatadescription"] + "' and object category: '" + row[
                "objectcategory"] + "' and policies: '" + row['policyname'] + "'")

        policies_list = []
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        if (len(row['policyname']) > 25):
            policies_id = row['policyname']
        else:
            policies_id = commonfunctions.get_policyid(row['policyname'])

        if (len(row['objectcategory']) > 25):
            categories_id = row['objectcategory']
        else:
            categories_id = commonfunctions.get_objectcategoryid(row['objectcategory'])

        data = {
            'name': row["objectdataname"],
            'description': row["objectdatadescription"],
        }

        response = requests.post(
            apis_urls.serverURL + "policies/" + str(policies_id) + "/" + apis_urls.dataobjectAPI + "/" + str(
                categories_id), headers=headers, data=json.dumps(data))

        if response.status_code == 200:
            GeneralVariables.api_responseflag['value'] = 'True'
        else:
            GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Delete object data by policy id, object data id, object category id
# 2) If the request code was 200 set the api response flag to true else false
@When('the user sets to delete the following object data')
def step_impl(context):
    logging.info("When the user sets to delete the following object data")
    model = getattr(context, "model", None)
    for row in context.table:

        logger.info("object data name:'" + row["objectdataname"] + "' and object category name:'" + row[
            "objectcategory"] + "' and policy name:'" + row["policyname"] + "'")

        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        response_data = requests.delete(
            apis_urls.serverURL + apis_urls.policyAPI + "/" + commonfunctions.get_policyid(row[
                                                                                               "policyname"]) + "/" + apis_urls.dataobjectAPI + "/" + commonfunctions.get_objectcategoryid(
                row["objectcategory"]) + "/" + commonfunctions.get_objectdataid(row["objectdataname"],
                                                                                commonfunctions.get_objectcategoryid(
                                                                                    row["objectcategory"]),
                                                                                commonfunctions.get_policyid(
                                                                                    row["policyname"])),
            headers=headers)

        if response_data.status_code == 200:
            GeneralVariables.api_responseflag['value'] = 'True'
        else:
            GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Add action data using the post request
# 2) If the request code was 200 set the api response flag to true else false
@When('the user sets to add the following action data')
def step_impl(context):
    logger.info("When the user sets to add the following action data")

    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "action data name: '" + row["actiondataname"] + "' action data description: '" + row[
                "actiondatadescription"] + "' and action category: '" + row[
                "actioncategory"] + "' and policies: '" + row['policyname'] + "'")

        policies_id = ""
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        if (len(row['policyname']) > 25):
            policies_id = row['policyname']
        else:
            policies_id = commonfunctions.get_policyid(row['policyname'])

        if (len(row['actioncategory']) > 25):
            categories_id = row['actioncategory']
        else:
            categories_id = commonfunctions.get_actioncategoryid(row['actioncategory'])

        data = {
            'name': row["actiondataname"],
            'description': row["actiondatadescription"],

        }
        response = requests.post(
            apis_urls.serverURL + "policies/" + str(policies_id) + "/" + apis_urls.dataactionAPI + "/" + str(
                categories_id), headers=headers, data=json.dumps(data))

    if response.status_code == 200:
        GeneralVariables.api_responseflag['value'] = 'True'
    else:
        GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Delete action data by policy id, action data id, action category id
# 2) If the request code was 200 set the api response flag to true else false
@When('the user sets to delete the following action data')
def step_impl(context):
    logging.info("When the user sets to delete the following action data")
    model = getattr(context, "model", None)
    for row in context.table:

        logger.info("action data name:'" + row["actiondataname"] + "' and action category name:'" + row[
            "actioncategory"] + "' and policy name:'" + row["policyname"] + "'")

        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        response_data = requests.delete(
            apis_urls.serverURL + apis_urls.policyAPI + "/" + commonfunctions.get_policyid(row[
                                                                                               "policyname"]) + "/" + apis_urls.dataactionAPI + "/" + commonfunctions.get_actioncategoryid(
                row["actioncategory"]) + "/" + commonfunctions.get_actiondataid(row["actiondataname"],
                                                                                commonfunctions.get_actioncategoryid(
                                                                                    row["actioncategory"]),
                                                                                commonfunctions.get_policyid(
                                                                                    row["policyname"])),
            headers=headers)

        if response_data.status_code == 200:
            GeneralVariables.api_responseflag['value'] = 'True'
        else:
            GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Get all the existing subject data by get request and put them into a table
# 2) Sort the table by policy name
# 3) Loop using both the expected and actual tables and assert the data row by row
@Then('the following subject data should be existed in the system')
def step_impl(context):
    logger.info("Then the following subject data should be existed in the system")
    model = getattr(context, "model", None)
    apiresult = Table(names=('subjectdataname', 'subjectdatadescription', 'subjectcategory', 'policyname'),
                      dtype=('S100', 'S100', 'S100', 'S100'))
    for row in context.table:
        if (row['policyname'] != ""):
            response = requests.get(
                apis_urls.serverURL + "policies/" + commonfunctions.get_policyid(
                    row['policyname']) + "/" + apis_urls.datasubjectAPI + "/" +
                commonfunctions.get_subjectcategoryid(row['subjectcategory']),headers=apis_urls.auth_headers)

            if len(response.json()[apis_urls.datasubjectAPI]) != 0:
                for ids in response.json()[apis_urls.datasubjectAPI][0]['data']:
                    apipolicies = ""
                    apisubjectdataname = response.json()[apis_urls.datasubjectAPI][0]['data'][str(ids)]['name']
                    apisubjectdatadescription = response.json()[apis_urls.datasubjectAPI][0]['data'][str(ids)][
                        'description']
                    apisubjectcategory = commonfunctions.get_subjectcategoryname(
                        response.json()[apis_urls.datasubjectAPI][0]['data'][str(ids)]['category_id'])
                    apipolicies = commonfunctions.get_policyname(
                        response.json()[apis_urls.datasubjectAPI][0]['data'][str(ids)]['policy_id'])
                    apiresult.add_row(vals=(
                        apisubjectdataname, apisubjectdatadescription, apisubjectcategory, apipolicies))
            else:
                apiresult.add_row(vals=("", "", "", ""))

        else:
            apiresult.add_row(vals=("", "", "", ""))

    apiresult.sort('policyname')
    for row1, row2 in zip(context.table, apiresult):
        logger.info("asserting the expected subject data name: '" + str(
            row1["subjectdataname"]) + "' is the same as the actual existing '" + str(
            row2["subjectdataname"]) + "'")
        assert str(row1["subjectdataname"]) == str(row2["subjectdataname"]), "subject data name is not correct!"
        logger.info("assertion passed!")

        logger.info("asserting the expected subject data description: '" + str(
            row1["subjectdatadescription"]) + "' is the same as the actual existing '" + str(
            row2["subjectdatadescription"]) + "'")
        assert str(row1["subjectdatadescription"]) == str(
            row2["subjectdatadescription"]), "subject data description is not correct!"
        logger.info("assertion passed!")

        logger.info("asserting the expected subject data password: '" + str(
            row1["subjectcategory"]) + "' is the same as the actual existing '" + str(
            row2["subjectcategory"]) + "'")
        assert str(row1["subjectcategory"]) == str(
            row2["subjectcategory"]), "subject category is not correct!"
        logger.info("assertion passed!")

        logger.info("asserting the expected policies: '" + str(
            row1["policyname"]) + "' is the same as the actual existing '" + str(
            row2["policyname"]) + "'")
        assert str(row1["policyname"]) == str(row2["policyname"]), " policies is not correct!"
        logger.info("assertion passed!")

# Step Definition Implementation:
# 1) Get all the existing object data by get request and put them into a table
# 2) Sort the table by policy name
# 3) Loop using both the expected and actual tables and assert the data row by row
@Then('the following object data should be existed in the system')
def step_impl(context):
    logger.info("Then the following object data should be existed in the system")
    model = getattr(context, "model", None)
    apiresult = Table(names=('objectdataname', 'objectdatadescription', 'objectcategory', 'policyname'),
                      dtype=('S100', 'S100', 'S100', 'S100'))

    for row in context.table:
        if (row['policyname'] != ""):
            response = requests.get(
                apis_urls.serverURL + "policies/" + commonfunctions.get_policyid(
                    row['policyname']) + "/" + apis_urls.dataobjectAPI + "/" +
                commonfunctions.get_objectcategoryid(row['objectcategory']),headers=apis_urls.auth_headers)

            if len(response.json()[apis_urls.dataobjectAPI]) != 0:
                for ids in response.json()[apis_urls.dataobjectAPI][0]['data']:
                    apipolicies = ""
                    apiobjectdataname = response.json()[apis_urls.dataobjectAPI][0]['data'][str(ids)]['name']
                    apiobjectdatadescription = response.json()[apis_urls.dataobjectAPI][0]['data'][str(ids)][
                        'description']
                    apiobjectcategory = commonfunctions.get_objectcategoryname(
                        response.json()[apis_urls.dataobjectAPI][0]['data'][str(ids)]['category_id'])
                    apipolicies = commonfunctions.get_policyname(
                        response.json()[apis_urls.dataobjectAPI][0]['data'][str(ids)]['policy_id'])

                    apiresult.add_row(vals=(
                        apiobjectdataname, apiobjectdatadescription, apiobjectcategory, apipolicies))
            else:
                apiresult.add_row(vals=("", "", "", ""))
        else:
            apiresult.add_row(vals=("", "", "", ""))

    apiresult.sort('policyname')
    for row1, row2 in zip(context.table, apiresult):
        logger.info("asserting the expected object data name: '" + str(
            row1["objectdataname"]) + "' is the same as the actual existing '" + str(
            row2["objectdataname"]) + "'")
        assert str(row1["objectdataname"]) == str(row2["objectdataname"]), "subject data name is not correct!"
        logger.info("assertion passed!")

        logger.info("asserting the expected object data description: '" + str(
            row1["objectdatadescription"]) + "' is the same as the actual existing '" + str(
            row2["objectdatadescription"]) + "'")
        assert str(row1["objectdatadescription"]) == str(
            row2["objectdatadescription"]), "object data description is not correct!"
        logger.info("assertion passed!")

        logger.info("asserting the expected object data category: '" + str(
            row1["objectcategory"]) + "' is the same as the actual existing '" + str(
            row2["objectcategory"]) + "'")
        assert str(row1["objectcategory"]) == str(
            row2["objectcategory"]), "object category is not correct!"
        logger.info("assertion passed!")

        logger.info("asserting the expected policies: '" + str(
            row1["policyname"]) + "' is the same as the actual existing '" + str(
            row2["policyname"]) + "'")
        assert str(row1["policyname"]) == str(row2["policyname"]), " policies is not correct!"
        logger.info("assertion passed!")

# Step Definition Implementation:
# 1) Get all the existing action data by get request and put them into a table
# 2) Sort the table by policy name
# 3) Loop using both the expected and actual tables and assert the data row by row
@Then('the following action data should be existed in the system')
def step_impl(context):
    logger.info("Then the following action data should be existed in the system")
    model = getattr(context, "model", None)
    apiresult = Table(names=('actiondataname', 'actiondatadescription', 'actioncategory', 'policyname'),
                      dtype=('S100', 'S100', 'S100', 'S100'))
    for row in context.table:
        if (row['policyname'] != ""):
            response = requests.get(
                apis_urls.serverURL + "policies/" + commonfunctions.get_policyid(
                    row['policyname']) + "/" + apis_urls.dataactionAPI + "/" +
                commonfunctions.get_actioncategoryid(row['actioncategory']),headers=apis_urls.auth_headers)

            if len(response.json()[apis_urls.dataactionAPI]) != 0:
                for ids in response.json()[apis_urls.dataactionAPI][0]['data']:
                    apipolicies = ""
                    apiactiondataname = response.json()[apis_urls.dataactionAPI][0]['data'][str(ids)]['name']
                    apiactiondatadescription = response.json()[apis_urls.dataactionAPI][0]['data'][str(ids)][
                        'description']
                    apiactioncategory = commonfunctions.get_actioncategoryname(
                        response.json()[apis_urls.dataactionAPI][0]['data'][str(ids)]['category_id'])
                    apipolicies = commonfunctions.get_policyname(
                        response.json()[apis_urls.dataactionAPI][0]['data'][str(ids)]['policy_id'])

                    apiresult.add_row(vals=(
                        apiactiondataname, apiactiondatadescription, apiactioncategory, apipolicies))
            else:
                apiresult.add_row(vals=("", "", "", ""))

        else:
            apiresult.add_row(vals=("", "", "", ""))
    apiresult.sort('policyname')
    for row1, row2 in zip(context.table, apiresult):
        logger.info("asserting the expected action data name: '" + str(
            row1["actiondataname"]) + "' is the same as the actual existing '" + str(
            row2["actiondataname"]) + "'")
        assert str(row1["actiondataname"]) == str(row2["actiondataname"]), "action data name is not correct!"
        logger.info("assertion passed!")

        logger.info("asserting the expected action data description: '" + str(
            row1["actiondatadescription"]) + "' is the same as the actual existing '" + str(
            row2["actiondatadescription"]) + "'")
        assert str(row1["actiondatadescription"]) == str(
            row2["actiondatadescription"]), "action data description is not correct!"
        logger.info("assertion passed!")

        logger.info("asserting the expected action data category: '" + str(
            row1["actioncategory"]) + "' is the same as the actual existing '" + str(
            row2["actioncategory"]) + "'")
        assert str(row1["actioncategory"]) == str(
            row2["actioncategory"]), "action category is not correct!"
        logger.info("assertion passed!")

        logger.info("asserting the expected policies: '" + str(
            row1["policyname"]) + "' is the same as the actual existing '" + str(
            row2["policyname"]) + "'")
        assert str(row1["policyname"]) == str(row2["policyname"]), " policies is not correct!"
        logger.info("assertion passed!")