1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.kew.api.KewApiServiceLocator;
22 import org.kuali.rice.kew.api.document.DocumentStatus;
23 import org.kuali.rice.kew.api.document.DocumentStatusCategory;
24 import org.kuali.rice.kim.impl.identity.IdentityArchiveService;
25 import org.kuali.rice.kim.api.identity.entity.EntityDefault;
26 import org.kuali.rice.kim.api.identity.principal.Principal;
27 import org.kuali.rice.kim.impl.identity.EntityDefaultInfoCacheBo;
28 import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
29 import org.kuali.rice.kim.service.impl.IdentityArchiveServiceImpl;
30 import org.kuali.rice.kim.test.KIMTestCase;
31 import org.kuali.rice.krad.service.KRADServiceLocator;
32 import org.kuali.rice.test.BaselineTestCase;
33
34 import java.lang.reflect.Field;
35 import java.lang.reflect.Method;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.UUID;
41
42 import static org.junit.Assert.*;
43
44
45
46
47
48
49 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
50 public class IdentityArchiveServiceTest extends KIMTestCase {
51 public static final String KIM_IDENTITY_ARCHIVE_SERVICE = "kimIdentityArchiveService";
52
53 private IdentityArchiveService identityArchiveService;
54
55 public static IdentityArchiveService getIdentityArchiveService() {
56 return GlobalResourceLoader.getService(KIM_IDENTITY_ARCHIVE_SERVICE);
57 }
58
59 public void setUp() throws Exception {
60 super.setUp();
61 final Map<String, Object> emptyMap = Collections.emptyMap();
62 KRADServiceLocator.getBusinessObjectService().deleteMatching(EntityDefaultInfoCacheBo.class, emptyMap);
63 if (null == identityArchiveService) {
64 identityArchiveService = getIdentityArchiveService();
65 }
66 }
67
68
69
70
71
72
73
74
75 @Test
76 public void testArchiveFlushesWhenQueueIsFull() throws Exception {
77 final int maxWriteQueueSize =
78 Integer.valueOf(ConfigContext.getCurrentContextConfig().getProperty("kim.identityArchiveServiceImpl.maxWriteQueueSize"));
79
80
81
82 identityArchiveService.flushToArchive();
83
84 log.info("Sleeping, waiting for the flush!");
85 for (int j=2; j >= 0; j--) {
86 Thread.sleep(1000);
87 log.info(String.valueOf(j));
88 }
89 log.info("Done sleeping!");
90
91 List<EntityDefault> added = new ArrayList<EntityDefault>();
92
93
94 for (int i=1; i<=(maxWriteQueueSize); i++) {
95 MinimalKEDIBuilder builder = new MinimalKEDIBuilder("bogusUser" + i);
96 builder.setEntityId("bogusUser" + i);
97 EntityDefault bogusUserInfo = builder.build();
98
99 EntityDefault retrieved = identityArchiveService.getEntityDefaultFromArchiveByPrincipalId(
100 builder.getPrincipalId());
101 assertNull(retrieved);
102 retrieved = identityArchiveService.getEntityDefaultFromArchiveByPrincipalName(builder.getPrincipalName());
103 assertNull(retrieved);
104 retrieved = identityArchiveService.getEntityDefaultFromArchive(builder.getEntityId());
105 assertNull(retrieved);
106
107 identityArchiveService.saveEntityDefaultToArchive(bogusUserInfo);
108 added.add(bogusUserInfo);
109 }
110
111
112 log.info("Sleeping, hoping for a flush to occur!");
113 for (int j=10; j >= 0; j--) {
114 Thread.sleep(1000);
115 log.info(String.valueOf(j));
116 }
117 log.info("Done sleeping!");
118
119
120
121 for (EntityDefault kedi : added) {
122
123 EntityDefault retrieved = identityArchiveService.getEntityDefaultFromArchiveByPrincipalId(
124 kedi.getPrincipals().get(0).getPrincipalId());
125 assertNotNull("no value retrieved for principalId: " + kedi.getPrincipals().get(0).getPrincipalId(), retrieved);
126 assertTrue(kedi.getPrincipals().get(0).getPrincipalId().equals(retrieved.getPrincipals().get(0).getPrincipalId()));
127 retrieved = identityArchiveService.getEntityDefaultFromArchiveByPrincipalName(
128 kedi.getPrincipals().get(0).getPrincipalName());
129 assertTrue(kedi.getPrincipals().get(0).getPrincipalId().equals(retrieved.getPrincipals().get(0).getPrincipalId()));
130 retrieved = identityArchiveService.getEntityDefaultFromArchive(kedi.getEntityId());
131 assertTrue(kedi.getPrincipals().get(0).getPrincipalId().equals(retrieved.getPrincipals().get(0).getPrincipalId()));
132 }
133 }
134
135 private static class MinimalKEDIBuilder {
136 private String entityId;
137 private String principalId;
138 private String principalName;
139 private Boolean active;
140
141 public MinimalKEDIBuilder(String name) {
142 entityId = UUID.randomUUID().toString();
143 principalId = principalName = name;
144 }
145
146 public EntityDefault build() {
147 if (entityId == null) entityId = UUID.randomUUID().toString();
148 if (principalId == null) principalId = UUID.randomUUID().toString();
149 if (principalName == null) principalName = principalId;
150 if (active == null) active = true;
151
152 Principal.Builder principal = Principal.Builder.create(principalName);
153 principal.setActive(active);
154 principal.setEntityId(entityId);
155 principal.setPrincipalId(principalId);
156
157 EntityDefault.Builder kedi = EntityDefault.Builder.create();
158 kedi.setPrincipals(Collections.singletonList(principal));
159 kedi.setEntityId(entityId);
160
161 return kedi.build();
162 }
163
164
165
166
167 public String getEntityId() {
168 return this.entityId;
169 }
170
171
172
173
174 public void setEntityId(String entityId) {
175 this.entityId = entityId;
176 }
177
178
179
180
181 public String getPrincipalId() {
182 return this.principalId;
183 }
184
185
186
187
188 public void setPrincipalId(String principalId) {
189 this.principalId = principalId;
190 }
191
192
193
194
195 public String getPrincipalName() {
196 return this.principalName;
197 }
198
199
200
201
202 public void setPrincipalName(String principalName) {
203 this.principalName = principalName;
204 }
205
206
207
208
209 public Boolean getActive() {
210 return this.active;
211 }
212
213
214
215
216 public void setActive(Boolean active) {
217 this.active = active;
218 }
219
220
221 }
222
223 }