Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/au/gov/amsa/sgb/decoder/Beacon23HexId.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.davidmoten.guavamini.Preconditions;

import au.gov.amsa.sgb.decoder.internal.Bits;
import au.gov.amsa.sgb.decoder.internal.Tac;
import au.gov.amsa.sgb.decoder.internal.json.Json;
import au.gov.amsa.sgb.decoder.vesselid.VesselId;

Expand All @@ -16,6 +17,7 @@ public final class Beacon23HexId {

private final int countryCode;
private final int tac;
private final Optional<String> tacDescription;
private final int serialNumber;
private final boolean testProtocolFlag;
private final Optional<VesselId> vesselId;
Expand All @@ -27,6 +29,7 @@ private Beacon23HexId(String hex) {
countryCode = bits.readUnsignedInt(10);
bits.skip(3);
tac = bits.readUnsignedInt(16);
tacDescription = Tac.description(tac);
serialNumber = bits.readUnsignedInt(14);
testProtocolFlag = bits.readBoolean();
vesselId = Detection.readVesselId(bits);
Expand All @@ -43,6 +46,10 @@ public int countryCode() {
public int tac() {
return tac;
}

public Optional<String> tacDescription() {
return tacDescription;
}

public int serialNumber() {
return serialNumber;
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/au/gov/amsa/sgb/decoder/internal/Tac.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package au.gov.amsa.sgb.decoder.internal;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.TreeMap;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;

import com.github.davidmoten.guavamini.annotations.VisibleForTesting;

/**
* See C/S T.021 specification document.
*
*/
public final class Tac {

@VisibleForTesting
static final BinaryOperator<String> MERGE_FUNCTION = (v1, v2) -> {
throw new IllegalArgumentException(String.format("Duplicate key for values %s and %s", v1, v2));
};

private static final TreeMap<Integer, String> DESCRIPTIONS = loadDescriptions();

private Tac() {
// prevent instantiation
}

public static Optional<String> description(int tac) {
return Optional.ofNullable(DESCRIPTIONS.floorEntry(tac)).map(x -> x.getValue());
}

private static TreeMap<Integer, String> loadDescriptions() {
return loadDescriptions(Tac.class.getResource("/tac-descriptions.txt"));
}

@VisibleForTesting
static TreeMap<Integer, String> loadDescriptions(URL url) {
try {
List<String> list = Files.readAllLines(Paths.get(url.toURI()), StandardCharsets.UTF_8);
return list //
.stream() //
.map(line -> line.trim()) //
.filter(line -> !line.startsWith("#")) //
.filter(line -> !line.isEmpty()) //
.map(line -> line.split(",")) //
.collect(Collectors.toMap(items -> Integer.parseInt(items[0]), //
items -> items[1], //
MERGE_FUNCTION, //
TreeMap::new));
} catch (IOException | URISyntaxException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static String toJson(Object o) {
}
}

@SuppressWarnings("deprecation")
private static ObjectMapper createMapper() {
ObjectMapper m = new ObjectMapper();

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/beacon-23-hex-id-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
"tac" : {
"type" : "integer"
},
"tacDescription" : {
"type" : "string"
},
"serialNumber" : {
"type" : "integer"
},
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/tac-descriptions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#start of range, TAC description

9998,TYPE_APPROVAL_TESTING
10000,PLB
12000,EPIRB
14000,ELT
16000,ELT_DT
18000,RESERVED_FUTURE_USE
65521,RESERVED_SYSTEM_BEACONS
11 changes: 2 additions & 9 deletions src/test/java/au/gov/amsa/sgb/decoder/Beacon23HexIdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.junit.Test;

import com.google.common.io.Files;

import au.gov.amsa.sgb.decoder.internal.json.Json;
import au.gov.amsa.sgb.decoder.vesselid.AircraftRegistrationMarking;
import au.gov.amsa.sgb.decoder.vesselid.RadioCallSign;

Expand All @@ -22,16 +17,14 @@ public void testDecode() throws IOException {
Beacon23HexId b = Beacon23HexId.fromHex("9934039823D000000000000");
assertEquals(201, b.countryCode());
assertEquals(230, b.tac());
assertFalse(b.tacDescription().isPresent());
assertEquals(573, b.serialNumber());
assertFalse(b.testProtocolFlag());
assertFalse(b.vesselId().isPresent());
TestingUtil.assertJsonEquals(
"{\"countryCode\":201,\"tac\":230,\"serialNumber\":573,\"testProtocolFlag\":false}", b.toJson());
File f = new File("src/test/resources/compliance-kit/beacon-23-hex-id-sample.json");
f.delete();
Files.write(Json.prettyPrint(b.toJson()).getBytes(StandardCharsets.UTF_8), f);
}

@Test(expected = IllegalArgumentException.class)
public void testDecodeHexWrongSize() {
Beacon23HexId.fromHex("123");
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/au/gov/amsa/sgb/decoder/ComplianceKitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void runAllComplianceTests() throws IOException {
public void testCreateComplianceKitInTargetFolder() throws ComparisonFailure, IOException {

// When new hex tests are obtained (confirmed ones from community/spec authors)
// then they are added below. Once happy with the tests they are copied from
// then they are added below. Once happy with the tests they are copied manually from
// target/compliance-kit to src/test/resources/compliance-kit as a permanent
// test for the build

Expand Down Expand Up @@ -67,8 +67,8 @@ public void testCreateComplianceKitInTargetFolder() throws ComparisonFailure, IO
.filename("detection-with-aircraft-registration-marking.json") //
.addTo(kit, tests);
KitTest.type(TestType.BEACON_23_HEX_ID) //
.title("Specification example B-2") //
.hex("9934039823D000000000000") //
.title("G.005 Specification Checksum example") //
.hex("CCB7EA73355000000000000") //
.filename("beacon-23-hex-id-sample.json") //
.addTo(kit, tests);

Expand All @@ -78,7 +78,7 @@ public void testCreateComplianceKitInTargetFolder() throws ComparisonFailure, IO

private static void writeCsv(File kit, List<KitTest> tests) {
StringBuilder b = new StringBuilder();
b.append("TYPE,TITLE,HEX,JSON");
b.append("TYPE,TITLE,HEX,JSON\n");
for (KitTest test : tests) {
b.append(test.toCsvLine());
b.append("\n");
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/au/gov/amsa/sgb/decoder/DetectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ public void testReadVesselId() {
public void testRotatingFieldTypeInLongMessage() {
Detection d = Detection.fromHexGroundSegmentRepresentation(
"0039823D32618658622811F725F2B1C67703FFF004030680258");
System.out.println(d);
assertTrue(d.toString().startsWith("{"));
}

private static String leftPadWithZeros(String s, int length) {
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/au/gov/amsa/sgb/decoder/internal/TacTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package au.gov.amsa.sgb.decoder.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.net.MalformedURLException;
import java.net.URL;

import org.junit.Test;

public class TacTest {

@Test(expected = IllegalArgumentException.class)
public void testMerge() {
Tac.MERGE_FUNCTION.apply("a", "a");
}

@Test
public void testDescriptions() {
assertFalse(Tac.description(1).isPresent());
assertEquals("PLB", Tac.description(10000).get());
assertEquals("PLB", Tac.description(10001).get());
assertEquals("TYPE_APPROVAL_TESTING", Tac.description(9998).get());
assertEquals("EPIRB", Tac.description(12000).get());
assertEquals("ELT", Tac.description(14000).get());
assertEquals("ELT_DT", Tac.description(16000).get());
assertEquals("RESERVED_FUTURE_USE", Tac.description(18000).get());
assertEquals("RESERVED_SYSTEM_BEACONS", Tac.description(65535).get());
}

@Test(expected = RuntimeException.class)
public void testLoadErrors() throws MalformedURLException {
Tac.loadDescriptions(new URL("http://with space"));
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"countryCode" : 201,
"tac" : 230,
"serialNumber" : 573,
"countryCode" : 613,
"tac" : 64156,
"tacDescription" : "RESERVED_FUTURE_USE",
"serialNumber" : 13141,
"testProtocolFlag" : false
}
2 changes: 1 addition & 1 deletion src/test/resources/compliance-kit/tests.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ TYPE,TITLE,HEX,JSON
"Detection","Example B-2 with Vessel Id of type MMSI","0039823D32618658622811F23ADE68AA17E3FFF004030680258","detection-with-mmsi-vessel-id.json"
"Detection","Example B-2 with Vessel Id Aircraft Operator and Serial Number","0039823D32618658622811FB7AC403C00003FFF004030680258","detection-with-aircraft-operator-and-serial-number.json"
"Detection","Example B-2 with Vessel Id Aircraft Registration Marking","0039823D32618658622811F725F2B1C67703FFF004030680258","detection-with-aircraft-registration-marking.json"
"Beacon23HexId","Specification example B-2","9934039823D000000000000","beacon-23-hex-id-sample.json"
"Beacon 23 Hex Id","G.005 Specification Checksum example","CCB7EA73355000000000000","beacon-23-hex-id-sample.json"