Skip to content

Commit

Permalink
MODLD-601: Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiBordak committed Nov 26, 2024
1 parent 38ff32d commit 8082e32
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package org.folio.linked.data.service;

import java.util.Optional;
import org.folio.linked.data.domain.dto.SettingsItem;

public interface SettingsService {

Optional<SettingsItem> getSetting(String scope, String key);

boolean isSettingEnabled(String scope, String key, String property);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ public class SettingsServiceImpl implements SettingsService {

private final SettingsClient settingsClient;

@Override
public Optional<SettingsItem> getSetting(String scope, String key) {
return ofNullable(settingsClient.getEntries(buildQuery(scope, key)))
.map(ResponseEntity::getBody)
.flatMap(body -> body.getItems().stream().findFirst());
}

@Override
public boolean isSettingEnabled(String scope, String key, String property) {
return getSetting(scope, key)
.map(item -> isPropertyEnabled(item, property))
.orElse(false);
}

private Optional<SettingsItem> getSetting(String scope, String key) {
return ofNullable(settingsClient.getEntries(buildQuery(scope, key)))
.map(ResponseEntity::getBody)
.flatMap(body -> body.getItems().stream().findFirst());
}

private boolean isPropertyEnabled(SettingsItem item, String property) {
var propertyValue = item.getValue().getOrDefault(property, false);
return propertyValue instanceof Boolean value && value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.folio.linked.data.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Map;
import java.util.stream.Stream;
import org.folio.linked.data.client.SettingsClient;
import org.folio.linked.data.domain.dto.SettingsItem;
Expand Down Expand Up @@ -35,33 +33,16 @@ class SettingsServiceImplTest {
private SettingsClient settingsClient;

@Test
void getSettings_shouldReturnSettingsItem() {
void isSettingEnabled_shouldReturnFalse_ifSettingsAreEmpty() {
// given
var settingsItem = new SettingsItem().value(Map.of("property", true));
when(settingsClient.getEntries("(scope==scope and key==key)"))
.thenReturn(new ResponseEntity<>(
new SettingsSearchResponse().addItemsItem(settingsItem), HttpStatus.OK));

// when
var setting = settingsService.getSetting("scope", "key");

// then
assertThat(setting)
.get()
.satisfies(actual -> assertEquals(actual, settingsItem));
verify(settingsClient).getEntries("(scope==scope and key==key)");
}

@Test
void getSettings_shouldReturnEmptySettingsItem() {
// given
when(settingsClient.getEntries(any())).thenReturn(null);
when(settingsClient.getEntries(any()))
.thenReturn(new ResponseEntity<>(new SettingsSearchResponse(), HttpStatus.OK));

// when
var setting = settingsService.getSetting("scope", "key");
boolean isEnabled = settingsService.isSettingEnabled("scope", "key", "property");

// then
assertThat(setting).isEmpty();
assertFalse(isEnabled);
}

@ParameterizedTest
Expand All @@ -73,16 +54,17 @@ void isSettingEnabled_shouldBaseOnSettingPropertyValue(SettingsItem item, boolea
new SettingsSearchResponse().addItemsItem(item), HttpStatus.OK));

// when
boolean settingEnabled = settingsService.isSettingEnabled("scope", "key", "property");
boolean isEnabled = settingsService.isSettingEnabled("scope", "key", "property");

// then
assertEquals(expected, settingEnabled);
assertEquals(expected, isEnabled);
}

private static Stream<Arguments> settingsProvider() {
return Stream.of(
arguments(new SettingsItem().putValueItem("property", true), true),
arguments(new SettingsItem().putValueItem("property", false), false),
arguments(new SettingsItem().putValueItem("property", new Object()), false),
arguments(new SettingsItem(), false)
);
}
Expand Down

0 comments on commit 8082e32

Please sign in to comment.