Bug fixes with logging and gateway API
This commit is contained in:
parent
84a5fcce4a
commit
ce6eb3778d
@ -22,6 +22,7 @@ import io.jsonwebtoken.Claims;
|
|||||||
import io.jsonwebtoken.Header;
|
import io.jsonwebtoken.Header;
|
||||||
import io.jsonwebtoken.Jwt;
|
import io.jsonwebtoken.Jwt;
|
||||||
import io.jsonwebtoken.Jwts;
|
import io.jsonwebtoken.Jwts;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.hamcrest.Matcher;
|
import org.hamcrest.Matcher;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
@ -85,6 +86,7 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppC
|
|||||||
@ComponentScan({"org.thingsboard.server"})
|
@ComponentScan({"org.thingsboard.server"})
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
|
@Slf4j
|
||||||
public abstract class AbstractControllerTest {
|
public abstract class AbstractControllerTest {
|
||||||
|
|
||||||
protected static final String TEST_TENANT_NAME = "TEST TENANT";
|
protected static final String TEST_TENANT_NAME = "TEST TENANT";
|
||||||
@ -130,6 +132,7 @@ public abstract class AbstractControllerTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() throws Exception {
|
public void setup() throws Exception {
|
||||||
|
log.info("Executing setup");
|
||||||
if (this.mockMvc == null) {
|
if (this.mockMvc == null) {
|
||||||
this.mockMvc = webAppContextSetup(webApplicationContext)
|
this.mockMvc = webAppContextSetup(webApplicationContext)
|
||||||
.apply(springSecurity()).build();
|
.apply(springSecurity()).build();
|
||||||
@ -163,13 +166,16 @@ public abstract class AbstractControllerTest {
|
|||||||
createUserAndLogin(customerUser, CUSTOMER_USER_PASSWORD);
|
createUserAndLogin(customerUser, CUSTOMER_USER_PASSWORD);
|
||||||
|
|
||||||
logout();
|
logout();
|
||||||
|
log.info("Executed setup");
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void teardown() throws Exception {
|
public void teardown() throws Exception {
|
||||||
|
log.info("Executing teardown");
|
||||||
loginSysAdmin();
|
loginSysAdmin();
|
||||||
doDelete("/api/tenant/"+tenantId.getId().toString())
|
doDelete("/api/tenant/"+tenantId.getId().toString())
|
||||||
.andExpect(status().isOk());
|
.andExpect(status().isOk());
|
||||||
|
log.info("Executed teardown");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void loginSysAdmin() throws Exception {
|
protected void loginSysAdmin() throws Exception {
|
||||||
|
|||||||
@ -15,19 +15,14 @@
|
|||||||
*/
|
*/
|
||||||
package org.thingsboard.server.controller;
|
package org.thingsboard.server.controller;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.containsString;
|
|
||||||
import static org.hamcrest.Matchers.is;
|
|
||||||
import static org.hamcrest.Matchers.notNullValue;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
||||||
|
|
||||||
import org.thingsboard.server.common.data.AdminSettings;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.thingsboard.server.common.data.AdminSettings;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.*;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
|
|
||||||
|
|
||||||
public abstract class BaseAdminControllerTest extends AbstractControllerTest {
|
public abstract class BaseAdminControllerTest extends AbstractControllerTest {
|
||||||
|
|
||||||
|
|||||||
@ -7,8 +7,9 @@
|
|||||||
</encoder>
|
</encoder>
|
||||||
</appender>
|
</appender>
|
||||||
|
|
||||||
<logger name="org.thingsboard.server" level="WARN"/>
|
<logger name="org.thingsboard.server" level="INFO"/>
|
||||||
<logger name="org.springframework" level="WARN"/>
|
<logger name="org.springframework" level="WARN"/>
|
||||||
|
<logger name="org.springframework.boot.test" level="DEBUG"/>
|
||||||
<logger name="org.apache.cassandra" level="WARN"/>
|
<logger name="org.apache.cassandra" level="WARN"/>
|
||||||
<logger name="org.cassandraunit" level="INFO"/>
|
<logger name="org.cassandraunit" level="INFO"/>
|
||||||
|
|
||||||
|
|||||||
2
pom.xml
2
pom.xml
@ -37,7 +37,7 @@
|
|||||||
<json-path.version>2.2.0</json-path.version>
|
<json-path.version>2.2.0</json-path.version>
|
||||||
<junit.version>4.12</junit.version>
|
<junit.version>4.12</junit.version>
|
||||||
<slf4j.version>1.7.7</slf4j.version>
|
<slf4j.version>1.7.7</slf4j.version>
|
||||||
<logback.version>1.1.7</logback.version>
|
<logback.version>1.2.3</logback.version>
|
||||||
<mockito.version>1.9.5</mockito.version>
|
<mockito.version>1.9.5</mockito.version>
|
||||||
<rat.version>0.10</rat.version>
|
<rat.version>0.10</rat.version>
|
||||||
<cassandra.version>3.0.0</cassandra.version>
|
<cassandra.version>3.0.0</cassandra.version>
|
||||||
|
|||||||
@ -55,6 +55,7 @@ import static org.thingsboard.server.transport.mqtt.adaptors.JsonMqttAdaptor.val
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class GatewaySessionCtx {
|
public class GatewaySessionCtx {
|
||||||
|
|
||||||
|
private static final String DEFAULT_DEVICE_TYPE = "default";
|
||||||
private final Device gateway;
|
private final Device gateway;
|
||||||
private final SessionId gatewaySessionId;
|
private final SessionId gatewaySessionId;
|
||||||
private final SessionMsgProcessor processor;
|
private final SessionMsgProcessor processor;
|
||||||
@ -78,6 +79,11 @@ public class GatewaySessionCtx {
|
|||||||
JsonElement json = getJson(msg);
|
JsonElement json = getJson(msg);
|
||||||
String deviceName = checkDeviceName(getDeviceName(json));
|
String deviceName = checkDeviceName(getDeviceName(json));
|
||||||
String deviceType = getDeviceType(json);
|
String deviceType = getDeviceType(json);
|
||||||
|
onDeviceConnect(deviceName, deviceType);
|
||||||
|
ack(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onDeviceConnect(String deviceName, String deviceType) {
|
||||||
if (!devices.containsKey(deviceName)) {
|
if (!devices.containsKey(deviceName)) {
|
||||||
Optional<Device> deviceOpt = deviceService.findDeviceByTenantIdAndName(gateway.getTenantId(), deviceName);
|
Optional<Device> deviceOpt = deviceService.findDeviceByTenantIdAndName(gateway.getTenantId(), deviceName);
|
||||||
Device device = deviceOpt.orElseGet(() -> {
|
Device device = deviceOpt.orElseGet(() -> {
|
||||||
@ -95,7 +101,6 @@ public class GatewaySessionCtx {
|
|||||||
processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new AttributesSubscribeMsg())));
|
processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new AttributesSubscribeMsg())));
|
||||||
processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new RpcSubscribeMsg())));
|
processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new RpcSubscribeMsg())));
|
||||||
}
|
}
|
||||||
ack(msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onDeviceDisconnect(MqttPublishMessage msg) throws AdaptorException {
|
public void onDeviceDisconnect(MqttPublishMessage msg) throws AdaptorException {
|
||||||
@ -205,10 +210,9 @@ public class GatewaySessionCtx {
|
|||||||
private String checkDeviceConnected(String deviceName) {
|
private String checkDeviceConnected(String deviceName) {
|
||||||
if (!devices.containsKey(deviceName)) {
|
if (!devices.containsKey(deviceName)) {
|
||||||
log.debug("[{}] Missing device [{}] for the gateway session", gatewaySessionId, deviceName);
|
log.debug("[{}] Missing device [{}] for the gateway session", gatewaySessionId, deviceName);
|
||||||
throw new RuntimeException("Device " + deviceName + " is not connected!");
|
onDeviceConnect(deviceName, DEFAULT_DEVICE_TYPE);
|
||||||
} else {
|
|
||||||
return deviceName;
|
|
||||||
}
|
}
|
||||||
|
return deviceName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String checkDeviceName(String deviceName) {
|
private String checkDeviceName(String deviceName) {
|
||||||
@ -225,7 +229,7 @@ public class GatewaySessionCtx {
|
|||||||
|
|
||||||
private String getDeviceType(JsonElement json) throws AdaptorException {
|
private String getDeviceType(JsonElement json) throws AdaptorException {
|
||||||
JsonElement type = json.getAsJsonObject().get("type");
|
JsonElement type = json.getAsJsonObject().get("type");
|
||||||
return type == null ? "default" : type.getAsString();
|
return type == null ? DEFAULT_DEVICE_TYPE : type.getAsString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsonElement getJson(MqttPublishMessage mqttMsg) throws AdaptorException {
|
private JsonElement getJson(MqttPublishMessage mqttMsg) throws AdaptorException {
|
||||||
@ -236,7 +240,7 @@ public class GatewaySessionCtx {
|
|||||||
return processor;
|
return processor;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected DeviceAuthService getAuthService() {
|
DeviceAuthService getAuthService() {
|
||||||
return authService;
|
return authService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,7 +254,7 @@ public class GatewaySessionCtx {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void writeAndFlush(MqttMessage mqttMessage) {
|
void writeAndFlush(MqttMessage mqttMessage) {
|
||||||
channel.writeAndFlush(mqttMessage);
|
channel.writeAndFlush(mqttMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user