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