/** Test oneOf in presence of a fence constraint */
  public void testFenceOneOf() {
    Configuration cfg = new SimpleConfiguration();

    // Some nodes
    ExplodedSet<Node>[] parts = new ExplodedSet[2];
    for (int i = 0; i < parts.length; i++) {
      parts[i] = new ExplodedSet<Node>("$P" + (i + 1));
      for (int j = 0; j < 5; j++) {
        Node n = new SimpleNode("N" + (10 * i + j + 1), 10, 10, 10);
        parts[i].add(n);
        cfg.addOnline(n);
      }
    }

    // Some VMs
    ExplodedSet<VirtualMachine> app = new ExplodedSet<VirtualMachine>("$A");
    ExplodedSet<VirtualMachine> sub = new ExplodedSet<VirtualMachine>("$A/2");
    for (int i = 0; i < 10; i++) {
      VirtualMachine vm = new SimpleVirtualMachine("VM" + (i + 1), 1, 1, 1);
      cfg.addWaiting(vm);
      if (i < 5) {
        sub.add(vm);
      }
      app.add(vm);
    }

    PlanPartitioner part = new OtherPartitioning(cfg);
    ExplodedMultiSet<Node> m = new ExplodedMultiSet<Node>();
    m.add(parts[0]);
    m.add(parts[1]);
    OneOf of = new OneOf(app, m);
    Fence f = new Fence(app, parts[0]);

    try {
      part.part(f);
      part.part(of);
    } catch (PartitioningException e) {
      Assert.fail(e.getMessage(), e);
    }
    part.getResultingPartitions();
  }
  public void testSimpleFence() {
    Configuration cfg = new SimpleConfiguration();

    // Some nodes
    ExplodedSet<Node>[] parts = new ExplodedSet[4];
    for (int i = 0; i < parts.length; i++) {
      parts[i] = new ExplodedSet<Node>("$P" + (i + 1));
      for (int j = 0; j < 5; j++) {
        Node n = new SimpleNode("N" + (10 * i + j + 1), 10, 10, 10);
        parts[i].add(n);
        cfg.addOnline(n);
      }
    }

    // Some VMs
    ExplodedSet<VirtualMachine>[] apps = new ExplodedSet[5];
    for (int i = 0; i < apps.length; i++) {
      apps[i] = new ExplodedSet<VirtualMachine>("$A" + (i + 1));
      for (int j = 0; j < 10; j++) {
        VirtualMachine vm = new SimpleVirtualMachine("VM" + (i * 10 + j + 1), 1, 1, 1);
        apps[i].add(vm);
        cfg.addWaiting(vm);
      }
    }

    PlanPartitioner part = new OtherPartitioning(cfg);
    for (int i = 0; i < apps.length; i++) {
      Fence f = new Fence(apps[i], parts[i % parts.length]);
      try {
        part.part(f);
      } catch (PartitioningException e) {
        Assert.fail();
      }
    }
    List<Partition> ps = part.getResultingPartitions();
    Assert.assertEquals(ps.size(), 4);
    Assert.assertTrue(
        ps.get(0).getNodes().equals(parts[0])
            && ps.get(0).getVirtualMachines().containsAll(apps[0])
            && ps.get(0).getVirtualMachines().containsAll(apps[4]));
    Assert.assertTrue(
        ps.get(1).getNodes().equals(parts[1])
            && ps.get(1).getVirtualMachines().containsAll(apps[1]));
    Assert.assertTrue(
        ps.get(2).getNodes().equals(parts[2])
            && ps.get(2).getVirtualMachines().containsAll(apps[2]));
    Assert.assertTrue(
        ps.get(3).getNodes().equals(parts[3])
            && ps.get(3).getVirtualMachines().containsAll(apps[3]));
  }