use of uk.gov.pay.connector.queue.statetransition.StateTransition in project pay-connector by alphagov.
the class EventFactoryTest method shouldCreateCorrectEventsFromRefundCreatedStateTransition.
@Test
public void shouldCreateCorrectEventsFromRefundCreatedStateTransition() throws Exception {
when(chargeService.findCharge(charge.getExternalId())).thenReturn(Optional.of(Charge.from(charge)));
RefundHistory refundCreatedHistory = RefundHistoryEntityFixture.aValidRefundHistoryEntity().withStatus(RefundStatus.CREATED.getValue()).withUserExternalId("user_external_id").withUserEmail("test@example.com").withChargeExternalId(charge.getExternalId()).withAmount(charge.getAmount()).build();
when(refundDao.getRefundHistoryByRefundExternalIdAndRefundStatus(refundCreatedHistory.getExternalId(), refundCreatedHistory.getStatus())).thenReturn(Optional.of(refundCreatedHistory));
StateTransition refundStateTransition = new RefundStateTransition(refundCreatedHistory.getExternalId(), refundCreatedHistory.getStatus(), RefundCreatedByUser.class);
List<Event> refundEvents = eventFactory.createEvents(refundStateTransition);
assertThat(refundEvents.size(), is(2));
RefundCreatedByUser refundCreatedByUser = (RefundCreatedByUser) refundEvents.stream().filter(e -> ResourceType.REFUND.equals(e.getResourceType())).findFirst().get();
RefundCreatedByUserEventDetails eventDetails = (RefundCreatedByUserEventDetails) refundCreatedByUser.getEventDetails();
assertThat(refundCreatedByUser.getParentResourceExternalId(), is(charge.getExternalId()));
assertThat(eventDetails.getRefundedBy(), is("user_external_id"));
assertThat(eventDetails.getUserEmail(), is("test@example.com"));
assertThat(refundCreatedByUser.getResourceType(), is(ResourceType.REFUND));
assertThat(refundCreatedByUser.getEventDetails(), is(instanceOf(RefundCreatedByUserEventDetails.class)));
RefundAvailabilityUpdated refundAvailabilityUpdated = (RefundAvailabilityUpdated) refundEvents.stream().filter(e -> ResourceType.PAYMENT.equals(e.getResourceType())).findFirst().get();
assertThat(refundAvailabilityUpdated.getResourceExternalId(), is(charge.getExternalId()));
assertThat(refundAvailabilityUpdated.getResourceType(), is(ResourceType.PAYMENT));
assertThat(refundAvailabilityUpdated.getEventDetails(), is(instanceOf(RefundAvailabilityUpdatedEventDetails.class)));
}
use of uk.gov.pay.connector.queue.statetransition.StateTransition in project pay-connector by alphagov.
the class EventFactoryTest method shouldCreateCorrectEventsFromRefundSucceededStateTransition.
@Test
public void shouldCreateCorrectEventsFromRefundSucceededStateTransition() throws Exception {
when(chargeService.findCharge(charge.getExternalId())).thenReturn(Optional.of(Charge.from(charge)));
RefundHistory refundSucceededHistory = RefundHistoryEntityFixture.aValidRefundHistoryEntity().withStatus(RefundStatus.REFUNDED.getValue()).withUserExternalId("user_external_id").withChargeExternalId(charge.getExternalId()).withAmount(charge.getAmount()).build();
when(refundDao.getRefundHistoryByRefundExternalIdAndRefundStatus(refundSucceededHistory.getExternalId(), refundSucceededHistory.getStatus())).thenReturn(Optional.of(refundSucceededHistory));
StateTransition refundStateTransition = new RefundStateTransition(refundSucceededHistory.getExternalId(), refundSucceededHistory.getStatus(), RefundSucceeded.class);
List<Event> refundEvents = eventFactory.createEvents(refundStateTransition);
assertThat(refundEvents.size(), is(1));
RefundSucceeded refundSucceeded = (RefundSucceeded) refundEvents.get(0);
assertThat(refundSucceeded.getParentResourceExternalId(), is(charge.getExternalId()));
assertThat(refundSucceeded.getResourceExternalId(), is(refundSucceededHistory.getExternalId()));
assertThat(refundSucceeded.getResourceType(), is(ResourceType.REFUND));
assertThat(refundSucceeded.getEventDetails(), is(instanceOf(RefundEventWithGatewayTransactionIdDetails.class)));
}
use of uk.gov.pay.connector.queue.statetransition.StateTransition in project pay-connector by alphagov.
the class EventFactoryTest method shouldCreateCorrectEventsFromRefundSubmittedStateTransition.
@Test
public void shouldCreateCorrectEventsFromRefundSubmittedStateTransition() throws Exception {
when(chargeService.findCharge(charge.getExternalId())).thenReturn(Optional.of(Charge.from(charge)));
RefundHistory refundSubmittedHistory = RefundHistoryEntityFixture.aValidRefundHistoryEntity().withStatus(RefundStatus.REFUND_SUBMITTED.getValue()).withUserExternalId("user_external_id").withChargeExternalId(charge.getExternalId()).withAmount(charge.getAmount()).build();
when(refundDao.getRefundHistoryByRefundExternalIdAndRefundStatus(refundSubmittedHistory.getExternalId(), refundSubmittedHistory.getStatus())).thenReturn(Optional.of(refundSubmittedHistory));
StateTransition refundStateTransition = new RefundStateTransition(refundSubmittedHistory.getExternalId(), refundSubmittedHistory.getStatus(), RefundSubmitted.class);
List<Event> refundEvents = eventFactory.createEvents(refundStateTransition);
assertThat(refundEvents.size(), is(1));
RefundSubmitted refundSubmitted = (RefundSubmitted) refundEvents.get(0);
assertThat(refundSubmitted.getParentResourceExternalId(), is(charge.getExternalId()));
assertThat(refundSubmitted.getResourceExternalId(), is(refundSubmittedHistory.getExternalId()));
assertThat(refundSubmitted.getResourceType(), is(ResourceType.REFUND));
assertThat(refundSubmitted.getEventDetails(), is(instanceOf(RefundEventWithGatewayTransactionIdDetails.class)));
}
use of uk.gov.pay.connector.queue.statetransition.StateTransition in project pay-connector by alphagov.
the class EventFactoryTest method shouldCreatedARefundAvailabilityUpdatedEvent_ifPaymentIsHistoric.
@Test
public void shouldCreatedARefundAvailabilityUpdatedEvent_ifPaymentIsHistoric() throws Exception {
ChargeResponse.RefundSummary refundSummary = new ChargeResponse.RefundSummary();
refundSummary.setStatus("available");
LedgerTransaction transaction = new LedgerTransaction();
transaction.setTransactionId(charge.getExternalId());
transaction.setPaymentProvider("sandbox");
transaction.setRefundSummary(refundSummary);
transaction.setAmount(charge.getAmount());
transaction.setTotalAmount(charge.getAmount());
transaction.setCreatedDate(Instant.now().toString());
transaction.setGatewayAccountId(charge.getGatewayAccount().getId().toString());
transaction.setServiceId(charge.getServiceId());
transaction.setLive(charge.getGatewayAccount().isLive());
when(chargeService.findCharge(transaction.getTransactionId())).thenReturn(Optional.of(Charge.from(transaction)));
RefundHistory refundErrorHistory = RefundHistoryEntityFixture.aValidRefundHistoryEntity().withStatus(RefundStatus.REFUND_ERROR.getValue()).withUserExternalId("user_external_id").withChargeExternalId(charge.getExternalId()).withAmount(charge.getAmount()).build();
when(refundDao.getRefundHistoryByRefundExternalIdAndRefundStatus(refundErrorHistory.getExternalId(), refundErrorHistory.getStatus())).thenReturn(Optional.of(refundErrorHistory));
StateTransition refundStateTransition = new RefundStateTransition(refundErrorHistory.getExternalId(), refundErrorHistory.getStatus(), RefundError.class);
List<Event> refundEvents = eventFactory.createEvents(refundStateTransition);
assertThat(refundEvents.size(), is(2));
RefundAvailabilityUpdated refundAvailabilityUpdated = (RefundAvailabilityUpdated) refundEvents.stream().filter(e -> ResourceType.PAYMENT.equals(e.getResourceType())).findFirst().get();
assertThat(refundAvailabilityUpdated.getResourceExternalId(), is(transaction.getTransactionId()));
assertThat(refundAvailabilityUpdated.getResourceType(), is(ResourceType.PAYMENT));
assertThat(refundAvailabilityUpdated.getEventDetails(), is(instanceOf(RefundAvailabilityUpdatedEventDetails.class)));
}
use of uk.gov.pay.connector.queue.statetransition.StateTransition in project pay-connector by alphagov.
the class HistoricalEventEmitterServiceTest method executeShouldOfferRefundEventsWithRefundHistory.
@Test
public void executeShouldOfferRefundEventsWithRefundHistory() {
RefundHistory refundHistory = RefundHistoryEntityFixture.aValidRefundHistoryEntity().withChargeExternalId(chargeEntity.getExternalId()).withStatus(RefundStatus.CREATED.toString()).build();
chargeEntity.getEvents().clear();
when(refundDao.searchAllHistoryByChargeExternalId(chargeEntity.getExternalId())).thenReturn(List.of(refundHistory));
when(chargeDao.findMaxId()).thenReturn(1L);
when(chargeDao.findById(1L)).thenReturn(Optional.of(chargeEntity));
when(chargeService.findCharge(chargeEntity.getExternalId())).thenReturn(Optional.of(Charge.from(chargeEntity)));
historicalEventEmitterService.emitHistoricEventsById(1L, OptionalLong.empty(), 1L);
ArgumentCaptor<StateTransition> argument = ArgumentCaptor.forClass(StateTransition.class);
verify(stateTransitionService).offerStateTransition(argument.capture(), any(RefundCreatedByService.class), isNotNull());
assertThat(argument.getAllValues().get(0).getStateTransitionEventClass(), is(RefundCreatedByService.class));
verify(emittedEventDao, atMostOnce()).recordEmission(any(), isNotNull());
}
Aggregations