Correcting Get Value from JSON
[integration/test.git] / csit / libraries / IoTDM / 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="http"):
6     """According to protocol, connect to iotdm."""
7     return ciotdm.connect(host, base="InCSE1", auth=(
8         user, password), protocol=prot)
9
10
11 def modify_headers_origin(connection, new_origin):
12     """Replace the headers origin with the neworigin to test ACP."""
13     connection.modify_headers_origin(new_origin)
14
15
16 def create_resource(connection, parent, restype, attribute=None):
17     """Create resource without command."""
18     connection.create(parent, restype, attribute)
19     check_response(connection.response, "create")
20     return connection.response
21
22
23 def create_resource_with_command(connection, parent, restype,
24                                  command, attribute=None):
25     """According to command in the header, create the resource."""
26     connection.create_with_command(parent, restype,
27                                    command, attribute)
28     check_response(connection.response, "create")
29     return connection.response
30
31
32 def create_subscription(connection, parent, ip, port):
33     """Create subscription."""
34     uri = "http://%s:%d" % (ip, int(port))
35     connection.create(parent, "subscription", {
36         "notificationURI": uri,
37         "notificationContentType": "wholeResource"})
38     check_response(connection.response, "create")
39     return connection.response
40
41
42 def retrieve_resource(connection, resid):
43     """Retrieve resource according to resourceID."""
44     connection.retrieve(resid)
45     check_response(connection.response, "retrieve")
46     return connection.response
47
48
49 def retrieve_resource_with_command(connection, resid, command):
50     """According to command, retrieve source with the resourceID."""
51     connection.retrieve_with_command(resid, command)
52     check_response(connection.response, "retrieve")
53     return connection.response
54
55
56 def update_resource(connection, resid, restype, attr):
57     """According to resourceID, update resource."""
58     connection.update(resid, restype, attr)
59     check_response(connection.response, "update")
60     return connection.response
61
62
63 def update_resource_with_command(connection, resid,
64                                  restype, command, attr):
65     """According to command, update resource with resourceID."""
66     connection.update_with_command(resid, restype, command, attr)
67     check_response(connection.response, "update")
68     return connection.response
69
70
71 def delete_resource(connection, resid):
72     """According to resourceID, delete the resource."""
73     connection.delete(resid)
74     check_response(connection.response, "delete")
75     return connection.response
76
77
78 def delete_resource_with_command(connection, resid, command):
79     """According to command, delete the resource with resourceID."""
80     connection.delete_with_command(resid, command)
81     check_response(connection.response, "delete")
82     return connection.response
83
84
85 def child_resource(response):
86     """Return child resource."""
87     return ciotdm.childResource(response)
88
89
90 def child_resource_first(response):
91     """Return child resource on top of dictionary."""
92     return ciotdm.childResourceFirst(response)
93
94
95 def resid(response):
96     """Return resource ID."""
97     return ciotdm.resid(response)
98
99
100 def parent_id(response):
101     """Return parent ID."""
102     return ciotdm.parent(response)
103
104
105 def name(response):
106     """Return resourceName."""
107     resource_name = ciotdm.name(response)
108     if resource_name is None:
109         raise AssertionError('Cannot find this resource')
110     return resource_name
111
112
113 def text(response):
114     """Return whole resource in text."""
115     return response.text
116
117
118 def last_modified_time(response):
119     """Return resource lastModifiedTime."""
120     return ciotdm.lastModifiedTime(response)
121
122
123 def state_tag(response):
124     """Return resource state tag."""
125     return ciotdm.stateTag(response)
126
127
128 def current_number_of_instances(response):
129     """Return current number of instances."""
130     return ciotdm.currentNumberOfInstances(response)
131
132
133 def current_byte_size(response):
134     """Return current byte size."""
135     return ciotdm.currentByteSize(response)
136
137
138 def max_number_of_instances(response):
139     """Return max number of instances."""
140     return ciotdm.maxNumberOfInstances(response)
141
142
143 def content(response):
144     """Return content child from response."""
145     return ciotdm.content(response)
146
147
148 def max_byte_size(response):
149     """Return max byte size."""
150     return ciotdm.maxByteSize(response)
151
152
153 def status_code(response):
154     """Return resource status_code."""
155     return response.status_code
156
157
158 def json(response):
159     """Return resource in json format."""
160     return response.json()
161
162
163 def elapsed(response):
164     """Return resource elapsed."""
165     return response.elapsed.total_seconds()
166
167
168 def location(response):
169     """Return response content-location."""
170     return response.headers['Content-Location']
171
172
173 def kill_the_tree(host, cseid, username, password):
174     """Delete the whole tree."""
175     connection = ciotdm.connect(host, base=cseid,
176                                 auth=(username, password), protocol="http")
177     connection.kill()
178
179
180 def check_response(response, operation):
181     """Check whether the connection is none."""
182     if response is None:
183         raise AssertionError('Cannot %s this resource') % (operation)
184     elif hasattr(response, 'status_code'):
185         if response.status_code < 200 or response.status_code > 299:
186             raise AssertionError(
187                 'Cannot %s this resource [%d] : %s' %
188                 (operation, response.status_code, response.text))