43 changed files with 933 additions and 72 deletions
@ -0,0 +1,98 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.queue; |
|||
|
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.params.ParameterizedTest; |
|||
import org.junit.jupiter.params.provider.Arguments; |
|||
import org.junit.jupiter.params.provider.MethodSource; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.rule.RuleNode; |
|||
import org.thingsboard.server.common.msg.queue.RuleEngineException; |
|||
import org.thingsboard.server.common.msg.queue.RuleNodeException; |
|||
import org.thingsboard.server.common.msg.tools.TbRateLimitsException; |
|||
|
|||
import java.util.UUID; |
|||
import java.util.stream.Stream; |
|||
|
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.Mockito.mock; |
|||
import static org.mockito.Mockito.never; |
|||
import static org.mockito.Mockito.spy; |
|||
import static org.mockito.Mockito.verify; |
|||
|
|||
class TbMsgPackCallbackTest { |
|||
|
|||
TenantId tenantId; |
|||
UUID msgId; |
|||
TbMsgPackProcessingContext ctx; |
|||
TbMsgPackCallback callback; |
|||
|
|||
@BeforeEach |
|||
void setUp() { |
|||
tenantId = TenantId.fromUUID(UUID.randomUUID()); |
|||
msgId = UUID.randomUUID(); |
|||
ctx = mock(TbMsgPackProcessingContext.class); |
|||
callback = spy(new TbMsgPackCallback(msgId, tenantId, ctx)); |
|||
} |
|||
|
|||
private static Stream<Arguments> testOnFailure_NotRateLimitException() { |
|||
return Stream.of( |
|||
Arguments.of(new RuleEngineException("rule engine no cause")), |
|||
Arguments.of(new RuleEngineException("rule engine caused 1 lvl", new RuntimeException())), |
|||
Arguments.of(new RuleEngineException("rule engine caused 2 lvl", new RuntimeException(new Exception()))), |
|||
Arguments.of(new RuleEngineException("rule engine caused 2 lvl Throwable", new RuntimeException(new Throwable()))), |
|||
Arguments.of(new RuleNodeException("rule node no cause", "RuleChain", new RuleNode())) |
|||
); |
|||
} |
|||
|
|||
@ParameterizedTest |
|||
@MethodSource |
|||
void testOnFailure_NotRateLimitException(RuleEngineException ree) { |
|||
callback.onFailure(ree); |
|||
|
|||
verify(callback, never()).onRateLimit(any()); |
|||
verify(callback, never()).onSuccess(); |
|||
verify(ctx, never()).onSuccess(any()); |
|||
} |
|||
|
|||
private static Stream<Arguments> testOnFailure_RateLimitException() { |
|||
return Stream.of( |
|||
Arguments.of(new RuleEngineException("caused lvl 1", new TbRateLimitsException(EntityType.ASSET))), |
|||
Arguments.of(new RuleEngineException("caused lvl 2", new RuntimeException(new TbRateLimitsException(EntityType.ASSET)))), |
|||
Arguments.of( |
|||
new RuleEngineException("caused lvl 3", |
|||
new RuntimeException( |
|||
new Exception( |
|||
new TbRateLimitsException(EntityType.ASSET))))) |
|||
); |
|||
} |
|||
|
|||
@ParameterizedTest |
|||
@MethodSource |
|||
void testOnFailure_RateLimitException(RuleEngineException ree) { |
|||
callback.onFailure(ree); |
|||
|
|||
verify(callback).onRateLimit(any()); |
|||
verify(callback).onFailure(any()); |
|||
verify(callback, never()).onSuccess(); |
|||
verify(ctx).onSuccess(msgId); |
|||
verify(ctx).onSuccess(any()); |
|||
verify(ctx, never()).onFailure(any(), any(), any()); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.exception; |
|||
|
|||
public abstract class AbstractRateLimitException extends RuntimeException { |
|||
|
|||
public AbstractRateLimitException() { |
|||
super(); |
|||
} |
|||
|
|||
public AbstractRateLimitException(String message) { |
|||
super(message); |
|||
} |
|||
|
|||
public AbstractRateLimitException(String message, Throwable cause) { |
|||
super(message, cause); |
|||
} |
|||
|
|||
public AbstractRateLimitException(Throwable cause) { |
|||
super(cause); |
|||
} |
|||
|
|||
protected AbstractRateLimitException(String message, Throwable cause, |
|||
boolean enableSuppression, |
|||
boolean writableStackTrace) { |
|||
super(message, cause, enableSuppression, writableStackTrace); |
|||
} |
|||
} |
|||
@ -0,0 +1,189 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.queue.discovery; |
|||
|
|||
import org.apache.curator.framework.CuratorFramework; |
|||
import org.apache.curator.framework.imps.CuratorFrameworkState; |
|||
import org.apache.curator.framework.recipes.cache.ChildData; |
|||
import org.apache.curator.framework.recipes.cache.PathChildrenCache; |
|||
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; |
|||
import org.junit.Before; |
|||
import org.junit.Test; |
|||
import org.junit.runner.RunWith; |
|||
import org.mockito.Mock; |
|||
import org.mockito.Mockito; |
|||
import org.mockito.junit.MockitoJUnitRunner; |
|||
import org.springframework.test.util.ReflectionTestUtils; |
|||
import org.thingsboard.common.util.ThingsBoardThreadFactory; |
|||
import org.thingsboard.server.gen.transport.TransportProtos; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.Collections; |
|||
import java.util.List; |
|||
import java.util.concurrent.Executors; |
|||
import java.util.concurrent.ScheduledExecutorService; |
|||
|
|||
import static org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent.Type.CHILD_ADDED; |
|||
import static org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent.Type.CHILD_REMOVED; |
|||
import static org.junit.Assert.assertEquals; |
|||
import static org.junit.Assert.assertTrue; |
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.ArgumentMatchers.eq; |
|||
import static org.mockito.Mockito.never; |
|||
import static org.mockito.Mockito.reset; |
|||
import static org.mockito.Mockito.times; |
|||
import static org.mockito.Mockito.verify; |
|||
import static org.mockito.Mockito.when; |
|||
|
|||
@RunWith(MockitoJUnitRunner.class) |
|||
public class ZkDiscoveryServiceTest { |
|||
|
|||
@Mock |
|||
private TbServiceInfoProvider serviceInfoProvider; |
|||
|
|||
@Mock |
|||
private PartitionService partitionService; |
|||
|
|||
@Mock |
|||
private CuratorFramework client; |
|||
|
|||
@Mock |
|||
private PathChildrenCache cache; |
|||
|
|||
@Mock |
|||
private CuratorFramework curatorFramework; |
|||
|
|||
private ZkDiscoveryService zkDiscoveryService; |
|||
|
|||
private static final long RECALCULATE_DELAY = 100L; |
|||
|
|||
final TransportProtos.ServiceInfo currentInfo = TransportProtos.ServiceInfo.newBuilder().setServiceId("tb-rule-engine-0").build(); |
|||
final ChildData currentData = new ChildData("/thingsboard/nodes/0000000010", null, currentInfo.toByteArray()); |
|||
final TransportProtos.ServiceInfo childInfo = TransportProtos.ServiceInfo.newBuilder().setServiceId("tb-rule-engine-1").build(); |
|||
final ChildData childData = new ChildData("/thingsboard/nodes/0000000020", null, childInfo.toByteArray()); |
|||
|
|||
@Before |
|||
public void setup() { |
|||
zkDiscoveryService = Mockito.spy(new ZkDiscoveryService(serviceInfoProvider, partitionService)); |
|||
ScheduledExecutorService zkExecutorService = Executors.newSingleThreadScheduledExecutor(ThingsBoardThreadFactory.forName("zk-discovery")); |
|||
when(client.getState()).thenReturn(CuratorFrameworkState.STARTED); |
|||
ReflectionTestUtils.setField(zkDiscoveryService, "stopped", false); |
|||
ReflectionTestUtils.setField(zkDiscoveryService, "client", client); |
|||
ReflectionTestUtils.setField(zkDiscoveryService, "cache", cache); |
|||
ReflectionTestUtils.setField(zkDiscoveryService, "nodePath", "/thingsboard/nodes/0000000010"); |
|||
ReflectionTestUtils.setField(zkDiscoveryService, "zkExecutorService", zkExecutorService); |
|||
ReflectionTestUtils.setField(zkDiscoveryService, "recalculateDelay", RECALCULATE_DELAY); |
|||
ReflectionTestUtils.setField(zkDiscoveryService, "zkDir", "/thingsboard"); |
|||
|
|||
when(serviceInfoProvider.getServiceInfo()).thenReturn(currentInfo); |
|||
|
|||
List<ChildData> dataList = new ArrayList<>(); |
|||
dataList.add(currentData); |
|||
when(cache.getCurrentData()).thenReturn(dataList); |
|||
} |
|||
|
|||
@Test |
|||
public void restartNodeInTimeTest() throws Exception { |
|||
startNode(childData); |
|||
|
|||
verify(partitionService, times(1)).recalculatePartitions(eq(currentInfo), eq(List.of(childInfo))); |
|||
|
|||
reset(partitionService); |
|||
|
|||
stopNode(childData); |
|||
|
|||
assertEquals(1, zkDiscoveryService.delayedTasks.size()); |
|||
|
|||
verify(partitionService, never()).recalculatePartitions(any(), any()); |
|||
|
|||
startNode(childData); |
|||
|
|||
verify(partitionService, never()).recalculatePartitions(any(), any()); |
|||
|
|||
Thread.sleep(RECALCULATE_DELAY * 2); |
|||
|
|||
verify(partitionService, never()).recalculatePartitions(any(), any()); |
|||
|
|||
assertTrue(zkDiscoveryService.delayedTasks.isEmpty()); |
|||
} |
|||
|
|||
@Test |
|||
public void restartNodeNotInTimeTest() throws Exception { |
|||
startNode(childData); |
|||
|
|||
verify(partitionService, times(1)).recalculatePartitions(eq(currentInfo), eq(List.of(childInfo))); |
|||
|
|||
reset(partitionService); |
|||
|
|||
stopNode(childData); |
|||
|
|||
assertEquals(1, zkDiscoveryService.delayedTasks.size()); |
|||
|
|||
Thread.sleep(RECALCULATE_DELAY * 2); |
|||
|
|||
assertTrue(zkDiscoveryService.delayedTasks.isEmpty()); |
|||
|
|||
startNode(childData); |
|||
|
|||
verify(partitionService, times(1)).recalculatePartitions(eq(currentInfo), eq(Collections.emptyList())); |
|||
|
|||
verify(partitionService, times(1)).recalculatePartitions(eq(currentInfo), eq(List.of(childInfo))); |
|||
|
|||
reset(partitionService); |
|||
} |
|||
|
|||
@Test |
|||
public void startAnotherNodeDuringRestartTest() throws Exception { |
|||
var anotherInfo = TransportProtos.ServiceInfo.newBuilder().setServiceId("tb-transport").build(); |
|||
var anotherData = new ChildData("/thingsboard/nodes/0000000030", null, anotherInfo.toByteArray()); |
|||
|
|||
startNode(childData); |
|||
|
|||
verify(partitionService, times(1)).recalculatePartitions(eq(currentInfo), eq(List.of(childInfo))); |
|||
|
|||
reset(partitionService); |
|||
|
|||
stopNode(childData); |
|||
|
|||
assertEquals(1, zkDiscoveryService.delayedTasks.size()); |
|||
|
|||
startNode(anotherData); |
|||
|
|||
assertTrue(zkDiscoveryService.delayedTasks.isEmpty()); |
|||
|
|||
verify(partitionService, times(1)).recalculatePartitions(eq(currentInfo), eq(List.of(anotherInfo))); |
|||
reset(partitionService); |
|||
|
|||
Thread.sleep(RECALCULATE_DELAY * 2); |
|||
|
|||
verify(partitionService, never()).recalculatePartitions(any(), any()); |
|||
|
|||
startNode(childData); |
|||
|
|||
verify(partitionService, times(1)).recalculatePartitions(eq(currentInfo), eq(List.of(anotherInfo, childInfo))); |
|||
} |
|||
|
|||
private void startNode(ChildData data) throws Exception { |
|||
cache.getCurrentData().add(data); |
|||
zkDiscoveryService.childEvent(curatorFramework, new PathChildrenCacheEvent(CHILD_ADDED, data)); |
|||
} |
|||
|
|||
private void stopNode(ChildData data) throws Exception { |
|||
cache.getCurrentData().remove(data); |
|||
zkDiscoveryService.childEvent(curatorFramework, new PathChildrenCacheEvent(CHILD_REMOVED, data)); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.queue.kafka; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.kafka.common.header.Header; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.thingsboard.server.queue.TbQueueMsg; |
|||
|
|||
import java.nio.charset.StandardCharsets; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.BDDMockito.willCallRealMethod; |
|||
import static org.mockito.BDDMockito.willReturn; |
|||
import static org.mockito.Mockito.mock; |
|||
|
|||
@Slf4j |
|||
class TbKafkaProducerTemplateTest { |
|||
|
|||
TbKafkaProducerTemplate<TbQueueMsg> producerTemplate; |
|||
|
|||
@BeforeEach |
|||
void setUp() { |
|||
producerTemplate = mock(TbKafkaProducerTemplate.class); |
|||
willCallRealMethod().given(producerTemplate).addAnalyticHeaders(any()); |
|||
willReturn("tb-core-to-core-notifications-tb-core-3").given(producerTemplate).getClientId(); |
|||
} |
|||
|
|||
@Test |
|||
void testAddAnalyticHeaders() { |
|||
List<Header> headers = new ArrayList<>(); |
|||
producerTemplate.addAnalyticHeaders(headers); |
|||
assertThat(headers).isNotEmpty(); |
|||
headers.forEach(r -> log.info("RecordHeader key [{}] value [{}]", r.key(), new String(r.value(), StandardCharsets.UTF_8))); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
<?xml version="1.0" encoding="UTF-8" ?> |
|||
|
|||
<configuration> |
|||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"> |
|||
<encoder> |
|||
<pattern>%d{ISO8601} [%thread] %-5level %logger{36} - %msg%n</pattern> |
|||
</encoder> |
|||
</appender> |
|||
|
|||
<!-- TbKafkaProducerTemplate will add headers for each message when log level: |
|||
- DEBUG - producerId and thread name |
|||
- TRACE - will add stacktrace. |
|||
Kafka compression is highly recommended --> |
|||
<logger name="org.thingsboard.server.queue.kafka.TbKafkaProducerTemplate" level="TRACE"/> |
|||
|
|||
<root level="INFO"> |
|||
<appender-ref ref="console"/> |
|||
</root> |
|||
|
|||
</configuration> |
|||
@ -0,0 +1,73 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.transport.lwm2m.server.model; |
|||
|
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.thingsboard.server.transport.lwm2m.server.store.TbLwM2MModelConfigStore; |
|||
|
|||
import java.util.Collections; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.mockito.BDDMockito.willReturn; |
|||
import static org.mockito.Mockito.mock; |
|||
|
|||
class LwM2MModelConfigServiceImplTest { |
|||
|
|||
LwM2MModelConfigServiceImpl service; |
|||
TbLwM2MModelConfigStore modelStore; |
|||
|
|||
@BeforeEach |
|||
void setUp() { |
|||
service = new LwM2MModelConfigServiceImpl(); |
|||
modelStore = mock(TbLwM2MModelConfigStore.class); |
|||
service.modelStore = modelStore; |
|||
} |
|||
|
|||
@Test |
|||
void testInitWithDuplicatedModels() { |
|||
LwM2MModelConfig config = new LwM2MModelConfig("urn:imei:951358811362976"); |
|||
List<LwM2MModelConfig> models = List.of(config, config); |
|||
willReturn(models).given(modelStore).getAll(); |
|||
service.init(); |
|||
assertThat(service.currentModelConfigs).containsExactlyEntriesOf(Map.of(config.getEndpoint(), config)); |
|||
} |
|||
|
|||
@Test |
|||
void testInitWithNonUniqueEndpoints() { |
|||
LwM2MModelConfig configAlfa = new LwM2MModelConfig("urn:imei:951358811362976"); |
|||
LwM2MModelConfig configBravo = new LwM2MModelConfig("urn:imei:151358811362976"); |
|||
LwM2MModelConfig configDelta = new LwM2MModelConfig("urn:imei:151358811362976"); |
|||
assertThat(configBravo.getEndpoint()).as("non-unique endpoints provided").isEqualTo(configDelta.getEndpoint()); |
|||
List<LwM2MModelConfig> models = List.of(configAlfa, configBravo, configDelta); |
|||
willReturn(models).given(modelStore).getAll(); |
|||
service.init(); |
|||
assertThat(service.currentModelConfigs).containsExactlyInAnyOrderEntriesOf(Map.of( |
|||
configAlfa.getEndpoint(), configAlfa, |
|||
configBravo.getEndpoint(), configBravo |
|||
)); |
|||
} |
|||
|
|||
@Test |
|||
void testInitWithEmptyModels() { |
|||
willReturn(Collections.emptyList()).given(modelStore).getAll(); |
|||
service.init(); |
|||
assertThat(service.currentModelConfigs).isEmpty(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,67 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.common.util; |
|||
|
|||
import com.google.gson.JsonParseException; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.server.common.data.StringUtils; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
|
|||
import javax.script.ScriptException; |
|||
import java.io.PrintWriter; |
|||
import java.io.StringWriter; |
|||
|
|||
@Slf4j |
|||
public class ExceptionUtil { |
|||
|
|||
@SuppressWarnings("unchecked") |
|||
public static <T extends Exception> T lookupException(Throwable source, Class<T> clazz) { |
|||
Exception e = lookupExceptionInCause(source, clazz); |
|||
if (e != null) { |
|||
return (T) e; |
|||
} else { |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public static Exception lookupExceptionInCause(Throwable source, Class<? extends Exception>... clazzes) { |
|||
while (source != null) { |
|||
for (Class<? extends Exception> clazz : clazzes) { |
|||
if (clazz.isAssignableFrom(source.getClass())) { |
|||
return (Exception) source; |
|||
} |
|||
} |
|||
source = source.getCause(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
public static String toString(Exception e, EntityId componentId, boolean stackTraceEnabled) { |
|||
Exception exception = lookupExceptionInCause(e, ScriptException.class, JsonParseException.class); |
|||
if (exception != null && StringUtils.isNotEmpty(exception.getMessage())) { |
|||
return exception.getMessage(); |
|||
} else { |
|||
if (stackTraceEnabled) { |
|||
StringWriter sw = new StringWriter(); |
|||
e.printStackTrace(new PrintWriter(sw)); |
|||
return sw.toString(); |
|||
} else { |
|||
log.debug("[{}] Unknown error during message processing", componentId, e); |
|||
return "Please contact system administrator"; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,74 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.common.util; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import java.io.IOException; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
|
|||
class ExceptionUtilTest { |
|||
|
|||
final Exception cause = new RuntimeException(); |
|||
|
|||
@Test |
|||
void givenRootCause_whenLookupExceptionInCause_thenReturnRootCauseAndNoStackOverflow() { |
|||
Exception e = cause; |
|||
for (int i = 0; i <= 16384; i++) { |
|||
e = new Exception(e); |
|||
} |
|||
assertThat(ExceptionUtil.lookupExceptionInCause(e, RuntimeException.class)).isSameAs(cause); |
|||
} |
|||
|
|||
@Test |
|||
void givenCause_whenLookupExceptionInCause_thenReturnCause() { |
|||
assertThat(ExceptionUtil.lookupExceptionInCause(new Exception(cause), RuntimeException.class)).isSameAs(cause); |
|||
} |
|||
|
|||
@Test |
|||
void givenNoCauseAndExceptionIsWantedCauseClass_whenLookupExceptionInCause_thenReturnSelf() { |
|||
assertThat(ExceptionUtil.lookupExceptionInCause(cause, RuntimeException.class)).isSameAs(cause); |
|||
} |
|||
|
|||
@Test |
|||
void givenNoCause_whenLookupExceptionInCause_thenReturnNull() { |
|||
assertThat(ExceptionUtil.lookupExceptionInCause(new Exception(), RuntimeException.class)).isNull(); |
|||
} |
|||
|
|||
@Test |
|||
void givenNotWantedCause_whenLookupExceptionInCause_thenReturnNull() { |
|||
final Exception cause = new IOException(); |
|||
assertThat(ExceptionUtil.lookupExceptionInCause(new Exception(cause), RuntimeException.class)).isNull(); |
|||
} |
|||
|
|||
@Test |
|||
void givenCause_whenLookupExceptionInCauseByMany_thenReturnFirstCause() { |
|||
final Exception causeIAE = new IllegalAccessException(); |
|||
assertThat(ExceptionUtil.lookupExceptionInCause(new Exception(causeIAE))).isNull(); |
|||
assertThat(ExceptionUtil.lookupExceptionInCause(new Exception(causeIAE), IOException.class, NoSuchFieldException.class)).isNull(); |
|||
assertThat(ExceptionUtil.lookupExceptionInCause(new Exception(causeIAE), IllegalAccessException.class, IOException.class, NoSuchFieldException.class)).isSameAs(causeIAE); |
|||
assertThat(ExceptionUtil.lookupExceptionInCause(new Exception(causeIAE), IOException.class, NoSuchFieldException.class, IllegalAccessException.class)).isSameAs(causeIAE); |
|||
|
|||
final Exception causeIOE = new IOException(causeIAE); |
|||
assertThat(ExceptionUtil.lookupExceptionInCause(new Exception(causeIOE))).isNull(); |
|||
assertThat(ExceptionUtil.lookupExceptionInCause(new Exception(causeIAE), ClassNotFoundException.class, NoSuchFieldException.class)).isNull(); |
|||
assertThat(ExceptionUtil.lookupExceptionInCause(new Exception(causeIOE), IOException.class, NoSuchFieldException.class)).isSameAs(causeIOE); |
|||
assertThat(ExceptionUtil.lookupExceptionInCause(new Exception(causeIOE), IllegalAccessException.class, IOException.class, NoSuchFieldException.class)).isSameAs(causeIOE); |
|||
assertThat(ExceptionUtil.lookupExceptionInCause(new Exception(causeIOE), IOException.class, NoSuchFieldException.class, IllegalAccessException.class)).isSameAs(causeIOE); |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue