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