Step 2: Move test folder to root
[integration/test.git] / csit / libraries / criotdm.py
1 """This library should work with ciotdm, both work for iotdm project."""
2 import ciotdm
3
4
5 def connect_to_iotdm(host, user, password, prot):
6     """According to protocol, connect to iotdm."""
7     return ciotdm.connect(host, base="InCSE1", auth=(
8         user, password), protocol=prot)
9
10
11 def create_resource(connection, parent, restype, attribute=None, name=None):
12     """Create resource without command."""
13     restype = int(restype)
14     response = connection.create(parent, restype, attribute, name=name)
15     Check_Response(response, "create")
16     return response
17
18
19 def create_resource_with_command(connection, parent, restype,
20                                  command, attribute=None, name=None):
21     """According to command in the header, create the resource."""
22     restype = int(restype)
23     response = connection.createWithCommand(parent, restype,
24                                             command, attribute, name=name)
25     Check_Response(response, "create")
26     return response
27
28
29 def create_subscription(connection, parent, ip, port):
30     """Create subscription."""
31     uri = "http://%s:%d" % (ip, int(port))
32     response = connection.create(parent, "subscription", {
33         "notificationURI": uri,
34         "notificationContentType": "wholeResource"})
35     Check_Response(response, "create")
36     return response
37
38
39 def retrieve_resource(connection, resid):
40     """Retrieve resource according to resourceID."""
41     response = connection.retrieve(resid)
42     Check_Response(response, "retrieve")
43     return response
44
45
46 def retrieve_resource_with_command(connection, resid, command):
47     """According to command, retrieve source with the resourceID."""
48     response = connection.retrieveWithCommand(resid, command)
49     Check_Response(response, "retrieve")
50     return response
51
52
53 def update_resource(connection, resid, restype, attr, nm=None):
54     """According to resourceID, update resource."""
55     response = connection.update(resid, restype, attr, nm)
56     Check_Response(response, "update")
57     return response
58
59
60 def update_resource_with_command(connection, resid,
61                                  restype, command, attr, nm=None):
62     """According to command, update resource with resourceID."""
63     response = connection.updateWithCommand(resid, restype, command, attr, nm)
64     Check_Response(response, "update")
65     return response
66
67
68 def delete_resource(connection, resid):
69     """According to resourceID, delete the resource."""
70     response = connection.delete(resid)
71     Check_Response(response, "delete")
72     return response
73
74
75 def delete_resource_with_command(connection, resid, command):
76     """According to command, delete the resource with resourceID."""
77     response = connection.deleteWithCommand(resid, command)
78     Check_Response(response, "delete")
79     return response
80
81
82 def resid(response):
83     """Return resource ID."""
84     return ciotdm.resid(response)
85
86
87 def name(response):
88     """Return resourceName."""
89     resourceName = ciotdm.name(response)
90     if resourceName is None:
91         raise AssertionError('Cannot find this resource')
92     return resourceName
93
94
95 def text(response):
96     """Return whole resource in text."""
97     return response.text
98
99
100 def lastModifiedTime(response):
101     """Return resource lastModifiedTime."""
102     return ciotdm.lastModifiedTime(response)
103
104
105 def status_code(response):
106     """Return resource status_code."""
107     return response.status_code
108
109
110 def json(response):
111     """Return resource in json format."""
112     return response.json()
113
114
115 def elapsed(response):
116     """Return resource elapsed."""
117     return response.elapsed.total_seconds()
118
119
120 def kill_the_tree(host, CSEID, username, password):
121     """Delete the whole tree."""
122     connection = ciotdm.connect(host, base=CSEID,
123                                 auth=(username, password), protocol="http")
124     connection.kill()
125
126
127 def Check_Response(response, operation):
128     """Check whether the connection is none."""
129     if response is None:
130         raise AssertionError('Cannot %s this resource') % (operation)
131     elif hasattr(response, 'status_code'):
132         if response.status_code < 200 or response.status_code > 299:
133             raise AssertionError(
134                 'Cannot %s this resource [%d] : %s' %
135                 (operation, response.status_code, response.text))