-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MODLD-601: LCCN deduplication configuration setting
MODLD-601: Review fixes MODLD-601: Review fixes MODLD-601: Change log level for settings service outage
- Loading branch information
1 parent
440424c
commit 4d1c0c7
Showing
15 changed files
with
281 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/org/folio/linked/data/client/SettingsClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.folio.linked.data.client; | ||
|
||
import static org.folio.linked.data.util.Constants.Cache.SETTINGS_ENTRIES; | ||
|
||
import org.folio.linked.data.domain.dto.SettingsSearchResponse; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
@FeignClient("settings") | ||
public interface SettingsClient { | ||
|
||
@Cacheable(cacheNames = SETTINGS_ENTRIES, key = "#query") | ||
@GetMapping("/entries") | ||
ResponseEntity<SettingsSearchResponse> getEntries(@RequestParam("query") String query); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/java/org/folio/linked/data/service/SettingsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.folio.linked.data.service; | ||
|
||
public interface SettingsService { | ||
|
||
boolean isSettingEnabled(String scope, String key, String property); | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/org/folio/linked/data/service/SettingsServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.folio.linked.data.service; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.Optional.ofNullable; | ||
|
||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.folio.linked.data.client.SettingsClient; | ||
import org.folio.linked.data.domain.dto.SettingsItem; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class SettingsServiceImpl implements SettingsService { | ||
|
||
private static final String SCOPE_AND_KEY_PATTERN = "(scope==%s and key==%s)"; | ||
|
||
private final SettingsClient settingsClient; | ||
|
||
@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; | ||
} | ||
|
||
private String buildQuery(String scope, String key) { | ||
return format(SCOPE_AND_KEY_PATTERN, scope, key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/resources/swagger.api/folio-modules/search/settingsItem.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"description": "Settings item containing map of properties", | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"type": "object", | ||
"properties": { | ||
"value": { | ||
"type": "object", | ||
"additionalProperties": { | ||
"type": "object" | ||
}, | ||
"description": "Map of setting properties" | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/resources/swagger.api/folio-modules/search/settingsSearchResponse.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"description": "Wrapper for settings search response", | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"type": "object", | ||
"properties": { | ||
"items": { | ||
"description": "List of settings items", | ||
"type": "array", | ||
"items": { | ||
"$ref": "settingsItem.json" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/test/java/org/folio/linked/data/service/SettingsServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.folio.linked.data.service; | ||
|
||
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.when; | ||
|
||
import java.util.stream.Stream; | ||
import org.folio.linked.data.client.SettingsClient; | ||
import org.folio.linked.data.domain.dto.SettingsItem; | ||
import org.folio.linked.data.domain.dto.SettingsSearchResponse; | ||
import org.folio.spring.testing.type.UnitTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
@UnitTest | ||
@ExtendWith(MockitoExtension.class) | ||
class SettingsServiceImplTest { | ||
|
||
@InjectMocks | ||
private SettingsServiceImpl settingsService; | ||
|
||
@Mock | ||
private SettingsClient settingsClient; | ||
|
||
@Test | ||
void isSettingEnabled_shouldReturnFalse_ifSettingsAreEmpty() { | ||
// given | ||
when(settingsClient.getEntries(any())) | ||
.thenReturn(new ResponseEntity<>(new SettingsSearchResponse(), HttpStatus.OK)); | ||
|
||
// when | ||
boolean isEnabled = settingsService.isSettingEnabled("scope", "key", "property"); | ||
|
||
// then | ||
assertFalse(isEnabled); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("settingsProvider") | ||
void isSettingEnabled_shouldBaseOnSettingPropertyValue(SettingsItem item, boolean expected) { | ||
// given | ||
when(settingsClient.getEntries(any())) | ||
.thenReturn(new ResponseEntity<>( | ||
new SettingsSearchResponse().addItemsItem(item), HttpStatus.OK)); | ||
|
||
// when | ||
boolean isEnabled = settingsService.isSettingEnabled("scope", "key", "property"); | ||
|
||
// then | ||
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) | ||
); | ||
} | ||
} |
Oops, something went wrong.