@@ -525,6 +525,156 @@ public void testInteractiveSolrCloudExample() throws Exception {
525525 executor .execute (org .apache .commons .exec .CommandLine .parse ("bin/solr stop -p " + bindPort ));
526526 }
527527
528+ /**
529+ * Test the --prompts option that allows providing all prompt values as a comma-separated string
530+ * without requiring interactive input.
531+ */
532+ @ Test
533+ public void testSolrCloudExampleWithPrompts () throws Exception {
534+ Path solrHomeDir = ExternalPaths .SERVER_HOME ;
535+ if (!Files .isDirectory (solrHomeDir ))
536+ fail (solrHomeDir + " not found and is required to run this test!" );
537+
538+ Path solrExampleDir = createTempDir ();
539+ Path solrServerDir = solrHomeDir .getParent ();
540+
541+ int bindPort = -1 ;
542+ try (ServerSocket socket = new ServerSocket (0 )) {
543+ bindPort = socket .getLocalPort ();
544+ }
545+
546+ String collectionName = "testCloudExampleWithPrompts" ;
547+
548+ // Provide all prompt values via --prompts option:
549+ // numNodes, port1, collectionName, numShards, replicationFactor, configName
550+ String promptsValue = "1," + bindPort + ",\" " + collectionName + "\" ,2,2,_default" ;
551+
552+ String [] toolArgs =
553+ new String [] {
554+ "--example" ,
555+ "cloud" ,
556+ "--server-dir" ,
557+ solrServerDir .toString (),
558+ "--example-dir" ,
559+ solrExampleDir .toString (),
560+ "--prompts" ,
561+ promptsValue
562+ };
563+
564+ // capture tool output to stdout
565+ CLITestHelper .TestingRuntime runtime = new CLITestHelper .TestingRuntime (true );
566+
567+ RunExampleExecutor executor = new RunExampleExecutor ();
568+ closeables .add (executor );
569+
570+ RunExampleTool tool = new RunExampleTool (executor , System .in , runtime );
571+ try {
572+ tool .runTool (SolrCLI .processCommandLineArgs (tool , toolArgs ));
573+ } catch (Exception e ) {
574+ System .err .println (
575+ "RunExampleTool failed due to: "
576+ + e
577+ + "; stdout from tool prior to failure: "
578+ + runtime .getOutput ());
579+ throw e ;
580+ }
581+
582+ String toolOutput = runtime .getOutput ();
583+
584+ // verify Solr is running on the expected port and verify the collection exists
585+ String solrUrl = "http://localhost:" + bindPort + "/solr" ;
586+ if (!CLIUtils .safeCheckCollectionExists (solrUrl , collectionName , null )) {
587+ fail (
588+ "After running Solr cloud example with --prompts, test collection '"
589+ + collectionName
590+ + "' not found in Solr at: "
591+ + solrUrl
592+ + "; tool output: "
593+ + toolOutput );
594+ }
595+
596+ // verify the collection was created with the specified parameters
597+ try (CloudSolrClient cloudClient =
598+ new RandomizingCloudSolrClientBuilder (
599+ Collections .singletonList (executor .solrCloudCluster .getZkServer ().getZkAddress ()),
600+ Optional .empty ())
601+ .withDefaultCollection (collectionName )
602+ .build ()) {
603+
604+ // index some test docs to verify the collection works
605+ int numDocs = 5 ;
606+ for (int d = 0 ; d < numDocs ; d ++) {
607+ SolrInputDocument doc = new SolrInputDocument ();
608+ doc .setField ("id" , "doc" + d );
609+ doc .setField ("test_s" , "prompts" );
610+ cloudClient .add (doc );
611+ }
612+ cloudClient .commit ();
613+
614+ QueryResponse qr = cloudClient .query (new SolrQuery ("test_s:prompts" ));
615+ assertEquals (
616+ "Expected "
617+ + numDocs
618+ + " docs in the "
619+ + collectionName
620+ + " collection created via --prompts" ,
621+ numDocs ,
622+ qr .getResults ().getNumFound ());
623+ }
624+
625+ // Verify output contains the prompts values
626+ assertTrue (
627+ "Tool output should contain the collection name" , toolOutput .contains (collectionName ));
628+
629+ // delete the collection
630+ DeleteTool deleteTool = new DeleteTool (runtime );
631+ String [] deleteArgs = new String [] {"--name" , collectionName , "--solr-url" , solrUrl };
632+ deleteTool .runTool (SolrCLI .processCommandLineArgs (deleteTool , deleteArgs ));
633+
634+ // stop the test instance
635+ executor .execute (org .apache .commons .exec .CommandLine .parse ("bin/solr stop -p " + bindPort ));
636+ }
637+
638+ /** Test that using both --no-prompt and --prompts options together throws an error. */
639+ @ Test
640+ public void testPromptsAndNoPromptConflict () throws Exception {
641+ Path solrHomeDir = ExternalPaths .SERVER_HOME ;
642+ if (!Files .isDirectory (solrHomeDir ))
643+ fail (solrHomeDir + " not found and is required to run this test!" );
644+
645+ Path solrExampleDir = createTempDir ();
646+ Path solrServerDir = solrHomeDir .getParent ();
647+
648+ String [] toolArgs =
649+ new String [] {
650+ "--example" ,
651+ "cloud" ,
652+ "--server-dir" ,
653+ solrServerDir .toString (),
654+ "--example-dir" ,
655+ solrExampleDir .toString (),
656+ "--no-prompt" ,
657+ "--prompts" ,
658+ "1,8983,\" test\" ,2,2,_default"
659+ };
660+
661+ CLITestHelper .TestingRuntime runtime = new CLITestHelper .TestingRuntime (true );
662+ RunExampleExecutor executor = new RunExampleExecutor ();
663+ closeables .add (executor );
664+
665+ RunExampleTool tool = new RunExampleTool (executor , System .in , runtime );
666+
667+ int exitCode = 100 ; // Initialize to unexpected value
668+ try {
669+ exitCode = tool .runTool (SolrCLI .processCommandLineArgs (tool , toolArgs ));
670+ } catch (Exception e ) {
671+ fail ("Should not throw exception, but return error code. Got: " + e );
672+ }
673+
674+ assertEquals (
675+ "Expected error code 1 when using both --no-prompt and --prompts together" , 1 , exitCode );
676+ }
677+
528678 @ Test
529679 public void testFailExecuteScript () throws Exception {
530680 Path solrHomeDir = ExternalPaths .SERVER_HOME ;
0 commit comments