Skip to content

Commit

Permalink
BIGTOP-4337: Add some unit tests for dao module (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
xianrenzw authored Jan 29, 2025
1 parent 91c516e commit eae667c
Show file tree
Hide file tree
Showing 5 changed files with 387 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.dao.converter;

import org.apache.bigtop.manager.common.enums.Command;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

class CommandConverterTest {

private final CommandConverter converter = new CommandConverter();

@Test
void testConvertToDatabaseColumn() {
assertEquals("Add", converter.convertToDatabaseColumn(Command.ADD));
assertEquals("Start", converter.convertToDatabaseColumn(Command.START));
assertEquals("Stop", converter.convertToDatabaseColumn(Command.STOP));
assertEquals("Restart", converter.convertToDatabaseColumn(Command.RESTART));
assertEquals("Check", converter.convertToDatabaseColumn(Command.CHECK));
assertEquals("Configure", converter.convertToDatabaseColumn(Command.CONFIGURE));
assertEquals("Custom", converter.convertToDatabaseColumn(Command.CUSTOM));
assertEquals("Init", converter.convertToDatabaseColumn(Command.INIT));
assertEquals("Prepare", converter.convertToDatabaseColumn(Command.PREPARE));
assertEquals("Status", converter.convertToDatabaseColumn(Command.STATUS));

assertNull(converter.convertToDatabaseColumn(null));
}

@Test
void testConvertToEntityAttribute() {
assertEquals(Command.ADD, converter.convertToEntityAttribute("add"));
assertEquals(Command.START, converter.convertToEntityAttribute("start"));
assertEquals(Command.STOP, converter.convertToEntityAttribute("stop"));
assertEquals(Command.RESTART, converter.convertToEntityAttribute("restart"));
assertEquals(Command.CHECK, converter.convertToEntityAttribute("check"));
assertEquals(Command.CONFIGURE, converter.convertToEntityAttribute("configure"));
assertEquals(Command.CUSTOM, converter.convertToEntityAttribute("custom"));
assertEquals(Command.INIT, converter.convertToEntityAttribute("init"));
assertEquals(Command.PREPARE, converter.convertToEntityAttribute("prepare"));
assertEquals(Command.STATUS, converter.convertToEntityAttribute("status"));

assertNull(converter.convertToEntityAttribute(null));

assertEquals(Command.ADD, converter.convertToEntityAttribute("ADD"));
assertEquals(Command.START, converter.convertToEntityAttribute("START"));
assertEquals(Command.STOP, converter.convertToEntityAttribute("STOP"));
}

@Test
void testConvertToEntityAttributeInvalidValue() {
assertThrows(IllegalArgumentException.class, () -> converter.convertToEntityAttribute("invalid"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.dao.converter;

import org.apache.bigtop.manager.common.enums.JobState;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

class JobStateConverterTest {

private final JobStateConverter converter = new JobStateConverter();

@Test
void testConvertToDatabaseColumn() {
assertEquals("Pending", converter.convertToDatabaseColumn(JobState.PENDING));
assertEquals("Processing", converter.convertToDatabaseColumn(JobState.PROCESSING));
assertEquals("Successful", converter.convertToDatabaseColumn(JobState.SUCCESSFUL));
assertEquals("Failed", converter.convertToDatabaseColumn(JobState.FAILED));
assertEquals("Canceled", converter.convertToDatabaseColumn(JobState.CANCELED));

assertNull(converter.convertToDatabaseColumn(null));
}

@Test
void testConvertToEntityAttribute() {
assertEquals(JobState.PENDING, converter.convertToEntityAttribute("pending"));
assertEquals(JobState.PROCESSING, converter.convertToEntityAttribute("processing"));
assertEquals(JobState.SUCCESSFUL, converter.convertToEntityAttribute("successful"));
assertEquals(JobState.FAILED, converter.convertToEntityAttribute("failed"));
assertEquals(JobState.CANCELED, converter.convertToEntityAttribute("canceled"));

assertNull(converter.convertToEntityAttribute(null));

assertEquals(JobState.PENDING, converter.convertToEntityAttribute("PENDING"));
assertEquals(JobState.FAILED, converter.convertToEntityAttribute("FAILED"));
}

@Test
void testConvertToEntityAttributeInvalidValue() {
assertThrows(IllegalArgumentException.class, () -> converter.convertToEntityAttribute("invalid"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.dao.converter;

import org.apache.bigtop.manager.common.enums.MaintainState;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

class MaintainStateConverterTest {

private final MaintainStateConverter converter = new MaintainStateConverter();

@Test
void testConvertToDatabaseColumn() {
assertEquals("Uninstalled", converter.convertToDatabaseColumn(MaintainState.UNINSTALLED));
assertEquals("Installed", converter.convertToDatabaseColumn(MaintainState.INSTALLED));
assertEquals("Maintained", converter.convertToDatabaseColumn(MaintainState.MAINTAINED));
assertEquals("Started", converter.convertToDatabaseColumn(MaintainState.STARTED));
assertEquals("Stopped", converter.convertToDatabaseColumn(MaintainState.STOPPED));

assertNull(converter.convertToDatabaseColumn(null));
}

@Test
void testConvertToEntityAttribute() {
assertEquals(MaintainState.UNINSTALLED, converter.convertToEntityAttribute("uninstalled"));
assertEquals(MaintainState.INSTALLED, converter.convertToEntityAttribute("installed"));
assertEquals(MaintainState.MAINTAINED, converter.convertToEntityAttribute("maintained"));
assertEquals(MaintainState.STARTED, converter.convertToEntityAttribute("started"));
assertEquals(MaintainState.STOPPED, converter.convertToEntityAttribute("stopped"));

assertNull(converter.convertToEntityAttribute(null));

assertEquals(MaintainState.UNINSTALLED, converter.convertToEntityAttribute("UNINSTALLED"));
assertEquals(MaintainState.STARTED, converter.convertToEntityAttribute("STARTED"));
}

@Test
void testConvertToEntityAttributeInvalidValue() {
assertThrows(IllegalArgumentException.class, () -> converter.convertToEntityAttribute("invalid"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.dao.enums;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class DBTypeTest {

@Test
public void testToTypeMysql() {
String databaseId = "mysql";
DBType expected = DBType.MYSQL;
DBType result = DBType.toType(databaseId);
assertEquals(expected, result);
}

@Test
public void testToTypePostgresql() {
String databaseId = "postgresql";
DBType expected = DBType.POSTGRESQL;
DBType result = DBType.toType(databaseId);
assertEquals(expected, result);
}

@Test
public void testToTypeDm() {
String databaseId = "dm";
DBType expected = DBType.DM;
DBType result = DBType.toType(databaseId);
assertEquals(expected, result);
}

@Test
public void testToTypeUnsupportedDatabase() {
String databaseId = "unsupported";
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
DBType.toType(databaseId);
});
assertEquals("Unsupported database: unsupported", exception.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* 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.dao.interceptor;

import org.apache.bigtop.manager.dao.annotations.CreateBy;
import org.apache.bigtop.manager.dao.annotations.CreateTime;
import org.apache.bigtop.manager.dao.annotations.UpdateBy;
import org.apache.bigtop.manager.dao.annotations.UpdateTime;

import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Invocation;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import lombok.Getter;

import java.sql.Timestamp;
import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class AuditingInterceptorTest {

private AuditingInterceptor auditingInterceptor;

@BeforeEach
void setUp() {
Supplier<Long> currentUserSupplier = () -> 1L; // Mock current user ID
auditingInterceptor = new AuditingInterceptor(currentUserSupplier);
}

@Test
void testInterceptWithInsertCommand() throws Throwable {
// Mock MappedStatement and Invocation
MappedStatement mappedStatement = mock(MappedStatement.class);
when(mappedStatement.getSqlCommandType()).thenReturn(SqlCommandType.INSERT);

TestEntity entity = new TestEntity(); // Test entity with audit fields
Invocation invocation = mockInvocation(mappedStatement, entity);

// Execute intercept method
auditingInterceptor.intercept(invocation);

// Assert audit fields are set
assertEquals(1L, entity.getCreateBy());
assertEquals(1L, entity.getUpdateBy());
assertEquals(entity.getCreateTime(), entity.getUpdateTime());
}

@Test
void testInterceptWithUpdateCommand() throws Throwable {
Timestamp now = new Timestamp(System.currentTimeMillis());
// Mock MappedStatement and Invocation
MappedStatement mappedStatement = mock(MappedStatement.class);
when(mappedStatement.getSqlCommandType()).thenReturn(SqlCommandType.UPDATE);

TestEntity entity = new TestEntity(); // Test entity with audit fields
Invocation invocation = mockInvocation(mappedStatement, entity);

// Execute intercept method
auditingInterceptor.intercept(invocation);

// Assert update fields are set
assertEquals(1L, entity.getUpdateBy());
assertTrue(now.getTime() <= entity.getUpdateTime().getTime());
}

@Test
void testInterceptWithNonInsertOrUpdate() throws Throwable {
// Mock MappedStatement and Invocation
MappedStatement mappedStatement = mock(MappedStatement.class);
when(mappedStatement.getSqlCommandType()).thenReturn(SqlCommandType.DELETE);

TestEntity entity = new TestEntity();
Invocation invocation = mockInvocation(mappedStatement, entity);

// Execute intercept method
auditingInterceptor.intercept(invocation);

// Assert no changes to entity
assertNull(entity.getCreateBy());
assertNull(entity.getUpdateBy());
assertNull(entity.getCreateTime());
assertNull(entity.getUpdateTime());
}

private Invocation mockInvocation(MappedStatement mappedStatement, Object parameter) throws Throwable {
Invocation invocation = mock(Invocation.class);
when(invocation.getArgs()).thenReturn(new Object[] {mappedStatement, parameter});
when(invocation.proceed()).thenReturn(null);
return invocation;
}

@Getter
static class TestEntity {
@CreateBy
private Long createBy;

@CreateTime
private Timestamp createTime;

@UpdateBy
private Long updateBy;

@UpdateTime
private Timestamp updateTime;
}
}

0 comments on commit eae667c

Please sign in to comment.