Skip to content

Commit

Permalink
BIGTOP-4346: Add ut cases for enums classes in server module (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
xianrenzw authored Feb 8, 2025
1 parent e772d20 commit d3f6c2f
Show file tree
Hide file tree
Showing 8 changed files with 554 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.enums;

import org.apache.bigtop.manager.server.utils.MessageSourceUtils;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mockStatic;

@ExtendWith(MockitoExtension.class)
class ApiExceptionEnumTest {

@AfterEach
void tearDown() {
for (ApiExceptionEnum exception : ApiExceptionEnum.values()) {
exception.setArgs(null);
}
}

@Test
void testEnumProperties() {
assertEquals(10000, ApiExceptionEnum.NEED_LOGIN.getCode());
assertEquals(LocaleKeys.LOGIN_REQUIRED, ApiExceptionEnum.NEED_LOGIN.getKey());

assertEquals(11000, ApiExceptionEnum.CLUSTER_NOT_FOUND.getCode());
assertEquals(LocaleKeys.CLUSTER_NOT_FOUND, ApiExceptionEnum.CLUSTER_NOT_FOUND.getKey());

assertEquals(12000, ApiExceptionEnum.HOST_NOT_FOUND.getCode());
assertEquals(LocaleKeys.HOST_NOT_FOUND, ApiExceptionEnum.HOST_NOT_FOUND.getKey());
}

@Test
void testGetMessageWithoutArgs() {
try (MockedStatic<MessageSourceUtils> mocked = mockStatic(MessageSourceUtils.class)) {
mocked.when(() -> MessageSourceUtils.getMessage(LocaleKeys.CLUSTER_NOT_FOUND))
.thenReturn("Cluster not found");

String message = ApiExceptionEnum.CLUSTER_NOT_FOUND.getMessage();
assertEquals("Cluster not found", message);
}
}

@Test
void testGetMessageWithArgs() {
try (MockedStatic<MessageSourceUtils> mocked = mockStatic(MessageSourceUtils.class)) {
String[] args = {"example.txt"};
mocked.when(() -> MessageSourceUtils.getMessage(LocaleKeys.FILE_UPLOAD_FAILED, args))
.thenReturn("File upload failed: example.txt");

ApiExceptionEnum.FILE_UPLOAD_FAILED.setArgs(args);
String message = ApiExceptionEnum.FILE_UPLOAD_FAILED.getMessage();
assertEquals("File upload failed: example.txt", message);
}
}

@Test
void testSetArgs() {
String[] args = {"testArg"};
ApiExceptionEnum.FILE_UPLOAD_FAILED.setArgs(args);
assertArrayEquals(args, ApiExceptionEnum.FILE_UPLOAD_FAILED.getArgs());
}

@Test
void testNoArgsAfterReset() {
ApiExceptionEnum.FILE_UPLOAD_FAILED.setArgs(new String[] {"temp"});
tearDown();
assertNull(ApiExceptionEnum.FILE_UPLOAD_FAILED.getArgs());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.enums;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AuthPlatformStatusTest {

@Test
// Normal path test: Check if the code for each status is correct
public void testStatusCodes() {
assertEquals(1, AuthPlatformStatus.ACTIVE.getCode());
assertEquals(2, AuthPlatformStatus.AVAILABLE.getCode());
assertEquals(3, AuthPlatformStatus.UNAVAILABLE.getCode());
}

@Test
// Boundary case test: Check if invalid codes return UNAVAILABLE
public void testInvalidCode() {
assertEquals(AuthPlatformStatus.UNAVAILABLE, AuthPlatformStatus.fromCode(0));
assertEquals(AuthPlatformStatus.UNAVAILABLE, AuthPlatformStatus.fromCode(4));
assertEquals(AuthPlatformStatus.UNAVAILABLE, AuthPlatformStatus.fromCode(null));
}

@Test
// Normal path test: Check if the isAvailable method works correctly with valid codes
public void testIsAvailable() {
assertTrue(AuthPlatformStatus.isAvailable(2));
assertFalse(AuthPlatformStatus.isAvailable(1));
assertFalse(AuthPlatformStatus.isAvailable(3));
}

@Test
// Normal path test: Check if the isActive method works correctly with valid codes
public void testIsActive() {
assertTrue(AuthPlatformStatus.isActive(1));
assertFalse(AuthPlatformStatus.isActive(2));
assertFalse(AuthPlatformStatus.isActive(3));
}

@Test
// Normal path test: Check if the available method works correctly with valid codes
public void testAvailable() {
assertTrue(AuthPlatformStatus.available(1));
assertTrue(AuthPlatformStatus.available(2));
assertFalse(AuthPlatformStatus.available(3));
}

@Test
// Boundary case test: Check if the available method returns false with invalid codes
public void testAvailableWithInvalidCode() {
assertFalse(AuthPlatformStatus.available(0));
assertFalse(AuthPlatformStatus.available(4));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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.enums;

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.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ChatbotCommandTest {

@Test
public void testGetAllCommands() {
// Test getting all commands
List<String> allCommands = ChatbotCommand.getAllCommands();
assertEquals(3, allCommands.size());
assertTrue(allCommands.contains("info"));
assertTrue(allCommands.contains("search"));
assertTrue(allCommands.contains("help"));
}

@Test
public void testGetCommandWithValidInput() {
// Test getting valid command
assertEquals(ChatbotCommand.INFO, ChatbotCommand.getCommand("info"));
assertEquals(ChatbotCommand.SEARCH, ChatbotCommand.getCommand("search"));
assertEquals(ChatbotCommand.HELP, ChatbotCommand.getCommand("help"));
}

@Test
public void testGetCommandWithInvalidInput() {
// Test getting invalid command
assertNull(ChatbotCommand.getCommand("unknown"));
assertNull(ChatbotCommand.getCommand(""));
assertNull(ChatbotCommand.getCommand(null));
}

@Test
public void testGetCommandFromMessageWithValidInput() {
// Test getting valid command from message
assertEquals(ChatbotCommand.INFO, ChatbotCommand.getCommandFromMessage("/info"));
assertEquals(ChatbotCommand.SEARCH, ChatbotCommand.getCommandFromMessage("/search some text"));
assertEquals(ChatbotCommand.HELP, ChatbotCommand.getCommandFromMessage("/help"));
}

@Test
public void testGetCommandFromMessageWithInvalidInput() {
// Test getting invalid command from message
assertNull(ChatbotCommand.getCommandFromMessage("info"));
assertNull(ChatbotCommand.getCommandFromMessage("/unknown"));
assertNull(ChatbotCommand.getCommandFromMessage(""));
assertThrows(NullPointerException.class, () -> ChatbotCommand.getCommandFromMessage(null));
}

@Test
public void testGetCommandFromMessageWithNoSlash() {
// Test message with no slash
assertNull(ChatbotCommand.getCommandFromMessage("info"));
}

@Test
public void testGetCommandFromMessageWithOnlySlash() {
// Test message with only slash
assertNull(ChatbotCommand.getCommandFromMessage("/"));
}

@Test
public void testGetCommandFromMessageWithMultipleSlashes() {
// Test message with multiple slashes
assertNull(ChatbotCommand.getCommandFromMessage("//info"));
}

@Test
public void testGetCommandFromMessageWithTrailingSpace() {
// Test message with trailing space after command
assertEquals(ChatbotCommand.INFO, ChatbotCommand.getCommandFromMessage("/info "));
}

@Test
public void testGetCommandFromMessageWithCaseInsensitive() {
// Test message with case insensitive command
assertNull(ChatbotCommand.getCommandFromMessage("/INFO"));
assertNull(ChatbotCommand.getCommandFromMessage("/SEARCH"));
assertNull(ChatbotCommand.getCommandFromMessage("/HELP"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.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 CommandLevelTest {

@Test
public void testFromStringNormalPath() {
assertEquals(CommandLevel.CLUSTER, CommandLevel.fromString("CLUSTER"));
assertEquals(CommandLevel.SERVICE, CommandLevel.fromString("SERVICE"));
assertEquals(CommandLevel.COMPONENT, CommandLevel.fromString("COMPONENT"));
assertEquals(CommandLevel.HOST, CommandLevel.fromString("HOST"));
}

@Test
public void testFromStringLowercaseInput() {
assertEquals(CommandLevel.CLUSTER, CommandLevel.fromString("cluster"));
assertEquals(CommandLevel.SERVICE, CommandLevel.fromString("service"));
assertEquals(CommandLevel.COMPONENT, CommandLevel.fromString("component"));
assertEquals(CommandLevel.HOST, CommandLevel.fromString("host"));
}

@Test
public void testFromStringMixedCaseInput() {
assertEquals(CommandLevel.CLUSTER, CommandLevel.fromString("ClUsTeR"));
assertEquals(CommandLevel.SERVICE, CommandLevel.fromString("SeRvIcE"));
assertEquals(CommandLevel.COMPONENT, CommandLevel.fromString("CoMpOnEnT"));
assertEquals(CommandLevel.HOST, CommandLevel.fromString("HoSt"));
}

@Test
public void testFromStringEmptyStringInput() {
assertThrows(IllegalArgumentException.class, () -> CommandLevel.fromString(""));
}

@Test
public void testFromStringInvalidInput() {
assertThrows(IllegalArgumentException.class, () -> CommandLevel.fromString("INVALID"));
}

@Test
public void testToLowercaseNormalPath() {
assertEquals("cluster", CommandLevel.CLUSTER.toLowerCase());
assertEquals("service", CommandLevel.SERVICE.toLowerCase());
assertEquals("component", CommandLevel.COMPONENT.toLowerCase());
assertEquals("host", CommandLevel.HOST.toLowerCase());
}

@Test
public void testToLowercaseRepeatedCall() {
assertEquals("cluster", CommandLevel.CLUSTER.toLowerCase());
assertEquals("cluster", CommandLevel.CLUSTER.toLowerCase());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.enums;

import org.junit.jupiter.api.Test;

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

public class HealthyStatusEnumTest {

@Test
public void testFromCodeNormalCase() {
assertEquals(HealthyStatusEnum.HEALTHY, HealthyStatusEnum.fromCode(1));
assertEquals(HealthyStatusEnum.UNHEALTHY, HealthyStatusEnum.fromCode(2));
assertEquals(HealthyStatusEnum.UNKNOWN, HealthyStatusEnum.fromCode(3));
}

@Test
public void testFromCodeBoundaryCase() {
// Test code less than minimum value
assertEquals(HealthyStatusEnum.UNKNOWN, HealthyStatusEnum.fromCode(0));
// Test code greater than maximum value
assertEquals(HealthyStatusEnum.UNKNOWN, HealthyStatusEnum.fromCode(4));
// Test null value
assertEquals(HealthyStatusEnum.UNKNOWN, HealthyStatusEnum.fromCode(null));
}
}
Loading

0 comments on commit d3f6c2f

Please sign in to comment.