Skip to content

Commit

Permalink
Splitted signal service impl with its interface
Browse files Browse the repository at this point in the history
  • Loading branch information
pasqualino.cristaudo committed Oct 20, 2023
1 parent c8cc11f commit 3cf70e8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,52 +1,8 @@
package it.pagopa.interop.signalhub.persister.service;

import it.pagopa.interop.signalhub.persister.entity.DeadSignal;
import it.pagopa.interop.signalhub.persister.entity.Signal;
import it.pagopa.interop.signalhub.persister.mapper.DeadSignalMapper;
import it.pagopa.interop.signalhub.persister.repository.DeadSignalRepository;
import it.pagopa.interop.signalhub.persister.repository.SignalRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Mono;
import static it.pagopa.interop.signalhub.persister.exception.ExceptionTypeEnum.DUPLICATE_SIGNAL_ERROR;


@Service
@Slf4j
public class SignalService {
@Autowired
private SignalRepository signalRepository;
@Autowired
private DeadSignalRepository deadSignalRepository;
@Autowired
private DeadSignalMapper deadSignalMapper;


@Transactional
public Mono<Signal> signalServiceFlow(Signal signal) {

return getSignalById(signal.getSignalId(), signal.getEserviceId())
.switchIfEmpty(Mono.just(signal))
.filter(entity -> entity.getId() == null)
.flatMap(this::createSignal)
.switchIfEmpty(Mono.defer(() -> saveToDeadSignal(signal)));
}

private Mono<Signal> getSignalById(Long signalId, String eserviceId) { return this.signalRepository.findByIndexSignalAndEserviceId(signalId, eserviceId); }
private Mono<Signal> createSignal(Signal signal) { return this.signalRepository.save(signal); }
private Mono<DeadSignal> createDeadSignal(DeadSignal signal) { return this.deadSignalRepository.save(signal); }

private Mono<DeadSignal> getDeadSignal(Signal signal) {
DeadSignal deadSignal = deadSignalMapper.signalToDeadSignal(signal);
deadSignal.setErrorReason(DUPLICATE_SIGNAL_ERROR.toString());
return Mono.just(deadSignal);
}

private Mono<Signal> saveToDeadSignal(Signal signal) {
return getDeadSignal(signal)
.flatMap(this::createDeadSignal)
.then(Mono.just(signal));
}
}
public interface SignalService {
Mono<Signal> signalServiceFlow(Signal signal);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package it.pagopa.interop.signalhub.persister.service.impl;

import it.pagopa.interop.signalhub.persister.entity.DeadSignal;
import it.pagopa.interop.signalhub.persister.entity.Signal;
import it.pagopa.interop.signalhub.persister.mapper.DeadSignalMapper;
import it.pagopa.interop.signalhub.persister.repository.DeadSignalRepository;
import it.pagopa.interop.signalhub.persister.repository.SignalRepository;
import it.pagopa.interop.signalhub.persister.service.SignalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Mono;
import static it.pagopa.interop.signalhub.persister.exception.ExceptionTypeEnum.DUPLICATE_SIGNAL_ERROR;


@Service
@Slf4j
public class SignalServiceImpl implements SignalService {
@Autowired
private SignalRepository signalRepository;
@Autowired
private DeadSignalRepository deadSignalRepository;
@Autowired
private DeadSignalMapper deadSignalMapper;


@Transactional
public Mono<Signal> signalServiceFlow(Signal signal) {

return getSignalById(signal.getSignalId(), signal.getEserviceId())
.switchIfEmpty(Mono.just(signal))
.filter(entity -> entity.getId() == null)
.flatMap(this::createSignal)
.switchIfEmpty(Mono.defer(() -> saveToDeadSignal(signal)));
}

private Mono<Signal> getSignalById(Long signalId, String eserviceId) { return this.signalRepository.findByIndexSignalAndEserviceId(signalId, eserviceId); }
private Mono<Signal> createSignal(Signal signal) { return this.signalRepository.save(signal); }
private Mono<DeadSignal> createDeadSignal(DeadSignal signal) { return this.deadSignalRepository.save(signal); }

private Mono<DeadSignal> getDeadSignal(Signal signal) {
DeadSignal deadSignal = deadSignalMapper.signalToDeadSignal(signal);
deadSignal.setErrorReason(DUPLICATE_SIGNAL_ERROR.toString());
return Mono.just(deadSignal);
}

private Mono<Signal> saveToDeadSignal(Signal signal) {
return getDeadSignal(signal)
.flatMap(this::createDeadSignal)
.then(Mono.just(signal));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import it.pagopa.interop.signalhub.persister.queue.model.SignalType;
import it.pagopa.interop.signalhub.persister.repository.DeadSignalRepository;
import it.pagopa.interop.signalhub.persister.repository.SignalRepository;
import it.pagopa.interop.signalhub.persister.service.impl.SignalServiceImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -24,9 +25,9 @@


@ExtendWith(MockitoExtension.class)
class SignalServiceTest {
class SignalServiceImplTest {
@InjectMocks
SignalService signalService;
SignalServiceImpl signalServiceImpl;
@Mock
private SignalRepository signalRepository;
@Mock
Expand Down Expand Up @@ -56,7 +57,7 @@ void signalServiceFlowTest() {
.thenReturn(Mono.just(signalToSave));


signalService.signalServiceFlow(signalToSave)
signalServiceImpl.signalServiceFlow(signalToSave)
.flatMap(sig -> {
Assertions.assertNotNull(sig);
Assertions.assertEquals(signalToSave, sig);
Expand Down Expand Up @@ -107,7 +108,7 @@ void signalServiceFlowDuplicateSignalTest() {
.when(deadSignalRepository.save(deadSignal))
.thenReturn(Mono.empty());

signalService.signalServiceFlow(signalToUpdate)
signalServiceImpl.signalServiceFlow(signalToUpdate)
.flatMap(sig -> {
Assertions.assertNotNull(sig);
Assertions.assertEquals(signalToUpdate, sig);
Expand Down

0 comments on commit 3cf70e8

Please sign in to comment.