View Javadoc
1   /*
2    * Copyright 2006-2014 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.kim.test.service;
17  
18  import org.junit.Test;
19  import org.kuali.rice.core.api.config.property.ConfigContext;
20  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21  import org.kuali.rice.kim.api.identity.IdentityService;
22  import org.kuali.rice.kim.api.identity.entity.Entity;
23  import org.kuali.rice.kim.api.identity.entity.EntityDefault;
24  import org.kuali.rice.kim.api.identity.principal.Principal;
25  import org.kuali.rice.kim.impl.identity.EntityDefaultInfoCacheBo;
26  import org.kuali.rice.kim.impl.identity.IdentityArchiveService;
27  import org.kuali.rice.kim.test.KIMTestCase;
28  import org.kuali.rice.krad.data.KradDataServiceLocator;
29  import org.kuali.rice.test.BaselineTestCase;
30  
31  import java.util.ArrayList;
32  import java.util.Collections;
33  import java.util.List;
34  import java.util.UUID;
35  
36  import static org.junit.Assert.*;
37  
38  /**
39   * Unit test for the IdentityCurrentAndArchivedService
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
44  public class IdentityCurrentAndArchivedServiceTest extends KIMTestCase {
45  
46      public static final String KIM_IDENTITY_SERVICE = "kimIdentityService";
47      public static final String KIM_IDENTITY_ARCHIVE_SERVICE = "kimIdentityArchiveService";
48  
49  
50      private IdentityService identityService;
51      private IdentityArchiveService identityArchiveService;
52  
53      public static IdentityArchiveService getIdentityArchiveService() {
54          return GlobalResourceLoader.getService(KIM_IDENTITY_ARCHIVE_SERVICE);
55      }
56      public static IdentityService getIdentityService() {
57          return GlobalResourceLoader.getService(KIM_IDENTITY_SERVICE);
58      }
59  
60      public void setUp() throws Exception {
61          super.setUp();
62          if (null == identityService) {
63              identityService = getIdentityService();
64          }
65  
66          if (null == identityArchiveService) {
67              identityArchiveService = getIdentityArchiveService();
68          }
69      }
70  
71      @Test
72      public void testGetEntityDefault() throws Exception {
73  
74          MinimalKEDIBuilder builder = new MinimalKEDIBuilder("bogususer");
75          builder.setEntityId("bogususer");
76          EntityDefault bogusUserInfo = builder.build();
77  
78          //Create a record in KRIM_ENTITY_CACHE_T
79          identityArchiveService.saveEntityDefaultToArchive(bogusUserInfo);
80          identityArchiveService.flushToArchive();
81  
82          // retrieve using the archiveService to make sure its in the KRIM_ENTITY_CACHE_T
83          EntityDefault retrieved = identityArchiveService.getEntityDefaultFromArchiveByPrincipalId(
84                  builder.getPrincipalId());
85          assertNotNull("no value retrieved for principalId: " + retrieved.getPrincipals().get(0).getPrincipalId(), retrieved);
86  
87          // retrieve it using  the identity current and archived Service  should not use cache
88          retrieved = identityService.getEntityDefaultByPrincipalId("bogususer");
89          assertNotNull("no value retrieved for principalId: " + retrieved.getPrincipals().get(0).getPrincipalId(), retrieved);
90  
91          // retrieve again - should use cache
92          EntityDefault retrieveAgain = identityService.getEntityDefaultByPrincipalId("bogususer");
93          assertNotNull("no value retrieved for principalId: " + retrieveAgain.getPrincipals().get(0).getPrincipalId(), retrieveAgain);
94  
95  
96      }
97  
98  
99      @SuppressWarnings("unused")
100     private static class MinimalKEDIBuilder {
101         private String entityId;
102         private String principalId;
103         private String principalName;
104         private Boolean active;
105 
106         public MinimalKEDIBuilder(String name) {
107             entityId = UUID.randomUUID().toString();
108             principalId = principalName = name;
109         }
110 
111         public EntityDefault build() {
112             if (entityId == null) entityId = UUID.randomUUID().toString();
113             if (principalId == null) principalId = UUID.randomUUID().toString();
114             if (principalName == null) principalName = principalId;
115             if (active == null) active = true;
116 
117             Principal.Builder principal = Principal.Builder.create(principalName);
118             principal.setActive(active);
119             principal.setEntityId(entityId);
120             principal.setPrincipalId(principalId);
121 
122             EntityDefault.Builder kedi = EntityDefault.Builder.create();
123             kedi.setPrincipals(Collections.singletonList(principal));
124             kedi.setEntityId(entityId);
125 
126             return kedi.build();
127         }
128 
129         /**
130          * @return the entityId
131          */
132         public String getEntityId() {
133             return this.entityId;
134         }
135 
136         /**
137          * @param entityId the entityId to set
138          */
139         public void setEntityId(String entityId) {
140             this.entityId = entityId;
141         }
142 
143         /**
144          * @return the principalId
145          */
146         public String getPrincipalId() {
147             return this.principalId;
148         }
149 
150         /**
151          * @param principalId the principalId to set
152          */
153         public void setPrincipalId(String principalId) {
154             this.principalId = principalId;
155         }
156 
157         /**
158          * @return the principalName
159          */
160         public String getPrincipalName() {
161             return this.principalName;
162         }
163 
164         /**
165          * @param principalName the principalName to set
166          */
167         public void setPrincipalName(String principalName) {
168             this.principalName = principalName;
169         }
170 
171         /**
172          * @return the active
173          */
174         public Boolean getActive() {
175             return this.active;
176         }
177 
178         /**
179          * @param active the active to set
180          */
181         public void setActive(Boolean active) {
182             this.active = active;
183         }
184 
185 
186     }
187 
188 }