/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.activeblaze;

import junit.framework.TestCase;
import org.apache.activeblaze.wire.Buffer;
import org.apache.activeblaze.wire.IOUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

/**
 * Test BlazeMessage
 */
public class BlazeMessageTest extends TestCase {
    private final String NAME = "testName";

    public void testBytesConversion() throws Exception {
        BlazeMessage msg = new BlazeMessage();
        msg.setBooleanValue("boolean", true);
        msg.setByteValue("byte", (byte) 1);
        msg.setBytesValue("bytes", new byte[1]);
        msg.setCharValue("char", 'a');
        msg.setDoubleValue("double", 1.5);
        msg.setFloatValue("float", 1.5f);
        msg.setIntValue("int", 1);
        msg.setLongValue("long", 1);
        msg.put("object", "stringObj");
        msg.setShortValue("short", (short) 1);
        msg.setStringValue("string", "string");
        // Test with a 1Meg String
        StringBuffer bigSB = new StringBuffer(1024 * 1024);
        for (int i = 0; i < 1024 * 1024; i++) {
            bigSB.append((char) 'a' + i % 26);
        }
        String bigString = bigSB.toString();
        msg.setStringValue("bigString", bigString);
        msg = msg.clone();
        assertEquals(msg.getBooleanValue("boolean"), true);
        assertEquals(msg.getByteValue("byte"), (byte) 1);
        assertEquals(msg.getBytesValue("bytes").length, 1);
        assertEquals(msg.getCharValue("char"), 'a');
        assertEquals(msg.getDoubleValue("double"), 1.5, 0);
        assertEquals(msg.getFloatValue("float"), 1.5f, 0);
        assertEquals(msg.getIntValue("int"), 1);
        assertEquals(msg.getLongValue("long"), 1);
        assertEquals(msg.getObjectValue("object"), "stringObj");
        assertEquals(msg.getShortValue("short"), (short) 1);
        assertEquals(msg.getStringValue("string"), "string");
        assertEquals(msg.getStringValue("bigString"), bigString);
    }

    public void testGetBoolean() throws Exception {
        BlazeMessage msg = new BlazeMessage();
        msg.setBooleanValue(NAME, true);
        assertTrue(msg.getBooleanValue(NAME));
        msg.clear();
        msg.setStringValue(NAME, "true");
        msg = msg.clone();
        assertTrue(msg.getBooleanValue(NAME));
    }

    public void testGetByte() throws Exception {
        BlazeMessage msg = new BlazeMessage();
        msg.setByteValue(this.NAME, (byte) 1);
        msg = msg.clone();
        assertTrue(msg.getByteValue(this.NAME) == (byte) 1);
    }

    public void testGetShort() throws Exception {
        BlazeMessage msg = new BlazeMessage();
        msg.setShortValue(this.NAME, (short) 1);
        msg = msg.clone();
        assertTrue(msg.getShortValue(this.NAME) == (short) 1);
    }

    public void testGetChar() throws Exception {
        BlazeMessage msg = new BlazeMessage();
        msg.setCharValue(this.NAME, 'a');
        msg = msg.clone();
        assertTrue(msg.getCharValue(this.NAME) == 'a');
    }

    public void testGetInt() throws Exception {
        BlazeMessage msg = new BlazeMessage();
        msg.setIntValue(this.NAME, 1);
        msg = msg.clone();
        assertTrue(msg.getIntValue(this.NAME) == 1);
    }

    public void testGetLong() throws Exception {
        BlazeMessage msg = new BlazeMessage();
        msg.setLongValue(this.NAME, 1);
        msg = msg.clone();
        assertTrue(msg.getLongValue(this.NAME) == 1);
    }

    public void testGetFloat() throws Exception {
        BlazeMessage msg = new BlazeMessage();
        msg.setFloatValue(this.NAME, 1.5f);
        msg = msg.clone();
        assertTrue(msg.getFloatValue(this.NAME) == 1.5f);
    }

    public void testGetDouble() throws Exception {
        BlazeMessage msg = new BlazeMessage();
        msg.setDoubleValue(this.NAME, 1.5);
        msg = msg.clone();
        assertTrue(msg.getDoubleValue(this.NAME) == 1.5);
    }

    public void testGetString() throws Exception {
        BlazeMessage msg = new BlazeMessage();
        String str = "test";
        msg.setStringValue(this.NAME, str);
        msg = msg.clone();
        assertEquals(msg.getStringValue(this.NAME), str);
    }

    public void testGetText() throws Exception {
        String str = "test";
        BlazeMessage msg = new BlazeMessage(str);
        msg = msg.clone();
        assertEquals(msg.getText(), str);
    }

    public void testGetBytes() throws Exception {
        BlazeMessage msg = new BlazeMessage();
        byte[] bytes1 = new byte[3];
        byte[] bytes2 = new byte[2];
        System.arraycopy(bytes1, 0, bytes2, 0, 2);
        msg.setBytesValue(this.NAME, bytes1);
        msg.setBytesValue(this.NAME + "2", bytes1, 0, 2);
        msg = msg.clone();
        assertTrue(Arrays.equals(msg.getBytesValue(this.NAME), bytes1));
        assertEquals(msg.getBytesValue(this.NAME + "2").length, bytes2.length);
    }

    public void testGetObject() throws Exception {
        BlazeMessage msg = new BlazeMessage();
        Boolean booleanValue = Boolean.TRUE;
        Byte byteValue = Byte.valueOf("1");
        byte[] bytesValue = new byte[3];
        Character charValue = new Character('a');
        Double doubleValue = Double.valueOf("1.5");
        Float floatValue = Float.valueOf("1.5");
        Integer intValue = Integer.valueOf("1");
        Long longValue = Long.valueOf("1");
        Short shortValue = Short.valueOf("1");
        String stringValue = "string";
        msg.put("boolean", booleanValue);
        msg.put("byte", byteValue);
        msg.put("bytes", bytesValue);
        msg.put("char", charValue);
        msg.put("double", doubleValue);
        msg.put("float", floatValue);
        msg.put("int", intValue);
        msg.put("long", longValue);
        msg.put("short", shortValue);
        msg.put("string", stringValue);
        msg = msg.clone();
        assertTrue(msg.getObjectValue("boolean") instanceof Boolean);
        assertEquals(msg.getObjectValue("boolean"), booleanValue);
        assertEquals(msg.getBooleanValue("boolean"), booleanValue.booleanValue());
        assertTrue(msg.getObjectValue("byte") instanceof Byte);
        assertEquals(msg.getObjectValue("byte"), byteValue);
        assertEquals(msg.getByteValue("byte"), byteValue.byteValue());
        assertTrue(msg.getObjectValue("bytes") instanceof byte[]);
        assertEquals(((byte[]) msg.getObjectValue("bytes")).length,
                bytesValue.length);
        assertEquals(msg.getBytesValue("bytes").length, bytesValue.length);
        assertTrue(msg.getObjectValue("char") instanceof Character);
        assertEquals(msg.getObjectValue("char"), charValue);
        assertEquals(msg.getCharValue("char"), charValue.charValue());
        assertTrue(msg.getObjectValue("double") instanceof Double);
        assertEquals(msg.getObjectValue("double"), doubleValue);
        assertEquals(msg.getDoubleValue("double"), doubleValue.doubleValue(), 0);
        assertTrue(msg.getObjectValue("float") instanceof Float);
        assertEquals(msg.getObjectValue("float"), floatValue);
        assertEquals(msg.getFloatValue("float"), floatValue.floatValue(), 0);
        assertTrue(msg.getObjectValue("int") instanceof Integer);
        assertEquals(msg.getObjectValue("int"), intValue);
        assertEquals(msg.getIntValue("int"), intValue.intValue());
        assertTrue(msg.getObjectValue("long") instanceof Long);
        assertEquals(msg.getObjectValue("long"), longValue);
        assertEquals(msg.getLongValue("long"), longValue.longValue());
        assertTrue(msg.getObjectValue("short") instanceof Short);
        assertEquals(msg.getObjectValue("short"), shortValue);
        assertEquals(msg.getShortValue("short"), shortValue.shortValue());
        assertTrue(msg.getObjectValue("string") instanceof String);
        assertEquals(msg.getObjectValue("string"), stringValue);
        assertEquals(msg.getStringValue("string"), stringValue);
    }

    public void testGetMapNames() throws Exception {
        BlazeMessage msg = new BlazeMessage();
        msg.setBooleanValue("boolean", true);
        msg.setByteValue("byte", (byte) 1);
        msg.setBytesValue("bytes1", new byte[1]);
        msg.setBytesValue("bytes2", new byte[3], 0, 2);
        msg.setCharValue("char", 'a');
        msg.setDoubleValue("double", 1.5);
        msg.setFloatValue("float", 1.5f);
        msg.setIntValue("int", 1);
        msg.setLongValue("long", 1);
        msg.put("object", "stringObj");
        msg.setShortValue("short", (short) 1);
        msg.setStringValue("string", "string");
        msg = msg.clone();
        Enumeration<String> mapNamesEnum = msg.getNames();
        List<String> mapNamesList = Collections.list(mapNamesEnum);
        assertEquals(mapNamesList.size(), 12);
        assertTrue(mapNamesList.contains("boolean"));
        assertTrue(mapNamesList.contains("byte"));
        assertTrue(mapNamesList.contains("bytes1"));
        assertTrue(mapNamesList.contains("bytes2"));
        assertTrue(mapNamesList.contains("char"));
        assertTrue(mapNamesList.contains("double"));
        assertTrue(mapNamesList.contains("float"));
        assertTrue(mapNamesList.contains("int"));
        assertTrue(mapNamesList.contains("long"));
        assertTrue(mapNamesList.contains("object"));
        assertTrue(mapNamesList.contains("short"));
        assertTrue(mapNamesList.contains("string"));
    }

    public void testItemExists() throws Exception {
        BlazeMessage msg = new BlazeMessage();
        msg.setStringValue("exists", "test");
        msg = msg.clone();
        assertTrue(msg.containsKey("exists"));
        assertFalse(msg.containsKey("doesntExist"));
    }

    public void testMarshall() throws Exception {
        String str = "test";
        BlazeMessage msg = new BlazeMessage(str);
        Buffer buffer = IOUtils.writePacket(msg);
        msg = (BlazeMessage) IOUtils.readPacket(buffer);
        assertEquals(msg.getText(), str);
    }
}
