From 7dffcd8d1ebb44d65c0f981ab792c490e868c658 Mon Sep 17 00:00:00 2001 From: Zhiguo Wu Date: Tue, 30 Jul 2024 14:56:51 +0800 Subject: [PATCH] BIGTOP-4174: Add a separate ci job for unit testing (#29) --- .github/workflows/ci.yml | 27 ++-- .../manager/agent/AgentApplicationTests.java | 47 ------- bigtop-manager-bom/pom.xml | 6 - bigtop-manager-server/pom.xml | 6 - .../manager/server/utils/ResponseEntity.java | 10 ++ .../server/ServerApplicationTests.java | 44 ------- .../controller/ClusterControllerTest.java | 115 ++++++++++++++++++ pom.xml | 25 ++++ 8 files changed, 164 insertions(+), 116 deletions(-) delete mode 100644 bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/AgentApplicationTests.java delete mode 100644 bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/ServerApplicationTests.java create mode 100644 bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/ClusterControllerTest.java diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4f6357b9..f4b4530e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,24 +23,25 @@ jobs: cache: 'maven' - run: ./mvnw clean spotless:check + unit-tests: + name: "Run unit tests" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-java@v4 + with: + distribution: 'temurin' + java-version: '17' + cache: 'maven' + - run: ./mvnw clean test + build: + name: "Build project" runs-on: ubuntu-latest strategy: fail-fast: false matrix: java: ['17', '21'] - services: - db: - image: postgres - ports: - - 5432:5432 - env: - POSTGRES_DB: bigtop_manager - POSTGRES_PASSWORD: postgres - prom: - image: prom/prometheus - ports: - - 9090:9090 steps: - uses: actions/checkout@v4 - uses: actions/setup-java@v4 @@ -48,4 +49,4 @@ jobs: distribution: 'temurin' java-version: ${{ matrix.java }} cache: 'maven' - - run: ./mvnw clean install -B -Djava.version=${{ matrix.java }} + - run: ./mvnw clean install -DskipTests -B -Djava.version=${{ matrix.java }} diff --git a/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/AgentApplicationTests.java b/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/AgentApplicationTests.java deleted file mode 100644 index 7d93c6b1..00000000 --- a/bigtop-manager-agent/src/test/java/org/apache/bigtop/manager/agent/AgentApplicationTests.java +++ /dev/null @@ -1,47 +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.agent; - -import org.apache.bigtop.manager.agent.monitoring.AgentHostMonitoring; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -import com.fasterxml.jackson.databind.JsonNode; - -import java.net.UnknownHostException; -import java.text.DecimalFormat; - -@SpringBootTest -public class AgentApplicationTests { - - @Test - void contextLoads() {} - - @Test - void getHostAgentInfo() throws UnknownHostException { - JsonNode hostInfo = AgentHostMonitoring.getHostInfo(); - System.out.println(hostInfo.toPrettyString()); - } - - @Test - void testNum() { - System.out.println(new DecimalFormat("#.00").format(123.2344)); - } -} diff --git a/bigtop-manager-bom/pom.xml b/bigtop-manager-bom/pom.xml index 6db0c611..abd15d6e 100644 --- a/bigtop-manager-bom/pom.xml +++ b/bigtop-manager-bom/pom.xml @@ -61,12 +61,6 @@ import - - org.springframework - spring-test - test - - org.springdoc springdoc-openapi-starter-webmvc-ui diff --git a/bigtop-manager-server/pom.xml b/bigtop-manager-server/pom.xml index acbe9ffb..c3e6ccf8 100644 --- a/bigtop-manager-server/pom.xml +++ b/bigtop-manager-server/pom.xml @@ -124,12 +124,6 @@ true - - org.springframework.boot - spring-boot-starter-test - test - - org.springdoc springdoc-openapi-starter-webmvc-ui diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/utils/ResponseEntity.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/utils/ResponseEntity.java index 81769b13..c4a758b4 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/utils/ResponseEntity.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/utils/ResponseEntity.java @@ -23,6 +23,8 @@ import lombok.Data; +import java.util.Objects; + @Data public class ResponseEntity { @@ -75,4 +77,12 @@ public static ResponseEntity error(ResponseStatus status, String appendMe public static ResponseEntity error(ApiExceptionEnum ex) { return new ResponseEntity<>(ex.getCode(), ex.getMessage()); } + + public Boolean isSuccess() { + return Objects.equals(code, ResponseStatus.SUCCESS.getCode()); + } + + public Boolean isFailed() { + return !isSuccess(); + } } diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/ServerApplicationTests.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/ServerApplicationTests.java deleted file mode 100644 index 6e2937ef..00000000 --- a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/ServerApplicationTests.java +++ /dev/null @@ -1,44 +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; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.web.reactive.function.client.WebClient; - -import reactor.core.publisher.Mono; - -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -class ServerApplicationTests { - - @Test - void contextLoads() {} - - @Test - public void queryMonitoring() { - WebClient webClient = - WebClient.builder().baseUrl("http://localhost:9090").build(); - Mono body = webClient - .get() - .uri("/api/v1/query?query=absent(up{job=bm-agent-host}==1)", "") - .retrieve() - .bodyToMono(String.class); - System.out.println(body.block()); - } -} diff --git a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/ClusterControllerTest.java b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/ClusterControllerTest.java new file mode 100644 index 00000000..36516012 --- /dev/null +++ b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/controller/ClusterControllerTest.java @@ -0,0 +1,115 @@ +/* + * 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.ClusterReq; +import org.apache.bigtop.manager.server.model.vo.ClusterVO; +import org.apache.bigtop.manager.server.service.ClusterService; +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.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class ClusterControllerTest { + + @Mock + private ClusterService clusterService; + + @InjectMocks + private ClusterController clusterController; + + 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 listReturnsAllClusters() { + List clusters = Arrays.asList(new ClusterVO(), new ClusterVO()); + when(clusterService.list()).thenReturn(clusters); + + ResponseEntity> response = clusterController.list(); + + assertTrue(response.isSuccess()); + assertEquals(clusters, response.getData()); + } + + @Test + void getReturnsClusterById() { + Long id = 1L; + ClusterVO cluster = new ClusterVO(); + when(clusterService.get(id)).thenReturn(cluster); + + ResponseEntity response = clusterController.get(id); + + assertTrue(response.isSuccess()); + assertEquals(cluster, response.getData()); + } + + @Test + void updateModifiesCluster() { + Long id = 1L; + ClusterReq clusterReq = new ClusterReq(); + ClusterVO updatedCluster = new ClusterVO(); + when(clusterService.update(eq(id), any())).thenReturn(updatedCluster); + + ResponseEntity response = clusterController.update(id, clusterReq); + + assertTrue(response.isSuccess()); + assertEquals(updatedCluster, response.getData()); + } + + @Test + void getReturnsNotFoundForInvalidId() { + Long id = 999L; + when(clusterService.get(id)).thenReturn(null); + + ResponseEntity response = clusterController.get(id); + + assertTrue(response.isSuccess()); + assertNull(response.getData()); + } +} diff --git a/pom.xml b/pom.xml index 4e2d3477..a894fd5e 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,10 @@ 3.25.3 3.1.0.RELEASE + + 5.10.3 + 5.12.0 + 1.5.5.Final 1.18.30 0.2.0 @@ -128,6 +132,27 @@ + + + org.junit.jupiter + junit-jupiter + ${junit.version} + test + + + org.mockito + mockito-core + ${mockito.version} + test + + + org.mockito + mockito-junit-jupiter + ${mockito.version} + test + + +