diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/HostController.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/HostController.java index b617f4a3..561c39a7 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/HostController.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/HostController.java @@ -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; @@ -55,21 +57,22 @@ public ResponseEntity> list(@PathVariable Long clusterId) { } @Operation(summary = "get", description = "Get a host") - // @GetMapping("/{id}") - public ResponseEntity get(@PathVariable Long id) { + @GetMapping("/{id}") + public ResponseEntity get(@PathVariable Long id, @PathVariable Long clusterId) { return ResponseEntity.success(hostService.get(id)); } @Operation(summary = "update", description = "Update a host") - // @PutMapping("/{id}") - public ResponseEntity update(@PathVariable Long id, @RequestBody @Validated HostReq hostReq) { + @PutMapping("/{id}") + public ResponseEntity 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 delete(@PathVariable Long id) { + @DeleteMapping("/{id}") + public ResponseEntity delete(@PathVariable Long clusterId, @PathVariable Long id) { return ResponseEntity.success(hostService.delete(id)); } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/LoginController.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/LoginController.java index 895afca2..db92a781 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/LoginController.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/LoginController.java @@ -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; @@ -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; @@ -58,12 +56,4 @@ public ResponseEntity 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 test() { - Long userId = SessionUserHolder.getUserId(); - // throw new ServerException(ServerExceptionStatus.USERNAME_OR_PASSWORD_REQUIRED); - return ResponseEntity.success("111"); - } } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/MonitoringController.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/MonitoringController.java index de19f5c6..9ddd2134 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/MonitoringController.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/controller/MonitoringController.java @@ -37,7 +37,7 @@ public class MonitoringController { @Resource - MonitoringService monitoringService; + private MonitoringService monitoringService; @Operation(summary = "agent healthy", description = "agent healthy check") @GetMapping("agenthealthy") diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/enums/heartbeat/HostState.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/enums/heartbeat/HostState.java deleted file mode 100644 index cb6386b5..00000000 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/enums/heartbeat/HostState.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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.heartbeat; - -public enum HostState { - INITIALIZING, - - HEALTHY, - - LOST, -} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/CommandControllerTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/CommandControllerTest.java new file mode 100644 index 00000000..5c53c670 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/CommandControllerTest.java @@ -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 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 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 response = commandController.command(commandReq); + + assertTrue(response.isSuccess()); + assertNull(response.getData()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/ComponentControllerTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/ComponentControllerTest.java new file mode 100644 index 00000000..ef631f29 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/ComponentControllerTest.java @@ -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 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 components = Arrays.asList(new ComponentVO(), new ComponentVO()); + when(componentService.list(clusterId)).thenReturn(components); + + ResponseEntity> 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 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> response = componentController.list(clusterId); + + assertTrue(response.isSuccess()); + assertTrue(response.getData().isEmpty()); + } + + @Test + void getReturnsNotFoundForInvalidId() { + Long id = 999L; + when(componentService.get(id)).thenReturn(null); + + ResponseEntity response = componentController.get(id); + + assertTrue(response.isSuccess()); + assertNull(response.getData()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/ConfigControllerTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/ConfigControllerTest.java new file mode 100644 index 00000000..401ba0b3 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/ConfigControllerTest.java @@ -0,0 +1,111 @@ +/* + * 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.ServiceConfigVO; +import org.apache.bigtop.manager.server.service.ConfigService; +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.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class ConfigControllerTest { + + @Mock + private ConfigService configService; + + @InjectMocks + private ConfigController configController; + + private MockedStatic mockedMessageSourceUtils; + + @BeforeEach + void setUp() { + mockedMessageSourceUtils = Mockito.mockStatic(MessageSourceUtils.class); + when(MessageSourceUtils.getMessage(any())).thenReturn("Mocked message"); + } + + @AfterEach + void tearDown() { + mockedMessageSourceUtils.close(); + } + + @Test + void listReturnsAllConfigurations() { + Long clusterId = 1L; + List configs = Arrays.asList(new ServiceConfigVO(), new ServiceConfigVO()); + when(configService.list(clusterId)).thenReturn(configs); + + ResponseEntity> response = configController.list(clusterId); + + assertTrue(response.isSuccess()); + assertEquals(configs, response.getData()); + } + + @Test + void latestReturnsLatestConfigurations() { + Long clusterId = 1L; + List latestConfigs = Arrays.asList(new ServiceConfigVO(), new ServiceConfigVO()); + when(configService.latest(clusterId)).thenReturn(latestConfigs); + + ResponseEntity> response = configController.latest(clusterId); + + assertTrue(response.isSuccess()); + assertEquals(latestConfigs, response.getData()); + } + + @Test + void listReturnsEmptyForInvalidClusterId() { + Long clusterId = 999L; + when(configService.list(clusterId)).thenReturn(List.of()); + + ResponseEntity> response = configController.list(clusterId); + + assertTrue(response.isSuccess()); + assertTrue(response.getData().isEmpty()); + } + + @Test + void latestReturnsEmptyForInvalidClusterId() { + Long clusterId = 999L; + when(configService.latest(clusterId)).thenReturn(List.of()); + + ResponseEntity> response = configController.latest(clusterId); + + assertTrue(response.isSuccess()); + assertTrue(response.getData().isEmpty()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/HostComponentControllerTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/HostComponentControllerTest.java new file mode 100644 index 00000000..7f11d845 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/HostComponentControllerTest.java @@ -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.server.controller; + +import org.apache.bigtop.manager.server.model.vo.HostComponentVO; +import org.apache.bigtop.manager.server.service.HostComponentService; +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.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class HostComponentControllerTest { + + @Mock + private HostComponentService hostComponentService; + + @InjectMocks + private HostComponentController hostComponentController; + + private MockedStatic mockedMessageSourceUtils; + + @BeforeEach + void setUp() { + mockedMessageSourceUtils = Mockito.mockStatic(MessageSourceUtils.class); + when(MessageSourceUtils.getMessage(any())).thenReturn("Mocked message"); + } + + @AfterEach + void tearDown() { + mockedMessageSourceUtils.close(); + } + + @Test + void listReturnsAllHostComponents() { + Long clusterId = 1L; + List hostComponents = Arrays.asList(new HostComponentVO(), new HostComponentVO()); + when(hostComponentService.list(clusterId)).thenReturn(hostComponents); + + ResponseEntity> response = hostComponentController.list(clusterId); + + assertTrue(response.isSuccess()); + assertEquals(hostComponents, response.getData()); + } + + @Test + void listByHostReturnsHostComponentsForHost() { + Long clusterId = 1L; + Long hostId = 1L; + List hostComponents = Arrays.asList(new HostComponentVO(), new HostComponentVO()); + when(hostComponentService.listByHost(clusterId, hostId)).thenReturn(hostComponents); + + ResponseEntity> response = hostComponentController.listByHost(clusterId, hostId); + + assertTrue(response.isSuccess()); + assertEquals(hostComponents, response.getData()); + } + + @Test + void listByServiceReturnsHostComponentsForService() { + Long clusterId = 1L; + Long serviceId = 1L; + List hostComponents = Arrays.asList(new HostComponentVO(), new HostComponentVO()); + when(hostComponentService.listByService(clusterId, serviceId)).thenReturn(hostComponents); + + ResponseEntity> response = hostComponentController.listByService(clusterId, serviceId); + + assertTrue(response.isSuccess()); + assertEquals(hostComponents, response.getData()); + } + + @Test + void listReturnsEmptyForInvalidClusterId() { + Long clusterId = 999L; + when(hostComponentService.list(clusterId)).thenReturn(List.of()); + + ResponseEntity> response = hostComponentController.list(clusterId); + + assertTrue(response.isSuccess()); + assertTrue(response.getData().isEmpty()); + } + + @Test + void listByHostReturnsEmptyForInvalidHostId() { + Long clusterId = 1L; + Long hostId = 999L; + when(hostComponentService.listByHost(clusterId, hostId)).thenReturn(List.of()); + + ResponseEntity> response = hostComponentController.listByHost(clusterId, hostId); + + assertTrue(response.isSuccess()); + assertTrue(response.getData().isEmpty()); + } + + @Test + void listByServiceReturnsEmptyForInvalidServiceId() { + Long clusterId = 1L; + Long serviceId = 999L; + when(hostComponentService.listByService(clusterId, serviceId)).thenReturn(List.of()); + + ResponseEntity> response = hostComponentController.listByService(clusterId, serviceId); + + assertTrue(response.isSuccess()); + assertTrue(response.getData().isEmpty()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/HostControllerTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/HostControllerTest.java new file mode 100644 index 00000000..f7b6c2e8 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/HostControllerTest.java @@ -0,0 +1,196 @@ +/* + * 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.HostDTO; +import org.apache.bigtop.manager.server.model.req.HostReq; +import org.apache.bigtop.manager.server.model.req.HostnamesReq; +import org.apache.bigtop.manager.server.model.vo.HostVO; +import org.apache.bigtop.manager.server.service.HostService; +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.assertFalse; +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.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class HostControllerTest { + + @Mock + private HostService hostService; + + @InjectMocks + private HostController hostController; + + private MockedStatic mockedMessageSourceUtils; + + @BeforeEach + void setUp() { + mockedMessageSourceUtils = Mockito.mockStatic(MessageSourceUtils.class); + when(MessageSourceUtils.getMessage(any())).thenReturn("Mocked message"); + } + + @AfterEach + void tearDown() { + mockedMessageSourceUtils.close(); + } + + @Test + void listReturnsAllHosts() { + Long clusterId = 1L; + List hosts = Arrays.asList(new HostVO(), new HostVO()); + when(hostService.list(clusterId)).thenReturn(hosts); + + ResponseEntity> response = hostController.list(clusterId); + + assertTrue(response.isSuccess()); + assertEquals(hosts, response.getData()); + } + + @Test + void getReturnsHost() { + Long clusterId = 1L; + Long hostId = 1L; + HostVO host = new HostVO(); + when(hostService.get(hostId)).thenReturn(host); + + ResponseEntity response = hostController.get(hostId, clusterId); + + assertTrue(response.isSuccess()); + assertEquals(host, response.getData()); + } + + @Test + void updateReturnsUpdatedHost() { + Long clusterId = 1L; + Long hostId = 1L; + HostReq hostReq = new HostReq(); + HostVO updatedHost = new HostVO(); + when(hostService.update(anyLong(), any(HostDTO.class))).thenReturn(updatedHost); + + ResponseEntity response = hostController.update(clusterId, hostId, hostReq); + + assertTrue(response.isSuccess()); + assertEquals(updatedHost, response.getData()); + } + + @Test + void deleteReturnsSuccess() { + Long clusterId = 1L; + Long hostId = 1L; + when(hostService.delete(hostId)).thenReturn(true); + + ResponseEntity response = hostController.delete(clusterId, hostId); + + assertTrue(response.isSuccess()); + assertTrue(response.getData()); + } + + @Test + void checkConnectionReturnsSuccess() { + Long clusterId = 1L; + HostnamesReq hostnamesReq = new HostnamesReq(); + hostnamesReq.setHostnames(Arrays.asList("host1", "host2")); + when(hostService.checkConnection(hostnamesReq.getHostnames())).thenReturn(true); + + ResponseEntity response = hostController.checkConnection(clusterId, hostnamesReq); + + assertTrue(response.isSuccess()); + assertTrue(response.getData()); + } + + @Test + void listReturnsEmptyForInvalidClusterId() { + Long clusterId = 999L; + when(hostService.list(clusterId)).thenReturn(List.of()); + + ResponseEntity> response = hostController.list(clusterId); + + assertTrue(response.isSuccess()); + assertTrue(response.getData().isEmpty()); + } + + @Test + void getReturnsNullForInvalidHostId() { + Long clusterId = 1L; + Long hostId = 999L; + when(hostService.get(hostId)).thenReturn(null); + + ResponseEntity response = hostController.get(hostId, clusterId); + + assertTrue(response.isSuccess()); + assertNull(response.getData()); + } + + @Test + void updateReturnsNullForInvalidHostId() { + Long clusterId = 1L; + Long hostId = 999L; + HostReq hostReq = new HostReq(); + when(hostService.update(anyLong(), any(HostDTO.class))).thenReturn(null); + + ResponseEntity response = hostController.update(clusterId, hostId, hostReq); + + assertTrue(response.isSuccess()); + assertNull(response.getData()); + } + + @Test + void deleteReturnsFalseForInvalidHostId() { + Long clusterId = 1L; + Long hostId = 999L; + when(hostService.delete(hostId)).thenReturn(false); + + ResponseEntity response = hostController.delete(clusterId, hostId); + + assertTrue(response.isSuccess()); + assertFalse(response.getData()); + } + + @Test + void checkConnectionReturnsFalseForInvalidHostnames() { + Long clusterId = 1L; + HostnamesReq hostnamesReq = new HostnamesReq(); + hostnamesReq.setHostnames(Arrays.asList("invalidHost1", "invalidHost2")); + when(hostService.checkConnection(hostnamesReq.getHostnames())).thenReturn(false); + + ResponseEntity response = hostController.checkConnection(clusterId, hostnamesReq); + + assertTrue(response.isSuccess()); + assertFalse(response.getData()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/JobControllerTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/JobControllerTest.java new file mode 100644 index 00000000..9650074c --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/JobControllerTest.java @@ -0,0 +1,116 @@ +/* + * 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.JobVO; +import org.apache.bigtop.manager.server.model.vo.PageVO; +import org.apache.bigtop.manager.server.service.JobService; +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 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 JobControllerTest { + + @Mock + private JobService jobService; + + @InjectMocks + private JobController jobController; + + private MockedStatic mockedMessageSourceUtils; + + @BeforeEach + void setUp() { + mockedMessageSourceUtils = Mockito.mockStatic(MessageSourceUtils.class); + when(MessageSourceUtils.getMessage(any())).thenReturn("Mocked message"); + } + + @AfterEach + void tearDown() { + mockedMessageSourceUtils.close(); + } + + @Test + void listReturnsAllJobs() { + Long clusterId = 1L; + PageVO jobs = PageVO.of(Arrays.asList(new JobVO(), new JobVO()), 2L); + when(jobService.list(clusterId)).thenReturn(jobs); + + ResponseEntity> response = jobController.list(clusterId); + + assertTrue(response.isSuccess()); + assertEquals(jobs, response.getData()); + } + + @Test + void getReturnsJobById() { + Long id = 1L; + Long clusterId = 1L; + JobVO job = new JobVO(); + when(jobService.get(id)).thenReturn(job); + + ResponseEntity response = jobController.get(id, clusterId); + + assertTrue(response.isSuccess()); + assertEquals(job, response.getData()); + } + + @Test + void retryRetriesJob() { + Long id = 1L; + Long clusterId = 1L; + JobVO job = new JobVO(); + when(jobService.retry(id)).thenReturn(job); + + ResponseEntity response = jobController.retry(id, clusterId); + + assertTrue(response.isSuccess()); + assertEquals(job, response.getData()); + } + + @Test + void getReturnsNotFoundForInvalidId() { + Long id = 999L; + Long clusterId = 1L; + when(jobService.get(id)).thenReturn(null); + + ResponseEntity response = jobController.get(id, clusterId); + + assertTrue(response.isSuccess()); + assertNull(response.getData()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/LoginControllerTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/LoginControllerTest.java new file mode 100644 index 00000000..095afef1 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/LoginControllerTest.java @@ -0,0 +1,113 @@ +/* + * 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.enums.ApiExceptionEnum; +import org.apache.bigtop.manager.server.exception.ApiException; +import org.apache.bigtop.manager.server.model.dto.LoginDTO; +import org.apache.bigtop.manager.server.model.req.LoginReq; +import org.apache.bigtop.manager.server.model.vo.LoginVO; +import org.apache.bigtop.manager.server.service.LoginService; +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.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class LoginControllerTest { + + @Mock + private LoginService loginService; + + @InjectMocks + private LoginController loginController; + + private MockedStatic mockedMessageSourceUtils; + + @BeforeEach + void setUp() { + mockedMessageSourceUtils = Mockito.mockStatic(MessageSourceUtils.class); + when(MessageSourceUtils.getMessage(any())).thenReturn("Mocked message"); + } + + @AfterEach + void tearDown() { + mockedMessageSourceUtils.close(); + } + + @Test + void loginReturnsSuccessForValidCredentials() { + LoginReq loginReq = new LoginReq(); + loginReq.setUsername("validUser"); + loginReq.setPassword("validPassword"); + + LoginVO loginVO = new LoginVO(); + when(loginService.login(any(LoginDTO.class))).thenReturn(loginVO); + + ResponseEntity response = loginController.login(loginReq); + + assertEquals(loginVO, response.getData()); + } + + @Test + void loginThrowsExceptionForMissingUsername() { + LoginReq loginReq = new LoginReq(); + loginReq.setUsername(""); + loginReq.setPassword("validPassword"); + + ApiException exception = assertThrows(ApiException.class, () -> loginController.login(loginReq)); + + assertEquals(ApiExceptionEnum.USERNAME_OR_PASSWORD_REQUIRED, exception.getEx()); + } + + @Test + void loginThrowsExceptionForMissingPassword() { + LoginReq loginReq = new LoginReq(); + loginReq.setUsername("validUser"); + loginReq.setPassword(""); + + ApiException exception = assertThrows(ApiException.class, () -> loginController.login(loginReq)); + + assertEquals(ApiExceptionEnum.USERNAME_OR_PASSWORD_REQUIRED, exception.getEx()); + } + + @Test + void loginThrowsExceptionForMissingUsernameAndPassword() { + LoginReq loginReq = new LoginReq(); + loginReq.setUsername(""); + loginReq.setPassword(""); + + ApiException exception = assertThrows(ApiException.class, () -> loginController.login(loginReq)); + + assertEquals(ApiExceptionEnum.USERNAME_OR_PASSWORD_REQUIRED, exception.getEx()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/MonitoringControllerTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/MonitoringControllerTest.java new file mode 100644 index 00000000..cc391a17 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/MonitoringControllerTest.java @@ -0,0 +1,86 @@ +/* + * 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.service.MonitoringService; +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 com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +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 MonitoringControllerTest { + + @Mock + private MonitoringService monitoringService; + + @InjectMocks + private MonitoringController monitoringController; + + private MockedStatic mockedMessageSourceUtils; + + @BeforeEach + void setUp() { + mockedMessageSourceUtils = Mockito.mockStatic(MessageSourceUtils.class); + when(MessageSourceUtils.getMessage(any())).thenReturn("Mocked message"); + } + + @AfterEach + void tearDown() { + mockedMessageSourceUtils.close(); + } + + @Test + void agentHostsHealthyStatusReturnsSuccess() { + JsonNode mockResponse = new ObjectMapper().createObjectNode(); + when(monitoringService.queryAgentsHealthyStatus()).thenReturn(mockResponse); + + ResponseEntity response = monitoringController.agentHostsHealthyStatus(); + + assertTrue(response.isSuccess()); + assertEquals(mockResponse, response.getData()); + } + + @Test + void agentHostsHealthyStatusReturnsEmptyResponse() { + when(monitoringService.queryAgentsHealthyStatus()).thenReturn(null); + + ResponseEntity response = monitoringController.agentHostsHealthyStatus(); + + assertTrue(response.isSuccess()); + assertNull(response.getData()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/ServiceControllerTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/ServiceControllerTest.java new file mode 100644 index 00000000..7dabe079 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/ServiceControllerTest.java @@ -0,0 +1,100 @@ +/* + * 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.ServiceVO; +import org.apache.bigtop.manager.server.service.ServiceService; +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 ServiceControllerTest { + + @Mock + private ServiceService serviceService; + + @InjectMocks + private ServiceController serviceController; + + private MockedStatic mockedMessageSourceUtils; + + @BeforeEach + void setUp() { + mockedMessageSourceUtils = Mockito.mockStatic(MessageSourceUtils.class); + when(MessageSourceUtils.getMessage(any())).thenReturn("Mocked message"); + } + + @AfterEach + void tearDown() { + mockedMessageSourceUtils.close(); + } + + @Test + void listReturnsAllServices() { + List services = Arrays.asList(new ServiceVO(), new ServiceVO()); + when(serviceService.list(any())).thenReturn(services); + + ResponseEntity> response = serviceController.list(1L); + + assertTrue(response.isSuccess()); + assertEquals(services, response.getData()); + } + + @Test + void getReturnsServiceById() { + Long id = 1L; + ServiceVO service = new ServiceVO(); + when(serviceService.get(id)).thenReturn(service); + + ResponseEntity response = serviceController.get(id); + + assertTrue(response.isSuccess()); + assertEquals(service, response.getData()); + } + + @Test + void getReturnsNotFoundForInvalidId() { + Long id = 999L; + when(serviceService.get(id)).thenReturn(null); + + ResponseEntity response = serviceController.get(id); + + assertTrue(response.isSuccess()); + assertNull(response.getData()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/SseControllerTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/SseControllerTest.java new file mode 100644 index 00000000..607ff82f --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/SseControllerTest.java @@ -0,0 +1,116 @@ +/* + * 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.service.TaskLogService; +import org.apache.bigtop.manager.server.utils.MessageSourceUtils; + +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 org.springframework.web.servlet.mvc.method.annotation.SseEmitter; + +import reactor.core.publisher.FluxSink; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class SseControllerTest { + + @Mock + private TaskLogService taskLogService; + + @InjectMocks + private SseController sseController; + + private MockedStatic mockedMessageSourceUtils; + + @BeforeEach + void setUp() { + mockedMessageSourceUtils = Mockito.mockStatic(MessageSourceUtils.class); + when(MessageSourceUtils.getMessage(any())).thenReturn("Mocked message"); + } + + @AfterEach + void tearDown() { + mockedMessageSourceUtils.close(); + } + + @Test + void logReturnsSseEmitter() { + Long taskId = 1L; + Long clusterId = 1L; + doAnswer(invocation -> { + FluxSink sink = invocation.getArgument(1); + sink.next("log message"); + sink.complete(); + return null; + }) + .when(taskLogService) + .registerSink(eq(taskId), any()); + + SseEmitter emitter = sseController.log(taskId, clusterId); + + assertNotNull(emitter); + } + + @Test + void logHandlesExceptionDuringEmission() { + Long taskId = 1L; + Long clusterId = 1L; + doAnswer(invocation -> { + FluxSink sink = invocation.getArgument(1); + sink.error(new RuntimeException("Test exception")); + return null; + }) + .when(taskLogService) + .registerSink(eq(taskId), any()); + + SseEmitter emitter = sseController.log(taskId, clusterId); + + assertNotNull(emitter); + } + + @Test + void logCompletes() { + Long taskId = 1L; + Long clusterId = 1L; + doAnswer(invocation -> { + FluxSink sink = invocation.getArgument(1); + sink.complete(); + return null; + }) + .when(taskLogService) + .registerSink(eq(taskId), any()); + + SseEmitter emitter = sseController.log(taskId, clusterId); + + assertNotNull(emitter); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/StackControllerTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/StackControllerTest.java new file mode 100644 index 00000000..60b072f4 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/StackControllerTest.java @@ -0,0 +1,104 @@ +/* + * 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.ServiceComponentVO; +import org.apache.bigtop.manager.server.model.vo.ServiceConfigVO; +import org.apache.bigtop.manager.server.model.vo.StackVO; +import org.apache.bigtop.manager.server.service.StackService; +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.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class StackControllerTest { + + @Mock + private StackService stackService; + + @InjectMocks + private StackController stackController; + + private MockedStatic mockedMessageSourceUtils; + + @BeforeEach + void setUp() { + mockedMessageSourceUtils = Mockito.mockStatic(MessageSourceUtils.class); + when(MessageSourceUtils.getMessage(any())).thenReturn("Mocked message"); + } + + @AfterEach + void tearDown() { + mockedMessageSourceUtils.close(); + } + + @Test + void listReturnsAllStacks() { + List stacks = Arrays.asList(new StackVO(), new StackVO()); + when(stackService.list()).thenReturn(stacks); + + ResponseEntity> response = stackController.list(); + + assertTrue(response.isSuccess()); + assertEquals(stacks, response.getData()); + } + + @Test + void componentsReturnsAllComponentsForValidStack() { + String stackName = "bigtop"; + String stackVersion = "1.0.0"; + List components = Arrays.asList(new ServiceComponentVO(), new ServiceComponentVO()); + when(stackService.components(stackName, stackVersion)).thenReturn(components); + + ResponseEntity> response = stackController.components(stackName, stackVersion); + + assertTrue(response.isSuccess()); + assertEquals(components, response.getData()); + } + + @Test + void configurationsReturnsAllConfigurationsForValidStack() { + String stackName = "bigtop"; + String stackVersion = "1.0.0"; + List configurations = Arrays.asList(new ServiceConfigVO(), new ServiceConfigVO()); + when(stackService.configurations(stackName, stackVersion)).thenReturn(configurations); + + ResponseEntity> response = stackController.configurations(stackName, stackVersion); + + assertTrue(response.isSuccess()); + assertEquals(configurations, response.getData()); + } +} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/UserControllerTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/UserControllerTest.java new file mode 100644 index 00000000..7100b55b --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/UserControllerTest.java @@ -0,0 +1,83 @@ +/* + * 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.req.UserReq; +import org.apache.bigtop.manager.server.model.vo.UserVO; +import org.apache.bigtop.manager.server.service.UserService; +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.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class UserControllerTest { + + @Mock + private UserService userService; + + @InjectMocks + private UserController userController; + + private MockedStatic mockedMessageSourceUtils; + + @BeforeEach + void setUp() { + mockedMessageSourceUtils = Mockito.mockStatic(MessageSourceUtils.class); + when(MessageSourceUtils.getMessage(any())).thenReturn("Mocked message"); + } + + @AfterEach + void tearDown() { + mockedMessageSourceUtils.close(); + } + + @Test + void currentReturnsCurrentUser() { + UserVO userVO = new UserVO(); + when(userService.current()).thenReturn(userVO); + + ResponseEntity response = userController.current(); + + assertEquals(userVO, response.getData()); + } + + @Test + void updateReturnsUpdatedUser() { + UserReq userReq = new UserReq(); + UserVO userVO = new UserVO(); + when(userService.update(any())).thenReturn(userVO); + + ResponseEntity response = userController.update(userReq); + + assertEquals(userVO, response.getData()); + } +}