From 85e50d7d03c658ba6d6e3eede64e9145e2ae845e Mon Sep 17 00:00:00 2001 From: Carlos Ugarte Date: Tue, 22 Oct 2024 17:53:36 -0400 Subject: [PATCH] v2 JAX-RS ClusterProp API unit tests. --- .../admin/api/ClusterPropsAPITest.java | 178 ++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 solr/core/src/test/org/apache/solr/handler/admin/api/ClusterPropsAPITest.java diff --git a/solr/core/src/test/org/apache/solr/handler/admin/api/ClusterPropsAPITest.java b/solr/core/src/test/org/apache/solr/handler/admin/api/ClusterPropsAPITest.java new file mode 100644 index 00000000000..89ff8c8d8b4 --- /dev/null +++ b/solr/core/src/test/org/apache/solr/handler/admin/api/ClusterPropsAPITest.java @@ -0,0 +1,178 @@ +/* + * 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.api; + +import static org.apache.solr.common.util.Utils.getObjectByPath; + +import java.net.URL; +import java.util.List; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.entity.StringEntity; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.apache.solr.cloud.SolrCloudTestCase; +import org.apache.solr.common.util.Utils; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ClusterPropsAPITest extends SolrCloudTestCase { + + private URL baseUrl; + private String baseUrlV2ClusterProps; + + private static final String testClusterProperty = "ext.test"; + private static final String testClusterPropertyValue = "test value"; + private static final String testClusterPropertyNestedKeyAndValue = + " \"defaults\": {" + + " \"collection\": {" + + " \"numShards\": 4," + + " \"nrtReplicas\": 2," + + " \"tlogReplicas\": 2," + + " \"pullReplicas\": 2" + + " }" + + " }"; + private static final String testClusterPropertyBulkAndNestedValues = + "{ \"properties\": {" + + testClusterPropertyNestedKeyAndValue + + "," + + " \"" + + testClusterProperty + + "\": " + + "\"" + + testClusterPropertyValue + + "\"" + + " } }"; + + @BeforeClass + public static void setupCluster() throws Exception { + configureCluster(1).addConfig("conf", configset("cloud-minimal")).configure(); + } + + @Before + @Override + public void setUp() throws Exception { + super.setUp(); + + baseUrl = cluster.getJettySolrRunner(0).getBaseUrl(); + baseUrlV2ClusterProps = + cluster.getJettySolrRunner(0).getBaseURLV2().toString() + "/cluster/properties"; + } + + @After + @Override + public void tearDown() throws Exception { + super.tearDown(); + } + + @Test + public void testClusterPropertyOpsAllGood() throws Exception { + try (HttpSolrClient client = new HttpSolrClient.Builder(baseUrl.toString()).build()) { + // List Properties, confirm there aren't any + Object o = + Utils.executeGET(client.getHttpClient(), baseUrlV2ClusterProps, Utils.JSONCONSUMER); + assertNotNull(o); + assertEquals(0, ((List) getObjectByPath(o, true, "clusterProperties")).size()); + + // Create a single cluster property + String path = baseUrlV2ClusterProps + "/" + testClusterProperty; + HttpPut httpPut = new HttpPut(path); + httpPut.setHeader("Content-Type", "application/json"); + httpPut.setEntity(new StringEntity("{\"value\":\"" + testClusterPropertyValue + "\"}")); + o = Utils.executeHttpMethod(client.getHttpClient(), path, Utils.JSONCONSUMER, httpPut); + assertNotNull(o); + + // List Properties, this time there should be 1 + o = Utils.executeGET(client.getHttpClient(), baseUrlV2ClusterProps, Utils.JSONCONSUMER); + assertNotNull(o); + assertEquals(1, ((List) getObjectByPath(o, true, "clusterProperties")).size()); + assertEquals( + testClusterProperty, + (String) ((List) getObjectByPath(o, true, "clusterProperties")).get(0)); + + // Fetch Cluster Property + // Same path as used in the Create step above + o = Utils.executeGET(client.getHttpClient(), path, Utils.JSONCONSUMER); + assertNotNull(o); + assertEquals(testClusterProperty, (String) getObjectByPath(o, true, "clusterProperty/name")); + assertEquals( + testClusterPropertyValue, (String) getObjectByPath(o, true, "clusterProperty/value")); + + // Delete Cluster Property + // Same path as used in the Create step above + HttpDelete httpDelete = new HttpDelete(path); + o = Utils.executeHttpMethod(client.getHttpClient(), path, Utils.JSONCONSUMER, httpDelete); + assertNotNull(o); + + // List Properties, should be back to 0 + o = Utils.executeGET(client.getHttpClient(), baseUrlV2ClusterProps, Utils.JSONCONSUMER); + assertNotNull(o); + assertEquals(0, ((List) getObjectByPath(o, true, "clusterProperties")).size()); + } + } + + @Test + public void testClusterPropertyNestedBulkSet() throws Exception { + try (HttpSolrClient client = new HttpSolrClient.Builder(baseUrl.toString()).build()) { + // Create a single cluster property using the Bulk/Nested set ClusterProp API + HttpPut httpPut = new HttpPut(baseUrlV2ClusterProps); + httpPut.setHeader("Content-Type", "application/json"); + httpPut.setEntity(new StringEntity(testClusterPropertyBulkAndNestedValues)); + Object o = + Utils.executeHttpMethod( + client.getHttpClient(), baseUrlV2ClusterProps, Utils.JSONCONSUMER, httpPut); + assertNotNull(o); + + // Fetch Cluster Property checking the not-nested property set above + String path = baseUrlV2ClusterProps + "/" + testClusterProperty; + o = Utils.executeGET(client.getHttpClient(), path, Utils.JSONCONSUMER); + assertNotNull(o); + assertEquals(testClusterProperty, (String) getObjectByPath(o, true, "clusterProperty/name")); + assertEquals( + testClusterPropertyValue, (String) getObjectByPath(o, true, "clusterProperty/value")); + + // Fetch Cluster Property checking the nested property set above + path = baseUrlV2ClusterProps + "/" + "defaults"; + o = Utils.executeGET(client.getHttpClient(), path, Utils.JSONCONSUMER); + assertNotNull(o); + assertEquals("defaults", (String) getObjectByPath(o, true, "clusterProperty/name")); + assertEquals(4L, getObjectByPath(o, true, "clusterProperty/value/collection/numShards")); + + // Clean up to leave the state unchanged + HttpDelete httpDelete = new HttpDelete(path); + Utils.executeHttpMethod(client.getHttpClient(), path, Utils.JSONCONSUMER, httpDelete); + path = baseUrlV2ClusterProps + "/" + testClusterProperty; + httpDelete = new HttpDelete(path); + Utils.executeHttpMethod(client.getHttpClient(), path, Utils.JSONCONSUMER, httpDelete); + } + } + + @Test + public void testClusterPropertyFetchNonExistentProperty() throws Exception { + try (HttpSolrClient client = new HttpSolrClient.Builder(baseUrl.toString()).build()) { + // Fetch Cluster Property that doesn't exist + String path = baseUrlV2ClusterProps + "/ext.clusterPropThatDoesNotExist"; + HttpGet fetchClusterPropertyGet = new HttpGet(path); + HttpResponse httpResponse = client.getHttpClient().execute(fetchClusterPropertyGet); + assertEquals(400, httpResponse.getStatusLine().getStatusCode()); + } + } +}