/*
 * Copyright 2013 the original author or authors.
 *
 * 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.kuujo.vertigo.test.unit;

import net.kuujo.vertigo.context.ContextBuilder;
import net.kuujo.vertigo.context.NetworkContext;
import net.kuujo.vertigo.network.MalformedNetworkException;
import net.kuujo.vertigo.network.Network;
import net.kuujo.vertigo.serializer.SerializationException;
import net.kuujo.vertigo.serializer.Serializer;

import org.junit.Test;
import org.vertx.java.core.json.JsonArray;
import org.vertx.java.core.json.JsonObject;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

/**
 * Definition to context tests.
 *
 * @author Jordan Halterman
 */
public class ContextTest {

  @Test
  public void testNetworkContext() {
    Network network = new Network("test");
    network.setNumAckers(2);
    network.enableAcking();

    network.addVerticle("test1", "net.kuujo.vertigo.VertigoVerticle");
    network.addVerticle("test2", "net.kuujo.vertigo.VertigoVerticle").addInput("test1");

    NetworkContext context;
    try {
      context = ContextBuilder.buildContext(network);
      assertEquals(2, context.getAuditors().size());
      JsonObject serialized = Serializer.serialize(context);
      assertNotNull(serialized);
      try {
        context = Serializer.deserialize(serialized);
      }
      catch (SerializationException e) {
        fail(e.getMessage());
      }
    }
    catch (MalformedNetworkException e) {
      fail(e.getMessage());
    }
  }

  @Test
  public void testContextFromJson() {
    JsonObject json = new JsonObject();
    json.putString("address", "test");
    JsonObject components = new JsonObject();
    components.putObject("test2", new JsonObject()
      .putString("type", "verticle").putString("main", "net.kuujo.vertigo.input.grouping.RandomGrouping"));
    components.putObject("test2", new JsonObject()
      .putString("type", "verticle").putString("main", "net.kuujo.vertigo.VertigoVerticle")
      .putArray("inputs", new JsonArray().add(new JsonObject().putString("address", "test2").putObject("grouping",
          new JsonObject().putString("type", "net.kuujo.vertigo.input.grouping.RoundGrouping")))));

    try {
      Network network = Network.fromJson(json);
      ContextBuilder.buildContext(network);
    }
    catch (MalformedNetworkException e) {
      fail(e.getMessage());
    }
  }

}

@@ -1331,24 +1331,78 @@
 Context() %7B%0A
+    Serializer serializer = Serializer.getInstance();%0A
     Network 
@@ -1455,11 +1455,13 @@
 NumA
-cke
+udito
 rs(2
@@ -1828,17 +1828,17 @@
 lized = 
-S
+s
 erialize
@@ -1896,22 +1896,8 @@
 d);%0A
-      try %7B%0A  
     
@@ -1908,17 +1908,17 @@
 ntext = 
-S
+s
 erialize
@@ -1941,29 +1941,47 @@
 rialized
+, NetworkContext.class
 );%0A    
-  %7D%0A  
+%7D%0A
     catc
@@ -2010,68 +2010,10 @@
 ion 
-e) %7B%0A        fail(e.getMessage());%0A      %7D%0A    %7D%0A    catch (
+%7C 
 Malf

Update context tests.