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.KeyValueToken;
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.assertTrue;
32  
33  public class KeyValueTokenTest extends AbstractTestBase {
34  
35      @Test
36      public void testKeyValueToken() throws Exception {
37          String fileName = "KeyValueToken.xml";
38          String policyFile = loadPolicyFile("policy/model/sp12/" + fileName);
39          String serializedPolicyReferenceFile = loadPolicyFile("policy/model/sp12/serialized/" + fileName);
40          String normalizedPolicyReferenceFile = loadPolicyFile("policy/model/sp12/normalized/" + fileName);
41          Policy policy = loadPolicy(policyFile);
42          String serializedPolicy = serializePolicy(policy);
43          assertXMLisEqual(serializedPolicy, serializedPolicyReferenceFile);
44  
45          Iterator<List<Assertion>> alternativeIterator = policy.getAlternatives();
46          int count = 0;
47          while (alternativeIterator.hasNext()) {
48              List<Assertion> alternative = alternativeIterator.next();
49              assertEquals(1, alternative.size());
50              assertTrue(alternative.get(0) instanceof KeyValueToken);
51              KeyValueToken keyValueToken = (KeyValueToken) alternative.get(0);
52              assertFalse(keyValueToken.isNormalized());
53              assertTrue(keyValueToken.isIgnorable());
54              assertTrue(keyValueToken.isOptional());
55              assertEquals(Constants.TYPE_ASSERTION, keyValueToken.getType());
56              assertEquals(SP12Constants.KEY_VALUE_TOKEN, keyValueToken.getName());
57              assertTrue(keyValueToken.isRsaKeyValue());
58              count++;
59          }
60          assertEquals(1, count);
61  
62          policy = policy.normalize(true);
63          serializedPolicy = serializePolicy(policy);
64          assertXMLisEqual(serializedPolicy, normalizedPolicyReferenceFile);
65  
66          alternativeIterator = policy.getAlternatives();
67          List<Assertion> alternative = alternativeIterator.next();
68          assertEquals(0, alternative.size());
69  
70          List<PolicyComponent> policyComponents = policy.getPolicyComponents();
71          assertEquals(1, policyComponents.size());
72          PolicyOperator policyOperator = (PolicyOperator) policyComponents.get(0);
73          policyComponents = policyOperator.getPolicyComponents();
74          assertEquals(2, policyComponents.size());
75          All all = (All) policyComponents.get(0);
76          List<PolicyComponent> policyComponentsAll = all.getAssertions();
77          assertEquals(0, policyComponentsAll.size());
78  
79          all = (All) policyComponents.get(1);
80          policyComponentsAll = all.getAssertions();
81          assertEquals(1, policyComponentsAll.size());
82  
83          Iterator<PolicyComponent> policyComponentIterator = policyComponentsAll.iterator();
84          KeyValueToken keyValueToken = (KeyValueToken) policyComponentIterator.next();
85          assertTrue(keyValueToken.isNormalized());
86          assertTrue(keyValueToken.isIgnorable());
87          assertFalse(keyValueToken.isOptional());
88          assertEquals(Constants.TYPE_ASSERTION, keyValueToken.getType());
89          assertEquals(SP12Constants.KEY_VALUE_TOKEN, keyValueToken.getName());
90          assertTrue(keyValueToken.isRsaKeyValue());
91      }
92  }