Skip to content

Commit

Permalink
BIGTOP-4180: Add ut cases for controller classes in server module (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinw66 authored Aug 5, 2024
1 parent 4b3fa17 commit 45d36aa
Show file tree
Hide file tree
Showing 16 changed files with 1,373 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import org.apache.bigtop.manager.server.utils.ResponseEntity;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -55,21 +57,22 @@ public ResponseEntity<List<HostVO>> list(@PathVariable Long clusterId) {
}

@Operation(summary = "get", description = "Get a host")
// @GetMapping("/{id}")
public ResponseEntity<HostVO> get(@PathVariable Long id) {
@GetMapping("/{id}")
public ResponseEntity<HostVO> get(@PathVariable Long id, @PathVariable Long clusterId) {
return ResponseEntity.success(hostService.get(id));
}

@Operation(summary = "update", description = "Update a host")
// @PutMapping("/{id}")
public ResponseEntity<HostVO> update(@PathVariable Long id, @RequestBody @Validated HostReq hostReq) {
@PutMapping("/{id}")
public ResponseEntity<HostVO> update(
@PathVariable Long clusterId, @PathVariable Long id, @RequestBody @Validated HostReq hostReq) {
HostDTO hostDTO = HostConverter.INSTANCE.fromReq2DTO(hostReq);
return ResponseEntity.success(hostService.update(id, hostDTO));
}

@Operation(summary = "delete", description = "Delete a host")
// @DeleteMapping("/{id}")
public ResponseEntity<Boolean> delete(@PathVariable Long id) {
@DeleteMapping("/{id}")
public ResponseEntity<Boolean> delete(@PathVariable Long clusterId, @PathVariable Long id) {
return ResponseEntity.success(hostService.delete(id));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.apache.bigtop.manager.server.annotations.Audit;
import org.apache.bigtop.manager.server.enums.ApiExceptionEnum;
import org.apache.bigtop.manager.server.exception.ApiException;
import org.apache.bigtop.manager.server.holder.SessionUserHolder;
import org.apache.bigtop.manager.server.model.converter.LoginConverter;
import org.apache.bigtop.manager.server.model.dto.LoginDTO;
import org.apache.bigtop.manager.server.model.req.LoginReq;
Expand All @@ -30,7 +29,6 @@
import org.apache.bigtop.manager.server.utils.ResponseEntity;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -58,12 +56,4 @@ public ResponseEntity<LoginVO> login(@RequestBody LoginReq loginReq) {
LoginDTO loginDTO = LoginConverter.INSTANCE.fromReq2DTO(loginReq);
return ResponseEntity.success(loginService.login(loginDTO));
}

@Operation(summary = "test", description = "test")
@GetMapping(value = "/test")
public ResponseEntity<String> test() {
Long userId = SessionUserHolder.getUserId();
// throw new ServerException(ServerExceptionStatus.USERNAME_OR_PASSWORD_REQUIRED);
return ResponseEntity.success("111");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
public class MonitoringController {

@Resource
MonitoringService monitoringService;
private MonitoringService monitoringService;

@Operation(summary = "agent healthy", description = "agent healthy check")
@GetMapping("agenthealthy")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.controller;

import org.apache.bigtop.manager.server.model.dto.CommandDTO;
import org.apache.bigtop.manager.server.model.req.CommandReq;
import org.apache.bigtop.manager.server.model.vo.CommandVO;
import org.apache.bigtop.manager.server.service.CommandService;
import org.apache.bigtop.manager.server.utils.MessageSourceUtils;
import org.apache.bigtop.manager.server.utils.ResponseEntity;

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.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

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.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class CommandControllerTest {

@Mock
private CommandService commandService;

@InjectMocks
private CommandController commandController;

private MockedStatic<MessageSourceUtils> mockedMessageSourceUtils;

@BeforeEach
void setUp() {
mockedMessageSourceUtils = Mockito.mockStatic(MessageSourceUtils.class);
when(MessageSourceUtils.getMessage(any())).thenReturn("Mocked message");
}

@AfterEach
void tearDown() {
mockedMessageSourceUtils.close();
}

@Test
void commandExecutesSuccessfully() {
CommandReq commandReq = new CommandReq();
CommandVO commandVO = new CommandVO();
when(commandService.command(any(CommandDTO.class))).thenReturn(commandVO);

ResponseEntity<CommandVO> response = commandController.command(commandReq);

assertTrue(response.isSuccess());
assertEquals(commandVO, response.getData());
}

@Test
void commandHandlesInvalidRequest() {
CommandReq commandReq = new CommandReq(); // Assuming this is invalid
when(commandService.command(any(CommandDTO.class))).thenReturn(null);

ResponseEntity<CommandVO> response = commandController.command(commandReq);

assertTrue(response.isSuccess());
assertNull(response.getData());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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.controller;

import org.apache.bigtop.manager.server.model.vo.ComponentVO;
import org.apache.bigtop.manager.server.service.ComponentService;
import org.apache.bigtop.manager.server.utils.MessageSourceUtils;
import org.apache.bigtop.manager.server.utils.ResponseEntity;

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.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
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.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class ComponentControllerTest {

@Mock
private ComponentService componentService;

@InjectMocks
private ComponentController componentController;

private MockedStatic<MessageSourceUtils> mockedMessageSourceUtils;

@BeforeEach
void setUp() {
mockedMessageSourceUtils = Mockito.mockStatic(MessageSourceUtils.class);
when(MessageSourceUtils.getMessage(any())).thenReturn("Mocked message");
}

@AfterEach
void tearDown() {
mockedMessageSourceUtils.close();
}

@Test
void listReturnsAllComponents() {
Long clusterId = 1L;
List<ComponentVO> components = Arrays.asList(new ComponentVO(), new ComponentVO());
when(componentService.list(clusterId)).thenReturn(components);

ResponseEntity<List<ComponentVO>> response = componentController.list(clusterId);

assertTrue(response.isSuccess());
assertEquals(components, response.getData());
}

@Test
void getReturnsComponentById() {
Long id = 1L;
ComponentVO component = new ComponentVO();
when(componentService.get(id)).thenReturn(component);

ResponseEntity<ComponentVO> response = componentController.get(id);

assertTrue(response.isSuccess());
assertEquals(component, response.getData());
}

@Test
void listReturnsEmptyForNoComponents() {
Long clusterId = 1L;
when(componentService.list(clusterId)).thenReturn(List.of());

ResponseEntity<List<ComponentVO>> response = componentController.list(clusterId);

assertTrue(response.isSuccess());
assertTrue(response.getData().isEmpty());
}

@Test
void getReturnsNotFoundForInvalidId() {
Long id = 999L;
when(componentService.get(id)).thenReturn(null);

ResponseEntity<ComponentVO> response = componentController.get(id);

assertTrue(response.isSuccess());
assertNull(response.getData());
}
}
Loading

0 comments on commit 45d36aa

Please sign in to comment.