Search in sources :

Example 36 with Policy

use of org.apache.neethi.Policy 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 37 with Policy

use of org.apache.neethi.Policy in project cxf by apache.

the class EffectivePolicyImplTest method testInitialiseFromEndpointPolicy.

@Test
public void testInitialiseFromEndpointPolicy() throws NoSuchMethodException {
    Method m = EffectivePolicyImpl.class.getDeclaredMethod("initialiseInterceptors", new Class[] { PolicyEngine.class, Message.class });
    EffectivePolicyImpl effectivePolicy = EasyMock.createMockBuilder(EffectivePolicyImpl.class).addMockedMethod(m).createMock(control);
    EndpointPolicyImpl endpointPolicy = control.createMock(EndpointPolicyImpl.class);
    Policy p = control.createMock(Policy.class);
    EasyMock.expect(endpointPolicy.getPolicy()).andReturn(p);
    Collection<Assertion> chosenAlternative = new ArrayList<>();
    EasyMock.expect(endpointPolicy.getChosenAlternative()).andReturn(chosenAlternative);
    PolicyEngineImpl pe = new PolicyEngineImpl();
    effectivePolicy.initialiseInterceptors(pe, false, msg);
    EasyMock.expectLastCall();
    control.replay();
    effectivePolicy.initialise(endpointPolicy, pe, false, null);
    control.verify();
}
Also used : Policy(org.apache.neethi.Policy) Assertion(org.apache.neethi.Assertion) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 38 with Policy

use of org.apache.neethi.Policy in project cxf by apache.

the class EndpointPolicyImplTest method testInitializePolicy.

@Test
public void testInitializePolicy() {
    EndpointInfo ei = control.createMock(EndpointInfo.class);
    PolicyEngineImpl engine = control.createMock(PolicyEngineImpl.class);
    ServiceInfo si = control.createMock(ServiceInfo.class);
    EasyMock.expect(ei.getService()).andReturn(si);
    Policy sp = control.createMock(Policy.class);
    EasyMock.expect(engine.getAggregatedServicePolicy(si, null)).andReturn(sp);
    Policy ep = control.createMock(Policy.class);
    EasyMock.expect(engine.getAggregatedEndpointPolicy(ei, null)).andReturn(ep);
    Policy merged = control.createMock(Policy.class);
    EasyMock.expect(sp.merge(ep)).andReturn(merged);
    EasyMock.expect(merged.normalize(null, true)).andReturn(merged);
    control.replay();
    EndpointPolicyImpl epi = new EndpointPolicyImpl(ei, engine, true, null);
    epi.initializePolicy(null);
    assertSame(merged, epi.getPolicy());
    control.verify();
}
Also used : ServiceInfo(org.apache.cxf.service.model.ServiceInfo) Policy(org.apache.neethi.Policy) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Test(org.junit.Test)

Example 39 with Policy

use of org.apache.neethi.Policy 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 40 with Policy

use of org.apache.neethi.Policy in project cxf by apache.

the class NormalizeTest method doTestNormalize.

private void doTestNormalize(PolicyBuilderImpl builder, String sample, String normalized) throws Exception {
    InputStream sampleIn = NormalizeTest.class.getResourceAsStream(sample);
    assertNotNull("Could not get input stream for resource " + sample, sampleIn);
    InputStream normalisedIn = NormalizeTest.class.getResourceAsStream(normalized);
    assertNotNull("Could not get input stream for resource " + normalized, normalisedIn);
    Policy samplePolicy = builder.getPolicy(sampleIn);
    Policy normalisedPolicy = builder.getPolicy(normalisedIn);
    assertNotNull(samplePolicy);
    assertNotNull(normalisedPolicy);
    Policy normalisedSamplePolicy = samplePolicy.normalize(true);
    assertTrue(PolicyComparator.compare(normalisedPolicy, normalisedSamplePolicy));
}
Also used : Policy(org.apache.neethi.Policy) InputStream(java.io.InputStream)

Aggregations

Policy (org.apache.neethi.Policy)122 Test (org.junit.Test)47 Assertion (org.apache.neethi.Assertion)27 QName (javax.xml.namespace.QName)23 ArrayList (java.util.ArrayList)21 All (org.apache.neethi.All)18 ExactlyOne (org.apache.neethi.ExactlyOne)18 Message (org.apache.cxf.message.Message)15 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)14 Element (org.w3c.dom.Element)13 Bus (org.apache.cxf.Bus)12 PrimitiveAssertion (org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion)12 List (java.util.List)9 Interceptor (org.apache.cxf.interceptor.Interceptor)9 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)9 HashMap (java.util.HashMap)7 OMElement (org.apache.axiom.om.OMElement)7 MessageImpl (org.apache.cxf.message.MessageImpl)7 ReferenceResolver (org.apache.cxf.ws.policy.attachment.reference.ReferenceResolver)6 InputStream (java.io.InputStream)5