This is an automated email from the ASF dual-hosted git repository.
wuzhiguo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/bigtop-manager.git
The following commit(s) were added to refs/heads/main by this push:
new 7dffcd8 BIGTOP-4174: Add a separate ci job for unit testing (#29)
7dffcd8 is described below
commit 7dffcd8d1ebb44d65c0f981ab792c490e868c658
Author: Zhiguo Wu <[email protected]>
AuthorDate: Tue Jul 30 14:56:51 2024 +0800
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 ++
.../manager/server/ServerApplicationTests.java | 44 --------
.../server/controller/ClusterControllerTest.java | 115 +++++++++++++++++++++
pom.xml | 25 +++++
8 files changed, 164 insertions(+), 116 deletions(-)
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 4f6357b..f4b4530 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 7d93c6b..0000000
---
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 6db0c61..abd15d6 100644
--- a/bigtop-manager-bom/pom.xml
+++ b/bigtop-manager-bom/pom.xml
@@ -61,12 +61,6 @@
<scope>import</scope>
</dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <scope>test</scope>
- </dependency>
-
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
diff --git a/bigtop-manager-server/pom.xml b/bigtop-manager-server/pom.xml
index acbe9ff..c3e6ccf 100644
--- a/bigtop-manager-server/pom.xml
+++ b/bigtop-manager-server/pom.xml
@@ -124,12 +124,6 @@
<optional>true</optional>
</dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
-
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
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 81769b1..c4a758b 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 org.apache.bigtop.manager.server.enums.ResponseStatus;
import lombok.Data;
+import java.util.Objects;
+
@Data
public class ResponseEntity<T> {
@@ -75,4 +77,12 @@ public class ResponseEntity<T> {
public static <T> ResponseEntity<T> 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 6e2937e..0000000
---
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<String> 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 0000000..3651601
--- /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<MessageSourceUtils> 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<ClusterVO> clusters = Arrays.asList(new ClusterVO(), new
ClusterVO());
+ when(clusterService.list()).thenReturn(clusters);
+
+ ResponseEntity<List<ClusterVO>> 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<ClusterVO> 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<ClusterVO> 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<ClusterVO> response = clusterController.get(id);
+
+ assertTrue(response.isSuccess());
+ assertNull(response.getData());
+ }
+}
diff --git a/pom.xml b/pom.xml
index 4e2d347..a894fd5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -65,6 +65,10 @@
<protobuf-java-util.version>3.25.3</protobuf-java-util.version>
<grpc-spring-boot.version>3.1.0.RELEASE</grpc-spring-boot.version>
+ <!-- JUnit/Mockito -->
+ <junit.version>5.10.3</junit.version>
+ <mockito.version>5.12.0</mockito.version>
+
<mapstruct.version>1.5.5.Final</mapstruct.version>
<lombok.version>1.18.30</lombok.version>
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
@@ -128,6 +132,27 @@
</dependencies>
</dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter</artifactId>
+ <version>${junit.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>${mockito.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-junit-jupiter</artifactId>
+ <version>${mockito.version}</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
<build>
<pluginManagement>
<plugins>