1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.workgroup;
17
18 import static org.junit.Assert.assertNotNull;
19 import static org.junit.Assert.assertTrue;
20
21 import java.io.InputStream;
22 import java.io.StringBufferInputStream;
23 import java.io.StringWriter;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import javax.xml.bind.JAXBContext;
29 import javax.xml.bind.Marshaller;
30
31 import org.apache.log4j.Logger;
32 import org.junit.Test;
33 import org.kuali.rice.core.xml.dto.DataXmlDto;
34 import org.kuali.rice.kew.xml.GroupXmlJAXBParser;
35 import org.kuali.rice.kim.bo.types.dto.AttributeSet;
36 import org.kuali.rice.kim.xml.GroupMembershipXmlDto;
37 import org.kuali.rice.kim.xml.GroupXmlDto;
38
39
40
41
42
43
44
45 public class GroupXmlImportJAXBTest {
46 private static final Logger LOG = Logger.getLogger(GroupXmlImportJAXBTest.class);
47 private static final String INDENT = " ";
48
49
50
51
52
53
54
55
56 @Test public void testGroupImportXml() throws Exception {
57
58
59 InputStream xmlFile = getClass().getResourceAsStream("GroupXmlImportJAXBTest.xml");
60
61 GroupXmlJAXBParser parser = new GroupXmlJAXBParser();
62 DataXmlDto groupsInfo = null;
63 try{
64 groupsInfo = parser.parse(xmlFile);
65 }
66 catch(Exception ex){
67 ex.printStackTrace();
68 throw ex;
69 }
70
71 GroupXmlDto groupInfo = groupsInfo.getGroups().get(0);
72
73 assertNotNull(groupInfo);
74 assertTrue(groupInfo.getGroupName().equals("MyGroup"));
75 assertTrue(groupInfo.getGroupDescription().equals("Group Import unit test"));
76 assertTrue(groupInfo.getMembers().size()==3);
77 assertTrue(groupInfo.getAttributes().size()==4);
78
79 assertTrue(groupInfo.getAttributes().containsKey("documentTypeName"));
80 assertTrue(groupInfo.getAttributes().containsValue("Doc"));
81
82 Iterator iter = groupInfo.getMembers().iterator();
83 while (iter.hasNext()){
84 GroupMembershipXmlDto member = (GroupMembershipXmlDto) iter.next();
85 String memberName = member.getMemberId();
86 assertTrue (memberName.equals("ewestfal") ||
87 memberName.equals("rkirkend") ||
88 memberName.equals("TestWorkgroup"));
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102 }
103
104 @Test public void testOriginalGroupImportXml() throws Exception {
105 InputStream xmlFile = getClass().getResourceAsStream("GroupXmlImportTest.xml");
106
107 GroupXmlJAXBParser parser = new GroupXmlJAXBParser();
108 DataXmlDto groupsInfo = parser.parse(xmlFile);
109
110 GroupXmlDto groupInfo = groupsInfo.getGroups().get(0);
111 assertNotNull(groupInfo);
112 assertTrue(groupInfo.getGroupName().equals("TestUserGroup"));
113 assertTrue(groupInfo.getGroupDescription().equals("Group for test user"));
114
115 for (GroupMembershipXmlDto groupMember : groupInfo.getMembers()){
116 System.out.println(groupMember.getMemberId());
117 }
118 }
119
120
121 @Test public void testGroupExportXml() throws Exception {
122 JAXBContext context = JAXBContext.newInstance(DataXmlDto.class);
123 Marshaller marshaller = context.createMarshaller();
124 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
125
126 String GROUP_NAME = "MyGroup";
127 String GROUP_DESC = "Group Import unit test";
128
129 GroupXmlDto grp = new GroupXmlDto(
130 GROUP_NAME, GROUP_DESC, true);
131 List<GroupMembershipXmlDto> mbrs = new ArrayList<GroupMembershipXmlDto>();
132
133 GroupMembershipXmlDto mbr1 = new GroupMembershipXmlDto();
134 mbr1.setMemberId("ewestfal");
135 mbr1.setMemberTypeCode("P");
136 mbrs.add(mbr1);
137
138 GroupMembershipXmlDto mbr2 = new GroupMembershipXmlDto();
139 mbr2.setMemberId("rkirkend");
140 mbr2.setMemberTypeCode("P");
141 mbrs.add(mbr2);
142
143 GroupMembershipXmlDto mbr3 = new GroupMembershipXmlDto();
144 mbr3.setMemberId("TestWorkgroup");
145 mbr3.setMemberTypeCode("G");
146 mbrs.add(mbr3);
147
148 grp.setMembers(mbrs);
149
150
151 AttributeSet attrs = new AttributeSet();
152
153 attrs.put("documentTypeName","Doc");
154 attrs.put("routeNodeName","P");
155 attrs.put("required","false");
156 attrs.put("actionDetailsAtRoleMemberLevel","false");
157 grp.setAttributes(attrs);
158
159 DataXmlDto dataDto = new DataXmlDto();
160 List<GroupXmlDto> lGroup = new ArrayList<GroupXmlDto>();
161 lGroup.add(grp);
162 dataDto.setGroups(lGroup);
163 StringWriter fw = new StringWriter();
164 marshaller.marshal(dataDto, fw);
165
166 String output = fw.toString();
167 fw.close();
168
169 InputStream xmlFile = new StringBufferInputStream(output);
170
171 GroupXmlJAXBParser parser = new GroupXmlJAXBParser();
172 DataXmlDto groupsInfo = null;
173 try{
174 groupsInfo = parser.parse(xmlFile);
175 }
176 catch(Exception ex){
177 ex.printStackTrace();
178 throw ex;
179 }
180
181 GroupXmlDto groupInfo = groupsInfo.getGroups().get(0);
182
183 assertTrue(GROUP_NAME.equals(groupInfo.getGroupName()));
184 assertTrue(GROUP_DESC.equals(groupInfo.getGroupDescription()));
185 assertTrue(groupInfo.getMembers().size() == 3);
186
187
188 }
189 }