View Javadoc

1   /**
2    * Copyright 2005-2011 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.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   * Unit test for the IdentityArchiveService
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
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  	 * This tests
65  	 * <ol><li>trying to retrieve a non-existant {@link EntityDefault}
66  	 * <li>saving a {@link EntityDefault} and retrieving it.
67  	 * </ol>
68  	 * This test is specific to {@link IdentityArchiveServiceImpl}
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  		// exceed the max write queue size to initiate a flush
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  		// give it a second or three to flush
96  		log.info("Sleeping, hoping for a flush to occur!");
97  		Thread.sleep(1000);
98  		log.info("Done sleeping!");
99  
100 		// these should have been flushed by now, test retrieval
101 
102 		for (EntityDefault kedi : added) {
103 			// retrieve it every way we can
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 		 * @return the entityId
147 		 */
148 		public String getEntityId() {
149 			return this.entityId;
150 		}
151 
152 		/**
153 		 * @param entityId the entityId to set
154 		 */
155 		public void setEntityId(String entityId) {
156 			this.entityId = entityId;
157 		}
158 
159 		/**
160 		 * @return the principalId
161 		 */
162 		public String getPrincipalId() {
163 			return this.principalId;
164 		}
165 
166 		/**
167 		 * @param principalId the principalId to set
168 		 */
169 		public void setPrincipalId(String principalId) {
170 			this.principalId = principalId;
171 		}
172 
173 		/**
174 		 * @return the principalName
175 		 */
176 		public String getPrincipalName() {
177 			return this.principalName;
178 		}
179 
180 		/**
181 		 * @param principalName the principalName to set
182 		 */
183 		public void setPrincipalName(String principalName) {
184 			this.principalName = principalName;
185 		}
186 
187 		/**
188 		 * @return the active
189 		 */
190 		public Boolean getActive() {
191 			return this.active;
192 		}
193 
194 		/**
195 		 * @param active the active to set
196 		 */
197 		public void setActive(Boolean active) {
198 			this.active = active;
199 		}
200 
201 
202 	}
203 
204 }