Skip to content

Commit

Permalink
chore: upgrade gradle,java and spring-boot (#546)
Browse files Browse the repository at this point in the history
* chore: upgrade gradle,java and spring-boot

Bumped gradle to 8.5
Bumped spring-boot to 2.5.3
Removed useless BookiesServiceImplTest that was not asserting anything.
Removed static nature of HttpUtils to make it injectable and better testable (favor Mockito over staticly mocking things using Powermock).

* test: revert page helper version

Turned out newer versions of page helper do not work zero indexed but rather use one as the first page.

* test: fix flaky DashboardServiceImplTest

Removed unnecessary mocking of httpUtil.
The id generation is not always deterministic when saving an entity. Therefore, I opted to let the test code use the id returned by the save method instead of relying on a deterministic value.
Enabled distTar again by replacing the deprecated property by the new one.

* fix: make getTopicsStatsImplTest deterministic

---------

Co-authored-by: JonasG <[email protected]>
  • Loading branch information
Jonas Geiregat and JonasG authored Feb 16, 2024
1 parent 4476f5e commit 9e34505
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 182 deletions.
11 changes: 5 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

buildscript {
ext {
springBootVersion = '2.0.2.RELEASE'
springBootVersion = '2.5.3'
}
repositories {
mavenCentral()
Expand Down Expand Up @@ -72,12 +72,13 @@ task incrementVersion {
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Finchley.SR2'
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2020.0.6'
}
applyMavenExclusions = false
}

distributions {

main {
contents {
from 'src/main/resources/application.properties'
Expand All @@ -87,7 +88,7 @@ distributions {
}

distTar {
archiveName "pulsar-manager.tar"
archiveFileName = "pulsar-manager.tar"
}

jar {
Expand All @@ -108,7 +109,7 @@ dependencies {

implementation group: 'org.springframework.boot', name: 'spring-boot-starter', version: springBootVersion
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-zuul', version: springBootVersion
implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-zuul', version: '2.2.10.RELEASE'
implementation group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: springMybatisVersion
implementation group: 'com.github.pagehelper', name: 'pagehelper', version: pagehelperVersion
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: springBootVersion
Expand Down Expand Up @@ -152,8 +153,6 @@ dependencies {
compileOnly group: 'org.springframework.boot', name: 'spring-boot-devtools', version: springBootVersion
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: springBootVersion
testImplementation group: 'org.mockito', name: 'mockito-core', version: mockitoVersion
testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: apiMockitoVersion
testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: mockitoJunit4Version

constraints {
implementation("org.bouncycastle:bcprov-jdk15on:${bouncycastleVersion}")
Expand Down
10 changes: 4 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ hibernateValidatorVersion=6.0.13.Final
jsonWebTokenVersion=0.9.0
jsonWebTokenApiVersion=0.10.5
jsonWebTokenImplVersion=0.10.5
lombokVersion=1.18.26
pageHelperVersion=1.2.4
mockitoVersion=2.8.47
lombokVersion=1.18.30
pageHelperVersion=2.1.0
mockitoVersion=3.11.2
guavaVersion=21.0
pulsarVersion=2.7.0
athenzVersion=1.10.9
swagger2Version=2.9.2
swaggeruiVersion=2.9.2
apiMockitoVersion=1.7.1
mockitoJunit4Version=1.7.1
gsonVersion=2.8.2
postgresqlVersion=42.2.5
herddbVersion=0.24.0
herddbVersion=0.28.0
commonsValidatorVersion=1.6
bkvmVersion=3.1.1
tomcatVersion=8.5.31
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
}
}
return super.getTomcatWebServer(tomcat);
} catch (IOException | ServletException ex) {
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.apache.pulsar.manager.service.EnvironmentCacheService;
import org.apache.pulsar.manager.utils.HttpUtil;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -49,6 +51,9 @@ public class BookiesServiceImpl implements BookiesService {
@Value("${backend.jwt.token}")
private static String pulsarJwtToken;

@Autowired
HttpUtil httpUtil;


private static final Map<String, String> header = new HashMap<String, String>(){{
put("Authorization", String.format("Bearer %s", pulsarJwtToken));
Expand Down Expand Up @@ -76,15 +81,15 @@ public Map<String, Object> getBookiesList(Integer pageNum, Integer pageSize, Str
if(StringUtils.isBlank(bookieUrl)){
return bookiesMap;
}
String rwBookieList = HttpUtil.doGet(
String rwBookieList = httpUtil.doGet(
bookieUrl + "/api/v1/bookie/list_bookies?type=rw&print_hostnames=true", header);
Map<String, String> rwBookies = gson.fromJson(
rwBookieList, new TypeToken<Map<String, String>>() {}.getType());
String roBookieList = HttpUtil.doGet(
String roBookieList = httpUtil.doGet(
bookieUrl + "/api/v1/bookie/list_bookies?type=ro&print_hostnames=true", header);
Map<String, String> roBookies = gson.fromJson(
roBookieList, new TypeToken<Map<String, String>>() {}.getType());
String listBookieInfo = HttpUtil.doGet(
String listBookieInfo = httpUtil.doGet(
bookieUrl + "/api/v1/bookie/list_bookie_info", header);
Map<String, String> listBookies = gson.fromJson(
listBookieInfo, new TypeToken<Map<String, String>>() {}.getType());
Expand Down Expand Up @@ -122,13 +127,13 @@ public Map<String, Object> getBookiesList(Integer pageNum, Integer pageSize, Str

public String forwardBookiesHeartbeat(String bookie) {
bookie = checkBookie(bookie);
return HttpUtil.doGet(bookie + "/heartbeat", header);
return httpUtil.doGet(bookie + "/heartbeat", header);
}

public void forwardGc(String bookie) {
bookie = checkBookie(bookie);
try {
HttpUtil.doPut(bookie + "/api/v1/bookie/gc", header, "");
httpUtil.doPut(bookie + "/api/v1/bookie/gc", header, "");
} catch (UnsupportedEncodingException e) {

}
Expand All @@ -141,7 +146,7 @@ public void forwardAutorecovery(List<String> bookieSrc, List<String> bookieDest,
Map<String, Object> body = Maps.newHashMap();
body.put("bookie_src", bookieSrc);
body.put("bookie_dest", bookieDest);
HttpUtil.doPut(bookieUrl + "/api/v1/autorecovery/bookie/", header, gson.toJson(body));
httpUtil.doPut(bookieUrl + "/api/v1/autorecovery/bookie/", header, gson.toJson(body));
} catch (UnsupportedEncodingException e) {

}
Expand All @@ -153,7 +158,7 @@ public void forwardBookieDecommission(String bookie) {
body.put("bookie_src", bookie);
try {
Gson gson = new Gson();
HttpUtil.doPut(bookie + "/api/v1/autorecovery/decommission", header, gson.toJson(body));
httpUtil.doPut(bookie + "/api/v1/autorecovery/decommission", header, gson.toJson(body));
} catch (UnsupportedEncodingException e) {

}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/apache/pulsar/manager/utils/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static void initHttpClient() {
}
}

public static String doGet(String url, Map<String, String> header){
public String doGet(String url, Map<String, String> header){
HttpGet request = new HttpGet(url);
return httpRequest(request, header);
}
Expand All @@ -143,21 +143,21 @@ public static String doGet(String url, Map<String, String> header){
* @return HTTP response information
* @throws UnsupportedEncodingException
*/
public static String doPost(String url, Map<String, String> header, String body)
public String doPost(String url, Map<String, String> header, String body)
throws UnsupportedEncodingException {
HttpPost request = new HttpPost(url);
request.setEntity(new StringEntity(body));
return httpRequest(request, header);
}

public static String doPut(String url, Map<String, String> header, String body)
public String doPut(String url, Map<String, String> header, String body)
throws UnsupportedEncodingException {
HttpPut request = new HttpPut(url);
request.setEntity(new StringEntity(body));
return httpRequest(request, header);
}

public static String httpRequest(HttpUriRequest request, Map<String, String> header) {
public String httpRequest(HttpUriRequest request, Map<String, String> header) {
CloseableHttpResponse response = null;
try {
for (Map.Entry<String, String> entry: header.entrySet()) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,41 @@
*/
package org.apache.pulsar.manager.service;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.pulsar.manager.PulsarManagerApplication;
import org.apache.pulsar.manager.entity.*;
import org.apache.pulsar.manager.entity.ConsumerStatsEntity;
import org.apache.pulsar.manager.entity.ConsumersStatsRepository;
import org.apache.pulsar.manager.entity.NamespaceEntity;
import org.apache.pulsar.manager.entity.NamespacesRepository;
import org.apache.pulsar.manager.entity.TenantEntity;
import org.apache.pulsar.manager.entity.TenantsRepository;
import org.apache.pulsar.manager.entity.TopicStatsEntity;
import org.apache.pulsar.manager.entity.TopicsStatsRepository;
import org.apache.pulsar.manager.profiles.HerdDBTestProfile;
import org.apache.pulsar.manager.utils.HttpUtil;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PowerMockIgnore( {"javax.*", "sun.*", "com.sun.*", "org.xml.*", "org.w3c.*"})
@PrepareForTest(HttpUtil.class)
@TestPropertySource(locations= "classpath:test-bookie.properties")
@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:test-bookie.properties")
@SpringBootTest(
classes = {
PulsarManagerApplication.class,
HerdDBTestProfile.class
}
classes = {
PulsarManagerApplication.class,
HerdDBTestProfile.class
}
)
@ActiveProfiles("test")
public class DashboardServiceImplTest {
Expand All @@ -67,8 +67,9 @@ public class DashboardServiceImplTest {
@Autowired
NamespacesRepository namespacesRepository;

@Value("${backend.jwt.token}")
private static String pulsarJwtToken;
@MockBean
HttpUtil httpUtil;


@Test
public void getDashboardStatsTest() {
Expand All @@ -81,21 +82,7 @@ public void getDashboardStatsTest() {
int producerPerTopic = 1;
int consumerPerTopic = 1;

PowerMockito.mockStatic(HttpUtil.class);
Map<String, String> header = Maps.newHashMap();
header.put("Content-Type", "application/json");
if (StringUtils.isNotBlank(pulsarJwtToken)) {
header.put("Authorization", String.format("Bearer %s", pulsarJwtToken));
}
PowerMockito.when(HttpUtil.doGet("http://localhost:8050/api/v1/bookie/list_bookies?type=rw&print_hostnames=true", header))
.thenReturn("{\"192.168.2.116:3181\" : \"192.168.2.116\"}");
PowerMockito.when(HttpUtil.doGet("http://localhost:8080/admin/v2/brokers/standalone", header))
.thenReturn("{ }");
PowerMockito.when(HttpUtil.doGet("http://localhost:8050/api/v1/bookie/list_bookie_info", header))
.thenReturn("{\"192.168.2.116:3181\" : \": {Free: 48920571904(48.92GB), Total: 250790436864(250.79GB)}," +
"\",\"ClusterInfo: \" : \"{Free: 48920571904(48.92GB), Total: 250790436864(250.79GB)}\" }");

long topicStatsId = 0L;
long topicStatsId;
for (String tenant: tenantList) {
TenantEntity tenantEntity = new TenantEntity();
tenantEntity.setEnvironmentName(environmentList.get(0));
Expand Down Expand Up @@ -123,21 +110,22 @@ public void getDashboardStatsTest() {
topicStatsEntity.setTopic("neutral");
topicStatsEntity.setProducerCount(producerPerTopic);
topicStatsEntity.setTime_stamp(timestamp);
topicsStatsRepository.save(topicStatsEntity);
topicStatsId++;
topicStatsId = topicsStatsRepository.save(topicStatsEntity);
for (int i = 0; i < consumerPerTopic; i++) {
ConsumerStatsEntity consumerStatsEntity = new ConsumerStatsEntity();
consumerStatsEntity.setConsumer("neutral");
consumerStatsEntity.setTopicStatsId(topicStatsId);
consumerStatsEntity.setTime_stamp(timestamp);
consumersStatsRepository.save(consumerStatsEntity);
System.out.println("2 saves, TS " + timestamp);
}
}
}
}
}

long topicCount = clusterList.size() * brokerList.size();
System.out.println("searching for env " + environmentList.get(0));
Map<String, Object> dashboardStats = dashboardService.getDashboardStats(Arrays.asList(environmentList.get(0)));
Assert.assertEquals(clusterList.size(), dashboardStats.get("totalClusterCount"));
Assert.assertEquals(brokerList.size(), dashboardStats.get("totalBrokerCount"));
Expand Down
Loading

0 comments on commit 9e34505

Please sign in to comment.