/*
 * Copyright 2016 higherfrequencytrading.com
 *
 * Licensed 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 net.openhft.chronicle.wire;

import net.openhft.chronicle.bytes.Bytes;
import org.junit.Assert;
import org.junit.Test;

import java.nio.ByteBuffer;

import static net.openhft.chronicle.wire.WireType.*;

/**
 * @author Rob Austin.
 */
public class ReadAnyWireTest {

    @Test
    public void testReadAny() {
        final Bytes<ByteBuffer> t = Bytes.elasticByteBuffer();
        final Wire wire = TEXT.apply(t);
        wire.write((() -> "hello")).text("world");
        Assert.assertEquals("world", READ_ANY.apply(t).read(() -> "hello").text());
    }

    @Test
    public void testCreateReadAnyFirstTextWire() {
        final Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer();
        final String expected = "world";
        TEXT.apply(bytes).write((() -> "hello")).text(expected);
        Assert.assertEquals(expected, READ_ANY.apply(bytes).read((() -> "hello")).text());
    }

    @Test
    public void testCreateReadAnyFirstBinaryWire() {
        final Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer();
        final String expected = "world";
        BINARY.apply(bytes).write((() -> "hello")).text(expected);
        Assert.assertEquals(expected, READ_ANY.apply(bytes).read((() -> "hello")).text());
    }

    @Test
    public void testCreateReadAnyFirstFIELDLESS_BINARYWire() {
        final Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer();
        final String expected = "world";
        FIELDLESS_BINARY.apply(bytes).writeDocument(false, w -> w.write(() -> "hello").text(expected));
        READ_ANY.apply(bytes).readDocument(null, w -> w.read(() -> "hello").text(expected, Assert::assertEquals));
    }
}


@@ -1882,32 +1882,20 @@
 AnyFirst
-FIELDLESS_BINARY
+JSON
 Wire() %7B
@@ -2015,24 +2015,12 @@
     
-FIELDLESS_BINARY
+JSON
 .app
@@ -2038,36 +2038,9 @@
 rite
-Document(false, w -%3E w.write
+(
 (() 
@@ -2042,32 +2042,33 @@
 ((() -%3E %22hello%22)
+)
 .text(expected))
@@ -2066,17 +2066,16 @@
 xpected)
-)
 ;%0A      
@@ -2080,60 +2080,323 @@
     
-READ_ANY.apply(bytes).readDocument(null, w -%3E w.read
+Assert.assertEquals(expected, READ_ANY.apply(bytes).read((() -%3E %22hello%22)).text());%0A    %7D%0A%0A    @Test%0A    public void testCreateReadAnyFirstFIELDLESS_BINARYWire() %7B%0A        final Bytes%3CByteBuffer%3E bytes = Bytes.elasticByteBuffer();%0A        final String expected = %22world%22;%0A        FIELDLESS_BINARY.apply(bytes).write(
 (() 
@@ -2402,24 +2402,25 @@
  -%3E %22hello%22)
+)
 .text(expect
@@ -2425,18 +2425,26 @@
 cted
-,
+);%0A       
  Assert
-::
+.
 asse
@@ -2451,16 +2451,76 @@
 rtEquals
+(expected, READ_ANY.apply(bytes).read((() -%3E %22hello%22)).text(
 ));%0A    

Check for JSON in AnyWire