Skip to content

Commit

Permalink
Create helper service for language
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibstars committed Sep 17, 2023
1 parent 9b53713 commit 0490fb0
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.github.thibstars.btsd.internal.PropertiesServiceImpl;
import com.github.thibstars.btsd.irail.client.LiveBoardServiceImpl;
import com.github.thibstars.btsd.irail.client.StationServiceImpl;
import com.github.thibstars.btsd.irail.helper.LanguageService;
import com.github.thibstars.btsd.irail.helper.LanguageServiceImpl;
import okhttp3.OkHttpClient;

/**
Expand Down Expand Up @@ -38,10 +40,12 @@ public void run() {

PreferencesService preferencesService = new PreferencesServiceImpl();

LanguageService languageService = new LanguageServiceImpl();

this.creatable = new Services(
new PropertiesServiceImpl(),
new LiveBoardServiceImpl(okHttpClient, objectMapper),
new StationServiceImpl(okHttpClient, objectMapper),
new LiveBoardServiceImpl(okHttpClient, objectMapper, languageService),
new StationServiceImpl(okHttpClient, objectMapper, languageService),
preferencesService,
new I18NServiceImpl(preferencesService)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.thibstars.btsd.irail.exceptions.ClientException;
import com.github.thibstars.btsd.irail.helper.LanguageService;
import com.github.thibstars.btsd.irail.model.LiveBoard;
import java.io.IOException;
import java.util.Objects;
Expand Down Expand Up @@ -30,9 +31,12 @@ public class LiveBoardServiceImpl implements LiveBoardService {

private final ObjectMapper objectMapper;

public LiveBoardServiceImpl(OkHttpClient client, ObjectMapper objectMapper) {
private final LanguageService languageService;

public LiveBoardServiceImpl(OkHttpClient client, ObjectMapper objectMapper, LanguageService languageService) {
this.client = client;
this.objectMapper = objectMapper;
this.languageService = languageService;
}

@Override
Expand All @@ -48,7 +52,7 @@ private LiveBoard fetchLiveBoard(String id, String language) throws IOException
LOGGER.info("Fetching live board for station: {}", id);

Request request = new Request.Builder()
.url(URL.replace(ID_PLACEHOLDER, id).replace(LANG_PLACEHOLDER, language))
.url(URL.replace(ID_PLACEHOLDER, id).replace(LANG_PLACEHOLDER, languageService.getLanguageOrFallback(language)))
.build();

ResponseBody responseBody;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.thibstars.btsd.irail.exceptions.ClientException;
import com.github.thibstars.btsd.irail.helper.LanguageService;
import com.github.thibstars.btsd.irail.model.Station;
import com.github.thibstars.btsd.irail.model.Stations;
import com.google.common.cache.CacheBuilder;
Expand All @@ -11,10 +12,8 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand All @@ -37,17 +36,18 @@ public class StationServiceImpl implements StationService {

private static final String URL = "https://api.irail.be/stations?format=json&lang=" + LANG_PLACEHOLDER;

private static final List<String> SUPPORTED_LANGS = List.of("en", "nl", "fr", "de");

private final OkHttpClient client;

private final ObjectMapper objectMapper;

private final LanguageService languageService;

private final LoadingCache<String, Map<String, Station>> cache;

public StationServiceImpl(OkHttpClient client, ObjectMapper objectMapper) {
public StationServiceImpl(OkHttpClient client, ObjectMapper objectMapper, LanguageService languageService) {
this.client = client;
this.objectMapper = objectMapper;
this.languageService = languageService;
CacheLoader<String, Map<String, Station>> loader = new CacheLoader<>() {
@NotNull
@Override
Expand All @@ -74,28 +74,17 @@ public Map<String, Map<String, Station>> loadAll(@NotNull Iterable<? extends Str

@Override
public Set<Station> getStations(String language) {
Optional<String> optionalLanguage = SUPPORTED_LANGS.stream()
.filter(lang -> lang.equals(language))
.findFirst();

String fallbackLanguage = SUPPORTED_LANGS.get(0);
if (optionalLanguage.isEmpty()) {
LOGGER.warn("Language {} is not supported, using {} as a fallback.", language, fallbackLanguage);
}

String languageOrFallback = optionalLanguage.orElse(fallbackLanguage);

return new HashSet<>(cache.getUnchecked(languageOrFallback).values());
return new HashSet<>(cache.getUnchecked(languageService.getLanguageOrFallback(language)).values());
}

private Map<String, Map<String, Station>> getAllStations() {
return cache.getAllPresent(SUPPORTED_LANGS);
return cache.getAllPresent(languageService.getSupportedLanguages());
}

private Map<String, Map<String, Station>> fetchAllStations() throws IOException {
Map<String, Map<String, Station>> stationMap = new HashMap<>();

for (String language : SUPPORTED_LANGS) {
for (String language : languageService.getSupportedLanguages()) {
stationMap.put(language, fetchStations(language));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.thibstars.btsd.irail.helper;

import java.util.Set;

/**
* @author Thibault Helsmoortel
*/
public interface LanguageService {

Set<String> getSupportedLanguages();

String getLanguageOrFallback(String language);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.thibstars.btsd.irail.helper;

import java.util.Optional;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author Thibault Helsmoortel
*/
public class LanguageServiceImpl implements LanguageService {

private static final Logger LOGGER = LoggerFactory.getLogger(LanguageServiceImpl.class);

private static final Set<String> SUPPORTED_LANGS = Set.of("en", "nl", "fr", "de");


@Override
public Set<String> getSupportedLanguages() {
return SUPPORTED_LANGS;
}

@Override
public String getLanguageOrFallback(String language) {
Optional<String> optionalLanguage = SUPPORTED_LANGS.stream()
.filter(lang -> lang.equals(language))
.findFirst();

String fallbackLanguage = SUPPORTED_LANGS.stream().findFirst().orElseThrow();
if (optionalLanguage.isEmpty()) {
LOGGER.warn("Language {} is not supported, using {} as a fallback.", language, fallbackLanguage);
}

return optionalLanguage.orElse(fallbackLanguage);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.thibstars.btsd.irail.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.thibstars.btsd.irail.helper.LanguageService;
import com.github.thibstars.btsd.irail.model.LiveBoard;
import java.io.IOException;
import okhttp3.Call;
Expand All @@ -22,6 +23,7 @@ class LiveBoardServiceImplTest {
void shouldGetForStation() throws IOException {
OkHttpClient client = Mockito.mock(OkHttpClient.class);
ObjectMapper objectMapper = Mockito.mock(ObjectMapper.class);
LanguageService languageService = Mockito.mock(LanguageService.class);

Call call = Mockito.mock(Call.class);
Response response = Mockito.mock(Response.class);
Expand Down Expand Up @@ -84,9 +86,11 @@ void shouldGetForStation() throws IOException {
LiveBoard liveBoard = Mockito.mock(LiveBoard.class);
Mockito.when(objectMapper.readValue(responseBody.string(), LiveBoard.class)).thenReturn(liveBoard);

LiveBoardService liveBoardService = new LiveBoardServiceImpl(client, objectMapper);
String language = "en";
Mockito.when(languageService.getLanguageOrFallback(ArgumentMatchers.anyString())).thenReturn(language);
LiveBoardService liveBoardService = new LiveBoardServiceImpl(client, objectMapper, languageService);

LiveBoard result = liveBoardService.getForStation("6", "en").orElseThrow();
LiveBoard result = liveBoardService.getForStation("6", language).orElseThrow();

Assertions.assertNotNull(result, "Result must not be null.");
Assertions.assertEquals(liveBoard, result, "Result must match the expected.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.thibstars.btsd.irail.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.thibstars.btsd.irail.helper.LanguageService;
import com.github.thibstars.btsd.irail.model.Station;
import com.github.thibstars.btsd.irail.model.Stations;
import java.io.IOException;
Expand All @@ -24,6 +25,7 @@ class StationServiceImplTest {
void shouldGetStations() throws IOException {
OkHttpClient client = Mockito.mock(OkHttpClient.class);
ObjectMapper objectMapper = Mockito.mock(ObjectMapper.class);
LanguageService languageService = Mockito.mock(LanguageService.class);

Call call = Mockito.mock(Call.class);
Response response = Mockito.mock(Response.class);
Expand All @@ -50,9 +52,12 @@ void shouldGetStations() throws IOException {
Stations stations = new Stations(Set.of(station));
Mockito.when(objectMapper.readValue(responseBody.string(), Stations.class)).thenReturn(stations);

StationServiceImpl stationService = new StationServiceImpl(client, objectMapper);
String language = "en";
Mockito.when(languageService.getLanguageOrFallback(ArgumentMatchers.anyString())).thenReturn(language);
Mockito.when(languageService.getSupportedLanguages()).thenReturn(Set.of("en"));
StationServiceImpl stationService = new StationServiceImpl(client, objectMapper, languageService);

Set<Station> result = stationService.getStations("en");
Set<Station> result = stationService.getStations(language);

Assertions.assertNotNull(result, "Result must not be null.");
Assertions.assertFalse(result.isEmpty(), "Result must not be empty.");
Expand Down

0 comments on commit 0490fb0

Please sign in to comment.