Skip to content

Commit

Permalink
feat: optional custom settings validation (#93)
Browse files Browse the repository at this point in the history
Refs: #91
  • Loading branch information
Jumas authored Aug 27, 2024
1 parent eb7ac85 commit beba621
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ public boolean isEnabled() {
@JsonIgnore
public abstract String getDefaultSettings();


/**
* Called each time the settings are updated in the administration panel.
*
* @return null if the settings are valid and an error message if the settings are invalid (it will be displayed to the user)
*/
@SuppressWarnings("java:S1172") // parameter 'model' will be used by subclasses
public String validateSettings(HookModel model) {
return null;
}

@NotNull
protected String getSettingsValue(@NotNull String settingsId, String... selectors) {
LinkedHashMap<String, String> valueMap = getSettingsValuesWithSelector(settingsId, selectors);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package ch.sbb.polarion.extension.interceptor_manager.settings;

import ch.sbb.polarion.extension.generic.settings.GenericNamedSettings;
import ch.sbb.polarion.extension.generic.settings.SettingsService;
import ch.sbb.polarion.extension.interceptor_manager.model.ActionHook;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.VisibleForTesting;

import java.util.Optional;

public class HookSettings extends GenericNamedSettings<HookModel> {
private final ActionHook hook;
Expand All @@ -12,9 +16,18 @@ public HookSettings(ActionHook hook) {
this.hook = hook;
}

@VisibleForTesting
public HookSettings(ActionHook hook, SettingsService settingsService) {
super(hook.getName(), settingsService);
this.hook = hook;
}

@Override
public void beforeSave(@NotNull HookModel what) {
what.setHookVersion(hook.getVersion());
Optional.ofNullable(hook.validateSettings(what)).ifPresent(error -> {
throw new IllegalArgumentException(error);
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ function saveSettings() {
SbbCommon.setNewerVersionNotificationVisible(false);
readAndFillRevisions();
},
onError: () => SbbCommon.showSaveErrorAlert()
onError: (status, errorMessage) => {
if (errorMessage) {
SbbCommon.showActionAlert({
containerId: 'action-error',
message: errorMessage,
hideAlertByTimeout: false
});
} else {
SbbCommon.showSaveErrorAlert()
}
}
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ch.sbb.polarion.extension.interceptor_manager.settings;

import ch.sbb.polarion.extension.generic.settings.SettingsService;
import ch.sbb.polarion.extension.interceptor_manager.model.ActionHook;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class HookSettingsTest {

@Test
public void testValidate() {
ActionHook hook = mock(ActionHook.class);

HookModel hookModel = mock(HookModel.class);
HookSettings settings = new HookSettings(hook, mock(SettingsService.class));

settings.beforeSave(hookModel);
verify(hook, times(1)).validateSettings(any());

when(hook.validateSettings(any())).thenReturn("Some validation error");
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> settings.beforeSave(hookModel));
assertEquals("Some validation error", exception.getMessage());
verify(hook, times(2)).validateSettings(any());
}
}

0 comments on commit beba621

Please sign in to comment.