XBaith commented on code in PR #3473: URL: https://github.com/apache/amoro/pull/3473#discussion_r1995108105
########## amoro-ams/src/test/java/org/apache/amoro/server/util/TestValidateGroupName.java: ########## @@ -0,0 +1,67 @@ +/* + * 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.amoro.server.util; + +import org.apache.amoro.server.dashboard.controller.OptimizerGroupController; +import org.junit.Assert; +import org.junit.Test; + +public class TestValidateGroupName { Review Comment: We can mock the group controller in right test package path. For example: ```suggestion class TestOptimizerGroupController { private TableManager tableManager; private DefaultOptimizingService optimizingService; private OptimizerManager optimizerManager; private OptimizerGroupController controller; private Context ctx; @BeforeEach void setUp() { tableManager = mock(TableManager.class); optimizingService = mock(DefaultOptimizingService.class); optimizerManager = mock(OptimizerManager.class); controller = new OptimizerGroupController(tableManager, optimizingService, optimizerManager); ctx = mock(Context.class); } private static Stream<String> invalidGroupNames() { return Stream.of( "invalid group name!", "invalidGroupName!", "invalid group name", "invalidGroupName\t", "invalidGroupName\n" ); } @ParameterizedTest @MethodSource("invalidGroupNames") void createInvalidGroupName(String groupName) { Map<String, Object> requestBody = new HashMap<>(); requestBody.put("name", groupName); requestBody.put("container", "Container"); requestBody.put("properties", new HashMap<String, String>()); when(ctx.bodyAsClass(Map.class)).thenReturn(requestBody); assertThrows(BadRequestException.class, () -> controller.createResourceGroup(ctx)); } ``` ########## amoro-ams/src/main/java/org/apache/amoro/server/dashboard/controller/OptimizerGroupController.java: ########## @@ -308,4 +315,8 @@ public void getContainers(Context ctx) { .map(ContainerMetadata::getName) .collect(Collectors.toList()))); } + + public static boolean validateGroupName(String groupName) { Review Comment: I thought of a more concise way, which is to put the group name pattern and existence in `validateGroupName` and throw an exception when appropriate. ```suggestion private void validateGroupName(String groupName) { // validate name pattern if (!GROUP_NAME_PATTERN.matcher(groupName).matches()) { throw new BadRequestException(...) } if (optimizerManager.getResourceGroup(name) != null) { throw new BadRequestException(String.format("Optimizer group:%s already existed.", name)); } } ``` -- 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]
