Search in sources :

Example 31 with Assertion

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);
}
Also used : Policy(org.apache.neethi.Policy) All(org.apache.neethi.All) PolicyContainingPrimitiveAssertion(org.apache.neethi.builders.PolicyContainingPrimitiveAssertion) PrimitiveAssertion(org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion) QName(javax.xml.namespace.QName) PolicyContainingPrimitiveAssertion(org.apache.neethi.builders.PolicyContainingPrimitiveAssertion) PolicyContainingPrimitiveAssertion(org.apache.neethi.builders.PolicyContainingPrimitiveAssertion) PrimitiveAssertion(org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion) Assertion(org.apache.neethi.Assertion) ExactlyOne(org.apache.neethi.ExactlyOne) Test(org.junit.Test)

Example 32 with Assertion

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();
}
Also used : Policy(org.apache.neethi.Policy) All(org.apache.neethi.All) PolicyAssertion(org.apache.cxf.ws.policy.PolicyAssertion) Message(org.apache.cxf.message.Message) Assertion(org.apache.neethi.Assertion) TestAssertion(org.apache.cxf.ws.policy.TestAssertion) PolicyAssertion(org.apache.cxf.ws.policy.PolicyAssertion) PolicyEngine(org.apache.cxf.ws.policy.PolicyEngine) ExactlyOne(org.apache.neethi.ExactlyOne) AlternativeSelector(org.apache.cxf.ws.policy.AlternativeSelector) Assertor(org.apache.cxf.ws.policy.Assertor) TestAssertion(org.apache.cxf.ws.policy.TestAssertion) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 33 with Assertion

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();
}
Also used : QName(javax.xml.namespace.QName) PolicyContainingPrimitiveAssertion(org.apache.neethi.builders.PolicyContainingPrimitiveAssertion) PrimitiveAssertion(org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion) PolicyContainingPrimitiveAssertion(org.apache.neethi.builders.PolicyContainingPrimitiveAssertion) PrimitiveAssertion(org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion) Assertion(org.apache.neethi.Assertion) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 34 with Assertion

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();
}
Also used : Policy(org.apache.neethi.Policy) Message(org.apache.cxf.message.Message) Assertion(org.apache.neethi.Assertion) ArrayList(java.util.ArrayList) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 35 with Assertion

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();
}
Also used : Policy(org.apache.neethi.Policy) PrimitiveAssertion(org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion) Assertion(org.apache.neethi.Assertion) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

Assertion (org.apache.neethi.Assertion)64 Policy (org.apache.neethi.Policy)27 Message (org.apache.cxf.message.Message)25 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)23 QName (javax.xml.namespace.QName)21 Interceptor (org.apache.cxf.interceptor.Interceptor)19 PrimitiveAssertion (org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion)19 PolicyContainingAssertion (org.apache.neethi.PolicyContainingAssertion)9 Element (org.w3c.dom.Element)9 MessageImpl (org.apache.cxf.message.MessageImpl)7 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)7 All (org.apache.neethi.All)7 ExactlyOne (org.apache.neethi.ExactlyOne)7 Bus (org.apache.cxf.Bus)6 InterceptorChain (org.apache.cxf.interceptor.InterceptorChain)6 PolicyEngine (org.apache.cxf.ws.policy.PolicyEngine)6 PolicyContainingPrimitiveAssertion (org.apache.neethi.builders.PolicyContainingPrimitiveAssertion)6 XMLPrimitiveAssertionBuilder (org.apache.neethi.builders.xml.XMLPrimitiveAssertionBuilder)6 List (java.util.List)5