package nl.sikken.bertrik.cayenne;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Unit test for SimpleCayenne.
 */
public final class SimpleCayenneTest {
    
    private static final Logger LOG = LoggerFactory.getLogger(SimpleCayenneTest.class);
    
    /**
     * Verifies basic functionality by adding some items and encoding it into a message
     * @throws CayenneException in case of a problem encoding/decoding
     */
    @Test
    public void testEncode() throws CayenneException {
        SimpleCayenne cayenne = new SimpleCayenne();
        cayenne.addGps(1, 52.0, 4.0, -1.0);
        cayenne.addAnalogInput(2, 3.82);
        cayenne.addTemperature(3, 19.0);
        LOG.info("Encoded message: {}", cayenne);
        
        // encode it
        byte[] data = cayenne.encode(500);
        Assert.assertNotNull(data);
        
        // decode it
        CayenneMessage message = CayenneMessage.parse(data);
        List<CayenneItem> items = message.getItems();
        Assert.assertEquals(52.0, items.get(0).getValues()[0], 0.1);
        Assert.assertEquals(3.82, items.get(1).getValue(), 0.01);
        Assert.assertEquals(19.0, items.get(2).getValues()[0], 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)
    public void testNonUniqueChannel() throws CayenneException {
        SimpleCayenne cayenne = new SimpleCayenne();
        cayenne.addTemperature(1, 19.0);
        cayenne.addAnalogInput(1, 3.90);
    }

}

@@ -675,28 +675,36 @@
 .add
-Gps
+Accelerometer
 (1, 
-52
+1
 .0, 
-4
+2
 .0, 
--1
+3
 .0);
@@ -743,24 +743,431 @@
 (2, 3.82);%0D%0A
+        cayenne.addAnalogOutput(3, 3.14);%0D%0A        cayenne.addBarometricPressure(4, 100000);%0D%0A        cayenne.addDigitalInput(5, true);%0D%0A        cayenne.addDigitalOutput(6, true);%0D%0A        cayenne.addGps(7, 52.0, 4.0, -1.0);%0D%0A        cayenne.addGyrometer(8, 1.0, 2.0, 3.0);%0D%0A        cayenne.addIlluminance(9, 1000.0);%0D%0A        cayenne.addPresence(10, true);%0D%0A        cayenne.addRelativeHumidity(11, 50.0);%0D%0A
         caye
@@ -1185,17 +1185,18 @@
 erature(
-3
+12
 , 19.0);
@@ -1467,33 +1467,31 @@
     
-List%3CCayenneItem%3E items =
+Assert.assertEquals(12,
  mes
@@ -1505,16 +1505,24 @@
 tItems()
+.size())
 ;%0D%0A     
@@ -1554,19 +1554,48 @@
 .0, 
-items.get(0
+message.ofType(ECayenneItem.GPS_LOCATION
 ).ge
@@ -1653,19 +1653,48 @@
 82, 
-items.get(1
+message.ofType(ECayenneItem.ANALOG_INPUT
 ).ge
@@ -1749,19 +1749,47 @@
 .0, 
-items.get(2
+message.ofType(ECayenneItem.TEMPERATURE
 ).ge
@@ -1786,38 +1786,34 @@
 RATURE).getValue
-s
 ()
-%5B0%5D
 , 0.1);%0D%0A    %7D%0D%0A

Update Cayenne test.