use of org.apache.neethi.Assertion in project cxf by apache.
the class AssertionInfoMapTest method testAllAssertionsIn.
@Test
public void testAllAssertionsIn() {
Policy nested = new Policy();
Assertion nb = new PrimitiveAssertion(new QName("http://x.y.z", "b"));
nested.addAssertion(nb);
Policy p = new Policy();
Assertion a1 = new PrimitiveAssertion(new QName("http://x.y.z", "a"));
Assertion a2 = new PrimitiveAssertion(new QName("http://x.y.z", "a"));
Assertion b = new PrimitiveAssertion(new QName("http://x.y.z", "b"));
Assertion c = new PolicyContainingPrimitiveAssertion(new QName("http://x.y.z", "c"), false, false, nested);
All alt1 = new All();
alt1.addAssertion(a1);
alt1.addAssertion(b);
All alt2 = new All();
alt1.addAssertion(a2);
alt2.addAssertion(c);
ExactlyOne ea = new ExactlyOne();
ea.addPolicyComponent(alt1);
ea.addPolicyComponent(alt2);
p.addPolicyComponent(ea);
AssertionInfoMap aim = new AssertionInfoMap(p);
Collection<AssertionInfo> listA = aim.getAssertionInfo(new QName("http://x.y.z", "a"));
assertEquals("2 A assertions should've been added", 2, listA.size());
AssertionInfo[] ais = listA.toArray(new AssertionInfo[] {});
assertTrue("Two different A instances should be added", ais[0].getAssertion() == a1 && ais[1].getAssertion() == a2 || ais[0].getAssertion() == a2 && ais[1].getAssertion() == a1);
Collection<AssertionInfo> listB = aim.getAssertionInfo(new QName("http://x.y.z", "b"));
assertEquals("2 B assertions should've been added", 2, listB.size());
ais = listB.toArray(new AssertionInfo[] {});
assertTrue("Two different B instances should be added", ais[0].getAssertion() == nb && ais[1].getAssertion() == b || ais[0].getAssertion() == b && ais[1].getAssertion() == nb);
Collection<AssertionInfo> listC = aim.getAssertionInfo(new QName("http://x.y.z", "c"));
assertEquals("1 C assertion should've been added", 1, listC.size());
ais = listC.toArray(new AssertionInfo[] {});
assertSame("One C instances should be added", ais[0].getAssertion(), c);
}
use of org.apache.neethi.Assertion in project cxf by apache.
the class MinimalMaximalAlternativeSelectorTest method testChooseMaxAlternative.
@Test
public void testChooseMaxAlternative() {
Message m = new MessageImpl();
AlternativeSelector selector = new MaximalAlternativeSelector();
PolicyEngine engine = control.createMock(PolicyEngine.class);
Assertor assertor = control.createMock(Assertor.class);
Policy policy = new Policy();
ExactlyOne ea = new ExactlyOne();
All all = new All();
PolicyAssertion a1 = new TestAssertion();
all.addAssertion(a1);
ea.addPolicyComponent(all);
Collection<PolicyAssertion> maxAlternative = CastUtils.cast(all.getPolicyComponents(), PolicyAssertion.class);
all = new All();
ea.addPolicyComponent(all);
Collection<PolicyAssertion> minAlternative = CastUtils.cast(all.getPolicyComponents(), PolicyAssertion.class);
policy.addPolicyComponent(ea);
EasyMock.expect(engine.supportsAlternative(maxAlternative, assertor, m)).andReturn(true);
EasyMock.expect(engine.supportsAlternative(minAlternative, assertor, m)).andReturn(true);
control.replay();
Collection<Assertion> choice = selector.selectAlternative(policy, engine, assertor, null, m);
assertEquals(1, choice.size());
assertSame(a1, choice.iterator().next());
control.verify();
}
use of org.apache.neethi.Assertion in project cxf by apache.
the class AssertionInfoMapTest method testCheck.
@Test
public void testCheck() throws PolicyException {
QName aqn = new QName("http://x.y.z", "a");
Assertion a = new PrimitiveAssertion(aqn);
Collection<Assertion> assertions = new ArrayList<>();
assertions.add(a);
AssertionInfoMap aim = new AssertionInfoMap(assertions);
try {
aim.check();
fail("Expected PolicyException not thrown.");
} catch (PolicyException ex) {
assertEquals("NOT_ASSERTED_EXC", ex.getCode());
}
aim.get(aqn).iterator().next().setAsserted(true);
aim.check();
}
use of org.apache.neethi.Assertion in project cxf by apache.
the class EndpointPolicyImplTest method testChooseAlternative.
@Test
public void testChooseAlternative() {
Policy policy = new Policy();
PolicyEngineImpl engine = control.createMock(PolicyEngineImpl.class);
Assertor assertor = control.createMock(Assertor.class);
AlternativeSelector selector = control.createMock(AlternativeSelector.class);
Message m = new MessageImpl();
EndpointPolicyImpl epi = new EndpointPolicyImpl(null, engine, true, assertor);
epi.setPolicy(policy);
EasyMock.expect(engine.isEnabled()).andReturn(true).anyTimes();
EasyMock.expect(engine.getAlternativeSelector()).andReturn(selector);
EasyMock.expect(selector.selectAlternative(policy, engine, assertor, null, m)).andReturn(null);
control.replay();
try {
epi.chooseAlternative(m);
fail("Expected PolicyException not thrown.");
} catch (PolicyException ex) {
// expected
}
control.verify();
control.reset();
EasyMock.expect(engine.isEnabled()).andReturn(true).anyTimes();
EasyMock.expect(engine.getAlternativeSelector()).andReturn(selector);
Collection<Assertion> alternative = new ArrayList<>();
EasyMock.expect(selector.selectAlternative(policy, engine, assertor, null, m)).andReturn(alternative);
control.replay();
epi.chooseAlternative(m);
Collection<Assertion> choice = epi.getChosenAlternative();
assertSame(choice, alternative);
control.verify();
control.reset();
EasyMock.expect(engine.isEnabled()).andReturn(false).anyTimes();
EasyMock.expect(engine.getAlternativeSelector()).andReturn(null).anyTimes();
control.replay();
try {
epi.chooseAlternative(m);
} catch (Exception ex) {
// no NPE expected
fail("No Exception expected: " + ex);
}
choice = epi.getChosenAlternative();
assertTrue("not an empty list", choice != null && choice.isEmpty());
control.verify();
}
use of org.apache.neethi.Assertion in project cxf by apache.
the class PolicyEngineTest method testGetAssertions.
@Test
public void testGetAssertions() throws NoSuchMethodException {
Method m = PolicyEngineImpl.class.getDeclaredMethod("addAssertions", new Class[] { PolicyComponent.class, boolean.class, Collection.class });
engine = EasyMock.createMockBuilder(PolicyEngineImpl.class).addMockedMethod(m).createMock(control);
PolicyAssertion a = control.createMock(PolicyAssertion.class);
EasyMock.expect(a.getType()).andReturn(Constants.TYPE_ASSERTION);
EasyMock.expect(a.isOptional()).andReturn(true);
control.replay();
assertTrue(engine.getAssertions(a, false).isEmpty());
control.verify();
control.reset();
EasyMock.expect(a.getType()).andReturn(Constants.TYPE_ASSERTION);
// EasyMock.expect(a.isOptional()).andReturn(false);
control.replay();
Collection<Assertion> ca = engine.getAssertions(a, true);
assertEquals(1, ca.size());
assertSame(a, ca.iterator().next());
control.verify();
control.reset();
Policy p = control.createMock(Policy.class);
EasyMock.expect(p.getType()).andReturn(Constants.TYPE_POLICY);
engine.addAssertions(EasyMock.eq(p), EasyMock.eq(false), CastUtils.cast(EasyMock.isA(Collection.class), Assertion.class));
EasyMock.expectLastCall();
control.replay();
assertTrue(engine.getAssertions(p, false).isEmpty());
control.verify();
}
Aggregations