diff --git a/descriptors/ModuleDescriptor-template.json b/descriptors/ModuleDescriptor-template.json index 1d349335..6a3b143a 100644 --- a/descriptors/ModuleDescriptor-template.json +++ b/descriptors/ModuleDescriptor-template.json @@ -39,6 +39,24 @@ } ] }, + { + "id": "tlr-settings", + "version": "1.0", + "handlers": [ + { + "methods": ["GET"], + "pathPattern": "/tlr/settings", + "permissionsRequired": ["tlr.settings.get"], + "modulePermissions": [] + }, + { + "methods": ["PUT"], + "pathPattern": "/tlr/settings", + "permissionsRequired": ["tlr.settings.put"], + "modulePermissions": [] + } + ] + }, { "id": "_tenant", "version": "2.0", @@ -95,6 +113,46 @@ "permissionName": "tlr.ecs-tlr.delete", "displayName": "ecs-tlr - remove ECS TLR", "description": "Remove ECS TLR" + }, + { + "permissionName": "tlr.settings.get", + "displayName": "tlr-settings - get ECS TLR Settings", + "description": "Get ECS TLR Settings" + }, + { + "permissionName": "tlr.settings.put", + "displayName": "tlr-settings - update ECS TLR Settings", + "description": "Update ECS TLR" + }, + { + "permissionName": "tlr.consortium-tlr.view", + "displayName": "Settings (Circulation): Consortium title level requests view", + "description": "Entire set of permissions needed to use Consortium title level requests settings view", + "subPermissions": [ + "tlr.settings.get", + "settings.circulation.enabled", + "mod-settings.global.read.circulation", + "mod-settings.entries.collection.get", + "mod-settings.entries.item.get" + ], + "visible": false + }, + { + "permissionName": "tlr.consortium-tlr.edit", + "displayName": "Settings (Circulation): Consortium title level requests edit", + "description": "Entire set of permissions needed to use Consortium title level requests settings edit", + "subPermissions": [ + "tlr.settings.get", + "tlr.settings.put", + "settings.circulation.enabled", + "mod-settings.global.read.circulation", + "mod-settings.entries.collection.get", + "mod-settings.entries.item.get", + "mod-settings.global.write.circulation", + "mod-settings.entries.item.put", + "mod-settings.entries.item.post" + ], + "visible": true } ], "requires": [ diff --git a/pom.xml b/pom.xml index b2183923..044d0ba2 100644 --- a/pom.xml +++ b/pom.xml @@ -308,6 +308,34 @@ java true true + true + + + + + tlr-settings + + generate + + + ${project.basedir}/src/main/resources/swagger.api/tlr-settings.yaml + ${project.build.directory}/generated-sources + spring + ${project.groupId}.domain.dto + ${project.groupId}.rest.resource + true + true + true + true + false + true + ApiUtil.java + true + + java + true + true + true diff --git a/src/main/java/org/folio/controller/EcsTlrController.java b/src/main/java/org/folio/controller/EcsTlrController.java index 800910e0..9bae039d 100644 --- a/src/main/java/org/folio/controller/EcsTlrController.java +++ b/src/main/java/org/folio/controller/EcsTlrController.java @@ -8,8 +8,9 @@ import java.util.UUID; import org.folio.domain.dto.EcsTlr; -import org.folio.rest.resource.TlrApi; +import org.folio.rest.resource.EcsTlrApi; import org.folio.service.EcsTlrService; +import org.folio.service.TlrSettingsService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RestController; @@ -20,9 +21,10 @@ @RestController @Log4j2 @AllArgsConstructor -public class EcsTlrController implements TlrApi { +public class EcsTlrController implements EcsTlrApi { private final EcsTlrService ecsTlrService; + private final TlrSettingsService tlrSettingsService; @Override public ResponseEntity getEcsTlrById(UUID requestId) { diff --git a/src/main/java/org/folio/controller/TlrSettingsController.java b/src/main/java/org/folio/controller/TlrSettingsController.java new file mode 100644 index 00000000..c9c1cb8d --- /dev/null +++ b/src/main/java/org/folio/controller/TlrSettingsController.java @@ -0,0 +1,42 @@ +package org.folio.controller; + +import static org.springframework.http.HttpStatus.NOT_FOUND; +import static org.springframework.http.HttpStatus.NO_CONTENT; +import static org.springframework.http.HttpStatus.OK; + +import org.folio.domain.dto.TlrSettings; +import org.folio.rest.resource.TlrSettingsApi; +import org.folio.service.TlrSettingsService; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RestController; + +import lombok.AllArgsConstructor; +import lombok.extern.log4j.Log4j2; + +@RestController +@Log4j2 +@AllArgsConstructor +public class TlrSettingsController implements TlrSettingsApi { + + private final TlrSettingsService tlrSettingsService; + + @Override + public ResponseEntity getTlrSettings() { + log.debug("getTlrSettings:: "); + + return tlrSettingsService.getTlrSettings() + .map(ResponseEntity.status(OK)::body) + .orElseGet(() -> ResponseEntity.notFound().build()); + } + + @Override + public ResponseEntity putTlrSettings(TlrSettings tlrSettings) { + log.debug("putTlrSettings:: parameters: {}", () -> tlrSettings); + + return ResponseEntity.status( + tlrSettingsService.updateTlrSettings(tlrSettings) + .map(entity -> NO_CONTENT) + .orElse(NOT_FOUND)) + .build(); + } +} diff --git a/src/main/java/org/folio/domain/entity/TlrSettingsEntity.java b/src/main/java/org/folio/domain/entity/TlrSettingsEntity.java new file mode 100644 index 00000000..d984087d --- /dev/null +++ b/src/main/java/org/folio/domain/entity/TlrSettingsEntity.java @@ -0,0 +1,24 @@ +package org.folio.domain.entity; + +import java.util.UUID; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Entity +@Table(name = "tlr_settings") +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class TlrSettingsEntity { + + @Id + private UUID id; + private boolean ecsTlrFeatureEnabled; +} diff --git a/src/main/java/org/folio/domain/mapper/TlrSettingsMapper.java b/src/main/java/org/folio/domain/mapper/TlrSettingsMapper.java new file mode 100644 index 00000000..96395a66 --- /dev/null +++ b/src/main/java/org/folio/domain/mapper/TlrSettingsMapper.java @@ -0,0 +1,16 @@ +package org.folio.domain.mapper; + +import org.folio.domain.dto.TlrSettings; +import org.folio.domain.entity.TlrSettingsEntity; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.NullValueCheckStrategy; + +@Mapper(componentModel = "spring", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS) +public interface TlrSettingsMapper { + + @Mapping(target = "id", ignore = true) + TlrSettings mapEntityToDto(TlrSettingsEntity tlrSettingsEntity); + + TlrSettingsEntity mapDtoToEntity(TlrSettings tlrSettings); +} diff --git a/src/main/java/org/folio/repository/TlrSettingsRepository.java b/src/main/java/org/folio/repository/TlrSettingsRepository.java new file mode 100644 index 00000000..4d6eed1a --- /dev/null +++ b/src/main/java/org/folio/repository/TlrSettingsRepository.java @@ -0,0 +1,9 @@ +package org.folio.repository; + +import java.util.UUID; + +import org.folio.domain.entity.TlrSettingsEntity; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface TlrSettingsRepository extends JpaRepository { +} diff --git a/src/main/java/org/folio/service/TlrSettingsService.java b/src/main/java/org/folio/service/TlrSettingsService.java new file mode 100644 index 00000000..1f9170bf --- /dev/null +++ b/src/main/java/org/folio/service/TlrSettingsService.java @@ -0,0 +1,10 @@ +package org.folio.service; + +import java.util.Optional; + +import org.folio.domain.dto.TlrSettings; + +public interface TlrSettingsService { + Optional getTlrSettings(); + Optional updateTlrSettings(TlrSettings tlrSettings); +} diff --git a/src/main/java/org/folio/service/impl/EcsTlrServiceImpl.java b/src/main/java/org/folio/service/impl/EcsTlrServiceImpl.java index 6339fab9..e25736d3 100644 --- a/src/main/java/org/folio/service/impl/EcsTlrServiceImpl.java +++ b/src/main/java/org/folio/service/impl/EcsTlrServiceImpl.java @@ -162,5 +162,4 @@ private void handleSecondaryRequestUpdate(EcsTlrEntity ecsTlr, UUID itemId) { ecsTlrRepository.save(ecsTlr); log.info("handleSecondaryRequestUpdate: ECS TLR {} is updated", ecsTlrId); } - } diff --git a/src/main/java/org/folio/service/impl/TlrSettingsServiceImpl.java b/src/main/java/org/folio/service/impl/TlrSettingsServiceImpl.java new file mode 100644 index 00000000..4d91e8e6 --- /dev/null +++ b/src/main/java/org/folio/service/impl/TlrSettingsServiceImpl.java @@ -0,0 +1,44 @@ +package org.folio.service.impl; + +import java.util.Optional; + +import org.folio.domain.dto.TlrSettings; +import org.folio.domain.mapper.TlrSettingsMapper; +import org.folio.repository.TlrSettingsRepository; +import org.folio.service.TlrSettingsService; +import org.springframework.data.domain.PageRequest; +import org.springframework.stereotype.Service; + +import lombok.RequiredArgsConstructor; +import lombok.extern.log4j.Log4j2; + +@Service +@RequiredArgsConstructor +@Log4j2 +public class TlrSettingsServiceImpl implements TlrSettingsService { + + private final TlrSettingsRepository tlrSettingsRepository; + private final TlrSettingsMapper tlrSettingsMapper; + + @Override + public Optional getTlrSettings() { + log.debug("getTlrSettings:: "); + + return tlrSettingsRepository.findAll(PageRequest.of(0, 1)) + .stream() + .findFirst() + .map(tlrSettingsMapper::mapEntityToDto); + } + + @Override + public Optional updateTlrSettings(TlrSettings tlrSettings) { + log.debug("updateTlrSettings:: parameters: {} ", () -> tlrSettings); + + return tlrSettingsRepository.findAll(PageRequest.of(0, 1)) + .stream() + .findFirst() + .map(entity -> tlrSettingsMapper.mapEntityToDto( + tlrSettingsRepository.save(tlrSettingsMapper.mapDtoToEntity( + tlrSettings.id(entity.getId().toString()))))); + } +} diff --git a/src/main/resources/db/changelog/changes/initial_schema.xml b/src/main/resources/db/changelog/changes/initial_schema.xml index 0176a775..e32665a5 100644 --- a/src/main/resources/db/changelog/changes/initial_schema.xml +++ b/src/main/resources/db/changelog/changes/initial_schema.xml @@ -35,4 +35,16 @@ + + + + + + + + + + + + diff --git a/src/main/resources/swagger.api/ecs-tlr.yaml b/src/main/resources/swagger.api/ecs-tlr.yaml index 274ecfb9..a9536c31 100644 --- a/src/main/resources/swagger.api/ecs-tlr.yaml +++ b/src/main/resources/swagger.api/ecs-tlr.yaml @@ -2,11 +2,15 @@ openapi: 3.0.0 info: title: ECS TLR API version: v1 +tags: + - name: ecsTlr paths: /tlr/ecs-tlr: post: description: Create ECS TLR operationId: postEcsTlr + tags: + - ecsTlr requestBody: content: application/json: @@ -24,6 +28,8 @@ paths: get: description: Retrieve ECS TLR by ID operationId: getEcsTlrById + tags: + - ecsTlr parameters: - $ref: '#/components/parameters/requestId' responses: @@ -38,6 +44,8 @@ paths: put: description: Update ECS TLR by ID operationId: putEcsTlrById + tags: + - ecsTlr requestBody: content: application/json: @@ -58,6 +66,8 @@ paths: delete: description: Remove ECS TLR by ID operationId: deleteEcsTlrById + tags: + - ecsTlr parameters: - $ref: '#/components/parameters/requestId' responses: diff --git a/src/main/resources/swagger.api/schemas/TlrSettings.yaml b/src/main/resources/swagger.api/schemas/TlrSettings.yaml new file mode 100644 index 00000000..3cea12a7 --- /dev/null +++ b/src/main/resources/swagger.api/schemas/TlrSettings.yaml @@ -0,0 +1,12 @@ +TlrSettings: + description: TLR Settings in a multi-tenant environment with Сonsortia support enabled + type: "object" + properties: + id: + description: "ID of the ECS TLR Settings" + $ref: "uuid.yaml" + ecsTlrFeatureEnabled: + description: "Indicates if TLR feature is enabled" + type: boolean + required: + - ecsTlrFeatureEnabled diff --git a/src/main/resources/swagger.api/tlr-settings.yaml b/src/main/resources/swagger.api/tlr-settings.yaml new file mode 100644 index 00000000..47da45a8 --- /dev/null +++ b/src/main/resources/swagger.api/tlr-settings.yaml @@ -0,0 +1,85 @@ +openapi: 3.0.0 +info: + title: TLR Settings API + version: v1 +tags: + - name: tlrSettings +paths: + /tlr/settings: + get: + description: Retrieve TLR Settings + operationId: getTlrSettings + tags: + - tlrSettings + responses: + '200': + $ref: '#/components/responses/tlr-settings' + '400': + $ref: '#/components/responses/badRequestResponse' + '404': + $ref: '#/components/responses/notFoundResponse' + '500': + $ref: '#/components/responses/internalServerErrorResponse' + put: + description: Update TLR Settings + operationId: putTlrSettings + tags: + - tlrSettings + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/tlr-settings" + required: true + responses: + '204': + description: Request successfully updated + '400': + $ref: '#/components/responses/badRequestResponse' + '404': + $ref: '#/components/responses/notFoundResponse' + '500': + $ref: '#/components/responses/internalServerErrorResponse' +components: + schemas: + tlr-settings: + $ref: 'schemas/TlrSettings.yaml#/TlrSettings' + errorResponse: + $ref: 'schemas/errors.json' + responses: + tlr-settings: + description: TLR Settings + content: + application/json: + schema: + $ref: 'schemas/TlrSettings.yaml#/TlrSettings' + badRequestResponse: + description: Validation errors + content: + application/json: + example: + errors: + - message: Request is invalid + total_records: 1 + schema: + $ref: "#/components/schemas/errorResponse" + notFoundResponse: + description: Not found + content: + application/json: + example: + errors: + - message: Request not found + total_records: 1 + schema: + $ref: "#/components/schemas/errorResponse" + internalServerErrorResponse: + description: When unhandled exception occurred during code execution, e.g. NullPointerException + content: + application/json: + example: + errors: + - message: Unexpected error + total_records: 1 + schema: + $ref: "#/components/schemas/errorResponse" diff --git a/src/test/java/org/folio/controller/EcsTlrControllerTest.java b/src/test/java/org/folio/controller/EcsTlrControllerTest.java index e85fe1e8..183fcb6f 100644 --- a/src/test/java/org/folio/controller/EcsTlrControllerTest.java +++ b/src/test/java/org/folio/controller/EcsTlrControllerTest.java @@ -13,6 +13,7 @@ import org.folio.domain.dto.EcsTlr; import org.folio.service.EcsTlrService; +import org.folio.service.TlrSettingsService; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -23,32 +24,33 @@ @ExtendWith(MockitoExtension.class) class EcsTlrControllerTest { @Mock - private EcsTlrService requestsService; - + private EcsTlrService ecsTlrService; + @Mock + private TlrSettingsService tlrSettingsService; @InjectMocks - private EcsTlrController requestsController; + private EcsTlrController ecsTlrController; @Test void getByIdNotFoundWhenNull() { - when(requestsService.get(any())).thenReturn(Optional.empty()); - var response = requestsController.getEcsTlrById(any()); - verify(requestsService).get(any()); + when(ecsTlrService.get(any())).thenReturn(Optional.empty()); + var response = ecsTlrController.getEcsTlrById(any()); + verify(ecsTlrService).get(any()); assertEquals(response.getStatusCode(), HttpStatusCode.valueOf(404)); } @Test void getById() { - when(requestsService.get(any())).thenReturn(Optional.of(new EcsTlr())); - var response = requestsController.getEcsTlrById(any()); + when(ecsTlrService.get(any())).thenReturn(Optional.of(new EcsTlr())); + var response = ecsTlrController.getEcsTlrById(any()); assertEquals(response.getStatusCode(), HttpStatusCode.valueOf(200)); } @Test void ecsTlrShouldSuccessfullyBeCreated() { var mockRequest = new EcsTlr(); - when(requestsService.create(any(EcsTlr.class))).thenReturn(mockRequest); + when(ecsTlrService.create(any(EcsTlr.class))).thenReturn(mockRequest); - var response = requestsController.postEcsTlr(new EcsTlr()); + var response = ecsTlrController.postEcsTlr(new EcsTlr()); assertEquals(CREATED, response.getStatusCode()); assertEquals(mockRequest, response.getBody()); @@ -59,16 +61,16 @@ void ecsTlrShouldSuccessfullyBeUpdated() { var id = UUID.randomUUID(); var mockRequest = new EcsTlr(); mockRequest.setId(id.toString()); - when(requestsService.update(any(UUID.class), any(EcsTlr.class))).thenReturn(true); + when(ecsTlrService.update(any(UUID.class), any(EcsTlr.class))).thenReturn(true); - var response = requestsController.putEcsTlrById(id, mockRequest); + var response = ecsTlrController.putEcsTlrById(id, mockRequest); assertEquals(NO_CONTENT, response.getStatusCode()); } @Test void ecsTlrShouldSuccessfullyBeDeleted() { - when(requestsService.delete(any(UUID.class))).thenReturn(true); - assertEquals(NO_CONTENT, requestsController.deleteEcsTlrById(UUID.randomUUID()).getStatusCode()); + when(ecsTlrService.delete(any(UUID.class))).thenReturn(true); + assertEquals(NO_CONTENT, ecsTlrController.deleteEcsTlrById(UUID.randomUUID()).getStatusCode()); } @Test @@ -77,12 +79,12 @@ void ecsTlrShouldNotBeFound() { var mockRequest = new EcsTlr(); mockRequest.setId(UUID.randomUUID().toString()); - when(requestsService.update(any(UUID.class), any(EcsTlr.class))).thenReturn(false); - var putResponse = requestsController.putEcsTlrById(id, mockRequest); + when(ecsTlrService.update(any(UUID.class), any(EcsTlr.class))).thenReturn(false); + var putResponse = ecsTlrController.putEcsTlrById(id, mockRequest); assertEquals(NOT_FOUND, putResponse.getStatusCode()); - when(requestsService.delete(any(UUID.class))).thenReturn(false); - var deleteResponse = requestsController.deleteEcsTlrById(id); + when(ecsTlrService.delete(any(UUID.class))).thenReturn(false); + var deleteResponse = ecsTlrController.deleteEcsTlrById(id); assertEquals(NOT_FOUND, deleteResponse.getStatusCode()); } } diff --git a/src/test/java/org/folio/controller/TlrSettingsControllerTest.java b/src/test/java/org/folio/controller/TlrSettingsControllerTest.java new file mode 100644 index 00000000..fe72bdd1 --- /dev/null +++ b/src/test/java/org/folio/controller/TlrSettingsControllerTest.java @@ -0,0 +1,62 @@ +package org.folio.controller; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.http.HttpStatus.NOT_FOUND; +import static org.springframework.http.HttpStatus.NO_CONTENT; + +import java.util.Optional; + +import org.folio.domain.dto.TlrSettings; +import org.folio.service.TlrSettingsService; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatusCode; + +@ExtendWith(MockitoExtension.class) +class TlrSettingsControllerTest { + @Mock + private TlrSettingsService tlrSettingsService; + @InjectMocks + private TlrSettingsController tlrSettingsController; + + @Test + void getSettingsNotFoundWhenNull() { + when(tlrSettingsService.getTlrSettings()).thenReturn(Optional.empty()); + var response = tlrSettingsController.getTlrSettings(); + verify(tlrSettingsService, times(1)).getTlrSettings(); + assertEquals(response.getStatusCode(), HttpStatusCode.valueOf(404)); + } + + @Test + void getSettings() { + when(tlrSettingsService.getTlrSettings()).thenReturn(Optional.of(new TlrSettings())); + var response = tlrSettingsController.getTlrSettings(); + verify(tlrSettingsService, times(1)).getTlrSettings(); + assertEquals(response.getStatusCode(), HttpStatusCode.valueOf(200)); + } + + @Test + void tlrSettingsShouldSuccessfullyBeUpdated() { + when(tlrSettingsService.updateTlrSettings(any(TlrSettings.class))) + .thenReturn(Optional.of(new TlrSettings())); + + var response = tlrSettingsController.putTlrSettings(new TlrSettings()); + assertEquals(NO_CONTENT, response.getStatusCode()); + } + + @Test + void tlrSettingsShouldNotBeUpdated() { + when(tlrSettingsService.updateTlrSettings(any(TlrSettings.class))) + .thenReturn(Optional.empty()); + + var response = tlrSettingsController.putTlrSettings(new TlrSettings()); + assertEquals(NOT_FOUND, response.getStatusCode()); + } +} diff --git a/src/test/java/org/folio/service/EcsTlrServiceTest.java b/src/test/java/org/folio/service/EcsTlrServiceTest.java index b788994e..7b40d621 100644 --- a/src/test/java/org/folio/service/EcsTlrServiceTest.java +++ b/src/test/java/org/folio/service/EcsTlrServiceTest.java @@ -9,8 +9,6 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.UUID; @@ -18,7 +16,6 @@ import org.folio.domain.RequestWrapper; import org.folio.domain.dto.EcsTlr; import org.folio.domain.dto.Request; -import org.folio.domain.dto.User; import org.folio.domain.entity.EcsTlrEntity; import org.folio.domain.mapper.EcsTlrMapper; import org.folio.domain.mapper.EcsTlrMapperImpl; diff --git a/src/test/java/org/folio/service/TlrSettingsServiceTest.java b/src/test/java/org/folio/service/TlrSettingsServiceTest.java new file mode 100644 index 00000000..b5c01f22 --- /dev/null +++ b/src/test/java/org/folio/service/TlrSettingsServiceTest.java @@ -0,0 +1,86 @@ +package org.folio.service; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +import org.folio.domain.dto.TlrSettings; +import org.folio.domain.entity.TlrSettingsEntity; +import org.folio.domain.mapper.TlrSettingsMapper; +import org.folio.domain.mapper.TlrSettingsMapperImpl; +import org.folio.repository.TlrSettingsRepository; +import org.folio.service.impl.TlrSettingsServiceImpl; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; + +@ExtendWith(MockitoExtension.class) +class TlrSettingsServiceTest { + + @InjectMocks + private TlrSettingsServiceImpl tlrSettingsService; + @Mock + private TlrSettingsRepository tlrSettingsRepository; + @Spy + private final TlrSettingsMapper tlrSettingsMapper = new TlrSettingsMapperImpl(); + + @Test + void getTlrSettings() { + when(tlrSettingsRepository.findAll(any(PageRequest.class))) + .thenReturn(new PageImpl<>(List.of(new TlrSettingsEntity(UUID.randomUUID(), true)))); + + Optional tlrSettings = tlrSettingsService.getTlrSettings(); + verify(tlrSettingsRepository).findAll(any(PageRequest.class)); + assertTrue(tlrSettings.isPresent()); + assertTrue(tlrSettings.map(TlrSettings::getEcsTlrFeatureEnabled).orElse(false)); + } + + @Test + void getTlrSettingsWithEmptyValue() { + when(tlrSettingsRepository.findAll(any(PageRequest.class))) + .thenReturn(new PageImpl<>(Collections.emptyList())); + + Optional tlrSettings = tlrSettingsService.getTlrSettings(); + verify(tlrSettingsRepository).findAll(any(PageRequest.class)); + assertFalse(tlrSettings.isPresent()); + } + + @Test + void updateTlrSettings() { + var tlrSettingsEntity = new TlrSettingsEntity(UUID.randomUUID(), true); + when(tlrSettingsRepository.findAll(any(PageRequest.class))) + .thenReturn(new PageImpl<>(List.of(tlrSettingsEntity))); + when(tlrSettingsRepository.save(any(TlrSettingsEntity.class))) + .thenReturn(tlrSettingsEntity); + + Optional tlrSettings = tlrSettingsService.updateTlrSettings(new TlrSettings()); + verify(tlrSettingsRepository, times(1)).findAll(any(PageRequest.class)); + verify(tlrSettingsRepository, times(1)).save(any(TlrSettingsEntity.class)); + assertTrue(tlrSettings.isPresent()); + assertTrue(tlrSettings.map(TlrSettings::getEcsTlrFeatureEnabled).orElse(false)); + } + + @Test + void cannotUpdateTlrSettingsIfItDoesNotExist() { + when(tlrSettingsRepository.findAll(any(PageRequest.class))) + .thenReturn(new PageImpl<>(Collections.emptyList())); + + Optional tlrSettings = tlrSettingsService.updateTlrSettings(new TlrSettings()); + verify(tlrSettingsRepository, times(1)).findAll(any(PageRequest.class)); + verify(tlrSettingsRepository, times(0)).save(any(TlrSettingsEntity.class)); + assertFalse(tlrSettings.isPresent()); + } +}