View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.rule;
17  
18  import org.junit.Test;
19  import org.kuali.rice.kew.routeheader.DocumentContent;
20  import org.kuali.rice.kew.routeheader.StandardDocumentContent;
21  
22  import java.util.Arrays;
23  import java.util.List;
24  import java.util.Map;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertTrue;
28  
29  public class GenericAttributeContentTest {
30      private static final String ATTRIB1_CONTENT = "    <boringAttribute>" +
31              "      <field>" +
32              "        <name>color</name>" +
33              "        <value>green</value>" +
34              "      </field>" +
35              "      <field>" +
36              "        <name>shape</name>" +
37              "        <value>circle</value>" +
38              "      </field>" +
39              "    </boringAttribute>";
40      private static final String ATTRIB2_CONTENT = "    <coolAttribute>" +
41              "      <field>" +
42              "        <name>car</name>" +
43              "        <value>KIT</value>" +
44              "      </field>" +
45              "      <field>" +
46              "        <name>driver</name>" +
47              "        <value>hasselhof</value>" +
48              "      </field>" +
49              "    </coolAttribute>";
50      private static final String ATTRIB2_CONTENT_REVERSED = "    <coolAttribute>" +
51              "      <field>" +
52              "        <name>driver</name>" +
53              "        <value>hasselhof</value>" +
54              "      </field>" +
55              "      <field>" +
56              "        <name>car</name>" +
57              "        <value>KIT</value>" +
58              "      </field>" +
59              "    </coolAttribute>";
60      private static final String TEST_CONTENT = "<documentContent>" +
61              "  <attributeContent>" +
62              ATTRIB1_CONTENT +
63              ATTRIB2_CONTENT +
64              "  </attributeContent>" +
65              "</documentContent>";
66  
67      @Test public void testGenerateContent() throws Exception {
68          DocumentContent dc = new StandardDocumentContent(TEST_CONTENT);
69          GenericAttributeContent gac = new GenericAttributeContent("boringAttribute");
70          List<Map<String, String>> attrs = gac.parseContent(dc.getAttributeContent());
71          assertEquals(1, attrs.size());
72          Map<String, String> properties = attrs.get(0);
73          assertEquals(2, properties.size());
74          assertEquals("green", properties.get("color"));
75          assertEquals("circle", properties.get("shape"));
76          String content = gac.generateContent(properties);
77          assertEquals(content.replaceAll("\\s+", ""), ATTRIB1_CONTENT.replaceAll("\\s+", ""));
78  
79          gac = new GenericAttributeContent("coolAttribute");
80          attrs = gac.parseContent(dc.getAttributeContent());
81          assertEquals(1, attrs.size());
82          properties = attrs.get(0);
83          assertEquals(2, properties.size());
84          assertEquals("hasselhof", properties.get("driver"));
85          assertEquals("KIT", properties.get("car"));
86          content = gac.generateContent(properties);
87  
88          // order is not guaranteed
89          List<String> validValues = Arrays.asList(ATTRIB2_CONTENT.replaceAll("\\s+", ""), ATTRIB2_CONTENT_REVERSED.replaceAll(
90                  "\\s+", ""));
91          content = content.replaceAll("\\s+", "");
92          assertTrue(validValues.contains(content));
93      }
94  }