Skip to content

Commit

Permalink
addressing code smells #EA-3587
Browse files Browse the repository at this point in the history
  • Loading branch information
gsergiu committed Oct 13, 2023
1 parent 3c2e217 commit f7793ce
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
import org.apache.commons.lang3.StringUtils;
import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException;

/**
* Dummy implementation preventing invocation of remote google service
* @author GordeaS
*
*/
public class DummyGLangDetectService extends GoogleLangDetectService {

public DummyGLangDetectService(String googleProjectId,
GoogleTranslationServiceClientWrapper clientWrapperBean) {
super(googleProjectId, clientWrapperBean);
/**
* Constructor using dummy project id
* @param clientWrapperBean the client wrapper implemnetation
*/
public DummyGLangDetectService(GoogleTranslationServiceClientWrapper clientWrapperBean) {
super(GoogleTranslationServiceClientWrapper.MOCK_CLIENT_PROJ_ID, clientWrapperBean);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
import java.util.List;
import eu.europeana.api.translation.definitions.service.exception.TranslationException;

/**
* Dummy implementation preventing invocation of remote google service
* @author GordeaS
*
*/
public class DummyGTranslateService extends GoogleTranslationService {

public DummyGTranslateService(String googleProjectId,
GoogleTranslationServiceClientWrapper clientWrapperBean) {
super(googleProjectId, clientWrapperBean);
/**
* Constructor using dummy project id
* @param clientWrapperBean the client wrapper implemnetation
*/
public DummyGTranslateService(GoogleTranslationServiceClientWrapper clientWrapperBean) {
super(GoogleTranslationServiceClientWrapper.MOCK_CLIENT_PROJ_ID, clientWrapperBean);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
import eu.europeana.api.translation.definitions.service.LanguageDetectionService;
import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException;

/**
* Translation service implementing remote invocation of google language detection service
* @author GordeaS
*
*/
public class GoogleLangDetectService implements LanguageDetectionService {

protected static final Logger LOG = LogManager.getLogger(GoogleLangDetectService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import eu.europeana.api.translation.definitions.service.exception.TranslationException;

/**
* Note that this requires the GOOGLE_APPLICATION_CREDENTIALS environment variable to be available
* Translation service implementing remote invocation of google language detection service
* Note that this requires the GOOGLE_APPLICATION_CREDENTIALS environment variable to be available
* as well as a projectId (defined in the application properties).
* @author GordeaS
*
*/
public class GoogleTranslationService implements TranslationService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import com.google.cloud.translate.v3.TranslationServiceClient;
import com.google.cloud.translate.v3.TranslationServiceSettings;

/**
* Wrapper for google client to handle the proper behavior for different configurations
* @author GordeaS
*
*/
public class GoogleTranslationServiceClientWrapper {

public static final String MOCK_CLIENT_PROJ_ID = "google-test";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@
import org.apache.commons.lang3.StringUtils;
import eu.europeana.api.translation.definitions.service.exception.LanguageDetectionException;

/**
* Dummy implementation preventing invocation of remote pangeanic service
* @author GordeaS
*
*/
public class DummyPangLangDetectService extends PangeanicLangDetectService {

/**
* Constructor using null as endpoint
*/
public DummyPangLangDetectService() {
super(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
import java.util.List;
import eu.europeana.api.translation.definitions.service.exception.TranslationException;

/**
* Dummy implementation preventing invocation of remote pangeanic service
* @author GordeaS
*
*/
public class DummyPangTranslationService extends PangeanicTranslationService{

/**
* Constructor using null as endpoint
*/
public DummyPangTranslationService() {
super(null, null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package eu.europeana.api.translation.config;

import static eu.europeana.api.translation.service.google.GoogleTranslationServiceClientWrapper.MOCK_CLIENT_PROJ_ID;
import java.io.IOException;
import java.util.Arrays;
import java.util.Locale;
Expand Down Expand Up @@ -111,8 +110,7 @@ public PangeanicTranslationService getPangeanicTranslationService(
public GoogleLangDetectService getGoogleLangDetectService(
@Qualifier(BeanNames.BEAN_GOOGLE_TRANSLATION_CLIENT_WRAPPER) GoogleTranslationServiceClientWrapper googleTranslationServiceClientWrapper) {
if (useDummyServices) {
return new DummyGLangDetectService(MOCK_CLIENT_PROJ_ID,
googleTranslationServiceClientWrapper);
return new DummyGLangDetectService(googleTranslationServiceClientWrapper);
} else {
return new GoogleLangDetectService(translationConfig.getGoogleTranslateProjectId(),
googleTranslationServiceClientWrapper);
Expand All @@ -123,7 +121,7 @@ public GoogleLangDetectService getGoogleLangDetectService(
public GoogleTranslationService getGoogleTranslationService(
@Qualifier(BeanNames.BEAN_GOOGLE_TRANSLATION_CLIENT_WRAPPER) GoogleTranslationServiceClientWrapper googleTranslationServiceClientWrapper) {
if (useDummyServices) {
return new DummyGTranslateService(MOCK_CLIENT_PROJ_ID, googleTranslationServiceClientWrapper);
return new DummyGTranslateService(googleTranslationServiceClientWrapper);
} else {
return new GoogleTranslationService(translationConfig.getGoogleTranslateProjectId(),
googleTranslationServiceClientWrapper);
Expand All @@ -135,10 +133,7 @@ public GoogleTranslationService getGoogleTranslationService(
BeanNames.BEAN_PANGEANIC_TRANSLATION_SERVICE, BeanNames.BEAN_GOOGLE_TRANSLATION_SERVICE})
public TranslationServiceProvider getTranslationServiceProvider() {
this.translationServiceConfigProvider = new TranslationServiceProvider();
// failing as the service beans are not initialized yet, would need to think of another way to
// call this initialization
// translationServiceConfigProvider#initTranslationServicesConfiguration;
return translationServiceConfigProvider;
return this.translationServiceConfigProvider;
}

@Override
Expand Down Expand Up @@ -169,16 +164,24 @@ private void loadServices(ApplicationStartedEvent event) {
}
}

/**
* Method for initialization of service provider using the service configurations
* @param ctx
* @throws TranslationServiceConfigurationException
* @throws LangDetectionServiceConfigurationException
*/
public void initTranslationServices(ApplicationContext ctx)
throws TranslationServiceConfigurationException, LangDetectionServiceConfigurationException {
TranslationServiceProvider translationServiceProvider =
(TranslationServiceProvider) ctx.getBean(BeanNames.BEAN_SERVICE_PROVIDER);
translationServiceProvider.initTranslationServicesConfiguration();
}

/**
* Method to verify required properties in translation config
* @param ctx the application context holding references to instantiated beans
*/
public void verifyMandatoryProperties(ApplicationContext ctx) {
TranslationConfig translationConfig =
(TranslationConfig) ctx.getBean(BeanNames.BEAN_TRANSLATION_CONFIG);
translationConfig.verifyRequiredProperties();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ public void initTranslationServicesConfiguration()
validateAndInitServices();
}

public void readServiceConfigurations() throws TranslationServiceConfigurationException {
/**
* Method for reading and parsing service configurations
* @throws TranslationServiceConfigurationException
*/
void readServiceConfigurations() throws TranslationServiceConfigurationException {
try (InputStream inputStream = getClass().getResourceAsStream(getServiceConfigFile())) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String content = reader.lines().collect(Collectors.joining(System.lineSeparator()));
Expand Down Expand Up @@ -300,7 +304,6 @@ private void validateDeclaredLangDetectionServices()
}



public String getServiceConfigFile() {
return serviceConfigFile;
}
Expand Down

0 comments on commit f7793ce

Please sign in to comment.