Commit 6d0dc981a0837defaa37c30f2a0edbf4f62fccaf
1 parent
2ec09e08
Adding tests for device provisioning feature
Showing
6 changed files
with
469 additions
and
4 deletions
... | ... | @@ -25,13 +25,18 @@ import org.junit.Assert; |
25 | 25 | import org.springframework.util.StringUtils; |
26 | 26 | import org.thingsboard.server.common.data.Device; |
27 | 27 | import org.thingsboard.server.common.data.DeviceProfile; |
28 | +import org.thingsboard.server.common.data.DeviceProfileProvisionType; | |
28 | 29 | import org.thingsboard.server.common.data.DeviceProfileType; |
29 | 30 | import org.thingsboard.server.common.data.DeviceTransportType; |
30 | 31 | import org.thingsboard.server.common.data.Tenant; |
31 | 32 | import org.thingsboard.server.common.data.TransportPayloadType; |
32 | 33 | import org.thingsboard.server.common.data.User; |
34 | +import org.thingsboard.server.common.data.device.profile.AllowCreateNewDevicesDeviceProfileProvisionConfiguration; | |
35 | +import org.thingsboard.server.common.data.device.profile.CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration; | |
33 | 36 | import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration; |
34 | 37 | import org.thingsboard.server.common.data.device.profile.DeviceProfileData; |
38 | +import org.thingsboard.server.common.data.device.profile.DeviceProfileProvisionConfiguration; | |
39 | +import org.thingsboard.server.common.data.device.profile.DisabledDeviceProfileProvisionConfiguration; | |
35 | 40 | import org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration; |
36 | 41 | import org.thingsboard.server.common.data.security.Authority; |
37 | 42 | import org.thingsboard.server.common.data.security.DeviceCredentials; |
... | ... | @@ -62,7 +67,18 @@ public abstract class AbstractMqttIntegrationTest extends AbstractControllerTest |
62 | 67 | protected Device savedGateway; |
63 | 68 | protected String gatewayAccessToken; |
64 | 69 | |
65 | - protected void processBeforeTest(String deviceName, String gatewayName, TransportPayloadType payloadType, String telemetryTopic, String attributesTopic) throws Exception { | |
70 | + protected void processBeforeTest (String deviceName, String gatewayName, TransportPayloadType payloadType, String telemetryTopic, String attributesTopic) throws Exception { | |
71 | + this.processBeforeTest(deviceName, gatewayName, payloadType, telemetryTopic, attributesTopic, null, null, null); | |
72 | + } | |
73 | + | |
74 | + protected void processBeforeTest(String deviceName, | |
75 | + String gatewayName, | |
76 | + TransportPayloadType payloadType, | |
77 | + String telemetryTopic, | |
78 | + String attributesTopic, | |
79 | + DeviceProfileProvisionType provisionType, | |
80 | + String provisionKey, String provisionSecret | |
81 | + ) throws Exception { | |
66 | 82 | loginSysAdmin(); |
67 | 83 | |
68 | 84 | Tenant tenant = new Tenant(); |
... | ... | @@ -91,7 +107,7 @@ public abstract class AbstractMqttIntegrationTest extends AbstractControllerTest |
91 | 107 | gateway.setAdditionalInfo(additionalInfo); |
92 | 108 | |
93 | 109 | if (payloadType != null) { |
94 | - DeviceProfile mqttDeviceProfile = createMqttDeviceProfile(payloadType, telemetryTopic, attributesTopic); | |
110 | + DeviceProfile mqttDeviceProfile = createMqttDeviceProfile(payloadType, telemetryTopic, attributesTopic, provisionType, provisionKey, provisionSecret); | |
95 | 111 | DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", mqttDeviceProfile, DeviceProfile.class); |
96 | 112 | device.setType(savedDeviceProfile.getName()); |
97 | 113 | device.setDeviceProfileId(savedDeviceProfile.getId()); |
... | ... | @@ -181,11 +197,23 @@ public abstract class AbstractMqttIntegrationTest extends AbstractControllerTest |
181 | 197 | return keyValueProtoBuilder.build(); |
182 | 198 | } |
183 | 199 | |
184 | - protected DeviceProfile createMqttDeviceProfile(TransportPayloadType transportPayloadType, String telemetryTopic, String attributesTopic) { | |
200 | + protected DeviceProfile createMqttDeviceProfile(TransportPayloadType transportPayloadType, | |
201 | + String telemetryTopic, String attributesTopic | |
202 | + ) { | |
203 | + return this.createMqttDeviceProfile(transportPayloadType, telemetryTopic, attributesTopic, null, null, null); | |
204 | + } | |
205 | + | |
206 | + protected DeviceProfile createMqttDeviceProfile(TransportPayloadType transportPayloadType, | |
207 | + String telemetryTopic, String attributesTopic, | |
208 | + DeviceProfileProvisionType provisionType, | |
209 | + String provisionKey, String provisionSecret | |
210 | + ) { | |
185 | 211 | DeviceProfile deviceProfile = new DeviceProfile(); |
186 | 212 | deviceProfile.setName(transportPayloadType.name()); |
187 | 213 | deviceProfile.setType(DeviceProfileType.DEFAULT); |
188 | 214 | deviceProfile.setTransportType(DeviceTransportType.MQTT); |
215 | + deviceProfile.setProvisionType(provisionType); | |
216 | + deviceProfile.setProvisionDeviceKey(provisionKey); | |
189 | 217 | deviceProfile.setDescription(transportPayloadType.name() + " Test"); |
190 | 218 | DeviceProfileData deviceProfileData = new DeviceProfileData(); |
191 | 219 | DefaultDeviceProfileConfiguration configuration = new DefaultDeviceProfileConfiguration(); |
... | ... | @@ -198,6 +226,19 @@ public abstract class AbstractMqttIntegrationTest extends AbstractControllerTest |
198 | 226 | transportConfiguration.setDeviceAttributesTopic(attributesTopic); |
199 | 227 | } |
200 | 228 | deviceProfileData.setTransportConfiguration(transportConfiguration); |
229 | + DeviceProfileProvisionConfiguration provisionConfiguration; | |
230 | + switch (provisionType) { | |
231 | + case ALLOW_CREATE_NEW_DEVICES: | |
232 | + provisionConfiguration = new AllowCreateNewDevicesDeviceProfileProvisionConfiguration(provisionSecret); | |
233 | + break; | |
234 | + case CHECK_PRE_PROVISIONED_DEVICES: | |
235 | + provisionConfiguration = new CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration(provisionSecret); | |
236 | + break; | |
237 | + case DISABLED: | |
238 | + default: | |
239 | + provisionConfiguration = new DisabledDeviceProfileProvisionConfiguration(provisionSecret); | |
240 | + } | |
241 | + deviceProfileData.setProvisionConfiguration(provisionConfiguration); | |
201 | 242 | deviceProfileData.setConfiguration(configuration); |
202 | 243 | deviceProfile.setProfileData(deviceProfileData); |
203 | 244 | deviceProfile.setDefault(false); | ... | ... |
... | ... | @@ -31,7 +31,8 @@ import java.util.Arrays; |
31 | 31 | "org.thingsboard.server.mqtt.telemetry.attributes.sql.*Test", |
32 | 32 | "org.thingsboard.server.mqtt.attributes.updates.sql.*Test", |
33 | 33 | "org.thingsboard.server.mqtt.attributes.request.sql.*Test", |
34 | - "org.thingsboard.server.mqtt.claim.sql.*Test" | |
34 | + "org.thingsboard.server.mqtt.claim.sql.*Test", | |
35 | + "org.thingsboard.server.mqtt.provision.sql.*Test" | |
35 | 36 | }) |
36 | 37 | public class MqttSqlTestSuite { |
37 | 38 | ... | ... |
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.mqtt.provision; | |
17 | + | |
18 | +import com.google.gson.JsonObject; | |
19 | +import io.netty.handler.codec.mqtt.MqttQoS; | |
20 | +import lombok.extern.slf4j.Slf4j; | |
21 | +import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; | |
22 | +import org.eclipse.paho.client.mqttv3.MqttAsyncClient; | |
23 | +import org.eclipse.paho.client.mqttv3.MqttCallback; | |
24 | +import org.eclipse.paho.client.mqttv3.MqttMessage; | |
25 | +import org.junit.After; | |
26 | +import org.junit.Assert; | |
27 | +import org.junit.Test; | |
28 | +import org.springframework.beans.factory.annotation.Autowired; | |
29 | +import org.thingsboard.server.common.data.Device; | |
30 | +import org.thingsboard.server.common.data.DeviceProfileProvisionType; | |
31 | +import org.thingsboard.server.common.data.TransportPayloadType; | |
32 | +import org.thingsboard.server.common.data.device.profile.MqttTopics; | |
33 | +import org.thingsboard.server.common.data.security.DeviceCredentials; | |
34 | +import org.thingsboard.server.common.transport.util.JsonUtils; | |
35 | +import org.thingsboard.server.dao.device.DeviceCredentialsService; | |
36 | +import org.thingsboard.server.dao.device.DeviceService; | |
37 | +import org.thingsboard.server.dao.device.provision.ProvisionResponseStatus; | |
38 | +import org.thingsboard.server.mqtt.AbstractMqttIntegrationTest; | |
39 | + | |
40 | +import java.util.concurrent.CountDownLatch; | |
41 | +import java.util.concurrent.TimeUnit; | |
42 | + | |
43 | +@Slf4j | |
44 | +public abstract class AbstractMqttProvisionJsonDeviceTest extends AbstractMqttIntegrationTest { | |
45 | + | |
46 | + @Autowired | |
47 | + DeviceCredentialsService deviceCredentialsService; | |
48 | + | |
49 | + @Autowired | |
50 | + DeviceService deviceService; | |
51 | + | |
52 | + @After | |
53 | + public void afterTest() throws Exception { | |
54 | + super.processAfterTest(); | |
55 | + } | |
56 | + | |
57 | + @Test | |
58 | + public void testProvisioningDisabledDevice() throws Exception { | |
59 | + processTestProvisioningDisabledDevice(); | |
60 | + } | |
61 | + | |
62 | + @Test | |
63 | + public void testProvisioningCheckPreProvisionedDevice() throws Exception { | |
64 | + processTestProvisioningCheckPreProvisionedDevice(); | |
65 | + } | |
66 | + | |
67 | + @Test | |
68 | + public void testProvisioningCreateNewDevice() throws Exception { | |
69 | + processTestProvisioningCreateNewDevice(); | |
70 | + } | |
71 | + | |
72 | + @Test | |
73 | + public void testProvisioningWithBadKeyDevice() throws Exception { | |
74 | + processTestProvisioningWithBadKeyDevice(); | |
75 | + } | |
76 | + | |
77 | + | |
78 | + protected void processTestProvisioningDisabledDevice() throws Exception { | |
79 | + super.processBeforeTest("Test Provision device", "Test Provision gateway", TransportPayloadType.JSON, null, null, DeviceProfileProvisionType.DISABLED, null, null); | |
80 | + byte[] result = createMqttClientAndPublish().getPayloadBytes(); | |
81 | + JsonObject response = JsonUtils.parse(new String(result)).getAsJsonObject(); | |
82 | + Assert.assertEquals("Provision data was not found!", response.get("errorMsg").getAsString()); | |
83 | + Assert.assertEquals(ProvisionResponseStatus.NOT_FOUND.name(), response.get("provisionDeviceStatus").getAsString()); | |
84 | + } | |
85 | + | |
86 | + | |
87 | + protected void processTestProvisioningCreateNewDevice() throws Exception { | |
88 | + super.processBeforeTest("Test Provision device3", "Test Provision gateway", TransportPayloadType.JSON, null, null, DeviceProfileProvisionType.ALLOW_CREATE_NEW_DEVICES, "testProvisionKey", "testProvisionSecret"); | |
89 | + byte[] result = createMqttClientAndPublish().getPayloadBytes(); | |
90 | + JsonObject response = JsonUtils.parse(new String(result)).getAsJsonObject(); | |
91 | + | |
92 | + Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); | |
93 | + | |
94 | + Assert.assertNotNull(createdDevice); | |
95 | + Assert.assertEquals(createdDevice.getId().toString(), response.get("deviceId").getAsString()); | |
96 | + | |
97 | + DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); | |
98 | + | |
99 | + Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.get("credentialsType").getAsString()); | |
100 | + Assert.assertEquals(deviceCredentials.getCredentialsId(), response.get("credentialsId").getAsString()); | |
101 | + Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.get("provisionDeviceStatus").getAsString()); | |
102 | + } | |
103 | + | |
104 | + protected void processTestProvisioningCheckPreProvisionedDevice() throws Exception { | |
105 | + super.processBeforeTest("Test Provision device", "Test Provision gateway", TransportPayloadType.JSON, null, null, DeviceProfileProvisionType.CHECK_PRE_PROVISIONED_DEVICES, "testProvisionKey", "testProvisionSecret"); | |
106 | + byte[] result = createMqttClientAndPublish().getPayloadBytes(); | |
107 | + JsonObject response = JsonUtils.parse(new String(result)).getAsJsonObject(); | |
108 | + Assert.assertEquals(savedDevice.getId().toString(), response.get("deviceId").getAsString()); | |
109 | + | |
110 | + DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), savedDevice.getId()); | |
111 | + | |
112 | + Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.get("credentialsType").getAsString()); | |
113 | + Assert.assertEquals(deviceCredentials.getCredentialsId(), response.get("credentialsId").getAsString()); | |
114 | + Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.get("provisionDeviceStatus").getAsString()); | |
115 | + } | |
116 | + | |
117 | + protected void processTestProvisioningWithBadKeyDevice() throws Exception { | |
118 | + super.processBeforeTest("Test Provision device", "Test Provision gateway", TransportPayloadType.JSON, null, null, DeviceProfileProvisionType.CHECK_PRE_PROVISIONED_DEVICES, "testProvisionKeyOrig", "testProvisionSecret"); | |
119 | + byte[] result = createMqttClientAndPublish().getPayloadBytes(); | |
120 | + JsonObject response = JsonUtils.parse(new String(result)).getAsJsonObject(); | |
121 | + Assert.assertEquals("Provision data was not found!", response.get("errorMsg").getAsString()); | |
122 | + Assert.assertEquals(ProvisionResponseStatus.NOT_FOUND.name(), response.get("provisionDeviceStatus").getAsString()); | |
123 | + } | |
124 | + | |
125 | + protected TestMqttCallback createMqttClientAndPublish() throws Exception{ | |
126 | + String provisionRequestMsg = createTestProvisionMessage(); | |
127 | + MqttAsyncClient client = getMqttAsyncClient("provision"); | |
128 | + TestMqttCallback onProvisionCallback = getTestMqttCallback(); | |
129 | + client.setCallback(onProvisionCallback); | |
130 | + client.subscribe(MqttTopics.DEVICE_PROVISION_RESPONSE_TOPIC, MqttQoS.AT_MOST_ONCE.value()); | |
131 | + Thread.sleep(2000); | |
132 | + client.publish(MqttTopics.DEVICE_PROVISION_REQUEST_TOPIC, new MqttMessage(provisionRequestMsg.getBytes())); | |
133 | + onProvisionCallback.getLatch().await(3, TimeUnit.SECONDS); | |
134 | + return onProvisionCallback; | |
135 | + } | |
136 | + | |
137 | + | |
138 | + protected TestMqttCallback getTestMqttCallback() { | |
139 | + CountDownLatch latch = new CountDownLatch(1); | |
140 | + return new TestMqttCallback(latch); | |
141 | + } | |
142 | + | |
143 | + | |
144 | + protected static class TestMqttCallback implements MqttCallback { | |
145 | + | |
146 | + private final CountDownLatch latch; | |
147 | + private Integer qoS; | |
148 | + private byte[] payloadBytes; | |
149 | + | |
150 | + TestMqttCallback(CountDownLatch latch) { | |
151 | + this.latch = latch; | |
152 | + } | |
153 | + | |
154 | + public int getQoS() { | |
155 | + return qoS; | |
156 | + } | |
157 | + | |
158 | + public byte[] getPayloadBytes() { | |
159 | + return payloadBytes; | |
160 | + } | |
161 | + | |
162 | + public CountDownLatch getLatch() { | |
163 | + return latch; | |
164 | + } | |
165 | + | |
166 | + @Override | |
167 | + public void connectionLost(Throwable throwable) { | |
168 | + } | |
169 | + | |
170 | + @Override | |
171 | + public void messageArrived(String requestTopic, MqttMessage mqttMessage) throws Exception { | |
172 | + qoS = mqttMessage.getQos(); | |
173 | + payloadBytes = mqttMessage.getPayload(); | |
174 | + latch.countDown(); | |
175 | + } | |
176 | + | |
177 | + @Override | |
178 | + public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { | |
179 | + | |
180 | + } | |
181 | + } | |
182 | + | |
183 | + | |
184 | + protected String createTestProvisionMessage() { | |
185 | + return "{\"deviceName\":\"Test Provision device\",\"provisionDeviceKey\":\"testProvisionKey\", \"provisionDeviceSecret\":\"testProvisionSecret\"}"; | |
186 | + } | |
187 | + | |
188 | +} | ... | ... |
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.mqtt.provision; | |
17 | + | |
18 | +import com.google.gson.JsonObject; | |
19 | +import com.sun.mail.iap.ByteArray; | |
20 | +import io.netty.handler.codec.mqtt.MqttQoS; | |
21 | +import lombok.extern.slf4j.Slf4j; | |
22 | +import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; | |
23 | +import org.eclipse.paho.client.mqttv3.MqttAsyncClient; | |
24 | +import org.eclipse.paho.client.mqttv3.MqttCallback; | |
25 | +import org.eclipse.paho.client.mqttv3.MqttMessage; | |
26 | +import org.junit.After; | |
27 | +import org.junit.Assert; | |
28 | +import org.junit.Test; | |
29 | +import org.springframework.beans.factory.annotation.Autowired; | |
30 | +import org.thingsboard.server.common.data.Device; | |
31 | +import org.thingsboard.server.common.data.DeviceProfileProvisionType; | |
32 | +import org.thingsboard.server.common.data.TransportPayloadType; | |
33 | +import org.thingsboard.server.common.data.device.profile.MqttTopics; | |
34 | +import org.thingsboard.server.common.data.security.DeviceCredentials; | |
35 | +import org.thingsboard.server.common.transport.util.JsonUtils; | |
36 | +import org.thingsboard.server.dao.device.DeviceCredentialsService; | |
37 | +import org.thingsboard.server.dao.device.DeviceService; | |
38 | +import org.thingsboard.server.dao.device.provision.ProvisionResponseStatus; | |
39 | +import org.thingsboard.server.gen.transport.TransportProtos; | |
40 | +import org.thingsboard.server.gen.transport.TransportProtos.ProvisionDeviceCredentialsMsg; | |
41 | +import org.thingsboard.server.gen.transport.TransportProtos.ProvisionDeviceRequestMsg; | |
42 | +import org.thingsboard.server.gen.transport.TransportProtos.ProvisionDeviceResponseMsg; | |
43 | +import org.thingsboard.server.mqtt.AbstractMqttIntegrationTest; | |
44 | + | |
45 | +import java.util.UUID; | |
46 | +import java.util.concurrent.CountDownLatch; | |
47 | +import java.util.concurrent.TimeUnit; | |
48 | + | |
49 | +@Slf4j | |
50 | +public abstract class AbstractMqttProvisionProtoDeviceTest extends AbstractMqttIntegrationTest { | |
51 | + | |
52 | + @Autowired | |
53 | + DeviceCredentialsService deviceCredentialsService; | |
54 | + | |
55 | + @Autowired | |
56 | + DeviceService deviceService; | |
57 | + | |
58 | + @After | |
59 | + public void afterTest() throws Exception { | |
60 | + super.processAfterTest(); | |
61 | + } | |
62 | + | |
63 | + @Test | |
64 | + public void testProvisioningDisabledDevice() throws Exception { | |
65 | + processTestProvisioningDisabledDevice(); | |
66 | + } | |
67 | + | |
68 | + @Test | |
69 | + public void testProvisioningCheckPreProvisionedDevice() throws Exception { | |
70 | + processTestProvisioningCheckPreProvisionedDevice(); | |
71 | + } | |
72 | + | |
73 | + @Test | |
74 | + public void testProvisioningCreateNewDevice() throws Exception { | |
75 | + processTestProvisioningCreateNewDevice(); | |
76 | + } | |
77 | + | |
78 | + @Test | |
79 | + public void testProvisioningWithBadKeyDevice() throws Exception { | |
80 | + processTestProvisioningWithBadKeyDevice(); | |
81 | + } | |
82 | + | |
83 | + | |
84 | + protected void processTestProvisioningDisabledDevice() throws Exception { | |
85 | + super.processBeforeTest("Test Provision device", "Test Provision gateway", TransportPayloadType.PROTOBUF, null, null, DeviceProfileProvisionType.DISABLED, null, null); | |
86 | + ProvisionDeviceResponseMsg result = ProvisionDeviceResponseMsg.parseFrom(createMqttClientAndPublish().getPayloadBytes()); | |
87 | + Assert.assertNotNull(result); | |
88 | + Assert.assertEquals(ProvisionResponseStatus.NOT_FOUND.name(), result.getProvisionResponseStatus().toString()); | |
89 | + } | |
90 | + | |
91 | + | |
92 | + protected void processTestProvisioningCreateNewDevice() throws Exception { | |
93 | + super.processBeforeTest("Test Provision device3", "Test Provision gateway", TransportPayloadType.JSON, null, null, DeviceProfileProvisionType.ALLOW_CREATE_NEW_DEVICES, "testProvisionKey", "testProvisionSecret"); | |
94 | + ProvisionDeviceResponseMsg response = ProvisionDeviceResponseMsg.parseFrom(createMqttClientAndPublish().getPayloadBytes()); | |
95 | + | |
96 | + Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device"); | |
97 | + | |
98 | + Assert.assertNotNull(createdDevice); | |
99 | + Assert.assertEquals(createdDevice.getId().getId(), new UUID(response.getDeviceCredentials().getDeviceIdMSB(), response.getDeviceCredentials().getDeviceIdLSB())); | |
100 | + | |
101 | + DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId()); | |
102 | + | |
103 | + Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.getDeviceCredentials().getCredentialsType().toString()); | |
104 | + Assert.assertEquals(deviceCredentials.getCredentialsId(), response.getDeviceCredentials().getCredentialsId()); | |
105 | + Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.getProvisionResponseStatus().toString()); | |
106 | + } | |
107 | + | |
108 | + protected void processTestProvisioningCheckPreProvisionedDevice() throws Exception { | |
109 | + super.processBeforeTest("Test Provision device", "Test Provision gateway", TransportPayloadType.JSON, null, null, DeviceProfileProvisionType.CHECK_PRE_PROVISIONED_DEVICES, "testProvisionKey", "testProvisionSecret"); | |
110 | + ProvisionDeviceResponseMsg response = ProvisionDeviceResponseMsg.parseFrom(createMqttClientAndPublish().getPayloadBytes()); | |
111 | + Assert.assertEquals(savedDevice.getId().getId(), new UUID(response.getDeviceCredentials().getDeviceIdMSB(), response.getDeviceCredentials().getDeviceIdLSB())); | |
112 | + | |
113 | + DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), savedDevice.getId()); | |
114 | + | |
115 | + Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.getDeviceCredentials().getCredentialsType().toString()); | |
116 | + Assert.assertEquals(deviceCredentials.getCredentialsId(), response.getDeviceCredentials().getCredentialsId()); | |
117 | + Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.getProvisionResponseStatus().toString()); | |
118 | + } | |
119 | + | |
120 | + protected void processTestProvisioningWithBadKeyDevice() throws Exception { | |
121 | + super.processBeforeTest("Test Provision device", "Test Provision gateway", TransportPayloadType.JSON, null, null, DeviceProfileProvisionType.CHECK_PRE_PROVISIONED_DEVICES, "testProvisionKeyOrig", "testProvisionSecret"); | |
122 | + ProvisionDeviceResponseMsg response = ProvisionDeviceResponseMsg.parseFrom(createMqttClientAndPublish().getPayloadBytes()); | |
123 | + Assert.assertEquals(ProvisionResponseStatus.NOT_FOUND.name(), response.getProvisionResponseStatus().toString()); | |
124 | + } | |
125 | + | |
126 | + protected TestMqttCallback createMqttClientAndPublish() throws Exception{ | |
127 | + byte[] provisionRequestMsg = createTestProvisionMessage(); | |
128 | + MqttAsyncClient client = getMqttAsyncClient("provision"); | |
129 | + TestMqttCallback onProvisionCallback = getTestMqttCallback(); | |
130 | + client.setCallback(onProvisionCallback); | |
131 | + client.subscribe(MqttTopics.DEVICE_PROVISION_RESPONSE_TOPIC, MqttQoS.AT_MOST_ONCE.value()); | |
132 | + Thread.sleep(2000); | |
133 | + client.publish(MqttTopics.DEVICE_PROVISION_REQUEST_TOPIC, new MqttMessage(provisionRequestMsg)); | |
134 | + onProvisionCallback.getLatch().await(3, TimeUnit.SECONDS); | |
135 | + return onProvisionCallback; | |
136 | + } | |
137 | + | |
138 | + | |
139 | + protected TestMqttCallback getTestMqttCallback() { | |
140 | + CountDownLatch latch = new CountDownLatch(1); | |
141 | + return new TestMqttCallback(latch); | |
142 | + } | |
143 | + | |
144 | + | |
145 | + protected static class TestMqttCallback implements MqttCallback { | |
146 | + | |
147 | + private final CountDownLatch latch; | |
148 | + private Integer qoS; | |
149 | + private byte[] payloadBytes; | |
150 | + | |
151 | + TestMqttCallback(CountDownLatch latch) { | |
152 | + this.latch = latch; | |
153 | + } | |
154 | + | |
155 | + public int getQoS() { | |
156 | + return qoS; | |
157 | + } | |
158 | + | |
159 | + public byte[] getPayloadBytes() { | |
160 | + return payloadBytes; | |
161 | + } | |
162 | + | |
163 | + public CountDownLatch getLatch() { | |
164 | + return latch; | |
165 | + } | |
166 | + | |
167 | + @Override | |
168 | + public void connectionLost(Throwable throwable) { | |
169 | + } | |
170 | + | |
171 | + @Override | |
172 | + public void messageArrived(String requestTopic, MqttMessage mqttMessage) throws Exception { | |
173 | + qoS = mqttMessage.getQos(); | |
174 | + payloadBytes = mqttMessage.getPayload(); | |
175 | + latch.countDown(); | |
176 | + } | |
177 | + | |
178 | + @Override | |
179 | + public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { | |
180 | + | |
181 | + } | |
182 | + } | |
183 | + | |
184 | + | |
185 | + protected byte[] createTestProvisionMessage() { | |
186 | + return ProvisionDeviceRequestMsg.newBuilder().setX509CertPubKey("").setDeviceName("Test Provision device").setProvisionDeviceCredentialsMsg(ProvisionDeviceCredentialsMsg.newBuilder().setProvisionDeviceKey("testProvisionKey").setProvisionDeviceSecret("testProvisionSecret")).build().toByteArray(); | |
187 | + } | |
188 | + | |
189 | +} | ... | ... |
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.mqtt.provision.sql; | |
17 | + | |
18 | +import org.thingsboard.server.dao.service.DaoSqlTest; | |
19 | +import org.thingsboard.server.mqtt.provision.AbstractMqttProvisionJsonDeviceTest; | |
20 | + | |
21 | +@DaoSqlTest | |
22 | +public class MqttProvisionDeviceJsonSqlTest extends AbstractMqttProvisionJsonDeviceTest { | |
23 | +} | ... | ... |
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.mqtt.provision.sql; | |
17 | + | |
18 | +import org.thingsboard.server.dao.service.DaoSqlTest; | |
19 | +import org.thingsboard.server.mqtt.provision.AbstractMqttProvisionProtoDeviceTest; | |
20 | + | |
21 | +@DaoSqlTest | |
22 | +public class MqttProvisionDeviceProtoSqlTest extends AbstractMqttProvisionProtoDeviceTest { | |
23 | +} | ... | ... |