Step 2: Move test folder to root
[integration/test.git] / csit / libraries / riotdm.py
1 import iotdm
2
3 application = iotdm.application
4 container = iotdm.container
5 contentInstance = iotdm.contentInstance
6
7
8 def connect_to_iotdm(host, user, pw, p):
9     return iotdm.connect(host, base="InCSE1", auth=(user, pw), protocol=p)
10
11
12 def create_resource(connection, parent, restype, a=None):
13     restype = int(restype)
14     if a is None:
15         x = connection.create(parent, restype)
16     else:
17         x = connection.create(parent, restype, attr=a)
18     if x is None:
19         raise AssertionError('Cannot create this resource')
20     elif hasattr(x, 'status_code'):
21         if x.status_code < 200 or x.status_code > 299:
22             raise AssertionError(
23                 'Cannot create this resource [%d] : %s' %
24                 (x.status_code, x.text))
25     return x
26
27 # this might not be necessary now that the library functions can take dicts
28
29
30 def create_subscription(connection, parent, ip, port):
31     uri = "http://%s:%d" % (ip, int(port))
32     x = connection.create(parent, "subscription", {
33         "notificationURI": uri,
34         "notificationContentType": "wholeResource"})
35     if x is None:
36         raise AssertionError('Cannot create this subscription')
37     elif hasattr(x, 'status_code'):
38         if x.status_code < 200 or x.status_code > 299:
39             raise AssertionError('Cannot create subscription [%d] : %s' %
40                                  (x.status_code, x.text))
41     return x
42
43
44 def retrieve_resource(connection, resid):
45     x = connection.retrieve(resid)
46     if x is None:
47         raise AssertionError('Cannot retrieve this resource')
48     elif hasattr(x, 'status_code'):
49         if x.status_code < 200 or x.status_code > 299:
50             raise AssertionError('Cannot retrieve this resource [%d] : %s' %
51                                  (x.status_code, x.text))
52     return x
53
54
55 def update_resource(connection, resid, attr):
56     x = connection.update(resid, attr)
57     if x is None:
58         raise AssertionError('Cannot update this resource')
59     elif hasattr(x, 'status_code'):
60         if x.status_code < 200 or x.status_code > 299:
61             raise AssertionError('Cannot update this resource [%d] : %s' %
62                                  (x.status_code, x.text))
63     return x
64
65
66 def delete_resource(connection, resid):
67     x = connection.delete(resid)
68     if x is None:
69         raise AssertionError('Cannot delete this resource')
70     elif hasattr(x, 'status_code'):
71         if x.status_code < 200 or x.status_code > 299:
72             raise AssertionError('Cannot delete this resource [%d] : %s' %
73                                  (x.status_code, x.text))
74     return x
75
76
77 def text(x):
78     return x.text
79
80
81 def status_code(x):
82     return x.status_code
83
84
85 def json(x):
86     return x.json()
87
88
89 def elapsed(x):
90     return x.elapsed.total_seconds()