cpoerschke commented on code in PR #931:
URL: https://github.com/apache/solr/pull/931#discussion_r927858121


##########
solr/core/src/test/org/apache/solr/handler/admin/CoreAdminHandlerActionTest.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.handler.admin;
+
+import java.util.Map;
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.CoreAdminParams;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.SimpleOrderedMap;
+import org.apache.solr.handler.admin.CoreAdminHandler.CallInfo;
+import org.apache.solr.handler.admin.CoreAdminHandler.CoreAdminOp;
+import org.apache.solr.response.SolrQueryResponse;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class CoreAdminHandlerActionTest extends SolrTestCaseJ4 {
+
+  private static CoreAdminHandler admin = null;
+
+  @BeforeClass
+  public static void beforeClass() throws Exception {
+
+    setupNoCoreTest(createTempDir(), null);
+
+    admin = new CoreAdminHandler(h.getCoreContainer());
+
+    admin.registerCustomActions(
+        Map.of(
+            "action1",
+            new CoreAdminHandlerTestAction1(),
+            "action2",
+            new CoreAdminHandlerTestAction2()));
+  }

Review Comment:
   Just added 
https://github.com/apache/solr/pull/931/commits/60805de04b06118ba1b5d49332d41b6dad1e5c07
 commit, what do you think?



##########
solr/core/src/test/org/apache/solr/handler/admin/CoreAdminHandlerActionTest.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.handler.admin;
+
+import java.util.Locale;
+import java.util.Map;
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.CoreAdminParams;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.SimpleOrderedMap;
+import org.apache.solr.handler.admin.CoreAdminHandler.CallInfo;
+import org.apache.solr.handler.admin.CoreAdminHandler.CoreAdminOp;
+import org.apache.solr.response.SolrQueryResponse;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class CoreAdminHandlerActionTest extends SolrTestCaseJ4 {
+
+  private static CoreAdminHandler admin = null;
+
+  @BeforeClass
+  public static void beforeClass() throws Exception {
+
+    setupNoCoreTest(createTempDir(), null);
+
+    admin = new CoreAdminHandler(h.getCoreContainer());
+
+    admin.registerCustomActions(
+        Map.of(
+            "action1",
+            new CoreAdminHandlerTestAction1(),
+            "action2",
+            new CoreAdminHandlerTestAction2()));
+  }
+
+  @Test
+  @SuppressWarnings("unchecked")
+  public void testAction1() throws Exception {
+
+    SolrQueryResponse response = new SolrQueryResponse();
+
+    admin.handleRequestBody(req(CoreAdminParams.ACTION, "action1"), response);
+
+    Map<String, Object> actionResponse = ((NamedList<Object>) 
response.getResponse()).asMap();
+
+    assertTrue(
+        String.format(
+            Locale.ROOT,
+            "Action response should contain %s property",
+            CoreAdminHandlerTestAction1.PROPERTY_NAME),
+        actionResponse.containsKey(CoreAdminHandlerTestAction1.PROPERTY_NAME));
+    assertEquals(
+        String.format(
+            Locale.ROOT,
+            "Action response should contain %s value for %s property",
+            CoreAdminHandlerTestAction1.PROPERTY_VALUE,
+            CoreAdminHandlerTestAction1.PROPERTY_NAME),
+        CoreAdminHandlerTestAction1.PROPERTY_VALUE,
+        actionResponse.get(CoreAdminHandlerTestAction1.PROPERTY_NAME));
+  }
+
+  @Test
+  @SuppressWarnings("unchecked")
+  public void testAction2() throws Exception {
+
+    SolrQueryResponse response = new SolrQueryResponse();
+
+    admin.handleRequestBody(req(CoreAdminParams.ACTION, "action2"), response);
+
+    Map<String, Object> actionResponse = ((NamedList<Object>) 
response.getResponse()).asMap();
+
+    assertTrue(
+        String.format(
+            Locale.ROOT,
+            "Action response should contain %s property",
+            CoreAdminHandlerTestAction2.PROPERTY_NAME),
+        actionResponse.containsKey(CoreAdminHandlerTestAction2.PROPERTY_NAME));
+    assertEquals(
+        String.format(
+            Locale.ROOT,
+            "Action response should contain %s value for %s property",
+            CoreAdminHandlerTestAction2.PROPERTY_VALUE,
+            CoreAdminHandlerTestAction2.PROPERTY_NAME),
+        CoreAdminHandlerTestAction2.PROPERTY_VALUE,
+        actionResponse.get(CoreAdminHandlerTestAction2.PROPERTY_NAME));
+  }
+
+  @Test
+  public void testUnregisteredAction() throws Exception {
+
+    SolrQueryResponse response = new SolrQueryResponse();
+
+    assertThrows(
+        "Attempt to execute unregistered action should throw SolrException",
+        SolrException.class,
+        () -> admin.handleRequestBody(req(CoreAdminParams.ACTION, "action3"), 
response));
+  }
+
+  public static String randomString() {
+
+    int leftBound = 97; // a
+    int rightBound = 122; // z
+    int length = random().nextInt(6) + 5;
+
+    String randomString =
+        random()
+            .ints(leftBound, rightBound + 1)
+            .limit(length)
+            .collect(
+                () -> new StringBuilder(),
+                (item, value) -> item.appendCodePoint(value),
+                (item1, item2) -> item1.append(item2))
+            .toString();
+
+    return randomString;
+  }

Review Comment:
   Good question, not that I know of or remember seeing. I've usually "fudged" 
random test strings by taking a fixed text and then making it a bit random by 
appending some random digits, something like that.



##########
solr/core/src/test/org/apache/solr/handler/admin/CoreAdminHandlerActionTest.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.handler.admin;
+
+import java.util.Locale;
+import java.util.Map;
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.CoreAdminParams;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.SimpleOrderedMap;
+import org.apache.solr.handler.admin.CoreAdminHandler.CallInfo;
+import org.apache.solr.handler.admin.CoreAdminHandler.CoreAdminOp;
+import org.apache.solr.response.SolrQueryResponse;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class CoreAdminHandlerActionTest extends SolrTestCaseJ4 {
+
+  private static CoreAdminHandler admin = null;
+
+  @BeforeClass
+  public static void beforeClass() throws Exception {
+
+    setupNoCoreTest(createTempDir(), null);
+
+    admin = new CoreAdminHandler(h.getCoreContainer());
+
+    admin.registerCustomActions(
+        Map.of(
+            "action1",
+            new CoreAdminHandlerTestAction1(),
+            "action2",
+            new CoreAdminHandlerTestAction2()));
+  }
+
+  @Test
+  @SuppressWarnings("unchecked")
+  public void testAction1() throws Exception {
+
+    SolrQueryResponse response = new SolrQueryResponse();
+
+    admin.handleRequestBody(req(CoreAdminParams.ACTION, "action1"), response);
+
+    Map<String, Object> actionResponse = ((NamedList<Object>) 
response.getResponse()).asMap();
+
+    assertTrue(
+        String.format(
+            Locale.ROOT,
+            "Action response should contain %s property",
+            CoreAdminHandlerTestAction1.PROPERTY_NAME),
+        actionResponse.containsKey(CoreAdminHandlerTestAction1.PROPERTY_NAME));
+    assertEquals(
+        String.format(
+            Locale.ROOT,
+            "Action response should contain %s value for %s property",
+            CoreAdminHandlerTestAction1.PROPERTY_VALUE,
+            CoreAdminHandlerTestAction1.PROPERTY_NAME),
+        CoreAdminHandlerTestAction1.PROPERTY_VALUE,
+        actionResponse.get(CoreAdminHandlerTestAction1.PROPERTY_NAME));
+  }
+
+  @Test
+  @SuppressWarnings("unchecked")
+  public void testAction2() throws Exception {

Review Comment:
   subjective: testAction1 and testAction2 seem very similar, so could 
potentially reduce code duplication via an helper method e.g.
   
   ```
   public void testAction1()
   {
       implTestAction("action1", CoreAdminHandlerTestAction1.PROPERTY_VALUE, 
CoreAdminHandlerTestAction1.PROPERTY_NAME);
   }
   
   public void testAction2()
   {
       implTestAction("action2", CoreAdminHandlerTestAction2.PROPERTY_VALUE, 
CoreAdminHandlerTestAction2.PROPERTY_NAME);
   }
   ```



##########
solr/core/src/test/org/apache/solr/core/TestSolrXml.java:
##########
@@ -382,6 +405,26 @@ public void 
testFailAtConfigParseTimeWhenSolrConfigParamsAreDuplicated() {
     SolrXmlConfig.fromString(solrHome, solrXml); // return not used, only for 
validation
   }
 
+  public void 
testFailAtConfigParseTimeWhenCoreAdminHandlerActionsConfigParamsAreDuplicated() 
{
+    String v1 = "" + random().nextInt();
+    String v2 = "" + random().nextInt();
+    String solrXml =
+        String.format(
+            Locale.ROOT,
+            "<solr><coreAdminHandlerActions>"
+                + "<str name=\"action\">%s</str>"
+                + "<str name=\"action\">%s</str>"
+                + "</coreAdminHandlerActions></solr>",
+            v1,
+            v2);

Review Comment:
   minor: wondering if `%d` instead of `%s` can help avoid int-to-string 
conversion via `""` concatenation?
   ```suggestion
       String solrXml =
           String.format(
               Locale.ROOT,
               "<solr><coreAdminHandlerActions>"
                   + "<str name=\"action\">random%d</str>"
                   + "<str name=\"action\">random%d</str>"
                   + "</coreAdminHandlerActions></solr>",
               random().nextInt(),
               random().nextInt());
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to