View Javadoc

1   /*
2    * Copyright 2007-2009 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 java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.UUID;
22  
23  import org.junit.Ignore;
24  import org.junit.Test;
25  import org.kuali.rice.core.config.ConfigContext;
26  import org.kuali.rice.kim.bo.entity.dto.KimEntityDefaultInfo;
27  import org.kuali.rice.kim.bo.entity.dto.KimPrincipalInfo;
28  import org.kuali.rice.kim.bo.entity.impl.KimEntityDefaultInfoCacheImpl;
29  import org.kuali.rice.kim.service.IdentityArchiveService;
30  import org.kuali.rice.kim.service.KIMServiceLocator;
31  import org.kuali.rice.kim.service.impl.IdentityArchiveServiceImpl;
32  import org.kuali.rice.kim.test.KIMTestCase;
33  import org.kuali.rice.kns.service.KNSServiceLocator;
34  
35  /**
36   * Unit test for the IdentityArchiveService
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  @Ignore
41  public class IdentityArchiveServiceTest extends KIMTestCase {
42  
43  	private IdentityArchiveService identityArchiveService;
44  
45  	public void setUp() throws Exception {
46  		super.setUp();
47  		KNSServiceLocator.getBusinessObjectService().deleteMatching(KimEntityDefaultInfoCacheImpl.class, Collections.emptyMap());
48  		if (null == identityArchiveService) {
49  			identityArchiveService = KIMServiceLocator.getIdentityArchiveService();
50  		}
51  	}
52  
53  	/**
54  	 * This tests
55  	 * <ol><li>trying to retrieve a non-existant {@link KimEntityDefaultInfo}
56  	 * <li>saving a {@link KimEntityDefaultInfo} and retrieving it.
57  	 * </ol>
58  	 * This test is specific to {@link IdentityArchiveServiceImpl}
59  	 */
60  	@Test
61  	public void testArchiveFlushesWhenQueueIsFull() throws Exception {
62  		final int maxWriteQueueSize =
63  			Integer.valueOf(ConfigContext.getCurrentContextConfig().getProperty("kim.identityArchiveServiceImpl.maxWriteQueueSize"));
64  
65  		List<KimEntityDefaultInfo> added = new ArrayList<KimEntityDefaultInfo>();
66  
67  		// exceed the max write queue size to initiate a flush
68  		for (int i=1; i<=maxWriteQueueSize; i++) {
69  			MinimalKEDIBuilder builder = new MinimalKEDIBuilder("bogusUser" + i);
70  			builder.setEntityId("bogusUser" + i);
71  			KimEntityDefaultInfo bogusUserInfo = builder.build();
72  
73  			KimEntityDefaultInfo retrieved = identityArchiveService.getEntityDefaultInfoFromArchiveByPrincipalId(builder.getPrincipalId());
74  			assertNull(retrieved);
75  			retrieved = identityArchiveService.getEntityDefaultInfoFromArchiveByPrincipalName(builder.getPrincipalName());
76  			assertNull(retrieved);
77  			retrieved = identityArchiveService.getEntityDefaultInfoFromArchive(builder.getEntityId());
78  			assertNull(retrieved);
79  
80  			identityArchiveService.saveDefaultInfoToArchive(bogusUserInfo);
81  			added.add(bogusUserInfo);
82  		}
83  
84  		// give it a second to flush
85  		log.info("Sleeping, hoping for a flush to occur!");
86  		Thread.sleep(1000);
87  		log.info("Done sleeping!");
88  
89  		// these should have been flushed by now, test retrieval
90  
91  		for (KimEntityDefaultInfo kedi : added) {
92  			// retrieve it every way we can
93  			KimEntityDefaultInfo retrieved = identityArchiveService.getEntityDefaultInfoFromArchiveByPrincipalId(kedi.getPrincipals().get(0).getPrincipalId());
94  			assertTrue(kedi.getPrincipals().get(0).getPrincipalId().equals(retrieved.getPrincipals().get(0).getPrincipalId()));
95  			retrieved = identityArchiveService.getEntityDefaultInfoFromArchiveByPrincipalName(kedi.getPrincipals().get(0).getPrincipalName());
96  			assertTrue(kedi.getPrincipals().get(0).getPrincipalId().equals(retrieved.getPrincipals().get(0).getPrincipalId()));
97  			retrieved = identityArchiveService.getEntityDefaultInfoFromArchive(kedi.getEntityId());
98  			assertTrue(kedi.getPrincipals().get(0).getPrincipalId().equals(retrieved.getPrincipals().get(0).getPrincipalId()));
99  		}
100 	}
101 
102 	private static class MinimalKEDIBuilder {
103 		private String entityId;
104 		private String principalId;
105 		private String principalName;
106 		private Boolean active;
107 
108 		public MinimalKEDIBuilder(String name) {
109 			entityId = UUID.randomUUID().toString();
110 			principalId = principalName = name;
111 		}
112 
113 		public KimEntityDefaultInfo build() {
114 			if (entityId == null) entityId = UUID.randomUUID().toString();
115 			if (principalId == null) principalId = UUID.randomUUID().toString();
116 			if (principalName == null) principalName = principalId;
117 			if (active == null) active = true;
118 
119 			KimPrincipalInfo principal = new KimPrincipalInfo();
120 			principal.setActive(active);
121 			principal.setEntityId(entityId);
122 			principal.setPrincipalId(principalId);
123 			principal.setPrincipalName(principalName);
124 
125 			KimEntityDefaultInfo kedi = new KimEntityDefaultInfo();
126 			kedi.setPrincipals(Collections.singletonList(principal));
127 			kedi.setEntityId(entityId);
128 
129 			return kedi;
130 		}
131 
132 		/**
133 		 * @return the entityId
134 		 */
135 		public String getEntityId() {
136 			return this.entityId;
137 		}
138 
139 		/**
140 		 * @param entityId the entityId to set
141 		 */
142 		public void setEntityId(String entityId) {
143 			this.entityId = entityId;
144 		}
145 
146 		/**
147 		 * @return the principalId
148 		 */
149 		public String getPrincipalId() {
150 			return this.principalId;
151 		}
152 
153 		/**
154 		 * @param principalId the principalId to set
155 		 */
156 		public void setPrincipalId(String principalId) {
157 			this.principalId = principalId;
158 		}
159 
160 		/**
161 		 * @return the principalName
162 		 */
163 		public String getPrincipalName() {
164 			return this.principalName;
165 		}
166 
167 		/**
168 		 * @param principalName the principalName to set
169 		 */
170 		public void setPrincipalName(String principalName) {
171 			this.principalName = principalName;
172 		}
173 
174 		/**
175 		 * @return the active
176 		 */
177 		public Boolean getActive() {
178 			return this.active;
179 		}
180 
181 		/**
182 		 * @param active the active to set
183 		 */
184 		public void setActive(Boolean active) {
185 			this.active = active;
186 		}
187 
188 
189 	}
190 
191 }