1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.repository;
17
18 import junit.framework.Assert;
19 import org.junit.Before;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.kuali.ole.BaseTestCase;
23 import org.kuali.ole.RepositoryManager;
24 import org.kuali.ole.docstore.DocStoreConstants;
25 import org.kuali.ole.docstore.model.enums.DocCategory;
26 import org.kuali.ole.docstore.model.enums.DocType;
27 import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
28 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
29 import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
30 import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
31 import org.kuali.ole.docstore.service.BeanLocator;
32 import org.kuali.ole.docstore.service.IngestNIndexHandlerService;
33 import org.kuali.ole.pojo.OleException;
34 import org.mockito.MockitoAnnotations;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import javax.jcr.Node;
39 import javax.jcr.RepositoryException;
40 import javax.jcr.Session;
41 import javax.jcr.version.VersionHistory;
42 import javax.jcr.version.VersionIterator;
43 import javax.jcr.version.VersionManager;
44 import java.io.File;
45 import java.io.IOException;
46 import java.net.URISyntaxException;
47 import java.net.URL;
48 import java.util.Iterator;
49 import java.util.List;
50
51 import static org.junit.Assert.assertNotNull;
52 @Ignore
53 @Deprecated
54 public class CheckinManager_UT
55 extends BaseTestCase {
56
57 private static final Logger LOG = LoggerFactory
58 .getLogger(CheckinManager_UT.class);
59 private IngestNIndexHandlerService ingestNIndexHandlerService = BeanLocator
60 .getIngestNIndexHandlerService();
61
62 @Before
63 public void setUp() throws Exception {
64 super.setUp();
65 MockitoAnnotations.initMocks(this);
66 }
67
68 @Test
69 public void testUpdateRecord() throws Exception {
70 Request request = getRequestObject();
71 Response response = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
72 assertNotNull(response);
73 List<RequestDocument> docStoreDocuments = request.getRequestDocuments();
74 for (Iterator<RequestDocument> iterator = docStoreDocuments.iterator(); iterator.hasNext(); ) {
75 RequestDocument docStoreDocument = iterator.next();
76 if (docStoreDocument.getCategory().equals(DocCategory.WORK.getCode()) && docStoreDocument.getType()
77 .equals(DocType.BIB
78 .getDescription())) {
79 CheckinManager checkinManager = new CheckinManager();
80 if (DocStoreConstants.isVersioningEnabled) {
81 String currentVersionForRecord = getCurrentVersionForRecord(docStoreDocument.getUuid());
82 assertNotNull(currentVersionForRecord);
83 }
84 docStoreDocument.setId(docStoreDocument.getUuid());
85 checkinManager.updateContent(docStoreDocument);
86 if (DocStoreConstants.isVersioningEnabled) {
87 String currentVersionAfterUpdate = getCurrentVersionForRecord(docStoreDocument.getUuid());
88 assertNotNull(currentVersionAfterUpdate);
89 }
90 }
91 }
92 }
93
94 private Request getRequestObject() throws URISyntaxException, IOException {
95 URL resource = getClass().getResource("request.xml");
96 File file = new File(resource.toURI());
97 RequestHandler requestHandler = new RequestHandler();
98 Request request = requestHandler.toObject(readFile(file));
99 return request;
100 }
101
102 private String getCurrentVersionForRecord(String uuid) {
103 String version = null;
104 Session session = null;
105 try {
106 session = RepositoryManager.getRepositoryManager().getSession();
107 VersionManager versionManager = session.getWorkspace().getVersionManager();
108 Node nodeByUUID = new NodeHandler().getNodeByUUID(session, uuid);
109 VersionHistory versionHistory = versionManager.getVersionHistory(nodeByUUID.getPath());
110 VersionIterator allVersions = versionHistory.getAllVersions();
111 while (allVersions.hasNext()) {
112 version = allVersions.nextVersion().toString();
113 LOG.info(version);
114 }
115 } catch (RepositoryException e) {
116 LOG.info(e.getMessage());
117 } catch (OleException e) {
118 LOG.info(e.getMessage());
119 } finally {
120 try {
121 RepositoryManager.getRepositoryManager().logout(session);
122 } catch (OleException e) {
123 LOG.info(e.getMessage());
124 }
125 }
126 return version;
127 }
128
129 @Test
130 public void testUpdateItemRecord() throws Exception {
131 Request request = getRequestObject();
132 Response response = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
133 String itemUUID = response.getDocuments().get(0).getLinkedInstanceDocuments().get(2).getUuid();
134 URL resource = getClass().getResource("update-item-request.xml");
135 File file = new File(resource.toURI());
136 RequestHandler requestHandler = new RequestHandler();
137 request = requestHandler.toObject(readFile(file));
138 List<RequestDocument> docStoreDocuments = request.getRequestDocuments();
139 for (Iterator<RequestDocument> iterator = docStoreDocuments.iterator(); iterator.hasNext(); ) {
140 RequestDocument docStoreDocument = iterator.next();
141 docStoreDocument.setId(itemUUID);
142 if (docStoreDocument.getCategory().equals(DocCategory.WORK.getCode()) && docStoreDocument.getType()
143 .equals(DocType.ITEM
144 .getDescription())) {
145 CheckinManager checkinManager = new CheckinManager();
146 if (DocStoreConstants.isVersioningEnabled) {
147 String currentVersionForRecord = getCurrentVersionForRecord(docStoreDocument.getUuid());
148 assertNotNull(currentVersionForRecord);
149 }
150 docStoreDocument.setUuid(docStoreDocument.getId());
151 checkinManager.updateContent(docStoreDocument);
152 CheckoutManager checkOut = new CheckoutManager();
153 String checkedOutContent = checkOut.checkOut(docStoreDocument.getId(), "mockUser", "checkout");
154 Assert.assertNotNull(checkedOutContent);
155 Assert.assertEquals(checkedOutContent, docStoreDocument.getContent().getContent());
156 if (DocStoreConstants.isVersioningEnabled) {
157 String currentVersionAfterUpdate = getCurrentVersionForRecord(docStoreDocument.getUuid());
158 assertNotNull(currentVersionAfterUpdate);
159 }
160 }
161 }
162 }
163
164 @Test
165 public void testUpdateSourceHoldingRecord() throws Exception {
166 Request request = getRequestObject();
167 Response response = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
168 String srHolUUID = response.getDocuments().get(0).getLinkedInstanceDocuments().get(1).getUuid();
169 URL resource = getClass().getResource("update-sourceHolding-request.xml");
170 File file = new File(resource.toURI());
171 RequestHandler requestHandler = new RequestHandler();
172 request = requestHandler.toObject(readFile(file));
173 List<RequestDocument> docStoreDocuments = request.getRequestDocuments();
174 for (Iterator<RequestDocument> iterator = docStoreDocuments.iterator(); iterator.hasNext(); ) {
175 RequestDocument docStoreDocument = iterator.next();
176 docStoreDocument.setId(srHolUUID);
177 if (docStoreDocument.getCategory().equals(DocCategory.WORK.getCode()) && docStoreDocument.getType()
178 .equals(DocType.SOURCEHOLDINGS
179 .getDescription())) {
180 CheckinManager checkinManager = new CheckinManager();
181 if (DocStoreConstants.isVersioningEnabled) {
182 String currentVersionForRecord = getCurrentVersionForRecord(docStoreDocument.getUuid());
183 assertNotNull(currentVersionForRecord);
184 }
185 docStoreDocument.setUuid(docStoreDocument.getId());
186 checkinManager.updateContent(docStoreDocument);
187 CheckoutManager checkOut = new CheckoutManager();
188 String checkedOutContent = checkOut.checkOut(docStoreDocument.getId(), "mockUser", "checkout");
189 Assert.assertNotNull(checkedOutContent);
190 Assert.assertEquals(checkedOutContent, docStoreDocument.getContent().getContent());
191 if (DocStoreConstants.isVersioningEnabled) {
192 String currentVersionAfterUpdate = getCurrentVersionForRecord(docStoreDocument.getUuid());
193 assertNotNull(currentVersionAfterUpdate);
194 }
195 }
196 }
197 }
198
199 @Test
200 public void testAddItemRecord() throws Exception {
201 Request request = getRequestObject();
202 Response response = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
203 String instanceUUID = response.getDocuments().get(0).getLinkedDocuments().get(0).getUuid();
204 URL resource = getClass().getResource("Add-newitem-instance-request.xml");
205 File file = new File(resource.toURI());
206 RequestHandler requestHandler = new RequestHandler();
207 request = requestHandler.toObject(readFile(file));
208 List<RequestDocument> docStoreDocuments = request.getRequestDocuments();
209 for (Iterator<RequestDocument> iterator = docStoreDocuments.iterator(); iterator.hasNext(); ) {
210 RequestDocument docStoreDocument = iterator.next();
211 docStoreDocument.setId(instanceUUID);
212 if (docStoreDocument.getCategory().equals(DocCategory.WORK.getCode()) && docStoreDocument.getType()
213 .equals(DocType.INSTANCE
214 .getDescription())) {
215 CheckinManager checkinManager = new CheckinManager();
216 if (DocStoreConstants.isVersioningEnabled) {
217 String currentVersionForRecord = getCurrentVersionForRecord(docStoreDocument.getUuid());
218 assertNotNull(currentVersionForRecord);
219 }
220 docStoreDocument.setUuid(docStoreDocument.getId());
221 checkinManager.updateContent(docStoreDocument);
222 CheckoutManager checkOut = new CheckoutManager();
223 for (RequestDocument addedItemRecords : docStoreDocument.getLinkedRequestDocuments()) {
224 String newItemUuid = addedItemRecords.getId();
225 String checkedOutContent = checkOut.checkOut(newItemUuid, "mockUser", "checkout");
226 Assert.assertNotNull(checkedOutContent);
227 Assert.assertEquals(checkedOutContent, addedItemRecords.getContent().getContent());
228 }
229 if (DocStoreConstants.isVersioningEnabled) {
230 String currentVersionAfterUpdate = getCurrentVersionForRecord(docStoreDocument.getUuid());
231 assertNotNull(currentVersionAfterUpdate);
232 }
233 }
234 }
235 }
236
237 @Test
238 public void testUpdateHoldingRecord() throws Exception {
239 Request request = getRequestObject();
240 Response response = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
241 String itemUUID = response.getDocuments().get(0).getLinkedInstanceDocuments().get(0).getUuid();
242 URL resource = getClass().getResource("update-holding-request.xml");
243 File file = new File(resource.toURI());
244 RequestHandler requestHandler = new RequestHandler();
245 request = requestHandler.toObject(readFile(file));
246 List<RequestDocument> docStoreDocuments = request.getRequestDocuments();
247 for (Iterator<RequestDocument> iterator = docStoreDocuments.iterator(); iterator.hasNext(); ) {
248 RequestDocument docStoreDocument = iterator.next();
249 docStoreDocument.setId(itemUUID);
250 if (docStoreDocument.getCategory().equals(DocCategory.WORK.getCode()) && docStoreDocument.getType()
251 .equals(DocType.HOLDINGS
252 .getDescription())) {
253 CheckinManager checkinManager = new CheckinManager();
254 if (DocStoreConstants.isVersioningEnabled) {
255 String currentVersionForRecord = getCurrentVersionForRecord(docStoreDocument.getUuid());
256 assertNotNull(currentVersionForRecord);
257 }
258 docStoreDocument.setUuid(docStoreDocument.getId());
259 checkinManager.updateContent(docStoreDocument);
260 CheckoutManager checkOut = new CheckoutManager();
261 String checkedOutContent = checkOut.checkOut(docStoreDocument.getId(), "mockUser", "checkout");
262 Assert.assertNotNull(checkedOutContent);
263 Assert.assertEquals(checkedOutContent, docStoreDocument.getContent().getContent());
264 if (DocStoreConstants.isVersioningEnabled) {
265 String currentVersionAfterUpdate = getCurrentVersionForRecord(docStoreDocument.getUuid());
266 assertNotNull(currentVersionAfterUpdate);
267 }
268 }
269 }
270 }
271
272 @Test
273 public void testCheckInManager() throws URISyntaxException, IOException, OleException, RepositoryException {
274 CheckinManager checkinManager = (CheckinManager) BeanLocator.getBean("checkinManagerService");
275 checkinManager.getDocStoreLogger();
276 Session session = RepositoryManager.getRepositoryManager().getSession("CheckinManager", "updateRecordInDocStore");
277 checkinManager.getVersionManager(session);
278 }
279 }