[Demonstration 1]

[Buggy Method]
public synchronized void createDisconnectOrRemoveLocalSubscription(LocalSubscription subscription, SubscriptionChange type) throws AndesException {
    Boolean allowSharedSubscribers = AndesConfigurationManager.readValue(AndesConfiguration.ALLOW_SHARED_SHARED_SUBSCRIBERS);
    //We need to handle durable topic subscriptions
    boolean hasDurableSubscriptionAlreadyInPlace = false;
    if (subscription.isBoundToTopic() && subscription.isDurable()) {
        /**
         * Check if an active durable topic subscription already in place. If so we should not accept the subscription
         */
        //scan all the destinations as the subscription can come for different topic
        for (String destination : clusterTopicSubscriptionMap.keySet()) {
            List<AndesSubscription> existingSubscriptions = clusterTopicSubscriptionMap.get(destination);
            if (existingSubscriptions != null && !existingSubscriptions.isEmpty()) {
                for (AndesSubscription sub : existingSubscriptions) {
                    //queue is durable
                    if (sub.isDurable() && //target queues are matched
                    sub.getTargetQueue().equals(subscription.getTargetQueue()) && //target queue has a active subscriber
                    sub.hasExternalSubscriptions()) {
                        hasDurableSubscriptionAlreadyInPlace = true;
                        break;
                    }
                }
            }
            if (hasDurableSubscriptionAlreadyInPlace) {
                break;
            }
        }
        if (!hasDurableSubscriptionAlreadyInPlace && type == SubscriptionChange.DISCONNECTED) {
            //when there are multiple subscribers possible with same clientID we keep only one
            //topic subscription record for all of them. Thus when closing there can be no subscriber
            //to close in multiple durable topic subscription case
            if (!allowSharedSubscribers) {
                throw new AndesException("There is no active subscriber to close subscribed to " + subscription.getSubscribedDestination() + " with the queue " + subscription.getTargetQueue());
            }
        } else if (hasDurableSubscriptionAlreadyInPlace && type == SubscriptionChange.ADDED) {
            if (!allowSharedSubscribers) {
                //not permitted
                throw new AndesException("A subscription already exists for Durable subscriptions on " + subscription.getSubscribedDestination() + " with the queue " + subscription.getTargetQueue());
            }
        }
    }
    if (type == SubscriptionChange.ADDED || type == SubscriptionChange.DISCONNECTED) {
        String destinationQueue = subscription.getSubscribedDestination();
        //Store the subscription
        String destinationIdentifier = (subscription.isBoundToTopic() ? TOPIC_PREFIX : QUEUE_PREFIX) + destinationQueue;
        String subscriptionID = subscription.getSubscribedNode() + "_" + subscription.getSubscriptionID();
        andesContextStore.storeDurableSubscription(destinationIdentifier, subscriptionID, subscription.encodeAsStr());
        if (type == SubscriptionChange.ADDED) {
            log.info("New Local Subscription Added " + subscription.toString());
        } else {
            log.info("New Local Subscription Disconnected " + subscription.toString());
        }
        //add or update local subscription map
        if (subscription.getTargetQueueBoundExchangeName().equals(AMQPUtils.DIRECT_EXCHANGE_NAME)) {
            Map<String, LocalSubscription> localSubscriptions = localQueueSubscriptionMap.get(destinationQueue);
            if (localSubscriptions == null) {
                localSubscriptions = new ConcurrentHashMap<String, LocalSubscription>();
            }
            localSubscriptions.put(subscriptionID, subscription);
            localQueueSubscriptionMap.put(destinationQueue, localSubscriptions);
        } else if (subscription.getTargetQueueBoundExchangeName().equals(AMQPUtils.TOPIC_EXCHANGE_NAME)) {
            Map<String, LocalSubscription> localSubscriptions = localTopicSubscriptionMap.get(destinationQueue);
            if (localSubscriptions == null) {
                localSubscriptions = new ConcurrentHashMap<String, LocalSubscription>();
            }
            localSubscriptions.put(subscriptionID, subscription);
            localTopicSubscriptionMap.put(destinationQueue, localSubscriptions);
        }
    } else if (type == SubscriptionChange.DELETED) {
        removeLocalSubscription(subscription);
        log.info("Local Subscription Removed " + subscription.toString());
    }
    log.debug("===============Updated local subscription maps================");
    this.printLocalSubscriptionMap(localQueueSubscriptionMap);
    this.printLocalSubscriptionMap(localTopicSubscriptionMap);
    log.debug("========================================================");
}

[Buggy Hunk]
if (subscription.isBoundToTopic() && subscription.isDurable()) {

[Patch]
if (subscription.isDurable()) {

[Demonstration 2]

[Buggy Method]
public void deploy(DeploymentFileData deploymentFileData) throws DeploymentException {
    try {
        String path = deploymentFileData.getAbsolutePath();
        EventProcessorUtil.addTenantConfig(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(), configurationContext);
        if (!deployedExecutionPlanFilePaths.contains(path)) {
            try {
                processDeploy(deploymentFileData);
            } catch (ExecutionPlanConfigurationException e) {
                throw new DeploymentException("Execution plan not deployed properly.", e);
            }
        } else {
            log.debug("Execution plan file is already deployed :" + path);
            deployedExecutionPlanFilePaths.remove(path);
        }
    } catch (Throwable t) {
        log.error("Can't deploy the execution plan: " + deploymentFileData.getName(), t);
        throw new DeploymentException("Can't deploy the execution plan: " + deploymentFileData.getName(), t);
    }
}

[Buggy Hunk]
EventProcessorUtil.addTenantConfig(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(), configurationContext);

[Patch]
EventProcessorValueHolder.addTenantConfig(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(), configurationContext);

[Demonstration 3]

[Buggy Method]
@Override
public void undeploy(String fileName) throws DeploymentException {
    String platformId = getPlatformID(fileName);
    try {
        DataHolder.getInstance().getPlatformManager().unregister(CarbonContext.getThreadLocalCarbonContext().getTenantId(), platformId, true);
        log.info("Platform configuration : " + fileName + " un-deployed successfully");
    } catch (PlatformManagementException e) {
        log.error("Error occurred while un-deploying the platform - " + fileName);
    }
}

[Buggy Hunk]
log.error("Error occurred while un-deploying the platform - " + fileName);

[Patch]
log.error("Error occurred while un-deploying the platform - " + fileName, e);