class SubscriptionDelegate {

    synchronized Publisher getPublisher(NotificationSubscriptionContext subscriptionContext) {
        Publisher publisher = publisherDao.findPublisher(subscriptionContext.getContextId(), subscriptionContext.getContextType(), subscriptionContext.getSourceId(),
                subscriptionContext.getSourceType());
        if (publisher == null) {
            publisher = publisherDao.createAndSavePublisher(subscriptionContext.getContextId(), subscriptionContext.getContextType(), subscriptionContext.getSourceId(),
                    subscriptionContext.getSourceType(), subscriptionContext.getSubcontextId());
        }
        return publisher;
    }

}

class SubscriptionDelegateTest {

     SubscriptionDelegate subscriptionDelegateTestObject;
    private Subscriber subscriber;
    private Publisher publisher;
    private Identity subscriberIdentity;
    private Long contextId;
    private ContextType contextType;
    private Long sourceId;
    private String sourceType;
    private Long subcontextId;
    private NotificationSubscriptionContext subscriptionContext;
    private Subscription subscription;

    @Test
    public void getPublisher_PublisherExist() {

        when(subscriptionDelegateTestObject.publisherDao.findPublisher(contextId, contextType, sourceId, sourceType)).thenReturn(publisher);

        Publisher resultPublisher = subscriptionDelegateTestObject.getPublisher(subscriptionContext);

        assertEquals("getPublisher returns wrong publisher", publisher, resultPublisher);

    }

}