4444import org .apache .solr .client .solrj .SolrRequest ;
4545import org .apache .solr .client .solrj .SolrResponse ;
4646import org .apache .solr .client .solrj .request .CollectionAdminRequest ;
47+ import org .apache .solr .client .solrj .request .QueryRequest ;
48+ import org .apache .solr .client .solrj .response .QueryResponse ;
4749import org .apache .solr .cloud .SolrCloudTestCase ;
4850import org .apache .solr .common .util .EnvUtils ;
4951import org .apache .solr .common .util .Utils ;
@@ -106,7 +108,8 @@ public void testBasicRun() throws Exception {
106108 File jsonDoc = File .createTempFile ("temp" , ".json" );
107109
108110 FileWriter fw = new FileWriter (jsonDoc , StandardCharsets .UTF_8 );
109- Utils .writeJson (Utils .toJSONString (Map .of ("id" , "1" , "title" , "mytitle" )), fw , true );
111+ Utils .writeJson (Map .of ("id" , "1" , "title_s" , "mytitle" ), fw , true );
112+ fw .flush ();
110113
111114 String [] args = {
112115 "post" ,
@@ -117,6 +120,21 @@ public void testBasicRun() throws Exception {
117120 jsonDoc .getAbsolutePath ()
118121 };
119122 assertEquals (0 , runTool (args ));
123+
124+ int numFound = 0 ;
125+ int expectedDocCount = 1 ;
126+
127+ for (int idx = 0 ; idx < 100 ; ++idx ) {
128+ QueryRequest req = withBasicAuth (new QueryRequest (params ("q" , "*:*" )));
129+ QueryResponse rsp = req .process (cluster .getSolrClient (), collection );
130+
131+ numFound = (int ) rsp .getResults ().getNumFound ();
132+ if (numFound == expectedDocCount ) {
133+ break ;
134+ }
135+ Thread .sleep (100 );
136+ }
137+ assertEquals ("*:* found unexpected number of documents" , expectedDocCount , numFound );
120138 }
121139
122140 @ Test
@@ -129,15 +147,31 @@ public void testRunWithCollectionParam() throws Exception {
129147 withBasicAuth (CollectionAdminRequest .createCollection (collection , "conf1" , 1 , 1 , 0 , 0 ))
130148 .processAndWait (cluster .getSolrClient (), 10 );
131149
132- File jsonDoc = File .createTempFile ("temp" , "json" );
150+ File jsonDoc = File .createTempFile ("temp" , ". json" );
133151
134152 FileWriter fw = new FileWriter (jsonDoc , StandardCharsets .UTF_8 );
135- Utils .writeJson (Utils .toJSONString (Map .of ("id" , "1" , "title" , "mytitle" )), fw , true );
153+ Utils .writeJson (Map .of ("id" , "1" , "title_s" , "mytitle" ), fw , true );
154+ fw .flush ();
136155
137156 String [] args = {
138157 "post" , "-c" , collection , "-credentials" , USER + ":" + PASS , jsonDoc .getAbsolutePath ()
139158 };
140159 assertEquals (0 , runTool (args ));
160+
161+ int numFound = 0 ;
162+ int expectedDocCount = 1 ;
163+
164+ for (int idx = 0 ; idx < 100 ; ++idx ) {
165+ QueryRequest req = withBasicAuth (new QueryRequest (params ("q" , "*:*" )));
166+ QueryResponse rsp = req .process (cluster .getSolrClient (), collection );
167+
168+ numFound = (int ) rsp .getResults ().getNumFound ();
169+ if (numFound == expectedDocCount ) {
170+ break ;
171+ }
172+ Thread .sleep (100 );
173+ }
174+ assertEquals ("*:* found unexpected number of documents" , expectedDocCount , numFound );
141175 }
142176
143177 private int runTool (String [] args ) throws Exception {
@@ -230,6 +264,28 @@ public void testDoFilesMode() throws MalformedURLException {
230264 assertEquals (2 , num );
231265 }
232266
267+ @ Test
268+ public void testDetectingIfRecursionPossibleInFilesMode () throws IOException {
269+ PostTool postTool = new PostTool ();
270+ postTool .recursive = 1 ; // This is the default
271+ File dir = getFile ("exampledocs" );
272+ File doc = File .createTempFile ("temp" , ".json" );
273+ assertTrue (postTool .recursionPossible (new String [] {dir .toString ()}));
274+ assertFalse (postTool .recursionPossible (new String [] {doc .toString ()}));
275+ assertTrue (postTool .recursionPossible (new String [] {doc .toString (), dir .toString ()}));
276+ }
277+
278+ @ Test
279+ public void testRecursionAppliesToFilesMode () throws MalformedURLException {
280+ PostTool postTool = new PostTool ();
281+ postTool .recursive = 1 ; // This is the default
282+ postTool .dryRun = true ;
283+ postTool .solrUpdateUrl = new URL ("http://localhost:8983/solr/fake/update" );
284+ File dir = getFile ("exampledocs" );
285+ int num = postTool .postFiles (new String [] {dir .toString ()}, 0 , null , null );
286+ assertEquals (2 , num );
287+ }
288+
233289 @ Test
234290 public void testDoWebMode () throws IOException , URISyntaxException {
235291 PostTool postTool = new PostTool ();
0 commit comments