package test.propagation.impl;

import junit.framework.TestCase;
import propagation.PropagationManager;
import propagation.impl.PropagationManagerImpl;

public class PropagationManagerImplTest extends TestCase {
   private PropagationManager pm_;

   protected void setUp() throws Exception {
      pm_ = new PropagationManagerImpl();

      pm_.setRootNode("Japan");

      pm_.addNode("Japan", "Tokyo");
      pm_.addNode("Japan.Tokyo", "WindSensor1");
      pm_.addStateItem("Japan.Tokyo.WindSensor1", 1000, "power supply", 1040);
      pm_.addStateItem("Japan.Tokyo.WindSensor1", 1001, "sensor unit", 1040);
      pm_.addStateItem("Japan.Tokyo.WindSensor1", 1002, "network card", 1040);
      pm_.addNode("Japan.Tokyo", "RainSensor1");
      pm_.addStateItem("Japan.Tokyo.RainSensor1", 1003, "power supply", 1040);
      pm_.addStateItem("Japan.Tokyo.RainSensor1", 1004, "sensor unit", 1040);
      pm_.addStateItem("Japan.Tokyo.RainSensor1", 1005, "network card", 1030);

      pm_.addNode("Japan", "Yokohama");
      pm_.addNode("Japan.Yokohama", "WindSensor2");
      pm_.addStateItem("Japan.Yokohama.WindSensor2", 1000, "power supply", 1040);
      pm_.addStateItem("Japan.Yokohama.WindSensor2", 1001, "sensor unit", 1040);
      pm_.addStateItem("Japan.Yokohama.WindSensor2", 1002, "network card", 1040);
      pm_.addNode("Japan.Yokohama", "RainSensor2");
      pm_.addStateItem("Japan.Yokohama.RainSensor2", 1003, "power supply", 1040);
      pm_.addStateItem("Japan.Yokohama.RainSensor2", 1004, "sensor unit", 1040);
      pm_.addStateItem("Japan.Yokohama.RainSensor2", 1005, "network card", 1030);

      pm_.addNode("Japan", "Osaka");
      pm_.addNode("Japan.Osaka", "WindSensor3");
      pm_.addStateItem("Japan.Osaka.WindSensor3", 1000, "power supply", 1040);
      pm_.addStateItem("Japan.Osaka.WindSensor3", 1001, "sensor unit", 1040);
      pm_.addStateItem("Japan.Osaka.WindSensor3", 1002, "network card", 1040);
      pm_.addNode("Japan.Osaka", "RainSensor3");
      pm_.addStateItem("Japan.Osaka.RainSensor3", 1003, "power supply", 1040);
      pm_.addStateItem("Japan.Osaka.RainSensor3", 1004, "sensor unit", 1040);
      pm_.addStateItem("Japan.Osaka.RainSensor3", 1005, "network card", 1030);

      pm_.createNode("WindSummary", "WindSummary");
      pm_.setUpperNode("WindSummary", "Japan.Tokyo.WindSensor1");
      pm_.setUpperNode("WindSummary", "Japan.Yokohama.WindSensor2");
      pm_.setUpperNode("WindSummary", "Japan.Osaka.WindSensor3");
      pm_.createNode("RainSummary", "RainSummary");
      pm_.setUpperNode("RainSummary", "Japan.Tokyo.RainSensor1");
      pm_.setUpperNode("RainSummary", "Japan.Yokohama.RainSensor2");
      pm_.setUpperNode("RainSummary", "Japan.Osaka.RainSensor3");
   }

   public void testPropagation() {
      printStatus("Initial state");

      pm_.stateChange("Japan.Tokyo.RainSensor1", 1005, 1031);
      printStatus("Japan.Tokyo.RainSensor1:1005  1030->1031");

      pm_.stateChange("Japan.Yokohama.WindSensor2", 1001, 1041);
      printStatus("Japan.Yokohama.WindSensor2:1001  1040->1041");

      pm_.stateChange("Japan.Osaka.RainSensor3", 1003, 1041);
      printStatus("Japan.Osaka.RainSensor3:1003  1040->1041");

      pm_.stateChange("Japan.Yokohama.WindSensor2", 1001, 1040);
      printStatus("Japan.Yokohama.WindSensor2:1001  1041->1040");

      pm_.stateChange("Japan.Osaka.RainSensor3", 1003, 1040);
      printStatus("Japan.Osaka.RainSensor3:1003  1041->1040");

      pm_.stateChange("Japan.Tokyo.RainSensor1", 1005, 1030);
      printStatus("Japan.Tokyo.RainSensor1:1005  1031->1030");

   }

   private void printStatus(String msg) {
      System.out.println("\n\n");
      System.out.println("---------------------------------------------");
      System.out.println(msg);
      System.out.println("---------------------------------------------");
      pm_.printNodes();
      System.out.println("--------------------");
      pm_.printNodes("WindSummary");
      System.out.println("--------------------");
      pm_.printNodes("RainSummary");
   }

   public static void main(String[] args) throws Exception {
      PropagationManagerImplTest pmTest = new PropagationManagerImplTest();
      pmTest.setUp();
      pmTest.testPropagation();
   }
}
