Skip to content

Commit

Permalink
Added Interoperability's tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pasqualino.cristaudo committed Nov 24, 2023
1 parent 77647c2 commit c7bbe65
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ public class InteroperabilityCacheRepository {

public Mono<Agreement> findById(String purposeId) {
return this.reactiveRedisOperations.opsForValue().get(purposeId)
.flatMap(finded -> {
if (ObjectUtils.isEmpty(finded)) return Mono.empty();
return Mono.just(finded);
});
.flatMap(Mono::just);
}

public Mono<Agreement> save(Agreement agreement, String purposeId){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package it.pagopa.interop.signalhub.pull.service.cache.repository;

import it.pagopa.interop.signalhub.pull.service.generated.openapi.client.interop.model.v1.Agreement;
import it.pagopa.interop.signalhub.pull.service.generated.openapi.client.interop.model.v1.AgreementState;
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.redis.core.ReactiveRedisOperations;
import org.springframework.data.redis.core.ReactiveValueOperations;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;


@ExtendWith(MockitoExtension.class)
class InteroperabilityCacheRepositoryTest {
@InjectMocks
private InteroperabilityCacheRepository interoperabilityCacheRepository;
@Mock
private ReactiveRedisOperations<String, Agreement> reactiveRedisOperations;
@Mock
private ReactiveValueOperations<String, Agreement> valueOperations;

private String principalId;
private String purposeId;
private String eServiceId;
private String producerId;
private String descriptorId;
private AgreementState state;


@BeforeEach
void preTest() {
setUp();
}

@Test
void findById() {
Agreement agreement = getAgreement();
Mockito.when(reactiveRedisOperations.opsForValue())
.thenReturn(valueOperations);

Mockito.when(valueOperations.get(purposeId))
.thenReturn(Mono.just(agreement));

interoperabilityCacheRepository.findById(purposeId)
.flatMap(pa -> {
assertEquals(agreement.getEserviceId(), pa.getEserviceId());
assertEquals(agreement.getId(), pa.getId());
assertEquals(agreement.getConsumerId(), pa.getConsumerId());
assertEquals(agreement.getProducerId(), pa.getProducerId());
assertEquals(agreement.getDescriptorId(), pa.getDescriptorId());
return Mono.empty();
})
.block();

Mockito.when(valueOperations.get(purposeId))
.thenReturn(Mono.empty());

StepVerifier.create(interoperabilityCacheRepository.findById(purposeId))
.expectNextCount(0)
.verifyComplete();
}

@Test
void save() {
Agreement agreement = getAgreement();
Mockito.when(reactiveRedisOperations.opsForValue())
.thenReturn(valueOperations);

Mockito.when(valueOperations.set(purposeId, agreement))
.thenReturn(Mono.just(true));

assertNotNull(interoperabilityCacheRepository.save(agreement, purposeId)
.block());
}


private Agreement getAgreement() {
Agreement agreement = new Agreement();
agreement.setId(UUID.fromString(principalId));
agreement.setEserviceId(UUID.fromString(purposeId));
agreement.setDescriptorId(UUID.fromString(eServiceId));
agreement.setProducerId(UUID.fromString(producerId));
agreement.setConsumerId(UUID.fromString(descriptorId));
agreement.setState(state);
return agreement;
}

private void setUp() {
principalId = "0f14d0ab-9605-4a62-a9e4-5ed26688389b";
purposeId = "4a620f14-d0ab-9605-a9e4-5ed26688389b";
eServiceId = "5ed26688-389b-4a62-9605-0f14d0ab9605";
producerId = "8a631cab-797b-482d-bf7d-615bf1004368";
descriptorId = "01920f14-d0ab-9605-a9e4-374650192842";
state = AgreementState.ARCHIVED;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package it.pagopa.interop.signalhub.pull.service.mapper;

import it.pagopa.interop.signalhub.pull.service.auth.PrincipalAgreement;
import it.pagopa.interop.signalhub.pull.service.generated.openapi.client.interop.model.v1.Agreement;
import it.pagopa.interop.signalhub.pull.service.generated.openapi.client.interop.model.v1.AgreementState;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;


class PrincipalAgreementMapperTest {
private final PrincipalAgreementMapper principalAgreementMapper = Mappers.getMapper(PrincipalAgreementMapper.class);
private String principalId;
private String purposeId;
private String eServiceId;
private String producerId;
private String descriptorId;
private AgreementState state;

@BeforeEach
void preTest() {
setUp();
}

@Test
void toPrincipal() {
Agreement agreement = getAgreement();
PrincipalAgreement principalAgreement = principalAgreementMapper.toPrincipal(agreement);
assertNotNull(principalAgreement);
assertEquals(agreement.getEserviceId().toString(), principalAgreement.getEServiceId());
assertEquals(agreement.getId().toString(), principalAgreement.getPurposeId());
assertEquals(agreement.getConsumerId().toString(), principalAgreement.getPrincipalId());
assertEquals(agreement.getProducerId().toString(), principalAgreement.getProducerId());
assertEquals(agreement.getDescriptorId().toString(), principalAgreement.getDescriptorId());

agreement.setId(null);
principalAgreement = principalAgreementMapper.toPrincipal(agreement);
assertNull(principalAgreement.getPurposeId());

agreement.setConsumerId(null);
principalAgreement = principalAgreementMapper.toPrincipal(agreement);
assertNull(principalAgreement.getPrincipalId());

agreement.setEserviceId(null);
principalAgreement = principalAgreementMapper.toPrincipal(agreement);
assertNull(principalAgreement.getEServiceId());

agreement.setProducerId(null);
principalAgreement = principalAgreementMapper.toPrincipal(agreement);
assertNull(principalAgreement.getProducerId());

agreement.setDescriptorId(null);
principalAgreement = principalAgreementMapper.toPrincipal(agreement);
assertNull(principalAgreement.getDescriptorId());

agreement.setState(null);
principalAgreement = principalAgreementMapper.toPrincipal(agreement);
assertNull(principalAgreement.getState());
}

@Test
void whenCallToPrincipalAndAgreementIsNull() {
Agreement agreement = null;
PrincipalAgreement principalAgreement = principalAgreementMapper.toPrincipal(agreement);
assertNull(principalAgreement);
}

private Agreement getAgreement() {
Agreement agreement = new Agreement();
agreement.setId(UUID.fromString(principalId));
agreement.setEserviceId(UUID.fromString(purposeId));
agreement.setDescriptorId(UUID.fromString(eServiceId));
agreement.setProducerId(UUID.fromString(producerId));
agreement.setConsumerId(UUID.fromString(descriptorId));
agreement.setState(state);
return agreement;
}

private void setUp() {
principalId = "0f14d0ab-9605-4a62-a9e4-5ed26688389b";
purposeId = "4a620f14-d0ab-9605-a9e4-5ed26688389b";
eServiceId = "5ed26688-389b-4a62-9605-0f14d0ab9605";
producerId = "8a631cab-797b-482d-bf7d-615bf1004368";
descriptorId = "01920f14-d0ab-9605-a9e4-374650192842";
state = AgreementState.ARCHIVED;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package it.pagopa.interop.signalhub.pull.service.service.impl;

import it.pagopa.interop.signalhub.pull.service.auth.PrincipalAgreement;
import it.pagopa.interop.signalhub.pull.service.cache.repository.InteroperabilityCacheRepository;
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.externalclient.InteroperabilityClient;
import it.pagopa.interop.signalhub.pull.service.generated.openapi.client.interop.model.v1.Agreement;
import it.pagopa.interop.signalhub.pull.service.generated.openapi.client.interop.model.v1.AgreementState;
import it.pagopa.interop.signalhub.pull.service.mapper.PrincipalAgreementMapper;
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 reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.util.UUID;


@ExtendWith(MockitoExtension.class)
class InteropServiceImplTest {
@InjectMocks
private InteropServiceImpl interopService;
@Mock
private InteroperabilityClient interoperabilityClient;
@Mock
private PrincipalAgreementMapper principalAgreementMapper;
@Mock
private InteroperabilityCacheRepository cacheRepository;
private String principalId;
private String purposeId;
private String eServiceId;
private String producerId;
private String descriptorId;
private AgreementState state;

@BeforeEach
void preTest() {
setUp();
}

@Test
void getPrincipalFromPurposeIdWhenAgreementWasFoundTest() {
Agreement agreement = getAgreement();
PrincipalAgreement principalAgreement = getPrincipalAgreement();

Mockito.when(cacheRepository.findById(purposeId))
.thenReturn(Mono.just(agreement));
Mockito.when(cacheRepository.save(agreement, purposeId))
.thenReturn(Mono.just(agreement));
Mockito.when(principalAgreementMapper.toPrincipal(agreement))
.thenReturn(principalAgreement);

interopService.getPrincipalFromPurposeId(purposeId)
.flatMap(pa -> {
Assertions.assertEquals(purposeId, pa.getPurposeId());
Assertions.assertEquals(principalId, pa.getPrincipalId());
Assertions.assertEquals(eServiceId, pa.getEServiceId());
Assertions.assertEquals(producerId, pa.getProducerId());
Assertions.assertEquals(descriptorId, pa.getDescriptorId());
Assertions.assertEquals(state.getValue(), pa.getState());
return Mono.empty();
})
.block();
}

@Test
void getPrincipalFromPurposeIdWhenNoAgreementWasFoundTest() {
Agreement agreement = getAgreement();
PrincipalAgreement principalAgreement = getPrincipalAgreement();

Mockito.when(cacheRepository.findById(purposeId))
.thenReturn(Mono.empty());
Mockito.when(interoperabilityClient.getAgreementByPurposeId(purposeId))
.thenReturn(Mono.just(agreement));
Mockito.when(cacheRepository.save(agreement, purposeId))
.thenReturn(Mono.just(agreement));
Mockito.when(principalAgreementMapper.toPrincipal(agreement))
.thenReturn(principalAgreement);

interopService.getPrincipalFromPurposeId(purposeId)
.flatMap(pa -> {
Assertions.assertEquals(purposeId, pa.getPurposeId());
Assertions.assertEquals(principalId, pa.getPrincipalId());
Assertions.assertEquals(eServiceId, pa.getEServiceId());
Assertions.assertEquals(producerId, pa.getProducerId());
Assertions.assertEquals(descriptorId, pa.getDescriptorId());
Assertions.assertEquals(state.getValue(), pa.getState());
return Mono.empty();
})
.block();
}

@Test
void getPrincipalFromPurposeIdWhenNoAgreementWasFoundAndClientReturnErrorTest() {
Mockito.when(cacheRepository.findById(purposeId))
.thenReturn(Mono.empty());
Mockito.when(interoperabilityClient.getAgreementByPurposeId(purposeId))
.thenThrow(new PDNDGenericException(ExceptionTypeEnum.DETAIL_AGREEMENT_ERROR,ExceptionTypeEnum.DETAIL_AGREEMENT_ERROR.getMessage()));

StepVerifier.create(interopService.getPrincipalFromPurposeId(purposeId))
.expectErrorMatches(exception -> {
Assertions.assertEquals(exception.getClass(), PDNDGenericException.class);
Assertions.assertEquals(exception.getMessage(), ExceptionTypeEnum.DETAIL_AGREEMENT_ERROR.getMessage());
return true;
}).verify();
}

private PrincipalAgreement getPrincipalAgreement() {
PrincipalAgreement principalAgreement = new PrincipalAgreement();
principalAgreement.setPrincipalId(principalId);
principalAgreement.setPurposeId(purposeId);
principalAgreement.setEServiceId(eServiceId);
principalAgreement.setProducerId(producerId);
principalAgreement.setDescriptorId(descriptorId);
principalAgreement.setState(state.getValue());
return principalAgreement;
}

private Agreement getAgreement() {
Agreement agreement = new Agreement();
agreement.setId(UUID.fromString(principalId));
agreement.setEserviceId(UUID.fromString(purposeId));
agreement.setDescriptorId(UUID.fromString(eServiceId));
agreement.setProducerId(UUID.fromString(producerId));
agreement.setConsumerId(UUID.fromString(descriptorId));
agreement.setState(state);
return agreement;
}

private void setUp() {
principalId = "0f14d0ab-9605-4a62-a9e4-5ed26688389b";
purposeId = "4a620f14-d0ab-9605-a9e4-5ed26688389b";
eServiceId = "5ed26688-389b-4a62-9605-0f14d0ab9605";
producerId = "8a631cab-797b-482d-bf7d-615bf1004368";
descriptorId = "01920f14-d0ab-9605-a9e4-374650192842";
state = AgreementState.ARCHIVED;
}
}

0 comments on commit c7bbe65

Please sign in to comment.