Search in sources :

Example 1 with BusEvent

use of org.killbill.bus.api.BusEvent in project killbill by killbill.

the class AdminResource method getQueueEntries.

@GET
@Path("/queues")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Get queues entries", response = Response.class)
@ApiResponses(value = {})
public Response getQueueEntries(@QueryParam("accountId") final String accountIdStr, @QueryParam("queueName") final String queueName, @QueryParam("serviceName") final String serviceName, @QueryParam("withHistory") @DefaultValue("true") final Boolean withHistory, @QueryParam("minDate") final String minDateOrNull, @QueryParam("maxDate") final String maxDateOrNull, @QueryParam("withInProcessing") @DefaultValue("true") final Boolean withInProcessing, @QueryParam("withBusEvents") @DefaultValue("true") final Boolean withBusEvents, @QueryParam("withNotifications") @DefaultValue("true") final Boolean withNotifications, @javax.ws.rs.core.Context final HttpServletRequest request) {
    final TenantContext tenantContext = context.createContext(request);
    final Long tenantRecordId = recordIdApi.getRecordId(tenantContext.getTenantId(), ObjectType.TENANT, tenantContext);
    final Long accountRecordId = Strings.isNullOrEmpty(accountIdStr) ? null : recordIdApi.getRecordId(UUID.fromString(accountIdStr), ObjectType.ACCOUNT, tenantContext);
    // Limit search results by default
    final DateTime minDate = Strings.isNullOrEmpty(minDateOrNull) ? clock.getUTCNow().minusDays(2) : DATE_TIME_FORMATTER.parseDateTime(minDateOrNull).toDateTime(DateTimeZone.UTC);
    final DateTime maxDate = Strings.isNullOrEmpty(maxDateOrNull) ? clock.getUTCNow().plusDays(2) : DATE_TIME_FORMATTER.parseDateTime(maxDateOrNull).toDateTime(DateTimeZone.UTC);
    final StreamingOutput json = new StreamingOutput() {

        @Override
        public void write(final OutputStream output) throws IOException, WebApplicationException {
            Iterator<BusEventWithMetadata<BusEvent>> busEventsIterator = null;
            Iterator<NotificationEventWithMetadata<NotificationEvent>> notificationsIterator = null;
            try {
                final JsonGenerator generator = mapper.getFactory().createGenerator(output);
                generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
                generator.writeStartObject();
                if (withBusEvents) {
                    generator.writeFieldName("busEvents");
                    generator.writeStartArray();
                    busEventsIterator = getBusEvents(withInProcessing, withHistory, minDate, maxDate, accountRecordId, tenantRecordId).iterator();
                    while (busEventsIterator.hasNext()) {
                        final BusEventWithMetadata<BusEvent> busEvent = busEventsIterator.next();
                        generator.writeObject(new BusEventWithRichMetadata(busEvent));
                    }
                    generator.writeEndArray();
                }
                if (withNotifications) {
                    generator.writeFieldName("notifications");
                    generator.writeStartArray();
                    notificationsIterator = getNotifications(queueName, serviceName, withInProcessing, withHistory, minDate, maxDate, accountRecordId, tenantRecordId).iterator();
                    while (notificationsIterator.hasNext()) {
                        final NotificationEventWithMetadata<NotificationEvent> notification = notificationsIterator.next();
                        generator.writeObject(notification);
                    }
                    generator.writeEndArray();
                }
                generator.writeEndObject();
                generator.close();
            } finally {
                // In case the client goes away (IOException), make sure to close the underlying DB connection
                if (busEventsIterator != null) {
                    while (busEventsIterator.hasNext()) {
                        busEventsIterator.next();
                    }
                }
                if (notificationsIterator != null) {
                    while (notificationsIterator.hasNext()) {
                        notificationsIterator.next();
                    }
                }
            }
        }
    };
    return Response.status(Status.OK).entity(json).build();
}
Also used : BusEventWithMetadata(org.killbill.bus.api.BusEventWithMetadata) OutputStream(java.io.OutputStream) TenantContext(org.killbill.billing.util.callcontext.TenantContext) StreamingOutput(javax.ws.rs.core.StreamingOutput) NotificationEvent(org.killbill.notificationq.api.NotificationEvent) DateTime(org.joda.time.DateTime) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) NotificationEventWithMetadata(org.killbill.notificationq.api.NotificationEventWithMetadata) BusEvent(org.killbill.bus.api.BusEvent) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with BusEvent

use of org.killbill.bus.api.BusEvent in project killbill by killbill.

the class TestBeatrixListener method testAccountCreate.

@Test(groups = "fast")
public void testAccountCreate() throws Exception {
    AccountCreationInternalEvent event = mock(AccountCreationInternalEvent.class);
    provideCommonBusEventInfo(event);
    when(event.getBusEventType()).thenReturn(BusInternalEventType.ACCOUNT_CREATE);
    when(event.getId()).thenReturn(ACCOUNT_ID);
    ArgumentCaptor<BusEvent> eventCaptor = ArgumentCaptor.forClass(BusEvent.class);
    beatrixListener.handleAllInternalKillbillEvents(event);
    verify(externalBus, times(1)).post(eventCaptor.capture());
    DefaultBusExternalEvent postedEvent = (DefaultBusExternalEvent) eventCaptor.getValue();
    assertEquals(postedEvent.getObjectId(), ACCOUNT_ID);
    assertEquals(postedEvent.getObjectType(), ObjectType.ACCOUNT);
    assertEquals(postedEvent.getEventType(), ExtBusEventType.ACCOUNT_CREATION);
    assertNull(postedEvent.getMetaData());
    assertCommonFieldsWithAccountId(postedEvent);
}
Also used : BusEvent(org.killbill.bus.api.BusEvent) AccountCreationInternalEvent(org.killbill.billing.events.AccountCreationInternalEvent) Test(org.testng.annotations.Test)

Example 3 with BusEvent

use of org.killbill.bus.api.BusEvent in project killbill by killbill.

the class TestBeatrixListener method testPaymentInfo.

@Test(groups = "fast")
public void testPaymentInfo() throws Exception {
    PaymentInfoInternalEvent event = mock(PaymentInfoInternalEvent.class);
    provideCommonBusEventInfo(event);
    when(event.getBusEventType()).thenReturn(BusInternalEventType.PAYMENT_INFO);
    when(event.getPaymentId()).thenReturn(OBJECT_ID);
    provideCommonPaymentInfo(event);
    ArgumentCaptor<PaymentMetadata> metadataCaptor = ArgumentCaptor.forClass(PaymentMetadata.class);
    when(objectMapper.writeValueAsString(metadataCaptor.capture())).thenReturn(METADATA);
    when(internalCallContextFactory.getAccountId(OBJECT_ID, ObjectType.PAYMENT, tenantContext)).thenReturn(ACCOUNT_ID);
    ArgumentCaptor<BusEvent> eventCaptor = ArgumentCaptor.forClass(BusEvent.class);
    beatrixListener.handleAllInternalKillbillEvents(event);
    verify(externalBus).post(eventCaptor.capture());
    DefaultBusExternalEvent postedEvent = (DefaultBusExternalEvent) eventCaptor.getValue();
    assertEquals(postedEvent.getObjectId(), OBJECT_ID);
    assertEquals(postedEvent.getObjectType(), ObjectType.PAYMENT);
    assertEquals(postedEvent.getEventType(), ExtBusEventType.PAYMENT_SUCCESS);
    assertEquals(postedEvent.getMetaData(), METADATA);
    assertCommonFieldsWithAccountId(postedEvent);
    PaymentMetadata paymentMetadata = metadataCaptor.getValue();
    assertPaymentMetadataFields(paymentMetadata);
}
Also used : InvoicePaymentInfoInternalEvent(org.killbill.billing.events.InvoicePaymentInfoInternalEvent) PaymentInfoInternalEvent(org.killbill.billing.events.PaymentInfoInternalEvent) PaymentMetadata(org.killbill.billing.notification.plugin.api.PaymentMetadata) InvoicePaymentMetadata(org.killbill.billing.notification.plugin.api.InvoicePaymentMetadata) BusEvent(org.killbill.bus.api.BusEvent) Test(org.testng.annotations.Test)

Example 4 with BusEvent

use of org.killbill.bus.api.BusEvent in project killbill by killbill.

the class TestBeatrixListener method testInvoiceCreation.

@Test(groups = "fast")
public void testInvoiceCreation() throws Exception {
    InvoiceCreationInternalEvent event = mock(InvoiceCreationInternalEvent.class);
    provideCommonBusEventInfo(event);
    when(event.getBusEventType()).thenReturn(BusInternalEventType.INVOICE_CREATION);
    when(event.getInvoiceId()).thenReturn(OBJECT_ID);
    when(internalCallContextFactory.getAccountId(OBJECT_ID, ObjectType.INVOICE, tenantContext)).thenReturn(ACCOUNT_ID);
    ArgumentCaptor<BusEvent> eventCaptor = ArgumentCaptor.forClass(BusEvent.class);
    beatrixListener.handleAllInternalKillbillEvents(event);
    verify(externalBus).post(eventCaptor.capture());
    DefaultBusExternalEvent postedEvent = (DefaultBusExternalEvent) eventCaptor.getValue();
    assertEquals(postedEvent.getObjectId(), OBJECT_ID);
    assertEquals(postedEvent.getObjectType(), ObjectType.INVOICE);
    assertEquals(postedEvent.getEventType(), ExtBusEventType.INVOICE_CREATION);
    assertNull(postedEvent.getMetaData());
    assertCommonFieldsWithAccountId(postedEvent);
}
Also used : InvoiceCreationInternalEvent(org.killbill.billing.events.InvoiceCreationInternalEvent) BusEvent(org.killbill.bus.api.BusEvent) Test(org.testng.annotations.Test)

Example 5 with BusEvent

use of org.killbill.bus.api.BusEvent in project killbill by killbill.

the class TestBeatrixListener method testBroadcastService.

@Test(groups = "fast")
public void testBroadcastService() throws Exception {
    BroadcastInternalEvent event = mock(BroadcastInternalEvent.class);
    provideCommonBusEventInfo(event);
    when(event.getBusEventType()).thenReturn(BusInternalEventType.BROADCAST_SERVICE);
    when(event.getServiceName()).thenReturn(SERVICE_NAME);
    when(event.getType()).thenReturn(BROADCAST_EVENT_TYPE);
    when(event.getJsonEvent()).thenReturn(BROADCAST_EVENT_JSON);
    ArgumentCaptor<BroadcastMetadata> metadataCaptor = ArgumentCaptor.forClass(BroadcastMetadata.class);
    when(objectMapper.writeValueAsString(metadataCaptor.capture())).thenReturn(METADATA);
    ArgumentCaptor<BusEvent> eventCaptor = ArgumentCaptor.forClass(BusEvent.class);
    beatrixListener.handleAllInternalKillbillEvents(event);
    verify(externalBus).post(eventCaptor.capture());
    DefaultBusExternalEvent postedEvent = (DefaultBusExternalEvent) eventCaptor.getValue();
    assertNull(postedEvent.getObjectId());
    assertEquals(postedEvent.getObjectType(), ObjectType.SERVICE_BROADCAST);
    assertEquals(postedEvent.getEventType(), ExtBusEventType.BROADCAST_SERVICE);
    assertCommonFieldsWithNoAccountId(postedEvent);
    BroadcastMetadata broadcastMetadata = metadataCaptor.getValue();
    assertEquals(broadcastMetadata.getService(), SERVICE_NAME);
    assertEquals(broadcastMetadata.getCommandType(), BROADCAST_EVENT_TYPE);
    assertEquals(broadcastMetadata.getEventJson(), BROADCAST_EVENT_JSON);
}
Also used : BroadcastInternalEvent(org.killbill.billing.events.BroadcastInternalEvent) BroadcastMetadata(org.killbill.billing.notification.plugin.api.BroadcastMetadata) BusEvent(org.killbill.bus.api.BusEvent) Test(org.testng.annotations.Test)

Aggregations

BusEvent (org.killbill.bus.api.BusEvent)27 Test (org.testng.annotations.Test)20 InvoicePaymentMetadata (org.killbill.billing.notification.plugin.api.InvoicePaymentMetadata)5 EventBusException (org.killbill.bus.api.PersistentBus.EventBusException)4 PaymentMetadata (org.killbill.billing.notification.plugin.api.PaymentMetadata)3 DefaultEffectiveSubscriptionEvent (org.killbill.billing.subscription.api.user.DefaultEffectiveSubscriptionEvent)3 SubscriptionBaseTransitionData (org.killbill.billing.subscription.api.user.SubscriptionBaseTransitionData)3 DefaultTagDefinition (org.killbill.billing.util.tag.DefaultTagDefinition)3 NotificationEvent (org.killbill.notificationq.api.NotificationEvent)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 OutputStream (java.io.OutputStream)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 StreamingOutput (javax.ws.rs.core.StreamingOutput)2 DateTime (org.joda.time.DateTime)2 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)2 InvoicePaymentErrorInternalEvent (org.killbill.billing.events.InvoicePaymentErrorInternalEvent)2