@Override
    public void apply(IntentOperationContext<DomainIntent> context) {
        Optional<IntentData> toUninstall = context.toUninstall();
        Optional<IntentData> toInstall = context.toInstall();

        List<DomainIntent> uninstallIntents = context.intentsToUninstall();
        List<DomainIntent> installIntents = context.intentsToInstall();

        if (!toInstall.isPresent() && !toUninstall.isPresent()) {
            intentInstallCoordinator.intentInstallSuccess(context);
            return;
        }

        if (toUninstall.isPresent()) {
            IntentData intentData = toUninstall.get();
            trackerService.removeTrackedResources(intentData.key(), intentData.intent().resources());
            uninstallIntents.forEach(installable ->
                                             trackerService.removeTrackedResources(intentData.intent().key(),
                                                                                   installable.resources()));
        }

        if (toInstall.isPresent()) {
            IntentData intentData = toInstall.get();
            trackerService.addTrackedResources(intentData.key(), intentData.intent().resources());
            installIntents.forEach(installable ->
                                           trackerService.addTrackedResources(intentData.key(),
                                                                              installable.resources()));
        }

        // Generate domain Intent operations
        DomainIntentOperations.Builder builder = DomainIntentOperations.builder();
        DomainIntentOperationsContext domainOperationsContext;

        uninstallIntents.forEach(builder::remove);
        installIntents.forEach(builder::add);

        domainOperationsContext = new DomainIntentOperationsContext() {
            @Override
            public void onSuccess(DomainIntentOperations idops) {
                intentInstallCoordinator.intentInstallSuccess(context);
            }

            @Override
            public void onError(DomainIntentOperations idos) {
                intentInstallCoordinator.intentInstallFailed(context);
            }
        };
        log.debug("submitting domain intent {} -> {}",
                  toUninstall.map(x -> x.key().toString()).orElse("<empty>"),
                  toInstall.map(x -> x.key().toString()).orElse("<empty>"));

        // Submit domain Inten operations with domain context
        domainIntentService.sumbit(builder.build(domainOperationsContext));
    }
@Test
    public void testInstallFailed() {
        domainIntentService = new TestFailedDomainIntentService();
        installer.domainIntentService = domainIntentService;
        List<Intent> intentsToUninstall = Lists.newArrayList();
        List<Intent> intentsToInstall = createDomainIntents();
        IntentData toUninstall = null;
        IntentData toInstall = new IntentData(createP2PIntent(),
                                              IntentState.INSTALLING,
                                              new WallClockTimestamp());
        toInstall = IntentData.compiled(toInstall, intentsToInstall);
        IntentOperationContext<DomainIntent> operationContext;
        IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall);
        operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context);
        installer.apply(operationContext);
        assertEquals(intentInstallCoordinator.failedContext, operationContext);
    }