@Test
 public void testGetFactoryService() {
   GridConnection<GridNode> connection = new LocalGridNodeConnection("test-id");
   GridNode gnode = connection.connect();
   KnowledgeBuilderFactoryService kbfService = gnode.get(KnowledgeBuilderFactoryService.class);
   assertNotNull(kbfService);
 }
  @Test
  public void testSetObject() {
    GridConnection<GridNode> connection = new LocalGridNodeConnection("test-id");
    GridNode gnode = connection.connect();

    KnowledgeBaseFactoryService kbfService = gnode.get(KnowledgeBaseFactoryService.class);
    KnowledgeBase kbase = kbfService.newKnowledgeBase();
    gnode.set("id1", kbase);
    assertSame(kbase, gnode.get("id1", KnowledgeBase.class));
  }
  @Test
  public void testNodeCreationAndWhitePagesRegistration() {
    Grid grid = new GridImpl(new HashMap<String, Object>());

    GridPeerConfiguration conf = new GridPeerConfiguration();

    // Configuring the WhitePages
    GridPeerServiceConfiguration wplConf = new WhitePagesLocalConfiguration();
    conf.addConfiguration(wplConf);

    conf.configure(grid);

    GridNode gnode = grid.createGridNode("test1@local");

    WhitePages pages = grid.get(WhitePages.class);
    GridServiceDescription<GridNode> gsd = pages.create("test1@local", "grid0");

    GridServiceDescription<GridNode> serviceDescription = pages.lookup("test1@local");

    GridConnection connection = grid.get(ConnectionFactoryService.class).createConnection(gsd);
    assertSame(gnode, connection.connect());
  }
 @Test
 public void testConnectWithGivenGridNode() {
   GridNode gnode = new GridNodeImpl();
   GridConnection<GridNode> connection = new LocalGridNodeConnection(gnode);
   assertSame(gnode, connection.connect());
 }
 @Test
 public void testConnectWithId() {
   GridConnection<GridNode> connection = new LocalGridNodeConnection("test-id");
   GridNode gnode = connection.connect();
   assertNotNull(gnode);
 }
  public static synchronized GridNode getGridNode(String name, Grid grid, boolean forceRemote) {

    if (logger.isDebugEnabled()) {
      logger.debug(" ### Grid Helper trying to locate GridNode: " + name);
    }

    if (nodeCache.containsKey(name)) {
      logger.debug(" ### Grid Helper found node " + name + " in cache");
      return nodeCache.get(name);
    }

    GridServiceDescription<GridNode> nGsd = grid.get(WhitePages.class).lookup(name);

    if (nGsd == null) {
      if (logger.isDebugEnabled()) {
        logger.error(
            "("
                + Thread.currentThread().getId()
                + ")"
                + Thread.currentThread().getName()
                + " ### Grid Helper DOES NOT Found a Node Descriptor for: "
                + name);
      }
      return null;
    }
    if (logger.isDebugEnabled()) {

      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### Grid Helper Found Node Descriptor: "
              + nGsd);
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### \t id: "
              + nGsd.getId());
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### \t Address size: "
              + nGsd.getAddresses().size());
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### \t Addresses: "
              + nGsd.getAddresses());
      for (String key : nGsd.getAddresses().keySet()) {
        logger.debug(
            "("
                + Thread.currentThread().getId()
                + ")"
                + Thread.currentThread().getName()
                + " \t ### Address: "
                + nGsd.getAddresses().get(key));
      }

      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### \t Interface: "
              + nGsd.getServiceInterface());
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### \t DATA: "
              + nGsd.getData());
    }

    ConnectionFactoryService csf = grid.get(ConnectionFactoryService.class);
    boolean allowsLocal = csf.isLocalAllowed();
    csf.setLocalAllowed(!forceRemote);
    GridConnection<GridNode> conn = csf.createConnection(nGsd);
    csf.setLocalAllowed(allowsLocal);

    if (logger.isDebugEnabled()) {
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### Grid Helper Create a Conection: "
              + name);
    }
    GridNode node = conn.connect();

    if (logger.isDebugEnabled()) {
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### Grid Helper found GridNode: ("
              + name
              + ") -> "
              + node);
    }

    nodeCache.put(name, node);

    return node;
  }