net.sheehantech.cherry.ProtocolTest.java Source code

Java tutorial

Introduction

Here is the source code for net.sheehantech.cherry.ProtocolTest.java

Source

/*
 * This file is part of Cherry.
 *
 * Cherry is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cherry is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Cherry.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package net.sheehantech.cherry;

import org.apache.commons.codec.binary.Hex;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Date;

import static org.junit.Assert.assertEquals;

public class ProtocolTest {

    @BeforeClass
    public static void init() {
        System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "DEBUG");
    }

    @Test
    public void binaryEncodeShortBasic() throws Exception {
        String expectedContent = "{\"aps\":{\"alert\":\"0\"}}";
        Notification notification = new Notification();
        notification.setToken("00");
        notification.setAPS(new BasicAPSInfo("0"));
        byte[] encodedPayload = Protocol.legacyEncode(notification);
        //System.out.println("Length: " + encodedPayload.length);
        //for (byte b : encodedPayload) System.out.println(b);
        byte[] expected = expected((byte) 0, Hex.decodeHex("00".toCharArray()), expectedContent.getBytes());
        assertEquals(expected.length, encodedPayload.length);
        for (int b = 0; b < encodedPayload.length; b++) {
            //System.out.println("Checking byte " + b);
            assertEquals(expected[b], encodedPayload[b]);
        }
    }

    @Test
    public void binaryEncodeLongBasic() throws Exception {
        Notification notification = new Notification();
        notification.setToken("7504e94cc9c3b7a34f16edc0d1d1f44e07ff30c5881abbff756ba8834522ae5d");
        notification.setAPS(new BasicAPSInfo("Some alert!"));
        byte[] encodedPayload = Protocol.legacyEncode(notification);
        //System.out.println("Length: " + encodedPayload.length);
        //for (byte b : encodedPayload) System.out.println(b);
    }

    @Test
    public void binaryEncodeShortEnhanced() throws Exception {
        String expectedContent = "{\"aps\":{\"alert\":{\"body\":\"Some body\",\"loc-args\":[\"Blah blah\"],\"loc-key\":\"SOMETHING\",\"title\":\"Something\"},\"sound\":\"other.aiff\"},\"command\":\"doit\"}";
        NotificationAlert alert = new NotificationAlert();
        alert.setTitle("Something");
        alert.setBody("Some body");
        alert.setLocKey("SOMETHING");
        alert.addLocArg("Blah blah");
        ExtendedAPSInfo extendedAPSInfo = new ExtendedAPSInfo();
        extendedAPSInfo.setAlert(alert);
        extendedAPSInfo.setSound("other.aiff");
        Notification notification = new Notification();
        notification.setId(1);
        notification.setToken("6c2ea18861e0d1ccf521984655c3b6e8660bd2f3daff4e15156a73847a17e647");
        notification.setAPS(extendedAPSInfo);
        notification.addField("command", "doit");
        notification.setExpires(new Date());
        byte[] encodedPayload = Protocol.legacyEncode(notification);
        //System.out.println("Length: " + encodedPayload.length);
        byte[] expected = expectedEnhanced((byte) 1, 1, (int) (notification.getExpires().getTime() / 1000),
                Hex.decodeHex("6c2ea18861e0d1ccf521984655c3b6e8660bd2f3daff4e15156a73847a17e647".toCharArray()),
                expectedContent.getBytes());
        assertEquals(expected.length, encodedPayload.length);
        for (int b = 0; b < encodedPayload.length; b++) {
            //System.out.println("Checking (" + b + ") expected " + expected[b] + " against actual " + encodedPayload[b]);
            assertEquals(expected[b], encodedPayload[b]);
        }
    }

    private byte[] expected(byte command, byte[] token, byte[] payload) {
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        DataOutputStream dataStream = new DataOutputStream(byteStream);
        try {
            dataStream.writeByte(command);
            dataStream.writeShort(token.length);
            dataStream.write(token);
            dataStream.writeShort(payload.length);
            dataStream.write(payload);
            return (byteStream.toByteArray());
        } catch (final IOException e) {
            throw new AssertionError();
        }
    }

    private byte[] expectedEnhanced(byte command, int identifier, int expiryTime, byte[] deviceToken,
            byte[] payload) {
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        DataOutputStream dataStream = new DataOutputStream(byteStream);
        try {
            dataStream.writeByte(command);
            dataStream.writeInt(identifier);
            dataStream.writeInt(expiryTime);
            dataStream.writeShort(deviceToken.length);
            dataStream.write(deviceToken);
            dataStream.writeShort(payload.length);
            dataStream.write(payload);
            return byteStream.toByteArray();
        } catch (final IOException e) {
            throw new AssertionError();
        }
    }

}