@Test
  public void testFindAllPoliciesWithTheSameId() throws Exception {
    expect(algorithm.getId()).andReturn("testId");
    expect(decisionAlgorithms.isRuleAlgorithmProvided("testId")).andReturn(true);
    l.policyAdded(p1v2);
    expect(algorithm.getId()).andReturn("testId");
    expect(decisionAlgorithms.isRuleAlgorithmProvided("testId")).andReturn(true);
    l.policyAdded(p1v1);
    expect(algorithm.getId()).andReturn("testId");
    expect(decisionAlgorithms.isRuleAlgorithmProvided("testId")).andReturn(true);
    l.policyAdded(p1v3);
    expect(algorithm.getId()).andReturn("testId");
    expect(decisionAlgorithms.isRuleAlgorithmProvided("testId")).andReturn(true);
    l.policyAdded(p1v4);

    c.replay();

    r.add(p1v2);
    r.add(p1v1);
    r.add(p1v3);
    r.add(p1v4);

    Collection<Policy> found = r.getPolicies("id1", null, null, null);
    assertEquals(4, found.size());
    Iterator<Policy> it = found.iterator();

    assertEquals(Version.parse("1"), it.next().getVersion());
    assertEquals(Version.parse("1.1"), it.next().getVersion());
    assertEquals(Version.parse("1.2.1"), it.next().getVersion());
    assertEquals(Version.parse("2.0.1"), it.next().getVersion());

    c.verify();
  }
  @Test
  public void testOnePolicyDifferentVersion() throws Exception {

    expect(algorithm.getId()).andReturn("testId");
    expect(decisionAlgorithms.isRuleAlgorithmProvided("testId")).andReturn(true);
    l.policyAdded(p1v1);
    expect(algorithm.getId()).andReturn("testId");
    expect(decisionAlgorithms.isRuleAlgorithmProvided("testId")).andReturn(true);
    l.policyAdded(p1v2);
    expect(algorithm.getId()).andReturn("testId");
    expect(decisionAlgorithms.isRuleAlgorithmProvided("testId")).andReturn(true);
    l.policyAdded(p1v3);
    expect(algorithm.getId()).andReturn("testId");
    expect(decisionAlgorithms.isRuleAlgorithmProvided("testId")).andReturn(true);
    l.policyAdded(p1v4);

    c.replay();

    r.add(p1v1);
    r.add(p1v2);
    r.add(p1v3);
    r.add(p1v4);

    CompositeDecisionRule p = r.get("id1", Version.parse("1.0"));
    assertEquals(p1v1, p);
    p = r.get("id1", Version.parse("1.1"));
    assertEquals(p1v2, p);
    p = r.get("id1", Version.parse("1.2.1"));
    assertEquals(p1v3, p);
    p = r.get("id1", Version.parse("2.0.1"));
    assertEquals(p1v4, p);

    Collection<Policy> found = r.getPolicies("id1", VersionMatch.parse("1.+"));
    assertEquals(3, found.size());
    Iterator<Policy> it = found.iterator();

    // sorted by version
    assertEquals(Version.parse("1"), it.next().getVersion());
    assertEquals(Version.parse("1.1"), it.next().getVersion());
    assertEquals(Version.parse("1.2.1"), it.next().getVersion());

    found = r.getPolicies("id1", VersionMatch.parse("1.2.+"));
    assertEquals(1, found.size());

    found = r.getPolicies("id1", VersionMatch.parse("1.+"), VersionMatch.parse("1.2.*"));
    assertEquals(1, found.size());

    Policy policy = r.getPolicy("id1", null, null, null);
    assertEquals(Version.parse("2.0.1"), policy.getVersion());

    c.verify();
  }
  @Test
  public void testAddRemove() throws Exception {
    expect(algorithm.getId()).andReturn("testId");
    expect(decisionAlgorithms.isRuleAlgorithmProvided("testId")).andReturn(true);
    l.policyAdded(p1v2);
    l.policyRemoved(p1v2);
    c.replay();

    r.add(p1v2);

    CompositeDecisionRule p = r.get("id1", Version.parse("1.0"));
    assertNull(p);
    p = r.get("id1", Version.parse("1.1"));
    assertEquals(p1v2, p);
    r.remove(p1v2);

    Collection<Policy> found = r.getPolicies("id1", VersionMatch.parse("1.+"));
    assertEquals(0, found.size());

    c.verify();
  }
 @Test
 public void testAddPolicyWithTheSameIdAndSameVersion() {
   expect(algorithm.getId()).andReturn("testId");
   expect(decisionAlgorithms.isRuleAlgorithmProvided("testId")).andReturn(true);
   l.policyAdded(p1v2);
   expect(algorithm.getId()).andReturn("testId");
   expect(decisionAlgorithms.isRuleAlgorithmProvided("testId")).andReturn(true);
   c.replay();
   assertTrue(r.add(p1v2));
   assertFalse(r.add(p1v2DiffInstance));
   Policy p = (Policy) r.get("id1", Version.parse("1.1"));
   assertSame(p1v2, p);
   assertNotSame(p1v2DiffInstance, p);
   c.verify();
 }