Skip to content

Commit

Permalink
Added Tests for Messages types.
Browse files Browse the repository at this point in the history
  • Loading branch information
luckydonald committed May 12, 2016
1 parent fd6bdec commit e4a6e42
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 15 deletions.
30 changes: 15 additions & 15 deletions PROTOCOL.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Objects are unordered and keys will not be repeated.
| ----------- | ------------ | -------------- |
| size | 3b000000 | `59` bytes content, size of packages (next table) included |
| type | 03 | Type 3: Data Update |
| content | `030a0000002a0000`<br>`00070b0000000200`<br>`0100000002000000`<br>`080c000000020005`<br>`0000000500000066`<br>`6f6f000600000068`<br>`656c6c6f00020003`<br>`00000004000000` | **See next Table** |
| content | `030a0000002a0000`<br>`00070b0000000200`<br>`0100000002000000`<br>`080c000000020005`<br>`000000666f6f0006`<br>`00000068656c6c6f`<br>`0002000300000004`<br>`000000` | **See next Table** |


The content can contain many Data Packages.
Expand All @@ -182,25 +182,25 @@ They just follow after each other.
| Data Item | Attributes | Data Attributes | example (bytes) | Interpretation |
| ------------------ | ------------ | --------------- | --------------- | -------------- |
| **First Package** | type | | 03 | Data Type 3: `INT32` |
| | id | | 0a000000 | ID: 10 |
| | data | | 2a000000 | Value: 42 |
| | id | | 0a 00 00 00 | ID: 10 |
| | data | | 2a 00 00 00 | Value: 42 |
| | | | | |
| **Second Package** | type | | 07 | Data Type 7: Array |
| | id | | 0b000000 | ID: 11 |
| | data | length | 0200 | Length 2: Array has 2 entries |
| | | `id #1` | 01000000 | ID of element 1: >>1 |
| | | `id #2` | 02000000 | ID of element 2: >>2 |
| | id | | 0b 00 00 00 | ID: 11 |
| | data | length | 02 00 | Length 2: Array has 2 entries |
| | | `id #1` | 01 00 00 00 | ID of element 1: >>1 |
| | | `id #2` | 02 00 00 00 | ID of element 2: >>2 |
| | | | | |
| **Third Package** | type | | 08 | Data Type 8: |
| | id | | 0c000000 | ID: 12 |
| | id | | 0c 00 00 00 | ID: 12 |
| | added | length | 0200 | 2 elements to insert |
| | | `id #1` | 05000000 | ID of element 1: >>5 |
| | | `key #1` | 666f6f00 | Key of element 1: `"foo\0"` |
| | | `id #2` | 06000000 | ID of element 2: >>6 |
| | | `key #2` | 68656c6c6f00 | Key of element 2: `"hello\0"` |
| | removed | length | 0200 | 2 elements to remove |
| | | first id | 03000000 | remove element with ID >>3 |
| | | second id | 04000000 | remove element with ID >>4 |
| | | `id #1` | 05 00 00 00 | ID of element 1: >>5 |
| | | `key #1` | 66 6f 6f 00 | Key of element 1: `"foo\0"` |
| | | `id #2` | 06 00 00 00 | ID of element 2: >>6 |
| | | `key #2` |68 65 6c 6c 6f 00| Key of element 2: `"hello\0"` |
| | removed | length | 02 00 | 2 elements to remove |
| | | first id | 03 00 00 00 | remove element with ID >>3 |
| | | second id | 04 00 00 00 | remove element with ID >>4 |

corresponds to an update that:
* sets value with id `10` to be a `uint32` equal to `42`
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,10 @@
<artifactId>codacy-coverage-reporter</artifactId>
<version>1.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
</project>
79 changes: 79 additions & 0 deletions src/test/java/de/luckydonald/MessagesTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package de.luckydonald;

import de.luckydonald.pipboyserver.Messages.KeepAlive;
import de.luckydonald.pipboyserver.Messages.ConnectionAccepted;
import de.luckydonald.pipboyserver.Messages.ConnectionRefused;
import de.luckydonald.pipboyserver.Messages.DataUpdate;
import de.luckydonald.pipboyserver.Messages.LocalMapUpdate;
import de.luckydonald.pipboyserver.PipBoyServer.Database;
import de.luckydonald.pipboyserver.PipBoyServer.types.*;
import de.luckydonald.utils.Array;
import de.luckydonald.utils.ObjectWithLogger;
import org.junit.Test;

import java.util.Arrays;
import java.util.function.Function;

import static org.junit.Assert.*;

/**
Expand All @@ -14,10 +24,79 @@
**/
public class MessagesTest extends ObjectWithLogger {
private byte[] expected_keepAlive = {0x00, 0x00, 0x00, 0x00, 0x00};
private byte[] expected_connectionAccepted = {
0x25, 0x00, 0x00, 0x00, 0x01, 0x7B, 0x22, 0x6C, 0x61, 0x6E, 0x67, 0x22, 0x3A, 0x20, 0x22, 0x64, 0x65, 0x22,
0x2C, 0x20, 0x22, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x22, 0x3A, 0x20, 0x22, 0x31, 0x2E, 0x31, 0x2E,
0x33, 0x30, 0x2E, 0x30, 0x22, 0x7D
};
private byte[] expected_connectionRefused = {0x00, 0x00, 0x00, 0x00, 0x02};
private byte[] expectedDataUpdate = {
0x3b, 0x00, 0x00, 0x00, 0x03, 0x03, 0x0a, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x07, 0x0b, 0x00, 0x00,
0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x08, 0x0c, 0x00, 0x00, 0x00, 0x02, 0x00,
0x05, 0x00, 0x00, 0x00, 0x66, 0x6f, 0x6f, 0x00, 0x06, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00,
0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00
};

@Test
public void testKeepAlive_Content() throws Exception {
KeepAlive msg = new KeepAlive();
assertArrayEquals("toBytes()", expected_keepAlive, msg.toBytes());
}

@Test
public void testConnectionAccepted_Content() throws Exception {
ConnectionAccepted msg = new ConnectionAccepted();
assertArrayEquals("toBytes()", expected_connectionAccepted, msg.toBytes());
}
@Test
public void testConnectionRefused_Content() throws Exception {
ConnectionRefused msg = new ConnectionRefused();
assertArrayEquals("toBytes()", expected_connectionRefused, msg.toBytes());
}
@Test
public void testDataUpdate_Content() throws Exception {
DBEntry[] updates = new DBEntry[3];
Database db = new Database();
db.add(new DBString("Placeholder with ID 0"));
db.add(new DBString("Placeholder with ID 1"));
db.add(new DBString("Placeholder with ID 2"));
db.add(new DBString("Placeholder with ID 3"));
db.add(new DBString("Placeholder with ID 4"));
db.add(new DBString("Placeholder with ID 5"));
db.add(new DBString("Placeholder with ID 6"));
db.add(new DBString("Placeholder with ID 7"));
db.add(new DBString("Placeholder with ID 8"));
db.add(new DBString("Placeholder with ID 9"));
DBInteger32 package1 = new DBInteger32(42);
db.add(package1);
assertEquals("package1 id", 10, (int) package1.getID());
DBList package2 = new DBList();
db.add(package2);
assertEquals("package2 id", 11, (int) package2.getID());
package2.append(1);
package2.append(2);
DBDict package3 = new DBDict();
db.add(package3);
assertEquals("package3 id", 12, (int) package3.getID());
package3.add("deleteme_1\0", db.get(3));
package3.add("deleteme_2\0", db.get(4));
package3.add("foo\0", db.get(5));
package3.add("hello\0", db.get(6));
package3.remove(3);
package3.remove(4);
updates[0] = package1;
updates[1] = package2;
updates[2] = package3;
DataUpdate msg = new DataUpdate(updates);
Function<Object,String> func = this::formatByte;
assertEquals("toBytes()", Array.toString(expectedDataUpdate, func), Array.toString(msg.toBytes(), func));
assertArrayEquals("toBytes()", expectedDataUpdate, msg.toBytes());
}

private String formatByte(Object o) {
if (o instanceof Byte) {
return String.format("%02X", (byte)o);
}
return "";
}
}

0 comments on commit e4a6e42

Please sign in to comment.