Skip to content

Commit

Permalink
some fix
Browse files Browse the repository at this point in the history
  • Loading branch information
noemil12 committed Nov 14, 2023
1 parent fbcf62c commit 2fc6da9
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@EnableRedisRepositories
@Configuration
@Slf4j
public class RedisCacheConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Repository
public interface ConsumerEServiceRepository extends ReactiveCrudRepository<ConsumerEService, String> {


@Query("SELECT * FROM consumer_eservice c WHERE c.eservice_id = :eserviceId AND c.consumer_id = :consumerId AND UPPER(c.state) = UPPER(:state)")
Mono<ConsumerEService> findByConsumerIdAndEServiceIdAndState(String eserviceId, String consumerId, String state);
Flux<ConsumerEService> findByConsumerIdAndEServiceIdAndState(String eserviceId, String consumerId, String state);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Repository
public interface EServiceRepository extends ReactiveCrudRepository<EService, String> {

@Query("SELECT * FROM eservice e WHERE e.eservice_id = :eserviceId AND e.descriptor_id = :descriptorId AND UPPER(e.state) = UPPER(:state)")
Mono<EService> checkEServiceStatus(String eserviceId, String descriptorId, String state);
Flux<EService> checkEServiceStatus(String eserviceId, String descriptorId, String state);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ private Mono<ConsumerEService> getFromDbAndSaveOnCache(String eserviceId, String
.doOnNext(entity ->
log.info("[{}-{}] ConsumerEService founded into DB", consumerId, eserviceId)
)
.collectList()
.map(list -> list.get(0))
.flatMap(entity -> this.cacheRepository.save(mapper.toCache(entity)))
.map(mapper::toEntity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ private Mono<EService> getFromDbAndSaveOnCache(String eserviceId, String descrip
.doOnNext(entity ->
log.info("[{}-{}] EService founded into DB", eserviceId, descriptorId)
)
.collectList()
.map(list -> list.get(0))
.flatMap(entity -> this.cacheRepository.save(mapper.toCache(entity)))
.map(mapper::toEntity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.autoconfigure.data.r2dbc.DataR2dbcTest;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
Expand All @@ -18,7 +19,9 @@
@Import(LocalStackTestConfig.class)
public class BaseTest {


@ActiveProfiles("test")
@DataR2dbcTest
public static class WithR2DBC { }

@ActiveProfiles("test")
@Import(LocalStackTestConfig.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,62 @@
package it.pagopa.interop.signalhub.pull.service.repository;

import it.pagopa.interop.signalhub.pull.service.cache.model.ConsumerEServiceCache;
import it.pagopa.interop.signalhub.pull.service.cache.repository.ConsumerEServiceCacheRepository;
import it.pagopa.interop.signalhub.pull.service.config.BaseTest;
import it.pagopa.interop.signalhub.pull.service.entities.ConsumerEService;
import it.pagopa.interop.signalhub.pull.service.mapper.ConsumerEServiceMapper;
import it.pagopa.interop.signalhub.pull.service.service.impl.ConsumerServiceImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;

@ExtendWith(MockitoExtension.class)
class ConsumerEServiceRepositoryImplTest {
class ConsumerEServiceRepositoryImplTest extends BaseTest.WithR2DBC {

private static final String correctEservice = "1234";
private static final String correctConsumer = "1234";
private static final String correctState = "PUBLISHED";
private static final String incorrectState = "ACTIVE";

@Autowired
private ConsumerEServiceRepository consumerEServiceRepository;

@BeforeEach
void setUp(){
consumerEServiceRepository.save(getEntity());
}

@Test
void whenFindOrganizationWithBadlyParamThenReturnNull(){
List<ConsumerEService> entity =
consumerEServiceRepository.findByConsumerIdAndEServiceIdAndState(
correctEservice,
correctConsumer,
incorrectState).collectList().block();

Assertions.assertTrue(entity.isEmpty());
}


@Test
void whenFindOrganizationWithCorrectParamThenReturnEntity(){
List<ConsumerEService> entity =
consumerEServiceRepository.findByConsumerIdAndEServiceIdAndState(
correctEservice,
correctConsumer,
correctState).collectList().block();

Assertions.assertFalse(entity.isEmpty());
Assertions.assertEquals(entity.get(0), getEntity());
}


private ConsumerEService getEntity() {
ConsumerEService entity = new ConsumerEService();
entity.setEserviceId(correctEservice);
entity.setConsumerId(correctConsumer);
entity.setState(correctState);
entity.setTmstInsert(Timestamp.from(Instant.now()));
return entity;
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,67 @@
package it.pagopa.interop.signalhub.pull.service.repository;

import it.pagopa.interop.signalhub.pull.service.cache.model.EServiceCache;
import it.pagopa.interop.signalhub.pull.service.cache.repository.EServiceCacheRepository;
import it.pagopa.interop.signalhub.pull.service.config.BaseTest;
import it.pagopa.interop.signalhub.pull.service.entities.EService;
import it.pagopa.interop.signalhub.pull.service.exception.ExceptionTypeEnum;
import it.pagopa.interop.signalhub.pull.service.exception.PDNDGenericException;
import it.pagopa.interop.signalhub.pull.service.mapper.EServiceMapper;
import it.pagopa.interop.signalhub.pull.service.service.impl.OrganizationServiceImpl;
import it.pagopa.interop.signalhub.pull.service.utils.Const;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import static org.junit.jupiter.api.Assertions.*;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;

@ExtendWith(MockitoExtension.class)
class EServiceRepositoryImplTest {
class EServiceRepositoryImplTest extends BaseTest.WithR2DBC {
private static final String correctEservice = "1234";
private static final String correctDescriptor = "1234";
private static final String correctState = "PUBLISHED";
private static final String incorrectState = "ACTIVE";

@Autowired
private EServiceRepository eServiceRepository;

@BeforeEach
void setUp(){
eServiceRepository.save(getEntity());
}

@Test
void whenFindOrganizationWithBadlyParamThenReturnNull(){
List<EService> entity =
eServiceRepository.checkEServiceStatus(
correctEservice,
correctDescriptor,
incorrectState).collectList().block();

Assertions.assertTrue(entity.isEmpty());
}


@Test
void whenFindOrganizationWithCorrectParamThenReturnEntity(){
eServiceRepository.save(getEntity()).block();
Assertions.assertFalse(eServiceRepository.findAll().collectList().block().isEmpty());
List<EService> entity =
eServiceRepository.checkEServiceStatus(
correctEservice,
correctDescriptor,
correctState).collectList().block();

Assertions.assertFalse(entity.isEmpty());
Assertions.assertEquals(entity.get(0), getEntity());
}


private EService getEntity(){
EService entity = new EService();
entity.setEserviceId(correctEservice);
entity.setProducerId(correctDescriptor);
entity.setState("published");
entity.setTmstInsert(Timestamp.valueOf("2020-01-01 00:00:00"));
entity.setTmstLastEdit(Timestamp.valueOf("2020-01-01 00:00:00"));
return entity;
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

Expand All @@ -38,7 +39,7 @@ class ConsumerServiceImplTest {
@Test
void findByConsumerIdAndEServiceIdButNotExists() {
Mockito.when(cacheRepository.findById(Mockito.any(), Mockito.any())).thenReturn(Mono.empty());
Mockito.when(consumerEServiceRepository.findByConsumerIdAndEServiceIdAndState(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Mono.empty());
Mockito.when(consumerEServiceRepository.findByConsumerIdAndEServiceIdAndState(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Flux.empty());

StepVerifier.create(consumerService.getConsumerEservice("1234","1234"))
.expectErrorMatches((ex) -> {
Expand All @@ -62,7 +63,7 @@ void findByConsumerIdAndEServiceIdAndFoundedInCache() {
consumerEServiceCache.setState(Const.STATE_ACTIVE);
Mockito.when(cacheRepository.findById(Mockito.any(), Mockito.any())).thenReturn(Mono.just(consumerEServiceCache));
Mockito.when(mapper.toEntity(Mockito.any())).thenReturn(consumerEService);
Mockito.when(consumerEServiceRepository.findByConsumerIdAndEServiceIdAndState(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Mono.empty());
Mockito.when(consumerEServiceRepository.findByConsumerIdAndEServiceIdAndState(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Flux.empty());
assertNotNull(consumerService.getConsumerEservice("123", "123").block());

//case 2: founded in cache but state is not active
Expand All @@ -79,7 +80,7 @@ void findByConsumerIdAndEServiceIdAndFoundedInCache() {
@Test
void findByConsumerIdAndEServiceIdButNotFoundInCache() {
Mockito.when(cacheRepository.findById(Mockito.any(), Mockito.any())).thenReturn(Mono.empty());
Mockito.when(consumerEServiceRepository.findByConsumerIdAndEServiceIdAndState(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Mono.just(new ConsumerEService()));
Mockito.when(consumerEServiceRepository.findByConsumerIdAndEServiceIdAndState(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Flux.just(new ConsumerEService()));
Mockito.when(cacheRepository.save(Mockito.any())).thenReturn(Mono.just(new ConsumerEServiceCache()));
Mockito.when(mapper.toEntity(Mockito.any())).thenReturn(new ConsumerEService());
assertNotNull(consumerService.getConsumerEservice("123", "123").block());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

Expand All @@ -38,7 +39,7 @@ class OrganizationServiceImplTest {
@Test
void findByConsumerIdAndEServiceIdButNotExists() {
Mockito.when(cacheRepository.findById(Mockito.any(), Mockito.any())).thenReturn(Mono.empty());
Mockito.when(eServiceRepository.checkEServiceStatus(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Mono.empty());
Mockito.when(eServiceRepository.checkEServiceStatus(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Flux.empty());

StepVerifier.create(organizationService.getEService("1234","1234"))
.expectErrorMatches((ex) -> {
Expand All @@ -60,7 +61,7 @@ void findByConsumerIdAndEServiceIdAndFoundedInCache() {
eServiceCache.setState(Const.STATE_PUBLISHED);
Mockito.when(cacheRepository.findById(Mockito.any(), Mockito.any())).thenReturn(Mono.just(eServiceCache));
Mockito.when(mapper.toEntity(Mockito.any())).thenReturn(eService);
Mockito.when(eServiceRepository.checkEServiceStatus(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Mono.empty());
Mockito.when(eServiceRepository.checkEServiceStatus(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Flux.empty());

assertNotNull(organizationService.getEService("123", "123").block());

Expand All @@ -76,7 +77,7 @@ void findByConsumerIdAndEServiceIdButStateIsNotPublished() {
eServiceCache.setDescriptorId("123");
eServiceCache.setState("test");
Mockito.when(cacheRepository.findById(Mockito.any(), Mockito.any())).thenReturn(Mono.just(eServiceCache));
Mockito.when(eServiceRepository.checkEServiceStatus(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Mono.empty());
Mockito.when(eServiceRepository.checkEServiceStatus(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Flux.empty());

StepVerifier.create(organizationService.getEService("1234","1234"))
.expectErrorMatches((ex) -> {
Expand All @@ -90,7 +91,7 @@ void findByConsumerIdAndEServiceIdButStateIsNotPublished() {
@Test
void findByConsumerIdAndEServiceIdButNotFoundInCache() {
Mockito.when(cacheRepository.findById(Mockito.any(), Mockito.any())).thenReturn(Mono.empty());
Mockito.when(eServiceRepository.checkEServiceStatus(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Mono.just(new EService()));
Mockito.when(eServiceRepository.checkEServiceStatus(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Flux.just(new EService()));
Mockito.when(cacheRepository.save(Mockito.any())).thenReturn(Mono.just(new EServiceCache()));
Mockito.when(mapper.toEntity(Mockito.any())).thenReturn(new EService());
assertNotNull(organizationService.getEService("123", "123").block());
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/application-test.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mockserver.bean.port=1080
mockserver.bean.port=1180
pdnd.client.event.endpoint-url=http://localhost:${mockserver.bean.port}/1.0

spring.r2dbc.url=r2dbc:h2:mem:///db;MODE=PostgreSQL
Expand Down

0 comments on commit 2fc6da9

Please sign in to comment.