View Javadoc

1   /*
2    * Copyright 2007-2008 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.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   * This is a description of what this class does - jjhanso don't forget to fill this in.
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
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       * Verify that a workgroup with a bad user in the xml is not going to be put in the db.
52       *
53       * @throws Exception
54       */
55  
56      @Test public void testGroupImportXml() throws Exception {
57  //    	loadXmlFile("GroupXmlImportTest.xml");
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  //        IdentityManagementService identityManagementService = KIMServiceLocator.getIdentityManagementService();
92  //        //verify that the group was ingested
93  //        Group group = identityManagementService.getGroupByName(KimConstants.KIM_GROUP_WORKFLOW_NAMESPACE_CODE, "TestUserGroup");
94  //
95  //        assertNotNull(group);
96  //        List<String> members = identityManagementService.getGroupMemberPrincipalIds(group.getGroupId());
97  //        List<String> groups = identityManagementService.getMemberGroupIds(group.getGroupId());
98  //        assertTrue(identityManagementService.isMemberOfGroup(identityManagementService.getPrincipalByPrincipalName("ewestfal").getPrincipalId(), group.getGroupId()));
99  //        assertTrue(identityManagementService.isMemberOfGroup(identityManagementService.getPrincipalByPrincipalName("rkirkend").getPrincipalId(), group.getGroupId()));
100 //        assertTrue(identityManagementService.isMemberOfGroup("2015", group.getGroupId()));
101 //        assertTrue(KIMServiceLocator.getGroupService().isGroupMemberOfGroup(identityManagementService.getGroupByName(KimConstants.KIM_GROUP_WORKFLOW_NAMESPACE_CODE, "TestWorkgroup").getGroupId(), group.getGroupId()));
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       //  HashMap<String, String> attrs = new HashMap<String,String>();
151         AttributeSet attrs = new AttributeSet();
152 //        HashMap<String, String> attrs = new HashMap<String, String>();
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 }