amrishlal commented on code in PR #8965: URL: https://github.com/apache/pinot/pull/8965#discussion_r907788778
########## pinot-controller/src/test/java/org/apache/pinot/controller/recommender/data/generator/JsonGeneratorTest.java: ########## @@ -0,0 +1,59 @@ +/** + * 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.pinot.controller.recommender.data.generator; + +import java.io.IOException; +import org.apache.pinot.spi.utils.JsonUtils; +import org.testng.Assert; +import org.testng.annotations.Test; + + +public class JsonGeneratorTest { + @Test + public void testNext() + throws IOException { + // JsonGenerator generates empty json when size is less than JsonGenerator.DEFAULT_JSON_ELEMENT_LENGTH) + JsonGenerator jsonGenerator1 = new JsonGenerator(0); + Assert.assertEquals(jsonGenerator1.next(), "{}"); + + JsonGenerator jsonGenerator2 = new JsonGenerator(4); + Assert.assertEquals(jsonGenerator2.next(), "{}"); + + JsonGenerator jsonGenerator3 = new JsonGenerator(8); + checkJson((String) jsonGenerator3.next(), 8); + + JsonGenerator jsonGenerator4 = new JsonGenerator(20); + checkJson((String) jsonGenerator4.next(), 20); + } + + public static void checkJson(String jsonString, int desiredLength) + throws IOException { + + // Number of expected elements in the generated JSON string + int elementCount = desiredLength / JsonGenerator.DEFAULT_JSON_ELEMENT_LENGTH; + + // Make sure we are generating JSON string that is close to the desired length. Length of JSON string should be 2 + // (length of opening and closing parentheses) + number of commas + size of all the elements. + Assert.assertEquals(jsonString.length(), + "{}".length() + (elementCount - 1) + elementCount * JsonGenerator.DEFAULT_JSON_ELEMENT_LENGTH); + + // Check if json string is valid json. + JsonUtils.stringToJsonNode(jsonString); Review Comment: Fixed. -- 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: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org