Skip to content

Commit

Permalink
Added tests for DeviceCallExceptions
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Codutti <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed Aug 2, 2023
1 parent bae954f commit c17e9e6
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 0 deletions.
23 changes: 23 additions & 0 deletions service/device/call/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,28 @@
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-transport-api</artifactId>
</dependency>

<!-- -->
<!-- Test dependencies -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-qa-markers</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
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());
}
}
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);
}
}
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) {

}
}

0 comments on commit c17e9e6

Please sign in to comment.