Update iotdm library and tests.
[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 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 resid(response):
86     """Return resource ID."""
87     return ciotdm.resid(response)
88
89
90 def name(response):
91     """Return resourceName."""
92     resource_name = ciotdm.name(response)
93     if resource_name is None:
94         raise AssertionError('Cannot find this resource')
95     return resource_name
96
97
98 def text(response):
99     """Return whole resource in text."""
100     return response.text
101
102
103 def last_modified_time(response):
104     """Return resource lastModifiedTime."""
105     return ciotdm.lastModifiedTime(response)
106
107
108 def status_code(response):
109     """Return resource status_code."""
110     return response.status_code
111
112
113 def json(response):
114     """Return resource in json format."""
115     return response.json()
116
117
118 def elapsed(response):
119     """Return resource elapsed."""
120     return response.elapsed.total_seconds()
121
122
123 def location(response):
124     """Return response content-location."""
125     return response.headers['Content-Location']
126
127
128 def kill_the_tree(host, cseid, username, password):
129     """Delete the whole tree."""
130     connection = ciotdm.connect(host, base=cseid,
131                                 auth=(username, password), protocol="http")
132     connection.kill()
133
134
135 def check_response(response, operation):
136     """Check whether the connection is none."""
137     if response is None:
138         raise AssertionError('Cannot %s this resource') % (operation)
139     elif hasattr(response, 'status_code'):
140         if response.status_code < 200 or response.status_code > 299:
141             raise AssertionError(
142                 'Cannot %s this resource [%d] : %s' %
143                 (operation, response.status_code, response.text))