Skip to content

Commit

Permalink
refactor: update to get dialect from dialect registry.
Browse files Browse the repository at this point in the history
Signed-off-by: Aman Prashant <aman.prashant@broadcom.com>
  • Loading branch information
ap891843 committed Jan 12, 2023
1 parent 60583e5 commit bc90153
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
* Provides files in the working folder
*/
@Singleton
public
class WorkingFolderService {
public class WorkingFolderService {

private static final String DIALECTS_PATH_SYSTEM_PROPERTY = "dialect.path";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@
*/
package org.eclipse.lsp.cobol.core.messages;

import com.google.gson.JsonArray;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.lsp.cobol.common.AnalysisConfig;
import org.eclipse.lsp.cobol.common.DialectRegistryItem;
import org.eclipse.lsp.cobol.common.copybook.CopybookProcessingMode;
import org.eclipse.lsp.cobol.common.message.LocaleStore;
import org.eclipse.lsp.cobol.common.message.MessageService;
import org.eclipse.lsp.cobol.common.message.MessageTemplate;
import org.eclipse.lsp.cobol.service.settings.ConfigurationService;
import org.eclipse.lsp.cobol.core.engine.dialects.WorkingFolderService;
import org.eclipse.lsp.cobol.service.settings.ConfigHelper;
import org.eclipse.lsp.cobol.service.settings.SettingsService;

import java.io.IOException;
import java.util.Arrays;
Expand All @@ -36,6 +37,8 @@

import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.joining;
import static org.eclipse.lsp.cobol.service.settings.SettingsParametersEnum.DIALECTS;
import static org.eclipse.lsp.cobol.service.settings.SettingsParametersEnum.DIALECT_REGISTRY;

/**
* This class is an properties file implementation of {@link MessageService} . It loads messages
Expand All @@ -48,19 +51,21 @@ public class PropertiesMessageService implements MessageService {
private final String baseName;
private final LocaleStore localeStore;
private CobolLSPropertiesResourceBundle resourceBundle;
private final ConfigurationService configurationService;
private final SettingsService settingsService;
private final WorkingFolderService workingFolderService;

@Inject
public PropertiesMessageService(
@Named("resourceFileLocation") String baseName,
LocaleStore localeStore,
ConfigurationService configurationService) {
@Named("resourceFileLocation") String baseName,
LocaleStore localeStore,
SettingsService settingsService,
WorkingFolderService workingFolderService) {
this.baseName = baseName;
this.localeStore = localeStore;
this.configurationService = configurationService;
this.settingsService = settingsService;
this.workingFolderService = workingFolderService;
resourceBundle =
new CobolLSPropertiesResourceBundle(
baseName, localeStore.getApplicationLocale());
new CobolLSPropertiesResourceBundle(baseName, localeStore.getApplicationLocale());
subscribeToLocaleStore();
}

Expand All @@ -76,8 +81,7 @@ public void reloadMessages() {
private void reloadResourceBundle(Locale locale) {
ResourceBundle.clearCache();
resourceBundle =
new CobolLSPropertiesResourceBundle(
baseName, localeStore.getApplicationLocale());
new CobolLSPropertiesResourceBundle(baseName, localeStore.getApplicationLocale());
updateResourceBundle();
}

Expand All @@ -104,18 +108,51 @@ public String localizeTemplate(MessageTemplate template) {
}

private void updateResourceBundle() {
reloadResourceBundle(this.localeStore.getApplicationLocale());
AnalysisConfig config = configurationService.getConfig(CopybookProcessingMode.ENABLED);
List<String> configuredDialects = config.getDialects();
LOG.debug("Configured dialects : " + String.join(",", configuredDialects));
config.getDialectRegistry().stream()
.filter(registeredDialects -> configuredDialects.contains(registeredDialects.getName()))
this.settingsService
.fetchTextConfiguration(DIALECTS.label)
.thenAccept(
dialects ->
this.settingsService
.fetchConfiguration(DIALECT_REGISTRY.label)
.thenAccept(
registry -> {
List<DialectRegistryItem> dialectRegistryItems =
ConfigHelper.parseDialectRegistry((JsonArray) registry.get(0));
handleRegisteredDialects(dialects, dialectRegistryItems);
handleImplicitDialects(dialects, dialectRegistryItems);
}));
}

private void handleRegisteredDialects(List<String> dialects, List<DialectRegistryItem> dialectRegistryItems) {
dialectRegistryItems.stream()
.filter(
registeredDialects ->
dialects.contains(registeredDialects.getName()))
.forEach(this::updateResourceBundle);
}

private void handleImplicitDialects(List<String> dialects, List<DialectRegistryItem> dialectRegistryItems) {
if (dialectRegistryItems.isEmpty()) {
dialects.forEach(this::updateResourceBundle);
}
}

private void updateResourceBundle(String dialect) {
updateResourceBundle(
new DialectRegistryItem(
dialect,
this.workingFolderService.getWorkingFolder().getPath(),
"implicit found dialects",
"implicit-dialects"));
}

private void updateResourceBundle(DialectRegistryItem dialectRegistryItem) {
try {
LOG.debug("update resource for - " + dialectRegistryItem.getName() + " ," + dialectRegistryItem.getPath());
LOG.debug(
"update resource for - "
+ dialectRegistryItem.getName()
+ " ,"
+ dialectRegistryItem.getPath());
this.resourceBundle.updateMessageResourceBundle(dialectRegistryItem);
} catch (IOException e) {
LOG.error("Issue while loading resource bundle for " + dialectRegistryItem.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
import org.antlr.v4.runtime.Parser;
import org.eclipse.lsp.cobol.common.message.LocaleStore;
import org.eclipse.lsp.cobol.common.message.MessageService;
import org.eclipse.lsp.cobol.core.engine.dialects.WorkingFolderService;
import org.eclipse.lsp.cobol.core.messages.PropertiesMessageService;
import org.eclipse.lsp.cobol.core.strategy.CobolErrorStrategy;
import org.eclipse.lsp.cobol.service.settings.ConfigurationService;
import org.eclipse.lsp.cobol.service.settings.SettingsService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -43,10 +44,11 @@ void beforeEach() {
mockParser = mock(MessageServiceParser.class);
doCallRealMethod().when((MessageServiceParser) mockParser).notifyError(anyString());
LocaleStore localeMock = mock(LocaleStore.class);
ConfigurationService configurationService = mock(ConfigurationService.class);
SettingsService settingsService = mock(SettingsService.class);
WorkingFolderService workingFolderService = mock(WorkingFolderService.class);
when(localeMock.getApplicationLocale()).thenReturn(Locale.ENGLISH);
MessageService messageService =
new PropertiesMessageService("resourceBundles/test", localeMock, configurationService);
new PropertiesMessageService("resourceBundles/test", localeMock, settingsService, workingFolderService);
CobolErrorStrategy errorStrategy = mock(CobolErrorStrategy.class);
when(mockParser.getErrorHandler()).thenReturn(errorStrategy);
when(errorStrategy.getMessageService()).thenReturn(messageService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import org.eclipse.lsp.cobol.common.message.LocaleStore;
import org.eclipse.lsp.cobol.common.message.MessageService;
import org.eclipse.lsp.cobol.common.message.MessageTemplate;
import org.eclipse.lsp.cobol.service.settings.ConfigurationService;
import org.eclipse.lsp.cobol.core.engine.dialects.WorkingFolderService;
import org.eclipse.lsp.cobol.service.settings.SettingsService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -34,15 +35,16 @@ class PropertiesMessageServiceTest {

private static MessageService messageService;
private LocaleStore localeMock;
ConfigurationService configurationService = mock(ConfigurationService.class);
SettingsService settingsService = mock(SettingsService.class);
WorkingFolderService workingFolderService = mock(WorkingFolderService.class);

@BeforeEach
public void beforeAll() {
localeMock = mock(LocaleStore.class);
when(localeMock.getApplicationLocale()).thenReturn(Locale.ENGLISH);
messageService =
new PropertiesMessageService(
"resourceBundles/test", localeMock, configurationService);
"resourceBundles/test", localeMock, settingsService, workingFolderService);
}

@Test
Expand All @@ -59,7 +61,7 @@ void whenValidMessageTemplateProvideFR_getFormattedMessage() {
when(localeMock.getApplicationLocale()).thenReturn(Locale.FRENCH);
MessageService messageServiceFR =
new PropertiesMessageService(
"resourceBundles/test", localeMock, configurationService);
"resourceBundles/test", localeMock, settingsService, workingFolderService);
assertEquals("French test selected.", messageServiceFR.getMessage("1"));

assertEquals(
Expand All @@ -71,7 +73,7 @@ void whenValidMessageTemplateProvideFR_getFormattedMessage() {
void whenInValidMessageTemplatePathProvide_getException() {
Assertions.assertThrows(
MissingResourceException.class,
() -> new PropertiesMessageService("dummy", localeMock, configurationService));
() -> new PropertiesMessageService("dummy", localeMock, settingsService, workingFolderService));
}

@Test
Expand All @@ -80,7 +82,8 @@ void whenEmptyMessageTemplateProvided_getNoException_getKeyInstead() {
new PropertiesMessageService(
"resourceBundles/Test_messageServiceEmptyFile",
localeMock,
configurationService
settingsService,
workingFolderService
);
assertEquals("1", messageServiceLocal.getMessage("1"));
}
Expand All @@ -89,7 +92,7 @@ void whenEmptyMessageTemplateProvided_getNoException_getKeyInstead() {
void whenMultipleMsgServiceExist_thenSupportDuplicateKeys() {
MessageService messageService1 =
new PropertiesMessageService(
"resourceBundles/test-2", localeMock, configurationService);
"resourceBundles/test-2", localeMock, settingsService, workingFolderService);
final String formattedMessage = messageService1.getMessage("1", localeMock);
assertEquals("This is a duplicate key test for diff msg service.", formattedMessage);
}
Expand Down

0 comments on commit bc90153

Please sign in to comment.