Skip to content

Commit

Permalink
Switch to junit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
bertrik committed Dec 8, 2024
1 parent 0f1e7e6 commit 1ebf8d9
Show file tree
Hide file tree
Showing 22 changed files with 224 additions and 232 deletions.
9 changes: 8 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ subprojects {
}
}

test {
useJUnitPlatform()
}

// Standard libraries added to all projects
dependencies {
errorprone(libs.errorprone.core)
compileOnly libs.errorprone.annotations

implementation libs.slf4j.api

testImplementation libs.junit
testImplementation(platform(libs.junit.bom))
testImplementation libs.junit.jupiter
testRuntimeOnly libs.junit.platform.launcher

testImplementation libs.mockito.core
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package nl.sikken.bertrik.cayenne;

import java.util.Base64;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Base64;

/**
* Unit tests for CayenneMessage.
*/
Expand All @@ -18,67 +18,67 @@ public final class CayenneMessageTest {

/**
* Verifies example from specification.
*
*
* @throws CayenneException in case of a parsing exception
*/
@Test
public void testTwoTemperatureSensors() throws CayenneException {
byte[] data = { 0x03, 0x67, 0x01, 0x10, 0x05, 0x67, 0x00, (byte) 0xFF };
byte[] data = {0x03, 0x67, 0x01, 0x10, 0x05, 0x67, 0x00, (byte) 0xFF};
CayenneMessage payload = new CayenneMessage();
payload.parse(data);
LOG.info("payload: {}", payload);

Assert.assertArrayEquals(new String[] { "27.2" }, payload.ofChannel(3).format());
Assert.assertArrayEquals(new String[] { "25.5" }, payload.ofChannel(5).format());
Assertions.assertArrayEquals(new String[]{"27.2"}, payload.ofChannel(3).format());
Assertions.assertArrayEquals(new String[]{"25.5"}, payload.ofChannel(5).format());
}

/**
* Verifies example from specification.
*
*
* @throws CayenneException in case of a parsing exception
*/
@Test
public void testTemperaturePlusAccel() throws CayenneException {
byte[] data = { 0x01, 0x67, (byte) 0xFF, (byte) 0xD7, 0x06, 0x71, 0x04, (byte) 0xD2, (byte) 0xFB, 0x2E, 0x00,
0x00 };
byte[] data = {0x01, 0x67, (byte) 0xFF, (byte) 0xD7, 0x06, 0x71, 0x04, (byte) 0xD2, (byte) 0xFB, 0x2E, 0x00,
0x00};
CayenneMessage payload = new CayenneMessage();
payload.parse(data);

Assert.assertArrayEquals(new String[] { "-4.1" }, payload.ofChannel(1).format());
Assert.assertArrayEquals(new String[] { "1.234", "-1.234", "0.000" }, payload.ofChannel(6).format());
Assertions.assertArrayEquals(new String[]{"-4.1"}, payload.ofChannel(1).format());
Assertions.assertArrayEquals(new String[]{"1.234", "-1.234", "0.000"}, payload.ofChannel(6).format());
}

/**
* Verifies example from specification.
*
*
* @throws CayenneException in case of a parsing exception
*/
@Test
public void testGps() throws CayenneException {
byte[] data = { 0x01, (byte) 0x88, 0x06, 0x076, 0x5f, (byte) 0xf2, (byte) 0x96, 0x0a, 0x00, 0x03, (byte) 0xe8 };
byte[] data = {0x01, (byte) 0x88, 0x06, 0x076, 0x5f, (byte) 0xf2, (byte) 0x96, 0x0a, 0x00, 0x03, (byte) 0xe8};
CayenneMessage payload = new CayenneMessage();
payload.parse(data);

Assert.assertArrayEquals(new String[] { "42.3519", "-87.9094", "10.00" }, payload.ofChannel(1).format());
Assertions.assertArrayEquals(new String[]{"42.3519", "-87.9094", "10.00"}, payload.ofChannel(1).format());
}

/**
* Verifies parsing of humidity value.
*
*
* @throws CayenneException in case of a parsing exception
*/
@Test
public void testHumidity() throws CayenneException {
byte[] data = { 1, 104, 100 };
byte[] data = {1, 104, 100};
CayenneMessage payload = new CayenneMessage();
payload.parse(data);

Assert.assertArrayEquals(new String[] { "50.0" }, payload.ofChannel(1).format());
Assertions.assertArrayEquals(new String[]{"50.0"}, payload.ofChannel(1).format());
}

/**
* Verifies parsing of some actual data from a sodaq one.
*
*
* @throws CayenneException in case of a parsing exception
*/
@Test
Expand All @@ -88,15 +88,15 @@ public void testActualData() throws CayenneException {
CayenneMessage payload = new CayenneMessage();
payload.parse(data);

Assert.assertArrayEquals(new String[] { "52.0225", "4.6928", "-2.00" }, payload.ofChannel(1).format());
Assert.assertArrayEquals(new String[] { "247.84" }, payload.ofChannel(2).format());
Assert.assertArrayEquals(new String[] { "27.0" }, payload.ofChannel(3).format());
Assertions.assertArrayEquals(new String[]{"52.0225", "4.6928", "-2.00"}, payload.ofChannel(1).format());
Assertions.assertArrayEquals(new String[]{"247.84"}, payload.ofChannel(2).format());
Assertions.assertArrayEquals(new String[]{"27.0"}, payload.ofChannel(3).format());
}

/**
* Verifies parsing of some actual data from a sodaq one, with a fix applied to
* the voltage value.
*
*
* @throws CayenneException in case of a parsing exception
*/
@Test
Expand All @@ -107,69 +107,64 @@ public void testActualData2() throws CayenneException {
payload.parse(data);

// verify we can get at the data by channel
Assert.assertArrayEquals(new String[] { "52.0225", "4.6925", "-17.00" }, payload.ofChannel(1).format());
Assert.assertArrayEquals(new String[] { "4.15" }, payload.ofChannel(2).format());
Assert.assertArrayEquals(new String[] { "24.0" }, payload.ofChannel(3).format());
Assertions.assertArrayEquals(new String[]{"52.0225", "4.6925", "-17.00"}, payload.ofChannel(1).format());
Assertions.assertArrayEquals(new String[]{"4.15"}, payload.ofChannel(2).format());
Assertions.assertArrayEquals(new String[]{"24.0"}, payload.ofChannel(3).format());

// verify we can also get data by type
Assert.assertArrayEquals(new String[] { "52.0225", "4.6925", "-17.00" },
Assertions.assertArrayEquals(new String[]{"52.0225", "4.6925", "-17.00"},
payload.ofType(ECayenneItem.GPS_LOCATION).format());
Assert.assertArrayEquals(new String[] { "4.15" }, payload.ofType(ECayenneItem.ANALOG_INPUT).format());
Assert.assertArrayEquals(new String[] { "24.0" }, payload.ofType(ECayenneItem.TEMPERATURE).format());
Assertions.assertArrayEquals(new String[]{"4.15"}, payload.ofType(ECayenneItem.ANALOG_INPUT).format());
Assertions.assertArrayEquals(new String[]{"24.0"}, payload.ofType(ECayenneItem.TEMPERATURE).format());

// verify non-existing channel and type
Assert.assertNull(payload.ofChannel(0));
Assert.assertNull(payload.ofType(ECayenneItem.BAROMETER));
Assertions.assertNull(payload.ofChannel(0));
Assertions.assertNull(payload.ofType(ECayenneItem.BAROMETER));

// verify toString method
Assert.assertNotNull(payload.toString());
Assertions.assertNotNull(payload.toString());
}

/**
* Verifies parsing an empty string.
*
*
* @throws CayenneException in case of a parsing exception
*/
@Test
public void testParseEmpty() throws CayenneException {
CayenneMessage payload = new CayenneMessage();
payload.parse(new byte[0]);
Assert.assertTrue(payload.getItems().isEmpty());
Assertions.assertTrue(payload.getItems().isEmpty());
}

/**
* Verifies parsing a short buffer
*
* @throws CayenneException in case of a parsing exception
*/
@Test(expected = CayenneException.class)
public void testShortBuffer() throws CayenneException {
new CayenneMessage().parse(new byte[] { 0 });
@Test
public void testShortBuffer() {
Assertions.assertThrows(CayenneException.class, () -> new CayenneMessage().parse(new byte[]{0}));
}

/**
* Verifies parsing of a buffer containing a non-supported data type.
*
* @throws CayenneException in case of a parsing exception
*/
@Test(expected = CayenneException.class)
public void testInvalidType() throws CayenneException {
new CayenneMessage().parse(new byte[] { 0, 100 });
@Test
public void testInvalidType() {
Assertions.assertThrows(CayenneException.class, () -> new CayenneMessage().parse(new byte[]{0, 100}));
}

/**
* Verifies parsing of a buffer containing insufficient data during parsing.
*
* @throws CayenneException in case of a parsing exception
*/
@Test(expected = CayenneException.class)
public void testShortData() throws CayenneException {
new CayenneMessage().parse(new byte[] { 2, 1 });
@Test
public void testShortData() {
Assertions.assertThrows(CayenneException.class, () ->
new CayenneMessage().parse(new byte[]{2, 1}));
}

/**
* Verifies encoding of a float value.
*
*
* @throws CayenneException in case of a parsing exception
*/
@Test
Expand All @@ -181,12 +176,12 @@ public void encodeFloat() throws CayenneException {

CayenneMessage decoded = new CayenneMessage();
decoded.parse(encoded);
Assert.assertEquals(-12.34, decoded.getItems().get(0).getValues()[0].doubleValue(), 0.01);
Assertions.assertEquals(-12.34, decoded.getItems().get(0).getValues()[0].doubleValue(), 0.01);
}

/**
* Verifies encoding of a humidity value.
*
*
* @throws CayenneException in case of a parsing exception
*/
@Test
Expand All @@ -199,14 +194,14 @@ public void encodeHumidity() throws CayenneException {
decoded.parse(encoded);

CayenneItem item = decoded.getItems().get(0);
Assert.assertEquals(ECayenneItem.HUMIDITY, item.getType());
Assert.assertEquals(35.5, item.getValues()[0].doubleValue(), 0.1);
Assert.assertEquals("35.5", item.format()[0]);
Assertions.assertEquals(ECayenneItem.HUMIDITY, item.getType());
Assertions.assertEquals(35.5, item.getValues()[0].doubleValue(), 0.1);
Assertions.assertEquals("35.5", item.format()[0]);
}

/**
* Verifies encoding of a digital input.
*
*
* @throws CayenneException in case of a parsing exception
*/
@Test
Expand All @@ -219,8 +214,8 @@ public void testDigitalInput() throws CayenneException {
decoded.parse(encoded);

CayenneItem item = decoded.getItems().get(0);
Assert.assertEquals(ECayenneItem.DIGITAL_INPUT, item.getType());
Assert.assertEquals(1, item.getValues()[0].intValue());
Assertions.assertEquals(ECayenneItem.DIGITAL_INPUT, item.getType());
Assertions.assertEquals(1, item.getValues()[0].intValue());
}

/**
Expand All @@ -236,9 +231,9 @@ public void testPresence() throws CayenneException {
decoded.parse(encoded);

CayenneItem item = decoded.getItems().get(0);
Assert.assertEquals(ECayenneItem.PRESENCE, item.getType());
Assert.assertEquals(7, item.getValues()[0].intValue());
Assert.assertEquals("7", item.format()[0]);
Assertions.assertEquals(ECayenneItem.PRESENCE, item.getType());
Assertions.assertEquals(7, item.getValues()[0].intValue());
Assertions.assertEquals("7", item.format()[0]);
}

/**
Expand All @@ -247,11 +242,11 @@ public void testPresence() throws CayenneException {
@Test
public void testPackedFormat() throws CayenneException {
CayenneMessage message = new CayenneMessage(ECayennePayloadFormat.PACKED_SENSOR_PAYLOAD);
byte[] data = { 0x67, 0x01, 0x10, 0x67, 0x00, (byte) 0xFF };
byte[] data = {0x67, 0x01, 0x10, 0x67, 0x00, (byte) 0xFF};
message.parse(data);

Assert.assertEquals(27.2, message.ofChannel(0).getValue().doubleValue(), 0.01);
Assert.assertEquals(25.5, message.ofChannel(1).getValue().doubleValue(), 0.01);
Assertions.assertEquals(27.2, message.ofChannel(0).getValue().doubleValue(), 0.01);
Assertions.assertEquals(25.5, message.ofChannel(1).getValue().doubleValue(), 0.01);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nl.sikken.bertrik.cayenne;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* See https://community.mydevices.com/t/cayenne-lpp-2-0/7510
Expand All @@ -10,9 +10,9 @@ public final class CayennePayloadFormatTest {

@Test
public void testPort() {
Assert.assertNull(ECayennePayloadFormat.fromPort(0));
Assert.assertEquals(ECayennePayloadFormat.DYNAMIC_SENSOR_PAYLOAD, ECayennePayloadFormat.fromPort(1));
Assert.assertEquals(ECayennePayloadFormat.PACKED_SENSOR_PAYLOAD, ECayennePayloadFormat.fromPort(2));
Assertions.assertNull(ECayennePayloadFormat.fromPort(0));
Assertions.assertEquals(ECayennePayloadFormat.DYNAMIC_SENSOR_PAYLOAD, ECayennePayloadFormat.fromPort(1));
Assertions.assertEquals(ECayennePayloadFormat.PACKED_SENSOR_PAYLOAD, ECayennePayloadFormat.fromPort(2));
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nl.sikken.bertrik.cayenne;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -15,7 +15,7 @@ public final class SimpleCayenneTest {
/**
* Verifies basic functionality by adding some items and encoding it into a
* message
*
*
* @throws CayenneException in case of a problem encoding/decoding
*/
@Test
Expand All @@ -37,30 +37,31 @@ public void testEncode() throws CayenneException {

// encode it
byte[] data = cayenne.encode(500);
Assert.assertNotNull(data);
Assertions.assertNotNull(data);

// decode it
CayenneMessage message = new CayenneMessage();
message.parse(data);
Assert.assertEquals(12, message.getItems().size());
Assert.assertEquals(3.82, message.ofType(ECayenneItem.ANALOG_INPUT).getValue().doubleValue(), 0.01);
Assert.assertEquals(55, message.ofType(ECayenneItem.DIGITAL_INPUT).getValue().intValue());
Assert.assertEquals(66, message.ofType(ECayenneItem.DIGITAL_OUTPUT).getValue().intValue());
Assert.assertEquals(52.0, message.ofType(ECayenneItem.GPS_LOCATION).getValues()[0].doubleValue(), 0.1);
Assert.assertEquals(42, message.ofType(ECayenneItem.PRESENCE).getValue().intValue(), 42);
Assert.assertEquals(19.0, message.ofType(ECayenneItem.TEMPERATURE).getValue().doubleValue(), 0.1);
Assertions.assertEquals(12, message.getItems().size());
Assertions.assertEquals(3.82, message.ofType(ECayenneItem.ANALOG_INPUT).getValue().doubleValue(), 0.01);
Assertions.assertEquals(55, message.ofType(ECayenneItem.DIGITAL_INPUT).getValue().intValue());
Assertions.assertEquals(66, message.ofType(ECayenneItem.DIGITAL_OUTPUT).getValue().intValue());
Assertions.assertEquals(52.0, message.ofType(ECayenneItem.GPS_LOCATION).getValues()[0].doubleValue(), 0.1);
Assertions.assertEquals(42, message.ofType(ECayenneItem.PRESENCE).getValue().intValue(), 42);
Assertions.assertEquals(19.0, message.ofType(ECayenneItem.TEMPERATURE).getValue().doubleValue(), 0.1);
}

/**
* Verifies that a simple cayenne message with non-unique channels is rejected.
*
*
* @throws CayenneException in case of a problem encoding/decoding
*/
@Test(expected = CayenneException.class)
@Test
public void testNonUniqueChannel() throws CayenneException {
SimpleCayenne cayenne = new SimpleCayenne();
cayenne.addTemperature(1, 19.0);
cayenne.addAnalogInput(1, 3.90);
Assertions.assertThrows(CayenneException.class, () ->
cayenne.addAnalogInput(1, 3.90));
}

}
Loading

0 comments on commit 1ebf8d9

Please sign in to comment.