|
1
|
+/**
|
|
2
|
+ * Copyright © 2016-2020 The Thingsboard Authors
|
|
3
|
+ *
|
|
4
|
+ * Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+ * you may not use this file except in compliance with the License.
|
|
6
|
+ * You may obtain a copy of the License at
|
|
7
|
+ *
|
|
8
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+ *
|
|
10
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
11
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+ * See the License for the specific language governing permissions and
|
|
14
|
+ * limitations under the License.
|
|
15
|
+ */
|
|
16
|
+package org.thingsboard.server.controller;
|
|
17
|
+
|
|
18
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
19
|
+import org.junit.After;
|
|
20
|
+import org.junit.Assert;
|
|
21
|
+import org.junit.Before;
|
|
22
|
+import org.junit.Test;
|
|
23
|
+import org.thingsboard.server.common.data.DataConstants;
|
|
24
|
+import org.thingsboard.server.common.data.Device;
|
|
25
|
+import org.thingsboard.server.common.data.EntityType;
|
|
26
|
+import org.thingsboard.server.common.data.Tenant;
|
|
27
|
+import org.thingsboard.server.common.data.User;
|
|
28
|
+import org.thingsboard.server.common.data.id.DeviceId;
|
|
29
|
+import org.thingsboard.server.common.data.id.EntityId;
|
|
30
|
+import org.thingsboard.server.common.data.page.PageData;
|
|
31
|
+import org.thingsboard.server.common.data.query.DeviceTypeFilter;
|
|
32
|
+import org.thingsboard.server.common.data.query.EntityCountQuery;
|
|
33
|
+import org.thingsboard.server.common.data.query.EntityData;
|
|
34
|
+import org.thingsboard.server.common.data.query.EntityDataPageLink;
|
|
35
|
+import org.thingsboard.server.common.data.query.EntityDataQuery;
|
|
36
|
+import org.thingsboard.server.common.data.query.EntityDataSortOrder;
|
|
37
|
+import org.thingsboard.server.common.data.query.EntityKey;
|
|
38
|
+import org.thingsboard.server.common.data.query.EntityKeyType;
|
|
39
|
+import org.thingsboard.server.common.data.query.EntityListFilter;
|
|
40
|
+import org.thingsboard.server.common.data.query.KeyFilter;
|
|
41
|
+import org.thingsboard.server.common.data.query.NumericFilterPredicate;
|
|
42
|
+import org.thingsboard.server.common.data.security.Authority;
|
|
43
|
+
|
|
44
|
+import java.util.ArrayList;
|
|
45
|
+import java.util.Collections;
|
|
46
|
+import java.util.List;
|
|
47
|
+import java.util.stream.Collectors;
|
|
48
|
+
|
|
49
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
50
|
+
|
|
51
|
+public abstract class BaseEntityQueryControllerTest extends AbstractControllerTest {
|
|
52
|
+
|
|
53
|
+ private Tenant savedTenant;
|
|
54
|
+ private User tenantAdmin;
|
|
55
|
+
|
|
56
|
+ @Before
|
|
57
|
+ public void beforeTest() throws Exception {
|
|
58
|
+ loginSysAdmin();
|
|
59
|
+
|
|
60
|
+ Tenant tenant = new Tenant();
|
|
61
|
+ tenant.setTitle("My tenant");
|
|
62
|
+ savedTenant = doPost("/api/tenant", tenant, Tenant.class);
|
|
63
|
+ Assert.assertNotNull(savedTenant);
|
|
64
|
+
|
|
65
|
+ tenantAdmin = new User();
|
|
66
|
+ tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
|
|
67
|
+ tenantAdmin.setTenantId(savedTenant.getId());
|
|
68
|
+ tenantAdmin.setEmail("tenant2@thingsboard.org");
|
|
69
|
+ tenantAdmin.setFirstName("Joe");
|
|
70
|
+ tenantAdmin.setLastName("Downs");
|
|
71
|
+
|
|
72
|
+ tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ @After
|
|
76
|
+ public void afterTest() throws Exception {
|
|
77
|
+ loginSysAdmin();
|
|
78
|
+
|
|
79
|
+ doDelete("/api/tenant/" + savedTenant.getId().getId().toString())
|
|
80
|
+ .andExpect(status().isOk());
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ @Test
|
|
84
|
+ public void testCountEntitiesByQuery() throws Exception {
|
|
85
|
+ List<Device> devices = new ArrayList<>();
|
|
86
|
+ for (int i = 0; i < 97; i++) {
|
|
87
|
+ Device device = new Device();
|
|
88
|
+ device.setName("Device" + i);
|
|
89
|
+ device.setType("default");
|
|
90
|
+ device.setLabel("testLabel" + (int) (Math.random() * 1000));
|
|
91
|
+ devices.add(doPost("/api/device", device, Device.class));
|
|
92
|
+ }
|
|
93
|
+ DeviceTypeFilter filter = new DeviceTypeFilter();
|
|
94
|
+ filter.setDeviceType("default");
|
|
95
|
+ filter.setDeviceNameFilter("");
|
|
96
|
+
|
|
97
|
+ EntityCountQuery countQuery = new EntityCountQuery(filter);
|
|
98
|
+
|
|
99
|
+ Long count = doPostWithResponse("/api/entitiesQuery/count", countQuery, Long.class);
|
|
100
|
+ Assert.assertEquals(97, count.longValue());
|
|
101
|
+
|
|
102
|
+ filter.setDeviceType("unknown");
|
|
103
|
+ count = doPostWithResponse("/api/entitiesQuery/count", countQuery, Long.class);
|
|
104
|
+ Assert.assertEquals(0, count.longValue());
|
|
105
|
+
|
|
106
|
+ filter.setDeviceType("default");
|
|
107
|
+ filter.setDeviceNameFilter("Device1");
|
|
108
|
+
|
|
109
|
+ count = doPostWithResponse("/api/entitiesQuery/count", countQuery, Long.class);
|
|
110
|
+ Assert.assertEquals(11, count.longValue());
|
|
111
|
+
|
|
112
|
+ EntityListFilter entityListFilter = new EntityListFilter();
|
|
113
|
+ entityListFilter.setEntityType(EntityType.DEVICE);
|
|
114
|
+ entityListFilter.setEntityList(devices.stream().map(Device::getId).map(DeviceId::toString).collect(Collectors.toList()));
|
|
115
|
+
|
|
116
|
+ countQuery = new EntityCountQuery(entityListFilter);
|
|
117
|
+
|
|
118
|
+ count = doPostWithResponse("/api/entitiesQuery/count", countQuery, Long.class);
|
|
119
|
+ Assert.assertEquals(97, count.longValue());
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ @Test
|
|
123
|
+ public void testSimpleFindEntityDataByQuery() throws Exception {
|
|
124
|
+ List<Device> devices = new ArrayList<>();
|
|
125
|
+ for (int i = 0; i < 97; i++) {
|
|
126
|
+ Device device = new Device();
|
|
127
|
+ device.setName("Device" + i);
|
|
128
|
+ device.setType("default");
|
|
129
|
+ device.setLabel("testLabel" + (int) (Math.random() * 1000));
|
|
130
|
+ devices.add(doPost("/api/device", device, Device.class));
|
|
131
|
+ }
|
|
132
|
+
|
|
133
|
+ DeviceTypeFilter filter = new DeviceTypeFilter();
|
|
134
|
+ filter.setDeviceType("default");
|
|
135
|
+ filter.setDeviceNameFilter("");
|
|
136
|
+
|
|
137
|
+ EntityDataSortOrder sortOrder = new EntityDataSortOrder(
|
|
138
|
+ new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.ASC
|
|
139
|
+ );
|
|
140
|
+ EntityDataPageLink pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
|
|
141
|
+ List<EntityKey> entityFields = Collections.singletonList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"));
|
|
142
|
+
|
|
143
|
+ EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, null, null);
|
|
144
|
+
|
|
145
|
+ PageData<EntityData> data =
|
|
146
|
+ doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
|
|
147
|
+ });
|
|
148
|
+
|
|
149
|
+ Assert.assertEquals(97, data.getTotalElements());
|
|
150
|
+ Assert.assertEquals(10, data.getTotalPages());
|
|
151
|
+ Assert.assertTrue(data.hasNext());
|
|
152
|
+ Assert.assertEquals(10, data.getData().size());
|
|
153
|
+
|
|
154
|
+ List<EntityData> loadedEntities = new ArrayList<>(data.getData());
|
|
155
|
+ while (data.hasNext()) {
|
|
156
|
+ query = query.next();
|
|
157
|
+ data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
|
|
158
|
+ });
|
|
159
|
+ loadedEntities.addAll(data.getData());
|
|
160
|
+ }
|
|
161
|
+ Assert.assertEquals(97, loadedEntities.size());
|
|
162
|
+
|
|
163
|
+ List<EntityId> loadedIds = loadedEntities.stream().map(EntityData::getEntityId).collect(Collectors.toList());
|
|
164
|
+ List<EntityId> deviceIds = devices.stream().map(Device::getId).collect(Collectors.toList());
|
|
165
|
+
|
|
166
|
+ Assert.assertEquals(deviceIds, loadedIds);
|
|
167
|
+
|
|
168
|
+ List<String> loadedNames = loadedEntities.stream().map(entityData ->
|
|
169
|
+ entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
|
|
170
|
+ List<String> deviceNames = devices.stream().map(Device::getName).collect(Collectors.toList());
|
|
171
|
+
|
|
172
|
+ Assert.assertEquals(deviceNames, loadedNames);
|
|
173
|
+
|
|
174
|
+ sortOrder = new EntityDataSortOrder(
|
|
175
|
+ new EntityKey(EntityKeyType.ENTITY_FIELD, "name"), EntityDataSortOrder.Direction.DESC
|
|
176
|
+ );
|
|
177
|
+
|
|
178
|
+ pageLink = new EntityDataPageLink(10, 0, "device1", sortOrder);
|
|
179
|
+ query = new EntityDataQuery(filter, pageLink, entityFields, null, null);
|
|
180
|
+ data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
|
|
181
|
+ });
|
|
182
|
+ Assert.assertEquals(11, data.getTotalElements());
|
|
183
|
+ Assert.assertEquals("Device19", data.getData().get(0).getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue());
|
|
184
|
+
|
|
185
|
+ }
|
|
186
|
+
|
|
187
|
+ @Test
|
|
188
|
+ public void testFindEntityDataByQueryWithAttributes() throws Exception {
|
|
189
|
+
|
|
190
|
+ List<Device> devices = new ArrayList<>();
|
|
191
|
+ List<Long> temperatures = new ArrayList<>();
|
|
192
|
+ List<Long> highTemperatures = new ArrayList<>();
|
|
193
|
+ for (int i=0;i<67;i++) {
|
|
194
|
+ Device device = new Device();
|
|
195
|
+ String name = "Device"+i;
|
|
196
|
+ device.setName(name);
|
|
197
|
+ device.setType("default");
|
|
198
|
+ device.setLabel("testLabel"+(int)(Math.random()*1000));
|
|
199
|
+ devices.add(doPost("/api/device?accessToken="+name, device, Device.class));
|
|
200
|
+ long temperature = (long)(Math.random()*100);
|
|
201
|
+ temperatures.add(temperature);
|
|
202
|
+ if (temperature > 45) {
|
|
203
|
+ highTemperatures.add(temperature);
|
|
204
|
+ }
|
|
205
|
+ }
|
|
206
|
+ for (int i=0;i<devices.size();i++) {
|
|
207
|
+ Device device = devices.get(i);
|
|
208
|
+ String payload = "{\"temperature\":"+temperatures.get(i)+"}";
|
|
209
|
+ doPost("/api/plugins/telemetry/"+device.getId()+"/"+ DataConstants.SHARED_SCOPE, payload, String.class, status().isOk());
|
|
210
|
+ }
|
|
211
|
+ Thread.sleep(1000);
|
|
212
|
+
|
|
213
|
+ DeviceTypeFilter filter = new DeviceTypeFilter();
|
|
214
|
+ filter.setDeviceType("default");
|
|
215
|
+ filter.setDeviceNameFilter("");
|
|
216
|
+
|
|
217
|
+ EntityDataSortOrder sortOrder = new EntityDataSortOrder(
|
|
218
|
+ new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.ASC
|
|
219
|
+ );
|
|
220
|
+ EntityDataPageLink pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
|
|
221
|
+ List<EntityKey> entityFields = Collections.singletonList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"));
|
|
222
|
+ List<EntityKey> latestValues = Collections.singletonList(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
|
|
223
|
+
|
|
224
|
+ EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, null);
|
|
225
|
+ PageData<EntityData> data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
|
|
226
|
+ });
|
|
227
|
+
|
|
228
|
+ List<EntityData> loadedEntities = new ArrayList<>(data.getData());
|
|
229
|
+ while (data.hasNext()) {
|
|
230
|
+ query = query.next();
|
|
231
|
+ data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
|
|
232
|
+ });
|
|
233
|
+ loadedEntities.addAll(data.getData());
|
|
234
|
+ }
|
|
235
|
+ Assert.assertEquals(67, loadedEntities.size());
|
|
236
|
+
|
|
237
|
+ List<String> loadedTemperatures = loadedEntities.stream().map(entityData ->
|
|
238
|
+ entityData.getLatest().get(EntityKeyType.ATTRIBUTE).get("temperature").getValue()).collect(Collectors.toList());
|
|
239
|
+ List<String> deviceTemperatures = temperatures.stream().map(aLong -> Long.toString(aLong)).collect(Collectors.toList());
|
|
240
|
+ Assert.assertEquals(deviceTemperatures, loadedTemperatures);
|
|
241
|
+
|
|
242
|
+ pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
|
|
243
|
+ KeyFilter highTemperatureFilter = new KeyFilter();
|
|
244
|
+ highTemperatureFilter.setKey(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
|
|
245
|
+ NumericFilterPredicate predicate = new NumericFilterPredicate();
|
|
246
|
+ predicate.setValue(45);
|
|
247
|
+ predicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
|
|
248
|
+ highTemperatureFilter.setPredicate(predicate);
|
|
249
|
+ List<KeyFilter> keyFilters = Collections.singletonList(highTemperatureFilter);
|
|
250
|
+
|
|
251
|
+ query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, keyFilters);
|
|
252
|
+
|
|
253
|
+ data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
|
|
254
|
+ });
|
|
255
|
+ loadedEntities = new ArrayList<>(data.getData());
|
|
256
|
+ while (data.hasNext()) {
|
|
257
|
+ query = query.next();
|
|
258
|
+ data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
|
|
259
|
+ });
|
|
260
|
+ loadedEntities.addAll(data.getData());
|
|
261
|
+ }
|
|
262
|
+ Assert.assertEquals(highTemperatures.size(), loadedEntities.size());
|
|
263
|
+
|
|
264
|
+ List<String> loadedHighTemperatures = loadedEntities.stream().map(entityData ->
|
|
265
|
+ entityData.getLatest().get(EntityKeyType.ATTRIBUTE).get("temperature").getValue()).collect(Collectors.toList());
|
|
266
|
+ List<String> deviceHighTemperatures = highTemperatures.stream().map(aLong -> Long.toString(aLong)).collect(Collectors.toList());
|
|
267
|
+
|
|
268
|
+ Assert.assertEquals(deviceHighTemperatures, loadedHighTemperatures);
|
|
269
|
+
|
|
270
|
+ }
|
|
271
|
+} |
...
|
...
|
|