1 package org.kuali.ole.repository;
2
3 import org.apache.commons.io.FileUtils;
4 import org.junit.Before;
5 import org.junit.Ignore;
6 import org.junit.Test;
7 import org.kuali.ole.BaseTestCase;
8 import org.kuali.ole.RepositoryManager;
9 import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
10 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
11 import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
12 import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
13 import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
14 import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler;
15 import org.kuali.ole.docstore.service.BeanLocator;
16 import org.kuali.ole.docstore.service.IngestNIndexHandlerService;
17 import org.kuali.ole.pojo.OleException;
18 import org.mockito.MockitoAnnotations;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import javax.jcr.Node;
23 import javax.jcr.Session;
24 import java.io.File;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 @Ignore
31 @Deprecated
32 public class DeleteManager_UT
33 extends BaseTestCase {
34
35 private static final Logger LOG = LoggerFactory.getLogger(DeleteManager_UT.class);
36 List<String> bibIds;
37 List<String> instanceIds;
38
39 private IngestNIndexHandlerService ingestNIndexHandlerService = BeanLocator.getIngestNIndexHandlerService();
40
41 @Before
42 public void setUp() throws Exception {
43 super.setUp();
44 LOG.info("setup");
45 MockitoAnnotations.initMocks(this);
46 bibIds = new ArrayList<String>();
47 instanceIds = new ArrayList<String>();
48 ingestBibData();
49 }
50
51 public void testDeleteDocs() throws Exception {
52 Session session = null;
53 Request request = buildRequest(bibIds);
54 assertNotNull(request);
55 List<RequestDocument> requestDocuments = request.getRequestDocuments();
56 assertNotNull(requestDocuments);
57 request.setOperation("delete");
58 Response response = new DeleteManager().deleteDocs(request);
59 LOG.info("status-->" + response);
60 session = RepositoryManager.getRepositoryManager().getSession(request.getUser(), request.getOperation());
61 LOG.info("session-->" + session);
62 checkUuidInDocStore(bibIds, session);
63 ResponseHandler responseHandler = new ResponseHandler();
64 String deleteResponse = responseHandler.toXML(response);
65 LOG.info("delete response-->" + deleteResponse);
66 }
67
68 public void testDeleteWithLinkedDocs() throws Exception {
69 Session session = null;
70 Request req = buildRequest(bibIds);
71 List<RequestDocument> requestDocuments = req.getRequestDocuments();
72 assertNotNull(requestDocuments);
73 req.setOperation("deleteWithLinkedDocs");
74 Response response = new DeleteManager().deleteDocs(req);
75 session = RepositoryManager.getRepositoryManager().getSession(req.getUser(), req.getOperation());
76 LOG.info("status-->" + response);
77 checkUuidInDocStore(instanceIds, session);
78 checkUuidInDocStore(bibIds, session);
79 ResponseHandler responseHandler = new ResponseHandler();
80 String deleteResponse = responseHandler.toXML(response);
81 LOG.info("deleteWithLinkedDocs response-->" + deleteResponse);
82
83 }
84
85 private Request buildRequest(List<String> uuidList) {
86 Request request = new Request();
87 List<RequestDocument> requestDocumentList = new ArrayList<RequestDocument>();
88 request.setUser("ole-khuntley");
89 request.setOperation("delete");
90 for (int i = 0; i < uuidList.size(); i++) {
91 RequestDocument requestDocument = new RequestDocument();
92 requestDocument.setId(uuidList.get(i));
93 requestDocumentList.add(requestDocument);
94
95 }
96 request.setRequestDocuments(requestDocumentList);
97 return request;
98 }
99
100 private List<String> ingestBibData() throws Exception {
101 File file = new File(getClass().getResource("/org/kuali/ole/repository/request.xml").toURI());
102 try {
103 String input = FileUtils.readFileToString(file);
104 RequestHandler rh = new RequestHandler();
105 Request request = rh.toObject(input);
106 Response response = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
107 for (ResponseDocument resDoc : response.getDocuments()) {
108 bibIds.add(resDoc.getUuid());
109 for (ResponseDocument linkedDoc : resDoc.getLinkedDocuments()) {
110 instanceIds.add(linkedDoc.getUuid());
111 }
112 }
113 LOG.info("Bib Ids-->" + bibIds);
114 LOG.info("Instance Ids-->" + instanceIds);
115
116 } catch (Exception e) {
117 LOG.info("in excep" + e.getMessage() , e );
118 }
119 return bibIds;
120 }
121
122 private void checkUuidInDocStore(List<String> uuidsList, Session session) throws Exception {
123 for (int i = 0; i < uuidsList.size(); i++) {
124 Node deleteNode = new NodeHandler().getNodeByUUID(session, uuidsList.get(i));
125 LOG.info("deletedNodes..." + deleteNode);
126 assertNull(deleteNode);
127 }
128 }
129
130 @Test
131 public void testCleanupDocStoreData() throws Exception, OleException {
132 List<String> ingestedIds = ingestBibData();
133 LOG.info("bib ids-->" + ingestedIds);
134 new DeleteManager().cleanUpDocStoreData();
135 LOG.info("bib ids after delete-->" + ingestedIds);
136 }
137 }