Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BIGTOP-4355: Add and optimize ut cases for agent module #173

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void onReady() {
};
}

private void truncateLogFile(Long taskId) {
protected void truncateLogFile(Long taskId) {
String filePath = ProjectPathUtils.getLogFilePath(taskId);
File file = new File(filePath);
if (file.exists()) {
Expand All @@ -100,7 +100,7 @@ private void truncateLogFile(Long taskId) {
}
}

private Boolean isTaskRequest(Object obj) {
protected Boolean isTaskRequest(Object obj) {
if (obj == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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.agent;

import org.apache.bigtop.manager.agent.monitoring.AgentHostMonitoring;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.SpringApplication;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.MultiGauge;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class AgentApplicationTest {

@Mock
private MeterRegistry meterRegistry;

@Mock
private MultiGauge diskMultiGauge;

@Mock
private MultiGauge cpuMultiGauge;

@Mock
private MultiGauge memMultiGauge;

@Mock
private MultiGauge diskIOMultiGauge;

@InjectMocks
private AgentApplication agentApplication;

@Test
public void testDiskMultiGaugeBean() {
try (MockedStatic<AgentHostMonitoring> mockedStatic = mockStatic(AgentHostMonitoring.class)) {
when(AgentHostMonitoring.newDiskMultiGauge(meterRegistry)).thenReturn(diskMultiGauge);
MultiGauge result = agentApplication.diskMultiGauge(meterRegistry);
assertNotNull(result);
}
}

@Test
public void testCPUMultiGaugeBean() {
try (MockedStatic<AgentHostMonitoring> mockedStatic = mockStatic(AgentHostMonitoring.class)) {
when(AgentHostMonitoring.newCPUMultiGauge(meterRegistry)).thenReturn(cpuMultiGauge);
MultiGauge result = agentApplication.cpuMultiGauge(meterRegistry);
assertNotNull(result);
}
}

@Test
public void testMemMultiGaugeBean() {
try (MockedStatic<AgentHostMonitoring> mockedStatic = mockStatic(AgentHostMonitoring.class)) {
when(AgentHostMonitoring.newMemMultiGauge(meterRegistry)).thenReturn(memMultiGauge);
MultiGauge result = agentApplication.memMultiGauge(meterRegistry);
assertNotNull(result);
}
}

@Test
public void testDiskIOMultiGaugeBean() {
try (MockedStatic<AgentHostMonitoring> mockedStatic = mockStatic(AgentHostMonitoring.class)) {
when(AgentHostMonitoring.newDiskIOMultiGauge(meterRegistry)).thenReturn(diskIOMultiGauge);
MultiGauge result = agentApplication.diskIOMultiGauge(meterRegistry);
assertNotNull(result);
}
}

@Test
public void testMainMethod() {
try (MockedStatic<SpringApplication> mockedStatic = mockStatic(SpringApplication.class)) {
AgentApplication.main(new String[] {});
mockedStatic.verify(() -> SpringApplication.run(AgentApplication.class, new String[] {}));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* 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.agent.grpc.interceptor;

import org.apache.bigtop.manager.common.utils.ProjectPathUtils;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verifyNoInteractions;

@ExtendWith(MockitoExtension.class)
public class TaskInterceptorTest {

@Mock
private Logger log;

private TaskInterceptor taskInterceptor;
private File tempFile;

@BeforeEach
public void setUp() {
taskInterceptor = new TaskInterceptor();
}

@AfterEach
public void tearDown() {
if (tempFile != null && tempFile.exists()) {
tempFile.delete();
}
}

@Test
public void testTruncateLogFileFileExists() throws IOException {
Long taskId = 1L;
tempFile = File.createTempFile("logfile", ".log");

try (MockedStatic<ProjectPathUtils> projectPathUtils = mockStatic(ProjectPathUtils.class)) {
projectPathUtils.when(() -> ProjectPathUtils.getLogFilePath(taskId)).thenReturn(tempFile.getAbsolutePath());

taskInterceptor.truncateLogFile(taskId);

projectPathUtils.verify(() -> ProjectPathUtils.getLogFilePath(taskId), times(1));
assertTrue(tempFile.exists());
assertTrue(tempFile.length() == 0);
}
}

@Test
public void testTruncateLogFileFileDoesNotExist() throws IOException {
Long taskId = 1L;
String filePath = "path/to/nonexistentfile.log";

try (MockedStatic<ProjectPathUtils> projectPathUtils = mockStatic(ProjectPathUtils.class)) {
projectPathUtils.when(() -> ProjectPathUtils.getLogFilePath(taskId)).thenReturn(filePath);

taskInterceptor.truncateLogFile(taskId);

projectPathUtils.verify(() -> ProjectPathUtils.getLogFilePath(taskId), times(1));
verifyNoInteractions(log);
}
}

@Test
public void testIsTaskRequestTaskRequest() {
TaskLogRequest taskLogRequest = new TaskLogRequest();

Boolean result = taskInterceptor.isTaskRequest(taskLogRequest);

assertTrue(result);
}

@Test
public void testIsTaskRequestTaskWithGetTaskIdMethod() {
Task task = new Task();

Boolean result = taskInterceptor.isTaskRequest(task);

assertTrue(result);
}

@Test
public void testIsTaskRequestTaskWithoutGetTaskIdMethod() {
Object obj = new Object();

Boolean result = taskInterceptor.isTaskRequest(obj);

assertFalse(result);
}

@Test
public void testIsTaskRequestNullObject() {
Boolean result = taskInterceptor.isTaskRequest(null);

assertFalse(result);
}

private static class Task {
public Long getTaskId() {
return 1L;
}
}

private static class TaskLogRequest {
public Long getTaskId() {
return 1L;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* 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.agent.grpc.service;

import org.apache.bigtop.manager.common.shell.ShellResult;
import org.apache.bigtop.manager.common.utils.Environments;
import org.apache.bigtop.manager.common.utils.os.TimeSyncDetection;
import org.apache.bigtop.manager.grpc.generated.HostCheckReply;
import org.apache.bigtop.manager.grpc.generated.HostCheckRequest;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import io.grpc.stub.StreamObserver;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;

@ExtendWith(MockitoExtension.class)
public class HostCheckServiceGrpcImplTest {

@Mock
private StreamObserver<HostCheckReply> responseObserver;

private HostCheckServiceGrpcImpl hostCheckServiceGrpcImpl;

@BeforeEach
public void setUp() {
hostCheckServiceGrpcImpl = new HostCheckServiceGrpcImpl();
}

@Test
public void testCheckInDevMode() {
try (var environmentsMockedStatic = mockStatic(Environments.class)) {
// Mock environment in development mode
environmentsMockedStatic.when(Environments::isDevMode).thenReturn(true);

// Call check method
hostCheckServiceGrpcImpl.check(mock(HostCheckRequest.class), responseObserver);

// Verify responseObserver is correctly called
then(responseObserver).should(times(1)).onNext(any());
then(responseObserver).should(times(1)).onCompleted();
then(responseObserver).should(never()).onError(any());
}
}

@Test
public void testCheckInNonDevModeWithSuccess() {
try (var environmentsMockedStatic = mockStatic(Environments.class);
var timeSyncDetectionMockedStatic = mockStatic(TimeSyncDetection.class)) {
// Mock environment not in development mode
environmentsMockedStatic.when(Environments::isDevMode).thenReturn(false);
// Mock successful time synchronization check
timeSyncDetectionMockedStatic
.when(TimeSyncDetection::checkTimeSync)
.thenReturn(ShellResult.success("test"));

// Call check method
hostCheckServiceGrpcImpl.check(mock(HostCheckRequest.class), responseObserver);

// Verify responseObserver is correctly called
then(responseObserver).should(times(1)).onNext(any());
then(responseObserver).should(times(1)).onCompleted();
then(responseObserver).should(never()).onError(any());
}
}

@Test
public void testCheckInNonDevModeWithError() {
try (var environmentsMockedStatic = mockStatic(Environments.class);
var timeSyncDetectionMockedStatic = mockStatic(TimeSyncDetection.class)) {
// Mock environment not in development mode
environmentsMockedStatic.when(Environments::isDevMode).thenReturn(false);
// Mock failed time synchronization check
timeSyncDetectionMockedStatic.when(TimeSyncDetection::checkTimeSync).thenReturn(ShellResult.fail("test"));

// Call check method
hostCheckServiceGrpcImpl.check(mock(HostCheckRequest.class), responseObserver);

// Verify responseObserver is correctly called
then(responseObserver).should(times(1)).onNext(any());
then(responseObserver).should(times(1)).onCompleted();
then(responseObserver).should(never()).onError(any());
}
}

@Test
public void testCheckWithError() {
try (var environmentsMockedStatic = mockStatic(Environments.class);
var timeSyncDetectionMockedStatic = mockStatic(TimeSyncDetection.class)) {
// Mock environment not in development mode
environmentsMockedStatic.when(Environments::isDevMode).thenReturn(false);
// Mock a runtime exception during check execution
timeSyncDetectionMockedStatic
.when(TimeSyncDetection::checkTimeSync)
.thenThrow(new RuntimeException("Test Simulated Error"));

// Call check method
hostCheckServiceGrpcImpl.check(mock(HostCheckRequest.class), responseObserver);

// Verify responseObserver is correctly called
then(responseObserver).should(never()).onNext(any());
then(responseObserver).should(never()).onCompleted();
then(responseObserver).should(times(1)).onError(any());
}
}
}
Loading
Loading