Skip to content

Commit f933974

Browse files
committed
Fix quality flaws
1 parent d64c11b commit f933974

14 files changed

+88
-70
lines changed

sonar-project.properties

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sonar.exclusions=src/test/resources/*.sql

src/main/java/org/blonding/mpg/GrandslamPopulatorBatchApplication.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
1515
import org.springframework.batch.core.job.builder.JobBuilder;
1616
import org.springframework.batch.core.listener.ExecutionContextPromotionListener;
17-
import org.springframework.beans.factory.annotation.Autowired;
1817
import org.springframework.boot.SpringApplication;
1918
import org.springframework.boot.autoconfigure.SpringBootApplication;
2019
import org.springframework.context.annotation.Bean;
@@ -25,11 +24,11 @@
2524
@SpringBootApplication
2625
public class GrandslamPopulatorBatchApplication {
2726

28-
@Autowired
29-
private StepBuilderFactory steps;
27+
private final StepBuilderFactory steps;
3028

31-
GrandslamPopulatorBatchApplication() {
29+
GrandslamPopulatorBatchApplication(StepBuilderFactory steps) {
3230
super();
31+
this.steps = steps;
3332
}
3433

3534
public static void main(String[] args) {
@@ -67,19 +66,17 @@ public Step stepDataBaseUpdateGrandSlamDays(DataBaseUpdateGrandSlamDaysTasklet d
6766
}
6867

6968
@Bean
70-
public Job jobGrandSlamPopulator(JobBuilderFactory jobBuilderFactory, MpgDatasTasklet mpgDatas, WhichUsersTasklet whichUsers,
71-
DataBaseUpdateUsersTasklet dataBaseUpdateUsers, DataBaseUpdateLeaguesTasklet dataBaseUpdateLeagues,
72-
DataBaseUpdateRankingTeamsTasklet dataBaseUpdateRankingTeams, DataBaseUpdateGrandSlamDaysTasklet dataBaseUpdateGrandSlamDays) {
69+
public Job jobGrandSlamPopulator(JobBuilderFactory jobBuilderFactory, MpgDatasTasklet mpgDatas, WhichUsersTasklet whichUsers, DataBaseUpdateUsersTasklet dataBaseUpdateUsers,
70+
DataBaseUpdateLeaguesTasklet dataBaseUpdateLeagues, DataBaseUpdateRankingTeamsTasklet dataBaseUpdateRankingTeams, DataBaseUpdateGrandSlamDaysTasklet dataBaseUpdateGrandSlamDays) {
7371
JobBuilder jobBuilder = jobBuilderFactory.get("jobGrandSlamPopulator");
7472
Step stepMpgDatas = stepMpgDatas(mpgDatas);
7573
Step stepWhichUsers = stepWhichUsers(whichUsers);
7674
Step stepDataBaseUpdateUsers = stepDataBaseUpdateUsers(dataBaseUpdateUsers);
7775
Step stepDataBaseUpdateLeagues = stepDataBaseUpdateLeagues(dataBaseUpdateLeagues);
7876
Step stepDataBaseUpdateRankingTeams = stepDataBaseUpdateRankingTeams(dataBaseUpdateRankingTeams);
7977
Step stepDataBaseUpdateGrandSlamDays = stepDataBaseUpdateGrandSlamDays(dataBaseUpdateGrandSlamDays);
80-
return jobBuilder.start(stepMpgDatas).next(stepWhichUsers).on(ExitStatus.STOPPED.getExitCode()).end().on(ExitStatus.COMPLETED.getExitCode())
81-
.to(stepDataBaseUpdateUsers).next(stepDataBaseUpdateLeagues).next(stepDataBaseUpdateRankingTeams)
82-
.next(stepDataBaseUpdateGrandSlamDays).end().build();
78+
return jobBuilder.start(stepMpgDatas).next(stepWhichUsers).on(ExitStatus.STOPPED.getExitCode()).end().on(ExitStatus.COMPLETED.getExitCode()).to(stepDataBaseUpdateUsers)
79+
.next(stepDataBaseUpdateLeagues).next(stepDataBaseUpdateRankingTeams).next(stepDataBaseUpdateGrandSlamDays).end().build();
8380
}
8481

8582
@Bean

src/main/java/org/blonding/mpg/tasklet/DataBaseUpdateGrandSlamDaysTasklet.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ public class DataBaseUpdateGrandSlamDaysTasklet implements Tasklet {
3030

3131
private static final Logger LOG = LoggerFactory.getLogger(DataBaseUpdateGrandSlamDaysTasklet.class);
3232

33-
@Autowired
34-
private GrandSlamRepository grandSlamRepository;
33+
private final GrandSlamRepository grandSlamRepository;
34+
35+
private final GrandSlamDayRepository grandSlamDayRepository;
3536

3637
@Autowired
37-
private GrandSlamDayRepository grandSlamDayRepository;
38+
private DataBaseUpdateGrandSlamDaysTasklet(GrandSlamRepository grandSlamRepository, GrandSlamDayRepository grandSlamDayRepository) {
39+
super();
40+
this.grandSlamRepository = grandSlamRepository;
41+
this.grandSlamDayRepository = grandSlamDayRepository;
42+
}
3843

3944
@Override
4045
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
@@ -62,8 +67,7 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
6267
}
6368

6469
// Sort by pts and golf diff
65-
List<PlayerDayJson> playersOrdered = players.values().stream()
66-
.sorted(Comparator.comparingInt(PlayerDayJson::getPts).thenComparingInt(PlayerDayJson::getDiff).reversed())
70+
List<PlayerDayJson> playersOrdered = players.values().stream().sorted(Comparator.comparingInt(PlayerDayJson::getPts).thenComparingInt(PlayerDayJson::getDiff).reversed())
6771
.collect(Collectors.toCollection(ArrayList::new));
6872

6973
// Add position
@@ -81,7 +85,9 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
8185
gsd.setLabel(label);
8286
gsd.setPlayers(playersOrdered);
8387

84-
LOG.info("Updating '{}': {}", label, getListString(playersOrdered));
88+
if (LOG.isInfoEnabled()) {
89+
LOG.info("Updating '{}': {}", label, getListString(playersOrdered));
90+
}
8591
grandSlamDayRepository.save(gsd);
8692
return RepeatStatus.FINISHED;
8793
}

src/main/java/org/blonding/mpg/tasklet/DataBaseUpdateLeaguesTasklet.java

+12-11
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,29 @@ public class DataBaseUpdateLeaguesTasklet implements Tasklet {
3131

3232
private static final Logger LOG = LoggerFactory.getLogger(DataBaseUpdateLeaguesTasklet.class);
3333

34-
@Autowired
35-
private GrandSlamRepository grandSlamRepository;
34+
private final GrandSlamRepository grandSlamRepository;
35+
36+
private final LeagueRepository leagueRepository;
3637

3738
@Autowired
38-
private LeagueRepository leagueRepository;
39+
private DataBaseUpdateLeaguesTasklet(GrandSlamRepository grandSlamRepository, LeagueRepository leagueRepository) {
40+
super();
41+
this.grandSlamRepository = grandSlamRepository;
42+
this.leagueRepository = leagueRepository;
43+
}
3944

4045
@Override
4146
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
4247
LOG.info("--- Update leagues informations...");
4348
var gs = getOrCreateGrandSlam();
4449

4550
@SuppressWarnings("unchecked")
46-
Map<League, LeagueRanking> leaguesMpgOriginal = (Map<League, LeagueRanking>) contribution.getStepExecution().getJobExecution()
47-
.getExecutionContext().get("leagues");
51+
Map<League, LeagueRanking> leaguesMpgOriginal = (Map<League, LeagueRanking>) contribution.getStepExecution().getJobExecution().getExecutionContext().get("leagues");
4852
if (leaguesMpgOriginal == null) {
4953
throw new UnsupportedOperationException("Object 'leaguesMpgOriginal' cannot be null here");
5054
}
5155
// Copy to new map
52-
Map<League, LeagueRanking> leaguesMpg = leaguesMpgOriginal.entrySet().stream()
53-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
56+
Map<League, LeagueRanking> leaguesMpg = leaguesMpgOriginal.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
5457

5558
List<org.blonding.mpg.model.db.League> leagues = gs.getLeagues();
5659
for (Iterator<org.blonding.mpg.model.db.League> it = leagues.iterator(); it.hasNext();) {
@@ -62,8 +65,7 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
6265
leagueRepository.delete(league);
6366
} else {
6467
int played = getLeagueGamePlayed(leagueMpgEntry.getValue().getRanks());
65-
LOG.info("League update with games played: {} ({}) -> {}", leagueMpgEntry.getKey().getId(), leagueMpgEntry.getKey().getName(),
66-
played);
68+
LOG.info("League update with games played: {} ({}) -> {}", leagueMpgEntry.getKey().getId(), leagueMpgEntry.getKey().getName(), played);
6769
league.setName(leagueMpgEntry.getKey().getName());
6870
league.setType(leagueMpgEntry.getKey().getChampionship().getName());
6971
league.setGamePlayed(played);
@@ -73,8 +75,7 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
7375
for (Entry<League, LeagueRanking> l : leaguesMpg.entrySet()) {
7476
int played = getLeagueGamePlayed(l.getValue().getRanks());
7577
LOG.info("League add with games played: {} ({}) -> {}", l.getKey().getId(), l.getKey().getName(), played);
76-
leagues.add(new org.blonding.mpg.model.db.League(l.getKey().getId(), l.getKey().getChampionship().name(), l.getKey().getName(),
77-
gs.getYear(), gs.getStatus(), gs.getId(), played));
78+
leagues.add(new org.blonding.mpg.model.db.League(l.getKey().getId(), l.getKey().getChampionship().name(), l.getKey().getName(), gs.getYear(), gs.getStatus(), gs.getId(), played));
7879
}
7980
leagueRepository.saveAll(leagues);
8081
return RepeatStatus.FINISHED;

src/main/java/org/blonding/mpg/tasklet/DataBaseUpdateRankingTeamsTasklet.java

+15-13
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,19 @@ public class DataBaseUpdateRankingTeamsTasklet implements Tasklet {
3232

3333
private static final Logger LOG = LoggerFactory.getLogger(DataBaseUpdateRankingTeamsTasklet.class);
3434

35-
@Autowired
36-
private GrandSlamRepository grandSlamRepository;
35+
private final GrandSlamRepository grandSlamRepository;
3736

38-
@Autowired
39-
private PlayerRepository playerRepository;
37+
private final PlayerRepository playerRepository;
38+
39+
private final TeamRepository teamRepository;
4040

4141
@Autowired
42-
private TeamRepository teamRepository;
42+
private DataBaseUpdateRankingTeamsTasklet(GrandSlamRepository grandSlamRepository, PlayerRepository playerRepository, TeamRepository teamRepository) {
43+
super();
44+
this.grandSlamRepository = grandSlamRepository;
45+
this.playerRepository = playerRepository;
46+
this.teamRepository = teamRepository;
47+
}
4348

4449
@Override
4550
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
@@ -50,8 +55,7 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
5055
throw new UnsupportedOperationException("Object 'users' cannot be null here");
5156
}
5257
@SuppressWarnings("unchecked")
53-
Map<League, LeagueRanking> leaguesMpg = (Map<League, LeagueRanking>) contribution.getStepExecution().getJobExecution().getExecutionContext()
54-
.get("leagues");
58+
Map<League, LeagueRanking> leaguesMpg = (Map<League, LeagueRanking>) contribution.getStepExecution().getJobExecution().getExecutionContext().get("leagues");
5559
if (leaguesMpg == null) {
5660
throw new UnsupportedOperationException("Object 'leaguesMpgOriginal' cannot be null here");
5761
}
@@ -81,8 +85,8 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
8185
}
8286
MpgUser user = users.stream().filter(u -> u.getMpgId() == players2usersMpgMap.get(team.getPlayerId())).findFirst().orElseThrow();
8387
var rank = getRank(leaguesMpg, league.getMpgId(), user.getMpgId());
84-
LOG.info("Ranking update for '{} / {}' with victory={} draw={} defeat={} diff={}", league.getMpgId(), user.getName(),
85-
rank.getVictory(), rank.getDraw(), rank.getDefeat(), rank.getDifference());
88+
LOG.info("Ranking update for '{} / {}' with victory={} draw={} defeat={} diff={}", league.getMpgId(), user.getName(), rank.getVictory(), rank.getDraw(), rank.getDefeat(),
89+
rank.getDifference());
8690
var teamMpg = getTeam(leaguesMpg, league.getMpgId(), user.getMpgId());
8791
team.setName(teamMpg.getName());
8892
team.setShortName(teamMpg.getShortName());
@@ -103,8 +107,7 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
103107
var userUid = Long.valueOf(id.split("_")[1]);
104108
MpgUser user = users.stream().filter(u -> u.getMpgId() == userUid).findFirst().orElseThrow();
105109
var rank = getRank(leaguesMpg, league, userUid);
106-
LOG.info("Ranking add for '{} / {}' with victory={} draw={} defeat={} diff={}", league, user.getName(), rank.getVictory(), rank.getDraw(),
107-
rank.getDefeat(), rank.getDifference());
110+
LOG.info("Ranking add for '{} / {}' with victory={} draw={} defeat={} diff={}", league, user.getName(), rank.getVictory(), rank.getDraw(), rank.getDefeat(), rank.getDifference());
108111
var team = new Team();
109112
var teamMpg = getTeam(leaguesMpg, league, user.getMpgId());
110113
team.setPlayerId(usersMpg2PlayersMap.get(userUid));
@@ -134,8 +137,7 @@ private static Rank getRank(Map<League, LeagueRanking> leaguesMpg, String league
134137
for (Entry<League, LeagueRanking> entry : leaguesMpg.entrySet()) {
135138
if (entry.getKey().getId().equals(league)) {
136139
LeagueRanking lr = entry.getValue();
137-
String teamUserId = lr.getTeams().values().stream().filter(p -> p.getUserId().equals("user_" + user)).findFirst().orElseThrow()
138-
.getId();
140+
String teamUserId = lr.getTeams().values().stream().filter(p -> p.getUserId().equals("user_" + user)).findFirst().orElseThrow().getId();
139141
return lr.getRanks().stream().filter(p -> p.getTeamId().equals(teamUserId)).findFirst().orElseThrow();
140142
}
141143
}

src/main/java/org/blonding/mpg/tasklet/DataBaseUpdateUsersTasklet.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ public class DataBaseUpdateUsersTasklet implements Tasklet {
2424

2525
private static final Logger LOG = LoggerFactory.getLogger(DataBaseUpdateUsersTasklet.class);
2626

27+
private final PlayerRepository playerRepository;
28+
2729
@Autowired
28-
private PlayerRepository playerRepository;
30+
private DataBaseUpdateUsersTasklet(PlayerRepository playerRepository) {
31+
super();
32+
this.playerRepository = playerRepository;
33+
}
2934

3035
@Override
3136
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

src/main/java/org/blonding/mpg/tasklet/MpgDatasTasklet.java

+15-11
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,23 @@ public class MpgDatasTasklet implements Tasklet {
3737

3838
private static final String HEADER_AUTHORIZATION = "authorization";
3939

40+
private final MpgConfig mpgConfig;
41+
4042
@Autowired
41-
private MpgConfig mpgConfig;
43+
private MpgDatasTasklet(MpgConfig mpgConfig) {
44+
super();
45+
this.mpgConfig = mpgConfig;
46+
}
4247

4348
@Override
4449
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
4550
LOG.info("--- Retrieve MPG Datas and Leagues to use...");
46-
WebClient client = WebClient.builder().defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).baseUrl(mpgConfig.getUrl())
47-
.build();
51+
WebClient client = WebClient.builder().defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).baseUrl(mpgConfig.getUrl()).build();
4852
String token = client.post().uri("/user/sign-in").accept(MediaType.APPLICATION_JSON)
49-
.body(Mono.just(new UserSignInRequest(mpgConfig.getEmail(), mpgConfig.getPassword())), UserSignInRequest.class).retrieve()
50-
.bodyToMono(UserSignIn.class).blockOptional().orElseThrow().getToken();
51-
Dashboard dasboard = client.get().uri("/dashboard/leagues").accept(MediaType.APPLICATION_JSON).header(HEADER_AUTHORIZATION, token).retrieve()
52-
.toEntity(Dashboard.class).blockOptional().orElseThrow().getBody();
53+
.body(Mono.just(new UserSignInRequest(mpgConfig.getEmail(), mpgConfig.getPassword())), UserSignInRequest.class).retrieve().bodyToMono(UserSignIn.class).blockOptional().orElseThrow()
54+
.getToken();
55+
Dashboard dasboard = client.get().uri("/dashboard/leagues").accept(MediaType.APPLICATION_JSON).header(HEADER_AUTHORIZATION, token).retrieve().toEntity(Dashboard.class).blockOptional()
56+
.orElseThrow().getBody();
5357
if (dasboard == null) {
5458
throw new UnsupportedOperationException("The 'dasboard' cannot be null here");
5559
}
@@ -80,8 +84,8 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
8084
}
8185

8286
private LeagueRanking getLeagueRankingAndTeamInfos(WebClient client, String token, League league) {
83-
var leagueRanking = client.get().uri("/division/" + league.getDivisionId() + "/ranking/standings").accept(MediaType.APPLICATION_JSON)
84-
.header(HEADER_AUTHORIZATION, token).retrieve().toEntity(LeagueRanking.class).blockOptional().orElseThrow().getBody();
87+
var leagueRanking = client.get().uri("/division/" + league.getDivisionId() + "/ranking/standings").accept(MediaType.APPLICATION_JSON).header(HEADER_AUTHORIZATION, token).retrieve()
88+
.toEntity(LeagueRanking.class).blockOptional().orElseThrow().getBody();
8589
if (leagueRanking == null) {
8690
throw new UnsupportedOperationException("The 'leagueRanking' cannot be null here");
8791
}
@@ -90,8 +94,8 @@ private LeagueRanking getLeagueRankingAndTeamInfos(WebClient client, String toke
9094
return null;
9195
}
9296
LOG.info("League {} taken into account", league.getId());
93-
List<Team> teams = client.get().uri("/division/" + league.getDivisionId() + "/teams").accept(MediaType.APPLICATION_JSON)
94-
.header(HEADER_AUTHORIZATION, token).retrieve().toEntity(new ParameterizedTypeReference<List<Team>>() {
97+
List<Team> teams = client.get().uri("/division/" + league.getDivisionId() + "/teams").accept(MediaType.APPLICATION_JSON).header(HEADER_AUTHORIZATION, token).retrieve()
98+
.toEntity(new ParameterizedTypeReference<List<Team>>() {
9599
}).blockOptional().orElseThrow().getBody();
96100
if (teams == null) {
97101
throw new UnsupportedOperationException("The 'teams' cannot be null here");

src/main/java/org/blonding/mpg/tasklet/WhichUsersTasklet.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,26 @@ public class WhichUsersTasklet implements Tasklet {
2424

2525
private static final Logger LOG = LoggerFactory.getLogger(WhichUsersTasklet.class);
2626

27+
private final MpgConfig mpgConfig;
28+
2729
@Autowired
28-
private MpgConfig mpgConfig;
30+
private WhichUsersTasklet(MpgConfig mpgConfig) {
31+
super();
32+
this.mpgConfig = mpgConfig;
33+
}
2934

3035
@Override
3136
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
3237
LOG.info("--- Calculate users intersection...");
3338
@SuppressWarnings("unchecked")
34-
Map<League, LeagueRanking> leagues = (Map<League, LeagueRanking>) chunkContext.getStepContext().getStepExecution().getJobExecution()
35-
.getExecutionContext().get("leagues");
39+
Map<League, LeagueRanking> leagues = (Map<League, LeagueRanking>) chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().get("leagues");
3640
if (leagues == null) {
3741
throw new UnsupportedOperationException("Object 'leagues' cannot be null here");
3842
}
3943
List<MpgUser> usersTmp = new ArrayList<>();
4044
for (LeagueRanking lr : leagues.values()) {
4145
List<MpgUser> currentTeamUsers = lr.getTeams().values().stream()
42-
.map(team -> new MpgUser(Long.valueOf(team.getUserId().substring(team.getUserId().lastIndexOf('_') + 1)),
43-
team.getFirstName() + " " + team.getLastName()))
46+
.map(team -> new MpgUser(Long.valueOf(team.getUserId().substring(team.getUserId().lastIndexOf('_') + 1)), team.getFirstName() + " " + team.getLastName()))
4447
.collect(Collectors.toList());
4548
if (usersTmp.isEmpty()) {
4649
usersTmp.addAll(currentTeamUsers);

src/test/java/org/blonding/mpg/DataBaseUpdateGrandSlamDaysTaskletTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class DataBaseUpdateGrandSlamDaysTaskletTest {
3333
private GrandSlamRepository grandSlamRepository;
3434

3535
@Test
36-
void add() throws Exception {
36+
void add() {
3737
int initial = grandSlamRepository.findOne(Example.of(GrandSlam.fromCurrentRunning())).orElseThrow().getGrandSlamDays().size();
3838
ExecutionContext jobExecutionContext = new ExecutionContext();
3939
JobExecution jobExecution = jobLauncherTestUtils.launchStep("stepDataBaseUpdateGrandSlamDays", jobExecutionContext);

0 commit comments

Comments
 (0)