blob: bd4eceb56167c8f3b10a0a444bec9ddad5333600 [file] [log] [blame]
/*
* 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.schema;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.solr.SolrTestUtil;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudHttp2SolrClient;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.client.solrj.request.schema.SchemaRequest;
import org.apache.solr.client.solrj.response.CollectionAdminResponse;
import org.apache.solr.client.solrj.response.schema.SchemaResponse;
import org.apache.solr.cloud.SolrCloudTestCase;
import org.apache.solr.common.SolrInputDocument;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestManagedSchemaAPI extends SolrCloudTestCase {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
@BeforeClass
public static void beforeTestManagedSchemaAPI() throws Exception {
System.setProperty("managed.schema.mutable", "true");
configureCluster(2)
.addConfig("conf1", SolrTestUtil.TEST_PATH().resolve("configsets").resolve("cloud-managed").resolve("conf"))
.configure();
}
@AfterClass
public static void afterTestManagedSchemaAPI() throws Exception {
shutdownCluster();
}
@Test
public void test() throws Exception {
String collection = "testschemaapi";
CollectionAdminRequest.createCollection(collection, "conf1", 1, 2).process(cluster.getSolrClient());
cluster.getSolrClient().getZkStateReader().waitForActiveCollection(cluster.getSolrClient().getHttpClient(), collection, 5, TimeUnit.SECONDS, false, 1, 2, true, true);
testModifyField(collection);
testReloadAndAddSimple(collection);
testAddFieldAndDocument(collection);
}
private void testReloadAndAddSimple(String collection) throws IOException, SolrServerException {
CloudHttp2SolrClient cloudClient = cluster.getSolrClient();
String fieldName = "myNewField";
addStringField(fieldName, collection, cloudClient);
CollectionAdminRequest.Reload reloadRequest = CollectionAdminRequest.reloadCollection(collection);
CollectionAdminResponse response = reloadRequest.process(cloudClient);
assertEquals(0, response.getStatus());
assertTrue(response.isSuccess());
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "1");
doc.addField(fieldName, "val");
UpdateRequest ureq = new UpdateRequest().add(doc);
cloudClient.request(ureq, collection);
}
private void testAddFieldAndDocument(String collection) throws IOException, SolrServerException {
CloudHttp2SolrClient cloudClient = cluster.getSolrClient();
String fieldName = "myNewField1";
addStringField(fieldName, collection, cloudClient);
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "2");
doc.addField(fieldName, "val1");
UpdateRequest ureq = new UpdateRequest().add(doc);
cloudClient.request(ureq, collection);;
}
private void addStringField(String fieldName, String collection, CloudHttp2SolrClient cloudClient) throws IOException, SolrServerException {
Map<String, Object> fieldAttributes = new LinkedHashMap<>();
fieldAttributes.put("name", fieldName);
fieldAttributes.put("type", "string");
SchemaRequest.AddField addFieldUpdateSchemaRequest = new SchemaRequest.AddField(fieldAttributes);
SchemaResponse.UpdateResponse addFieldResponse = addFieldUpdateSchemaRequest.process(cloudClient, collection);
assertEquals(0, addFieldResponse.getStatus());
assertNull(addFieldResponse.getResponse().get("errors"));
log.info("added new field={}", fieldName);
}
private void testModifyField(String collection) throws IOException, SolrServerException {
CloudHttp2SolrClient cloudClient = cluster.getSolrClient();
SolrInputDocument doc = new SolrInputDocument("id", "3");
cloudClient.add(collection, doc);
cloudClient.commit(collection);
String fieldName = "id";
SchemaRequest.Field getFieldRequest = new SchemaRequest.Field(fieldName);
SchemaResponse.FieldResponse getFieldResponse = getFieldRequest.process(cloudClient, collection);
Map<String, Object> field = getFieldResponse.getField();
field.put("docValues", true);
SchemaRequest.ReplaceField replaceRequest = new SchemaRequest.ReplaceField(field);
SchemaResponse.UpdateResponse replaceResponse = replaceRequest.process(cloudClient, collection);
assertNull(replaceResponse.getResponse().get("errors"));
CollectionAdminRequest.Reload reloadRequest = CollectionAdminRequest.reloadCollection(collection);
CollectionAdminResponse response = reloadRequest.process(cloudClient);
assertEquals(0, response.getStatus());
assertTrue(response.isSuccess());
}
}