Search in sources :

Example 1 with SubscriptionName

use of uk.gov.justice.services.core.cdi.SubscriptionName in project microservice_framework by CJSCommonPlatform.

the class SubscriptionManagerProducer method subscriptionManager.

@Produces
public SubscriptionManager subscriptionManager(final InjectionPoint injectionPoint) {
    final SubscriptionName subscriptionName = subscriptionNameAnnotationExtractor.getFrom(injectionPoint);
    final Instance<EventSource> eventSourceInstance = eventsourceInstance.select(subscriptionName);
    if (eventSourceInstance != null) {
        final EventSource eventSource = eventSourceInstance.get();
        final Subscription subscription = subscriptionDescriptorRegistry.getSubscription(subscriptionName.value());
        return new SubscriptionManager(subscription, eventSource);
    }
    throw new SubscriptionManagerProducerException(format("Failed to find instance of event souce with Qualifier '%s'", subscriptionName.value()));
}
Also used : EventSource(uk.gov.justice.services.eventsourcing.source.core.EventSource) SubscriptionName(uk.gov.justice.services.core.cdi.SubscriptionName) Subscription(uk.gov.justice.subscription.domain.Subscription) Produces(javax.enterprise.inject.Produces)

Example 2 with SubscriptionName

use of uk.gov.justice.services.core.cdi.SubscriptionName in project microservice_framework by CJSCommonPlatform.

the class SubscriptionNameAnnotationExtractor method getFrom.

public SubscriptionName getFrom(final InjectionPoint injectionPoint) {
    final Set<Annotation> qualifiers = injectionPoint.getQualifiers();
    final Annotation subscriptionNameAnnotation = qualifiers.stream().filter(annotation -> annotation.annotationType() == SubscriptionName.class).findFirst().orElseThrow(() -> new SubscriptionManagerProducerException(format("Failed to find SubscriptionName annotation on EventListener '%s'", injectionPoint.getBean().getName())));
    return (SubscriptionName) subscriptionNameAnnotation;
}
Also used : SubscriptionName(uk.gov.justice.services.core.cdi.SubscriptionName) Annotation(java.lang.annotation.Annotation)

Example 3 with SubscriptionName

use of uk.gov.justice.services.core.cdi.SubscriptionName in project microservice_framework by CJSCommonPlatform.

the class SubscriptionManagerProducerTest method shouldThrowASubscriptioManagerProducerExceptionIfTheEventSourceInstanceReturnsANull.

@Test
public void shouldThrowASubscriptioManagerProducerExceptionIfTheEventSourceInstanceReturnsANull() throws Exception {
    final InjectionPoint injectionPoint = mock(InjectionPoint.class);
    final SubscriptionName subscriptionName = mock(SubscriptionName.class);
    when(subscriptionNameAnnotationExtractor.getFrom(injectionPoint)).thenReturn(subscriptionName);
    when(eventsourceInstance.select(subscriptionName)).thenReturn(null);
    when(subscriptionName.value()).thenReturn("my-subscription");
    try {
        subscriptionManagerProducer.subscriptionManager(injectionPoint);
        fail();
    } catch (final SubscriptionManagerProducerException expected) {
        assertThat(expected.getMessage(), is("Failed to find instance of event souce with Qualifier 'my-subscription'"));
    }
}
Also used : InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) SubscriptionName(uk.gov.justice.services.core.cdi.SubscriptionName) Test(org.junit.Test)

Example 4 with SubscriptionName

use of uk.gov.justice.services.core.cdi.SubscriptionName in project microservice_framework by CJSCommonPlatform.

the class MyOtherEventListener method shouldGetTheSubscriptionNameQualifierFromAnEventListenerClass.

@Test
public void shouldGetTheSubscriptionNameQualifierFromAnEventListenerClass() throws Exception {
    final InjectionPoint injectionPoint = mock(InjectionPoint.class);
    final Annotation annotation = MyEventListener.class.getDeclaredAnnotation(SubscriptionName.class);
    when(injectionPoint.getQualifiers()).thenReturn(ImmutableSet.of(annotation));
    final SubscriptionName subscriptionNameAnnotation = subscriptionNameAnnotationExtractor.getFrom(injectionPoint);
    assertThat(subscriptionNameAnnotation.value(), is("my-subscription"));
}
Also used : InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) SubscriptionName(uk.gov.justice.services.core.cdi.SubscriptionName) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 5 with SubscriptionName

use of uk.gov.justice.services.core.cdi.SubscriptionName in project microservice_framework by CJSCommonPlatform.

the class SubscriptionManagerProducerTest method shouldCreateSubscriptionManagersOnStartUp.

@Test
public void shouldCreateSubscriptionManagersOnStartUp() throws Exception {
    final InjectionPoint injectionPoint = mock(InjectionPoint.class);
    final SubscriptionName subscriptionName = mock(SubscriptionName.class);
    final EventSource eventSource = mock(EventSource.class);
    final Subscription subscription = mock(Subscription.class);
    when(subscriptionNameAnnotationExtractor.getFrom(injectionPoint)).thenReturn(subscriptionName);
    when(eventsourceInstance.select(subscriptionName).get()).thenReturn(eventSource);
    when(subscriptionDescriptorRegistry.getSubscription(subscriptionName.value())).thenReturn(subscription);
    final SubscriptionManager subscriptionManager = subscriptionManagerProducer.subscriptionManager(injectionPoint);
    assertThat(subscriptionManager.getEventSource(), is(eventSource));
    assertThat(subscriptionManager.getSubscription(), is(subscription));
}
Also used : EventSource(uk.gov.justice.services.eventsourcing.source.core.EventSource) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) SubscriptionName(uk.gov.justice.services.core.cdi.SubscriptionName) Subscription(uk.gov.justice.subscription.domain.Subscription) Test(org.junit.Test)

Aggregations

SubscriptionName (uk.gov.justice.services.core.cdi.SubscriptionName)5 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)3 Test (org.junit.Test)3 Annotation (java.lang.annotation.Annotation)2 EventSource (uk.gov.justice.services.eventsourcing.source.core.EventSource)2 Subscription (uk.gov.justice.subscription.domain.Subscription)2 Produces (javax.enterprise.inject.Produces)1