isaric commented on code in PR #5311: URL: https://github.com/apache/solr/pull/5311#discussion_r4540399422
########## solr/solrj/src/test/org/apache/solr/client/solrj/request/schema/SchemaRequestBodyTest.java: ########## @@ -0,0 +1,109 @@ +/* + * 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.client.solrj.request.schema; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import org.apache.solr.SolrTestCase; +import org.junit.Test; + +/** Pins the JSON bodies that {@link SchemaRequest.Update} requests send to the Schema API. */ +public class SchemaRequestBodyTest extends SolrTestCase { + + @Test + public void testAddFieldTypeBody() throws IOException { + FieldTypeDefinition fieldType = new FieldTypeDefinition(); + fieldType.setAttributes(map("name", "text_test", "class", "solr.TextField")); + AnalyzerDefinition analyzer = new AnalyzerDefinition(); + analyzer.setAttributes(map("type", "index")); + analyzer.setCharFilters(List.of(map("class", "solr.HTMLStripCharFilterFactory"))); + analyzer.setTokenizer(map("class", "solr.StandardTokenizerFactory")); + analyzer.setFilters( + List.of( + map("class", "solr.LowerCaseFilterFactory"), + map("class", "solr.ASCIIFoldingFilterFactory"))); + fieldType.setIndexAnalyzer(analyzer); + fieldType.setSimilarity(map("class", "solr.BM25SimilarityFactory", "k1", 1.2)); + + assertJSONEquals( + """ + { + "add-field-type": { + "name": "text_test", + "class": "solr.TextField", + "indexAnalyzer": { + "type": "index", + "charFilters": [{"class": "solr.HTMLStripCharFilterFactory"}], + "tokenizer": {"class": "solr.StandardTokenizerFactory"}, + "filters": [ + {"class": "solr.LowerCaseFilterFactory"}, + {"class": "solr.ASCIIFoldingFilterFactory"} + ] + }, + "similarity": {"class": "solr.BM25SimilarityFactory", "k1": 1.2} + } + } + """, + body(new SchemaRequest.AddFieldType(fieldType))); + } + + @Test + public void testAddFieldBody() throws IOException { + assertJSONEquals( + """ + {"add-field": {"name": "f1", "type": "string", "stored": true}} + """, + body(new SchemaRequest.AddField(map("name", "f1", "type", "string", "stored", true)))); + } + + @Test + public void testMultiUpdateRepeatsCommandKeys() throws IOException { + SchemaRequest.MultiUpdate multiUpdate = + new SchemaRequest.MultiUpdate( + List.of( + new SchemaRequest.AddField(map("name", "f1", "type", "string")), + new SchemaRequest.AddField(map("name", "f2", "type", "string")), + new SchemaRequest.DeleteField("f0"))); + + // The Schema API takes several commands of the same kind in one object, so the body must keep + // the repeated key. Compared as text: a JSON parser would collapse the duplicate. + assertEquals( + """ + {"add-field":{"name":"f1","type":"string"},\ + "add-field":{"name":"f2","type":"string"},\ Review Comment: Agreed it's out of scope here. I filed SOLR-18641 for switching MultiUpdate to the grouped list form, linked to SOLR-18452. -- 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]
