package tc.oc.protobuf.packet.base;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import tc.oc.protobuf.packet.*;
import tc.oc.protobuf.packet.protocols.TestExtendingMessage;
import tc.oc.protobuf.packet.protocols.TestGenericMessage;

import java.util.Random;

/**
 * File created at: 3/17/13 12:17 AM
 */
@RunWith(JUnit4.class)
public class SimplePacketManagerTest implements MessageListener {
    private MessageHandlerRegistry handlerRegistry;
    private PacketManager<TestGenericMessage.GenericMessage> packetManager;
    private int extendedInt;
    private int parsedInt;
    private boolean handlerHasRun;
    private boolean handlerHasParsed;

    @Before
    public void initialize() {
        this.handlerRegistry = new SimpleMessageHandlerRegistry();
        handlerRegistry.registerAll(this);
        this.packetManager = new SimplePacketManager<>(TestGenericMessage.GenericMessage.getDefaultInstance());
        this.handlerHasRun = false;
        this.handlerHasParsed = false;
        this.extendedInt = new Random().nextInt(15);
        this.parsedInt = -1;
    }

    @Test
    public void handlerRunTest() throws InterruptedException {
        this.packetManager.parse(TestGenericMessage.GenericMessage.getDefaultInstance(), this.handlerRegistry);
        Assert.assertTrue("handle() did not run", handlerHasRun);
    }

    @Test
    public void handlerNullCheckTest() {
        this.packetManager.parse(TestGenericMessage.GenericMessage.getDefaultInstance(), this.handlerRegistry);
        Assert.assertFalse("handle() continued even with a null ExtendingMessage", handlerHasParsed);
    }

    @Test
    public void handlerParseTest() {
        this.packetManager.parse(TestGenericMessage.GenericMessage.newBuilder().setExtension(TestExtendingMessage.ExtendingMessage.extendingMessage, TestExtendingMessage.ExtendingMessage.newBuilder().setNumericalValue(extendedInt).build()).build(), this.handlerRegistry);
        Assert.assertEquals("handle() incorrectly parsed ExtendingMessage (or got corrupt ExtendingMessage)", extendedInt, parsedInt);
    }

    public void buildTest() {
        //  TODO: Implementation.
    }

    @Handler
    public void handle(TestGenericMessage.GenericMessage message) throws HandlerException {
        TestExtendingMessage.ExtendingMessage extendingMessage = (TestExtendingMessage.ExtendingMessage) message.getExtension(TestExtendingMessage.ExtendingMessage.extendingMessage);
        if (extendingMessage != null) {
            this.parsedInt = extendingMessage.getNumericalValue();
            this.handlerHasParsed = true;
        }
        this.handlerHasRun = true;
    }
}

@@ -2156,24 +2156,34 @@
 nt);%0A    %7D%0A%0A
+    @Test%0A
     public v
@@ -2212,154 +2212,144 @@
     
-//  TODO: Implementation.%0A    %7D%0A%0A    @Handler%0A    public void handle(TestGenericMessage.GenericMessage message) throws HandlerException %7B%0A        
+parsedInt = packetManager.build(TestExtendingMessage.ExtendingMessage.newBuilder().setNumericalValue(extendedInt).build()).getExtension(
 Test
@@ -2381,17 +2381,17 @@
 gMessage
- 
+.
 extendin
@@ -2402,71 +2402,174 @@
 sage
- = (TestExtendingMessage.ExtendingMessage) message.getExtension
+).getNumericalValue();%0A        Assert.assertEquals(%22build() did not return an equivalent message%22, extendedInt, parsedInt);%0A    %7D%0A%0A    @Handler%0A    public void handle
 (Tes
@@ -2606,27 +2606,43 @@
 sage
-.extendingMessage);
+ message) throws HandlerException %7B
 %0A   
@@ -2650,26 +2650,17 @@
     if (
-extendingM
+m
 essage !
@@ -2698,26 +2698,17 @@
 edInt = 
-extendingM
+m
 essage.g

Add build() test