use of uk.gov.pay.connector.events.model.charge.RefundAvailabilityUpdated 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.events.model.charge.RefundAvailabilityUpdated 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.events.model.charge.RefundAvailabilityUpdated in project pay-connector by alphagov.
the class EventFactoryTest method shouldCreatedRefundAvailabilityUpdatedEventForTerminalAndOtherStatesAffectingRefundability.
@Test
@Parameters(method = "statesAffectingRefundability")
public void shouldCreatedRefundAvailabilityUpdatedEventForTerminalAndOtherStatesAffectingRefundability(Class eventClass, Class eventDetailsClass) throws Exception {
Long chargeEventEntityId = 100L;
ChargeEventEntity chargeEventEntity = ChargeEventEntityFixture.aValidChargeEventEntity().withCharge(charge).withId(chargeEventEntityId).build();
when(chargeEventDao.findById(ChargeEventEntity.class, chargeEventEntityId)).thenReturn(Optional.of(chargeEventEntity));
PaymentStateTransition paymentStateTransition = new PaymentStateTransition(chargeEventEntityId, eventClass);
List<Event> events = eventFactory.createEvents(paymentStateTransition);
assertThat(events.size(), is(2));
Event event1 = events.get(0);
assertThat(event1, instanceOf(eventClass));
assertThat(event1.getEventDetails(), instanceOf(eventDetailsClass));
RefundAvailabilityUpdated event2 = (RefundAvailabilityUpdated) events.get(1);
assertThat(event2, instanceOf(RefundAvailabilityUpdated.class));
assertThat(event2.getEventDetails(), instanceOf(RefundAvailabilityUpdatedEventDetails.class));
}
use of uk.gov.pay.connector.events.model.charge.RefundAvailabilityUpdated in project pay-connector by alphagov.
the class EventFactoryTest method shouldCreateCorrectEventsFromRefundErrorStateTransition.
@Test
public void shouldCreateCorrectEventsFromRefundErrorStateTransition() throws Exception {
when(chargeService.findCharge(charge.getExternalId())).thenReturn(Optional.of(Charge.from(charge)));
RefundHistory refundErrorHistory = RefundHistoryEntityFixture.aValidRefundHistoryEntity().withStatus(RefundStatus.REFUND_ERROR.getValue()).withUserExternalId("user_external_id").withChargeExternalId(charge.getExternalId()).withGatewayTransactionId(randomAlphanumeric(30)).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));
RefundError refundError = (RefundError) refundEvents.stream().filter(e -> ResourceType.REFUND.equals(e.getResourceType())).findFirst().get();
assertThat(refundError.getParentResourceExternalId(), is(charge.getExternalId()));
assertThat(((RefundEventWithGatewayTransactionIdDetails) refundError.getEventDetails()).getGatewayTransactionId(), is(refundErrorHistory.getGatewayTransactionId()));
assertThat(refundError.getResourceType(), is(ResourceType.REFUND));
assertThat(refundError.getEventDetails(), is(instanceOf(RefundEventWithGatewayTransactionIdDetails.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.events.model.charge.RefundAvailabilityUpdated in project pay-connector by alphagov.
the class EventFactoryTest method shouldCreatePaymentCreatedEventWithCorrectPayloadForPaymentCreatedStateTransition.
@Test
public void shouldCreatePaymentCreatedEventWithCorrectPayloadForPaymentCreatedStateTransition() throws Exception {
Long chargeEventEntityId = 100L;
ChargeEventEntity chargeEventEntity = ChargeEventEntityFixture.aValidChargeEventEntity().withCharge(charge).withId(chargeEventEntityId).build();
when(chargeEventDao.findById(ChargeEventEntity.class, chargeEventEntityId)).thenReturn(Optional.of(chargeEventEntity));
PaymentStateTransition paymentStateTransition = new PaymentStateTransition(chargeEventEntityId, PaymentCreated.class);
List<Event> events = eventFactory.createEvents(paymentStateTransition);
assertThat(events.size(), is(2));
PaymentCreated event = (PaymentCreated) events.get(0);
assertThat(event, instanceOf(PaymentCreated.class));
assertThat(event.getEventDetails(), instanceOf(PaymentCreatedEventDetails.class));
assertThat(event.getResourceExternalId(), Is.is(chargeEventEntity.getChargeEntity().getExternalId()));
RefundAvailabilityUpdated event2 = (RefundAvailabilityUpdated) events.get(1);
assertThat(event2, instanceOf(RefundAvailabilityUpdated.class));
assertThat(event2.getEventDetails(), instanceOf(RefundAvailabilityUpdatedEventDetails.class));
}
Aggregations