Skip to content

Commit

Permalink
BIGTOP-4342: Add some ut cases for config classes in server module (#162
Browse files Browse the repository at this point in the history
)
  • Loading branch information
xianrenzw authored Feb 4, 2025
1 parent eae667c commit f12ae98
Show file tree
Hide file tree
Showing 3 changed files with 274 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Class<?>> 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<Class<?>> groups = provider.getValidationGroups(req);
assertEquals(2, groups.size());
assertTrue(groups.contains(CommandReq.class));
assertTrue(groups.contains(CommandGroupSequenceProvider.ServiceCommandGroup.class));
}

@Test
void testGetValidationGroups_NullCommandReq() {
List<Class<?>> groups = provider.getValidationGroups(null);
assertEquals(1, groups.size());
assertTrue(groups.contains(CommandReq.class));
}

@Test
void testGetValidationGroups_UnsetCommand() {
req.setCommandLevel(CommandLevel.SERVICE);
List<Class<?>> groups = provider.getValidationGroups(req);
assertEquals(2, groups.size());
assertTrue(groups.contains(CommandReq.class));
assertTrue(groups.contains(CommandGroupSequenceProvider.ServiceCommandGroup.class));
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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;

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<ConstraintViolation<InvalidObject>> violations = validator.validate(invalidObject);

// Since failFast is enabled, there should be only one error
assertEquals(1, violations.size());
}

// 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;
}
}
}

0 comments on commit f12ae98

Please sign in to comment.