View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.wss4j.policy.tests;
20  
21  import org.apache.neethi.*;
22  import org.apache.wss4j.policy.SP12Constants;
23  import org.apache.wss4j.policy.model.SupportingTokens;
24  import org.junit.jupiter.api.Test;
25  
26  import java.util.Iterator;
27  import java.util.List;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  import static org.junit.jupiter.api.Assertions.assertFalse;
31  import static org.junit.jupiter.api.Assertions.assertNotNull;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  
34  public class SupportingTokensTest extends AbstractTestBase {
35  
36      @Test
37      public void testSupportingTokens() throws Exception {
38          String fileName = "SupportingTokens.xml";
39          String policyFile = loadPolicyFile("policy/model/sp12/" + fileName);
40          String serializedPolicyReferenceFile = loadPolicyFile("policy/model/sp12/serialized/" + fileName);
41          String normalizedPolicyReferenceFile = loadPolicyFile("policy/model/sp12/normalized/" + fileName);
42          Policy policy = loadPolicy(policyFile);
43          String serializedPolicy = serializePolicy(policy);
44          assertXMLisEqual(serializedPolicy, serializedPolicyReferenceFile);
45  
46          Iterator<List<Assertion>> alternativeIterator = policy.getAlternatives();
47          int count = 0;
48          while (alternativeIterator.hasNext()) {
49              List<Assertion> alternative = alternativeIterator.next();
50              assertEquals(1, alternative.size());
51              assertTrue(alternative.get(0) instanceof SupportingTokens);
52              SupportingTokens supportingTokens = (SupportingTokens) alternative.get(0);
53              assertFalse(supportingTokens.isNormalized());
54              assertTrue(supportingTokens.isIgnorable());
55              assertTrue(supportingTokens.isOptional());
56              assertEquals(Constants.TYPE_ASSERTION, supportingTokens.getType());
57              assertEquals(SP12Constants.SUPPORTING_TOKENS, supportingTokens.getName());
58              assertEquals(1, supportingTokens.getTokens().size());
59              assertNotNull(supportingTokens.getSignedParts());
60              assertNotNull(supportingTokens.getSignedElements());
61              assertNotNull(supportingTokens.getEncryptedParts());
62              assertNotNull(supportingTokens.getEncryptedElements());
63              assertNotNull(supportingTokens.getAlgorithmSuite());
64              count++;
65          }
66          assertEquals(1, count);
67  
68          policy = policy.normalize(true);
69          serializedPolicy = serializePolicy(policy);
70          assertXMLisEqual(serializedPolicy, normalizedPolicyReferenceFile);
71  
72          alternativeIterator = policy.getAlternatives();
73          List<Assertion> alternative = alternativeIterator.next();
74          assertEquals(0, alternative.size());
75  
76          List<PolicyComponent> policyComponents = policy.getPolicyComponents();
77          assertEquals(1, policyComponents.size());
78          PolicyOperator policyOperator = (PolicyOperator) policyComponents.get(0);
79          policyComponents = policyOperator.getPolicyComponents();
80          assertEquals(2, policyComponents.size());
81          All all = (All) policyComponents.get(0);
82          List<PolicyComponent> policyComponentsAll = all.getAssertions();
83          assertEquals(0, policyComponentsAll.size());
84  
85          all = (All) policyComponents.get(1);
86          policyComponentsAll = all.getAssertions();
87          assertEquals(1, policyComponentsAll.size());
88  
89          Iterator<PolicyComponent> policyComponentIterator = policyComponentsAll.iterator();
90          SupportingTokens supportingTokens = (SupportingTokens) policyComponentIterator.next();
91          assertTrue(supportingTokens.isNormalized());
92          assertTrue(supportingTokens.isIgnorable());
93          assertFalse(supportingTokens.isOptional());
94          assertEquals(Constants.TYPE_ASSERTION, supportingTokens.getType());
95          assertEquals(SP12Constants.SUPPORTING_TOKENS, supportingTokens.getName());
96          assertEquals(1, supportingTokens.getTokens().size());
97          assertNotNull(supportingTokens.getSignedParts());
98          assertNotNull(supportingTokens.getSignedElements());
99          assertNotNull(supportingTokens.getEncryptedParts());
100         assertNotNull(supportingTokens.getEncryptedElements());
101         assertNotNull(supportingTokens.getAlgorithmSuite());
102     }
103 }