View Javadoc
1   /**
2    * Copyright 2004-2015 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.kpme.core.principal;
17  
18  
19  
20  
21  import org.joda.time.DateTime;
22  import org.joda.time.LocalDate;
23  import org.junit.Assert;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  
28  import org.kuali.kpme.core.UnitTest;
29  import org.kuali.kpme.core.api.calendar.Calendar;
30  import org.kuali.kpme.core.api.leaveplan.LeavePlan;
31  import org.kuali.kpme.core.api.principal.PrincipalHRAttributes;
32  import org.kuali.kpme.core.calendar.CalendarBo;
33  import org.kuali.kpme.core.calendar.CalendarBoTest;
34  import org.kuali.kpme.core.leaveplan.LeavePlanBo;
35  import org.kuali.kpme.core.leaveplan.LeavePlanBoTest;
36  import org.kuali.rice.kim.api.identity.IdentityService;
37  import org.kuali.rice.kim.api.identity.name.EntityName;
38  import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
39  
40  import javax.xml.bind.JAXBContext;
41  import javax.xml.bind.JAXBException;
42  import javax.xml.bind.Marshaller;
43  import javax.xml.bind.Unmarshaller;
44  import java.io.StringReader;
45  import java.io.StringWriter;
46  import java.util.HashMap;
47  import java.util.Map;
48  
49  import static org.mockito.Matchers.anyString;
50  import static org.mockito.Mockito.mock;
51  import static org.mockito.Mockito.when;
52  
53  @UnitTest
54  public class PrincipalHRAttributesBoTest {
55      private static IdentityService mockIdentityService;
56  
57      private static Map<String, PrincipalHRAttributes> testPrincipalHRAttributeBos;
58  
59      public static PrincipalHRAttributes.Builder principalHrAttributesBuilder = PrincipalHRAttributes.Builder.create("testuser1");
60  
61      static {
62          testPrincipalHRAttributeBos = new HashMap<String, PrincipalHRAttributes>();
63          principalHrAttributesBuilder.setName("user, test1");
64          principalHrAttributesBuilder.setUserPrincipalId("admin");
65          principalHrAttributesBuilder.setLeavePlan("LEAVE");
66          principalHrAttributesBuilder.setServiceLocalDate(new LocalDate(2010, 1, 1));
67          principalHrAttributesBuilder.setFmlaEligible(true);
68          principalHrAttributesBuilder.setWorkersCompEligible(true);
69          principalHrAttributesBuilder.setTimezone("America/Indiana/Indianapolis");
70          principalHrAttributesBuilder.setPayCalendar("BWS-CAL");
71          principalHrAttributesBuilder.setLeaveCalendar("LM");
72  
73          principalHrAttributesBuilder.setHrPrincipalAttributeId("KPME_TEST_0001");
74          principalHrAttributesBuilder.setVersionNumber(1L);
75          principalHrAttributesBuilder.setObjectId("0804716a-cbb7-11e3-9cd3-51a754ad6a0a");
76          principalHrAttributesBuilder.setActive(true);
77          principalHrAttributesBuilder.setId(principalHrAttributesBuilder.getHrPrincipalAttributeId());
78          principalHrAttributesBuilder.setEffectiveLocalDate(new LocalDate(2012, 3, 1));
79          principalHrAttributesBuilder.setCreateTime(DateTime.now());
80  
81          principalHrAttributesBuilder.setLeaveCalObj(Calendar.Builder.create(CalendarBoTest.getTestCalendar("LM")));
82          principalHrAttributesBuilder.setCalendar(Calendar.Builder.create(CalendarBoTest.getTestCalendar("BWS-CAL")));
83          principalHrAttributesBuilder.setLeavePlanObj(LeavePlan.Builder.create(LeavePlanBoTest.getLeavePlan("LEAVE")));
84  
85          testPrincipalHRAttributeBos.put(principalHrAttributesBuilder.getPrincipalId(), principalHrAttributesBuilder.build());
86      }
87  
88      @Before
89      public void setup() throws Exception {
90          EntityName.Builder entityName = EntityName.Builder.create("testuser1", "entityId", "test1", "user", false);
91          EntityNamePrincipalName.Builder enpn = EntityNamePrincipalName.Builder.create();
92          enpn.setPrincipalName("testUser1");
93          enpn.setDefaultName(entityName);
94          mockIdentityService = mock(IdentityService.class);
95          {
96              when( mockIdentityService.getDefaultNamesForPrincipalId(anyString())).thenReturn(enpn.build());
97          }
98      }
99  
100     @Test
101     public void testPrincipalHRConversions() {
102         PrincipalHRAttributes immutable = PrincipalHRAttributesBoTest.getPrincipalHRAttributes("testuser1");
103         PrincipalHRAttributesBo bo = PrincipalHRAttributesBo.from(immutable);
104         //mockIdentityService
105         bo.setIdentityService(mockIdentityService);
106         Assert.assertFalse(bo.equals(immutable));
107         Assert.assertFalse(immutable.equals(bo));
108         Assert.assertEquals(immutable, PrincipalHRAttributesBo.to(bo));
109     }
110 
111     @Test
112     public void testNotEqualsWithCalendar()
113     {
114         PrincipalHRAttributes immutable1 = PrincipalHRAttributesBoTest.getPrincipalHRAttributes("testuser1");
115         PrincipalHRAttributesBo bo1 = PrincipalHRAttributesBo.from(immutable1);
116 
117         PrincipalHRAttributes immutable2 = PrincipalHRAttributesBoTest.getPrincipalHRAttributes("testuser1");
118         PrincipalHRAttributesBo bo2 = PrincipalHRAttributesBo.from(immutable2);
119 
120         bo1.setCalendar(CalendarBo.from(CalendarBoTest.getTestCalendar("LM")));
121         bo2.setCalendar(CalendarBo.from(CalendarBoTest.getTestCalendar("BWS-CAL")));
122 
123         Assert.assertNotEquals(bo1.getCalendar(), bo2.getCalendar());
124     }
125 
126     @Test
127     public void testServiceLocalDateNotEquals()
128     {
129         PrincipalHRAttributes immutable1 = PrincipalHRAttributesBoTest.getPrincipalHRAttributes("testuser1");
130         PrincipalHRAttributesBo bo1 = PrincipalHRAttributesBo.from(immutable1);
131 
132         PrincipalHRAttributes immutable2 = PrincipalHRAttributesBoTest.getPrincipalHRAttributes("testuser1");
133         PrincipalHRAttributesBo bo2 = PrincipalHRAttributesBo.from(immutable2);
134 
135         bo1.setServiceLocalDate(new LocalDate(2014,6,4));
136         bo2.setServiceLocalDate(new LocalDate(2014,6,4));
137 
138         Assert.assertEquals(bo1.getServiceLocalDate(), bo2.getServiceLocalDate());
139     }
140 
141     @Test
142     public void testLeavePlanNotEquals()
143     {
144         PrincipalHRAttributes immutable1 = PrincipalHRAttributesBoTest.getPrincipalHRAttributes("testuser1");
145         PrincipalHRAttributesBo bo1 = PrincipalHRAttributesBo.from(immutable1);
146 
147         PrincipalHRAttributes immutable2 = PrincipalHRAttributesBoTest.getPrincipalHRAttributes("testuser1");
148         PrincipalHRAttributesBo bo2 = PrincipalHRAttributesBo.from(immutable2);
149 
150         LeavePlanBo lpbo = LeavePlanBo.from(LeavePlan.Builder.create(LeavePlanBoTest.getLeavePlan("LEAVE")).build());
151         bo1.setLeavePlanObj(lpbo);
152         bo2.setLeavePlanObj(lpbo);
153 
154         Assert.assertEquals(bo1.getLeavePlanObj(), bo2.getLeavePlanObj());
155     }
156 
157     @Test
158     public void testXmlMarshaling() {
159         JAXBContext jc = null;
160         try {
161             jc = JAXBContext.newInstance(PrincipalHRAttributes.class);
162 
163             Marshaller marshaller = jc.createMarshaller();
164             StringWriter sw = new StringWriter();
165 
166             PrincipalHRAttributes principalHrAttributes = getPrincipalHRAttributes("testuser1");
167             marshaller.marshal(principalHrAttributes, sw);
168             String xml = sw.toString();
169             System.out.println(xml);
170             Assert.assertTrue(true);
171         } catch (JAXBException e) {
172             e.printStackTrace();
173         }
174     }
175 
176 
177     public static PrincipalHRAttributes getPrincipalHRAttributes(String principalId) {
178         return testPrincipalHRAttributeBos.get(principalId);
179     }
180 }