Skip to content

Commit

Permalink
BIGTOP-4174: Add a separate ci job for unit testing (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinw66 authored Jul 30, 2024
1 parent 538b969 commit 7dffcd8
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 116 deletions.
27 changes: 14 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,30 @@ 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
with:
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 }}

This file was deleted.

6 changes: 0 additions & 6 deletions bigtop-manager-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
6 changes: 0 additions & 6 deletions bigtop-manager-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import lombok.Data;

import java.util.Objects;

@Data
public class ResponseEntity<T> {

Expand Down Expand Up @@ -75,4 +77,12 @@ public static <T> ResponseEntity<T> error(ResponseStatus status, String appendMe
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();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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());
}
}
25 changes: 25 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit 7dffcd8

Please sign in to comment.