1 package org.kuali.ole.repository;
2
3 import org.apache.commons.io.FileUtils;
4 import org.apache.solr.client.solrj.SolrQuery;
5 import org.apache.solr.client.solrj.SolrServer;
6 import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
7 import org.apache.solr.client.solrj.response.QueryResponse;
8 import org.apache.solr.common.SolrDocumentList;
9 import org.junit.Test;
10 import org.kuali.ole.RepositoryManager;
11 import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
12 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
13 import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
14 import org.kuali.ole.docstore.util.PropertyUtil;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 import javax.jcr.Node;
19 import javax.jcr.Session;
20 import java.io.File;
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import static org.junit.Assert.*;
26
27
28
29
30
31 public class DeleteManage_AT{
32
33 private static final Logger LOG = LoggerFactory.getLogger(DeleteManage_AT.class);
34
35 @Test
36 public void deleteLinkedDocs() throws Exception {
37 Session session = null;
38
39 File file = new File(getClass().getResource("/org/kuali/ole/repository/request.xml").toURI());
40 String input = FileUtils.readFileToString(file);
41 RequestHandler rh = new RequestHandler();
42 Request request = rh.toObject(input);
43
44
45
46
47
48
49
50
51
52
53
54
55 List<RequestDocument> docStoreDocuments = null;
56 assertNotNull(docStoreDocuments);
57 List<String> uuidsList = new ArrayList<String>();
58 String status = null;
59 List<String> categoryList = new ArrayList<String>();
60 List<String> typeList = new ArrayList<String>();
61 List<String> formatList = new ArrayList<String>();
62 String category = null;
63 String type = null;
64 String format = null;
65 session = RepositoryManager.getRepositoryManager().getSession();
66 try {
67 for (Iterator<RequestDocument> iterator = docStoreDocuments
68 .iterator(); iterator.hasNext();) {
69 RequestDocument document = iterator.next();
70 category = document.getCategory();
71 type = document.getType();
72 format = document.getFormat();
73 LOG.info("UUIDs...." + document.getUuid()
74 + "category...." + category);
75 String uuid = document.getUuid();
76 uuidsList.add(uuid);
77 categoryList.add(category);
78 typeList.add(type);
79 formatList.add(format);
80 }
81 String deleteUuidXml = deleteuuidsXml(uuidsList, categoryList,
82 typeList, formatList);
83
84 LOG.info("status" + status);
85 checkUuidInSolr(uuidsList, category);
86 checkUuidInDocStore(uuidsList, session);
87 } catch (Exception e) {
88 e.printStackTrace();
89 LOG.info(e.getMessage());
90 status = "Failure";
91 LOG.info("status" + status);
92 } finally {
93 RepositoryManager.getRepositoryManager().logout(session);
94 }
95 }
96
97 private String deleteuuidsXml(List<String> uuidsList,
98 List<String> categoryList, List<String> typeList,
99 List<String> formatList) {
100
101 StringBuilder sb = new StringBuilder();
102 sb.append("<request>\n");
103 sb.append(" <user>").append("ole-khuntley").append("</user>\n");
104 sb.append(" <operation>").append("batchDeleteWithLinks")
105 .append("</operation>\n");
106 sb.append("<requestDocuments>\n");
107 for (int i = 0; i < uuidsList.size(); i++) {
108 sb.append("<ingestdocument id=\"" + uuidsList.get(i)
109 + "\" category=\"" + categoryList.get(i) + "\" type=\""
110 + typeList.get(i) + "\" format=\"" + formatList.get(i)
111 + "\">" + "</ingestdocument>\n");
112 }
113 sb.append(" </requestDocuments>\n");
114 sb.append(" </request>");
115 LOG.info("sb.tostring" + sb.toString());
116
117 return sb.toString();
118 }
119
120 private void checkUuidInSolr(List<String> uuidsList, String category)
121 throws Exception {
122 if (category.equalsIgnoreCase("bibliographic")) {
123 category = "bib";
124 }
125 SolrServer solr = new CommonsHttpSolrServer(PropertyUtil
126 .getPropertyUtil().getProperty("docSearchURL") + "/" + category);
127 SolrQuery query = new SolrQuery();
128 for (int i = 0; i < uuidsList.size(); i++) {
129 query.setQuery("uniqueId:" + uuidsList.get(i));
130 QueryResponse response = solr.query(query);
131 LOG.info("query..." + query);
132 SolrDocumentList doc = response.getResults();
133 LOG.info("NumFound..." + doc.getNumFound());
134 assertEquals(0, (int) (doc.getNumFound()));
135 }
136 }
137
138 private void checkUuidInDocStore(List<String> uuidsList, Session session)
139 throws Exception {
140 for (int i = 0; i < uuidsList.size(); i++) {
141 Node deleteNode = new NodeHandler().getNodeByUUID(session,
142 uuidsList.get(i));
143 LOG.info("deleteNodes..." + deleteNode);
144 assertNull(deleteNode);
145 }
146 }
147 }
148
149