package nl.sikken.bertrik.hab.lorawan;

import java.io.IOException;
import java.io.InputStream;

import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

import nl.sikken.bertrik.hab.lorawan.HeliumUplinkMessage.HotSpot;

public final class HeliumUplinkMessageTest {

    @Test
    public void testDecode() throws IOException {
        // decode JSON
        try (InputStream is = this.getClass().getResourceAsStream("/helium_uplink.json")) {
            ObjectMapper mapper = new ObjectMapper();
            HeliumUplinkMessage uplink = mapper.readValue(is, HeliumUplinkMessage.class);
            Assert.assertNotNull(uplink);

            Assert.assertEquals("6081F9D16837130E", uplink.appEui);
            Assert.assertEquals(0, uplink.fcnt);
            Assert.assertEquals("kissmapper", uplink.name);
            Assert.assertEquals(1, uplink.port);
            Assert.assertEquals(1631457565832L, uplink.reportedAt);
            Assert.assertArrayEquals(new byte[] { 3 }, uplink.payload);

            HotSpot hotSpot = uplink.hotSpots.get(0);
            Assert.assertEquals(52.01745, hotSpot.latitude, 0.00001);
            Assert.assertEquals(4.729876, hotSpot.longitude, 0.00001);
            Assert.assertEquals("melted-quartz-antelope", hotSpot.name);
            Assert.assertEquals(-120, hotSpot.rssi, 0.1);
            Assert.assertEquals(-7.5, hotSpot.snr, 0.1);
        }

    }

}

@@ -88,16 +88,42 @@
 tStream;
+%0Aimport java.time.Instant;
 %0A%0Aimport
@@ -391,16 +391,53 @@
 ption %7B%0A
+        HeliumUplinkMessage helium;%0A%0A
         
@@ -613,34 +613,14 @@
     
-H
+h
 elium
-UplinkMessage uplink
  = m
@@ -667,24 +667,30 @@
 class);%0A    
+    %7D%0A
         Asse
@@ -710,22 +710,18 @@
 ull(
-uplink);%0A%0A    
+helium);%0A%0A
     
@@ -764,22 +764,22 @@
 7130E%22, 
-uplink
+helium
 .appEui)
@@ -780,36 +780,32 @@
 ppEui);%0A        
-   
- 
 Assert.assertEqu
@@ -811,22 +811,22 @@
 uals(0, 
-uplink
+helium
 .fcnt);%0A
@@ -825,36 +825,32 @@
 .fcnt);%0A        
- 
-   
 Assert.assertEqu
@@ -871,22 +871,22 @@
 r%22, 
-uplink
+helium
 .name);%0A
     
@@ -873,36 +873,32 @@
 , helium.name);%0A
-    
         Assert.a
@@ -916,22 +916,22 @@
 (1, 
-uplink
+helium
 .port);%0A
     
@@ -922,28 +922,24 @@
 lium.port);%0A
-    
         Asse
@@ -970,22 +970,22 @@
 65832L, 
-uplink
+helium
 .reporte
@@ -986,28 +986,24 @@
 eportedAt);%0A
-    
         Asse
@@ -1041,22 +1041,22 @@
  %7B 3 %7D, 
-uplink
+helium
 .payload
@@ -1055,28 +1055,24 @@
 .payload);%0A%0A
-    
         HotS
@@ -1085,22 +1085,22 @@
 tSpot = 
-uplink
+helium
 .hotSpot
@@ -1109,36 +1109,32 @@
 get(0);%0A        
-  
-  
 Assert.assertEqu
@@ -1175,36 +1175,32 @@
 00001);%0A        
-   
- 
 Assert.assertEqu
@@ -1238,28 +1238,24 @@
 , 0.00001);%0A
-    
         Asse
@@ -1311,36 +1311,32 @@
 .name);%0A        
-    
 Assert.assertEqu
@@ -1361,28 +1361,24 @@
 rssi, 0.1);%0A
-    
         Asse
@@ -1426,18 +1426,521 @@
         
-%7D
 %0A
+        // decode to LoRaWAN message%0A        LoraWanUplinkMessage lorawan = helium.toUplinkMessage();%0A        Assert.assertEquals(Instant.parse(%222021-09-12T14:39:25.832Z%22), lorawan.getTime());%0A        Assert.assertEquals(%226081F9D16837130E%22, lorawan.getAppId());%0A        Assert.assertEquals(%22kissmapper%22, lorawan.getDevId());%0A        Assert.assertEquals(0, lorawan.getCounter());%0A        Assert.assertEquals(1, lorawan.getPort());%0A        Assert.assertArrayEquals(new byte%5B%5D %7B3%7D, lorawan.getPayloadRaw());
 %0A    %7D%0A%0A

Verify decoded helium message in unit test.