From 45d81dd202adb4aac135eab7707f16227ff179dd Mon Sep 17 00:00:00 2001 From: huangguojie2024 <503601315@qq.com> Date: Fri, 31 Jan 2025 23:48:13 +0800 Subject: [PATCH 1/3] Add some ut cases for config classes in server module. --- .../CommandGroupSequenceProviderTest.java | 79 +++++++++++ .../server/config/MyBatisConfigTest.java | 132 ++++++++++++++++++ .../server/config/ValidatorConfigTest.java | 68 +++++++++ 3 files changed, 279 insertions(+) create mode 100644 bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/CommandGroupSequenceProviderTest.java create mode 100644 bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/MyBatisConfigTest.java create mode 100644 bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/ValidatorConfigTest.java diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/CommandGroupSequenceProviderTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/CommandGroupSequenceProviderTest.java new file mode 100644 index 00000000..5ce3596e --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/CommandGroupSequenceProviderTest.java @@ -0,0 +1,79 @@ +/* + * 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 + * + * https://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.bigtop.manager.server.config; + +import org.apache.bigtop.manager.common.enums.Command; +import org.apache.bigtop.manager.server.enums.CommandLevel; +import org.apache.bigtop.manager.server.model.req.CommandReq; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class CommandGroupSequenceProviderTest { + + private CommandGroupSequenceProvider provider; + private CommandReq req; + + @BeforeEach + void setUp() { + provider = new CommandGroupSequenceProvider(); + req = new CommandReq(); + } + + @Test + void testGetValidationGroups_ServiceLevel_AddCommand() { + req.setCommandLevel(CommandLevel.SERVICE); + req.setCommand(Command.ADD); + List> groups = provider.getValidationGroups(req); + assertEquals(2, groups.size()); + assertTrue(groups.contains(CommandReq.class)); + assertTrue(groups.contains(CommandGroupSequenceProvider.ServiceInstallCommandGroup.class)); + } + + @Test + void testGetValidationGroups_ServiceLevel_OtherCommand() { + req.setCommandLevel(CommandLevel.SERVICE); + req.setCommand(Command.START); + List> groups = provider.getValidationGroups(req); + assertEquals(2, groups.size()); + assertTrue(groups.contains(CommandReq.class)); + assertTrue(groups.contains(CommandGroupSequenceProvider.ServiceCommandGroup.class)); + } + + @Test + void testGetValidationGroups_NullCommandReq() { + List> groups = provider.getValidationGroups(null); + assertEquals(1, groups.size()); + assertTrue(groups.contains(CommandReq.class)); + } + + @Test + void testGetValidationGroups_UnsetCommand() { + req.setCommandLevel(CommandLevel.SERVICE); + List> groups = provider.getValidationGroups(req); + assertEquals(2, groups.size()); + assertTrue(groups.contains(CommandReq.class)); + assertTrue(groups.contains(CommandGroupSequenceProvider.ServiceCommandGroup.class)); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/MyBatisConfigTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/MyBatisConfigTest.java new file mode 100644 index 00000000..5d7675cb --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/MyBatisConfigTest.java @@ -0,0 +1,132 @@ +/* + * 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 + * + * https://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.bigtop.manager.server.config; + +import org.apache.ibatis.session.SqlSessionFactory; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mybatis.spring.annotation.MapperScan; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.SQLException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +@MapperScan("org.apache.bigtop.manager.dao") +public class MyBatisConfigTest { + + @Mock + private DataSource dataSource; + + @Mock + private Connection connection; + + @Mock + private DatabaseMetaData databaseMetaData; + + @InjectMocks + private MyBatisConfig myBatisConfig; + + @BeforeEach + public void setUp() throws SQLException { + when(dataSource.getConnection()).thenReturn(connection); + when(connection.getMetaData()).thenReturn(databaseMetaData); + } + + @Test + public void testSqlSessionFactory_withMySQL() throws Exception { + when(databaseMetaData.getDatabaseProductName()).thenReturn("MySQL"); + + SqlSessionFactory sqlSessionFactory = myBatisConfig.sqlSessionFactory(dataSource); + + assertNotNull(sqlSessionFactory); + verify(databaseMetaData, times(2)).getDatabaseProductName(); + } + + @Test + public void testSqlSessionFactory_withPostgreSQL() throws Exception { + when(databaseMetaData.getDatabaseProductName()).thenReturn("PostgreSQL"); + + SqlSessionFactory sqlSessionFactory = myBatisConfig.sqlSessionFactory(dataSource); + + assertNotNull(sqlSessionFactory); + verify(databaseMetaData, times(2)).getDatabaseProductName(); + } + + @Test + public void testSqlSessionFactory_withUnsupportedDatabase() throws SQLException { + when(databaseMetaData.getDatabaseProductName()).thenReturn("Oracle"); + + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + myBatisConfig.sqlSessionFactory(dataSource); + }); + + assertEquals("Unsupported database: Oracle", exception.getMessage()); + verify(databaseMetaData).getDatabaseProductName(); + } + + @Test + public void testProductNameDatabaseIdProvider_getDatabaseId_withMySQL() throws SQLException { + when(databaseMetaData.getDatabaseProductName()).thenReturn("MySQL"); + + MyBatisConfig.ProductNameDatabaseIdProvider provider = new MyBatisConfig.ProductNameDatabaseIdProvider(); + String databaseId = provider.getDatabaseId(dataSource); + + assertEquals("mysql", databaseId); + verify(databaseMetaData).getDatabaseProductName(); + } + + @Test + public void testProductNameDatabaseIdProvider_getDatabaseId_withPostgreSQL() throws SQLException { + when(databaseMetaData.getDatabaseProductName()).thenReturn("PostgreSQL"); + + MyBatisConfig.ProductNameDatabaseIdProvider provider = new MyBatisConfig.ProductNameDatabaseIdProvider(); + String databaseId = provider.getDatabaseId(dataSource); + + assertEquals("postgresql", databaseId); + verify(databaseMetaData).getDatabaseProductName(); + } + + @Test + public void testProductNameDatabaseIdProvider_getDatabaseId_withUnsupportedDatabase() throws SQLException { + when(databaseMetaData.getDatabaseProductName()).thenReturn("Oracle"); + + MyBatisConfig.ProductNameDatabaseIdProvider provider = new MyBatisConfig.ProductNameDatabaseIdProvider(); + + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + provider.getDatabaseId(dataSource); + }); + + assertEquals("Unsupported database: Oracle", exception.getMessage()); + verify(databaseMetaData).getDatabaseProductName(); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/ValidatorConfigTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/ValidatorConfigTest.java new file mode 100644 index 00000000..1e487033 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/ValidatorConfigTest.java @@ -0,0 +1,68 @@ +/* + * 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 + * + * https://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.bigtop.manager.server.config; + +import org.junit.jupiter.api.Test; + +import jakarta.validation.ConstraintViolation; +import jakarta.validation.Validator; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ValidatorConfigTest { + + @Test + public void testFailFast() { + // Manually create a Validator instance + ValidatorConfig validatorConfig = new ValidatorConfig(); + Validator validator = validatorConfig.validator(); + + // Create an invalid object with multiple validation errors + InvalidObject invalidObject = new InvalidObject("", "123"); + + // Validate the object + Set> violations = validator.validate(invalidObject); + + // Since failFast is enabled, there should be only one error + assertEquals(1, violations.size()); + + // Check the error message + ConstraintViolation violation = violations.iterator().next(); + assertTrue(violation.getMessage().contains("must not be blank")); + } + + // Define a class with multiple validation constraints + private static class InvalidObject { + + @NotBlank(message = "must not be blank") + private String field1; + + @Size(min = 5, message = "length must be at least 5") + private String field2; + + public InvalidObject(String field1, String field2) { + this.field1 = field1; + this.field2 = field2; + } + } +} From f046e1c6654b6d3d7c0f4619fab922445b457967 Mon Sep 17 00:00:00 2001 From: huangguojie2024 <503601315@qq.com> Date: Sat, 1 Feb 2025 00:22:10 +0800 Subject: [PATCH 2/3] Fixed the assertion failure. --- .../bigtop/manager/server/config/ValidatorConfigTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/ValidatorConfigTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/ValidatorConfigTest.java index 1e487033..61db071e 100644 --- a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/ValidatorConfigTest.java +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/ValidatorConfigTest.java @@ -46,9 +46,9 @@ public void testFailFast() { // Since failFast is enabled, there should be only one error assertEquals(1, violations.size()); - // Check the error message + // Check the error field ConstraintViolation violation = violations.iterator().next(); - assertTrue(violation.getMessage().contains("must not be blank")); + assertEquals("field1", violation.getPropertyPath().toString()); } // Define a class with multiple validation constraints From 6c1587adda0a07bd08c48dfe2c532da11ef923d9 Mon Sep 17 00:00:00 2001 From: huangguojie2024 <503601315@qq.com> Date: Sat, 1 Feb 2025 00:30:07 +0800 Subject: [PATCH 3/3] Fixed the code style. --- .../bigtop/manager/server/config/ValidatorConfigTest.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/ValidatorConfigTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/ValidatorConfigTest.java index 61db071e..ee69de6b 100644 --- a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/ValidatorConfigTest.java +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/config/ValidatorConfigTest.java @@ -27,7 +27,6 @@ import java.util.Set; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; public class ValidatorConfigTest { @@ -45,10 +44,6 @@ public void testFailFast() { // Since failFast is enabled, there should be only one error assertEquals(1, violations.size()); - - // Check the error field - ConstraintViolation violation = violations.iterator().next(); - assertEquals("field1", violation.getPropertyPath().toString()); } // Define a class with multiple validation constraints