View Javadoc
1   /**
2    * Copyright 2005-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 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   * Unit test for the IdentityArchiveService
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
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  	 * 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  
76          //flush the archive service before trying this to make sure no records are sitting waiting for flush
77          identityArchiveService.flushToArchive();
78          // give it a second or 2 to flush
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  		// exceed the max write queue size to initiate a flush
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 		// give it a second or 10 to flush
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 		// these should have been flushed by now, test retrieval
115 
116 		for (EntityDefault kedi : added) {
117 			// retrieve it every way we can
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 		 * @return the entityId
162 		 */
163 		public String getEntityId() {
164 			return this.entityId;
165 		}
166 
167 		/**
168 		 * @param entityId the entityId to set
169 		 */
170 		public void setEntityId(String entityId) {
171 			this.entityId = entityId;
172 		}
173 
174 		/**
175 		 * @return the principalId
176 		 */
177 		public String getPrincipalId() {
178 			return this.principalId;
179 		}
180 
181 		/**
182 		 * @param principalId the principalId to set
183 		 */
184 		public void setPrincipalId(String principalId) {
185 			this.principalId = principalId;
186 		}
187 
188 		/**
189 		 * @return the principalName
190 		 */
191 		public String getPrincipalName() {
192 			return this.principalName;
193 		}
194 
195 		/**
196 		 * @param principalName the principalName to set
197 		 */
198 		public void setPrincipalName(String principalName) {
199 			this.principalName = principalName;
200 		}
201 
202 		/**
203 		 * @return the active
204 		 */
205 		public Boolean getActive() {
206 			return this.active;
207 		}
208 
209 		/**
210 		 * @param active the active to set
211 		 */
212 		public void setActive(Boolean active) {
213 			this.active = active;
214 		}
215 
216 
217 	}
218 
219 }