-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for DeviceCallExceptions
Signed-off-by: Alberto Codutti <alberto.codutti@eurotech.com>
- Loading branch information
Showing
4 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...rc/test/java/org/eclipse/kapua/service/device/call/exception/DeviceCallExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023, 2022 Eurotech and/or its affiliates and others | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Eurotech - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.kapua.service.device.call.exception; | ||
|
||
import org.eclipse.kapua.qa.markers.junit.JUnitTests; | ||
import org.eclipse.kapua.service.device.call.exception.model.TestCodesDeviceCallException; | ||
import org.eclipse.kapua.service.device.call.exception.model.TestDeviceMessage; | ||
import org.eclipse.kapua.service.device.call.message.DeviceMessage; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
|
||
import java.text.MessageFormat; | ||
|
||
/** | ||
* {@link DeviceCallException}s tests. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
@Category(JUnitTests.class) | ||
public class DeviceCallExceptionTest { | ||
|
||
private final Throwable cause = new Throwable("This is the cause"); | ||
|
||
private final Long aTimeout = 15000L; | ||
private final DeviceMessage<?, ?> deviceCallMessage = new TestDeviceMessage(); | ||
|
||
@Test | ||
public void testDeviceCallErrorCodesHaveMessages() { | ||
for (DeviceCallErrorCodes errorCode : DeviceCallErrorCodes.values()) { | ||
DeviceCallException deviceCallException = new TestCodesDeviceCallException(errorCode); | ||
|
||
Assert.assertNotEquals("DeviceCallErrorCodes." + errorCode + " doesn't have an error message", "Error: ", deviceCallException.getMessage()); | ||
Assert.assertNotEquals("DeviceCallErrorCodes." + errorCode + " doesn't have an error message", "Error: ", deviceCallException.getLocalizedMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDeviceCallSendException() { | ||
String exceptionMessage = "An error occurred when sending the message: " + deviceCallMessage; | ||
|
||
// Without cause | ||
DeviceCallSendException deviceCallSendException = new DeviceCallSendException(deviceCallMessage); | ||
|
||
Assert.assertEquals(DeviceCallErrorCodes.SEND_ERROR, deviceCallSendException.getCode()); | ||
Assert.assertNull(deviceCallSendException.getCause()); | ||
Assert.assertEquals(deviceCallMessage, deviceCallSendException.getRequestMessage()); | ||
Assert.assertEquals(exceptionMessage, deviceCallSendException.getMessage()); | ||
Assert.assertEquals(exceptionMessage, deviceCallSendException.getLocalizedMessage()); | ||
|
||
// With cause | ||
exceptionMessage = exceptionMessage + ". Caused by: " + cause.getMessage(); | ||
|
||
deviceCallSendException = new DeviceCallSendException(cause, deviceCallMessage); | ||
|
||
Assert.assertEquals(DeviceCallErrorCodes.SEND_ERROR_WITH_CAUSE, deviceCallSendException.getCode()); | ||
Assert.assertEquals(cause, deviceCallSendException.getCause()); | ||
Assert.assertEquals(deviceCallMessage, deviceCallSendException.getRequestMessage()); | ||
Assert.assertEquals(exceptionMessage, deviceCallSendException.getMessage()); | ||
Assert.assertEquals(exceptionMessage, deviceCallSendException.getLocalizedMessage()); | ||
} | ||
|
||
@Test | ||
public void testDeviceCallTimeoutException() { | ||
String exceptionMessage = "The request has not received a response within the timeout of: " + MessageFormat.format("{0}", aTimeout) + "ms"; | ||
|
||
DeviceCallTimeoutException deviceCallTimeoutException = new DeviceCallTimeoutException(cause, aTimeout); | ||
|
||
Assert.assertEquals(DeviceCallErrorCodes.TIMEOUT, deviceCallTimeoutException.getCode()); | ||
Assert.assertEquals(cause, deviceCallTimeoutException.getCause()); | ||
Assert.assertEquals(aTimeout, deviceCallTimeoutException.getTimeout()); | ||
Assert.assertEquals(exceptionMessage, deviceCallTimeoutException.getMessage()); | ||
Assert.assertEquals(exceptionMessage, deviceCallTimeoutException.getLocalizedMessage()); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...a/org/eclipse/kapua/service/device/call/exception/model/TestCodesDeviceCallException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023, 2022 Eurotech and/or its affiliates and others | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Eurotech - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.kapua.service.device.call.exception.model; | ||
|
||
import org.eclipse.kapua.service.device.call.exception.DeviceCallErrorCodes; | ||
import org.eclipse.kapua.service.device.call.exception.DeviceCallException; | ||
import org.eclipse.kapua.service.device.call.exception.DeviceCallExceptionTest; | ||
import org.eclipse.kapua.transport.exception.TransportException; | ||
|
||
/** | ||
* {@link TransportException} for testing. | ||
* | ||
* @see DeviceCallExceptionTest#testDeviceCallErrorCodesHaveMessages() | ||
* @since 2.0.0 | ||
*/ | ||
public class TestCodesDeviceCallException extends DeviceCallException { | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param code The {@link DeviceCallErrorCodes} to test. | ||
* @since 2.0.0 | ||
*/ | ||
public TestCodesDeviceCallException(DeviceCallErrorCodes code) { | ||
super(code); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...rc/test/java/org/eclipse/kapua/service/device/call/exception/model/TestDeviceMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023, 2022 Eurotech and/or its affiliates and others | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Eurotech - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.kapua.service.device.call.exception.model; | ||
|
||
import org.eclipse.kapua.service.device.call.message.DeviceChannel; | ||
import org.eclipse.kapua.service.device.call.message.DeviceMessage; | ||
import org.eclipse.kapua.service.device.call.message.DevicePayload; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* {@link DeviceMessage} for testing. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
public class TestDeviceMessage implements DeviceMessage<DeviceChannel, DevicePayload> { | ||
@Override | ||
public DeviceChannel getChannel() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void setChannel(DeviceChannel channel) { | ||
|
||
} | ||
|
||
@Override | ||
public DevicePayload getPayload() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void setPayload(DevicePayload payload) { | ||
|
||
} | ||
|
||
@Override | ||
public Date getTimestamp() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void setTimestamp(Date timestamp) { | ||
|
||
} | ||
} |