@Test
  public void testBuildCommandFail() {
    BuildMessage message = mock(BuildMessage.class);
    List<BuildMessage> messages = new ArrayList<BuildMessage>();
    messages.add(message);

    BuildResults results = mock(BuildResults.class);
    when(results.getErrorMessages()).thenReturn(messages);

    when(buildService.buildAndDeploy(any(Project.class))).thenReturn(results);

    presenter.triggerBuild();

    verify(notificationEvent)
        .fire(
            argThat(
                new ArgumentMatcher<NotificationEvent>() {
                  @Override
                  public boolean matches(final Object argument) {
                    final NotificationEvent event = (NotificationEvent) argument;
                    final String notification = event.getNotification();
                    final NotificationEvent.NotificationType type = event.getType();

                    return notification.equals(ProjectEditorResources.CONSTANTS.BuildFailed())
                        && type.equals(NotificationEvent.NotificationType.ERROR);
                  }
                }));

    verify(view, times(1)).showBusyIndicator(eq(ProjectEditorResources.CONSTANTS.Building()));
    // There are two calls to "hide" by this stage; one from the view initialisation one for the
    // build
    verify(view, times(2)).hideBusyIndicator();
  }
  @Test
  public void testNonPackageResourceUpdated() throws Exception {
    // This tests changes to a resource that is neither pom.xml nor kmodule.xml nor within a Package
    final Bean buildChangeListenerBean =
        (Bean)
            beanManager
                .getBeans(org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder.class)
                .iterator()
                .next();
    final CreationalContext cc = beanManager.createCreationalContext(buildChangeListenerBean);
    final org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder buildChangeListener =
        (org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder)
            beanManager.getReference(
                buildChangeListenerBean,
                org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder.class,
                cc);

    final URL resourceUrl = this.getClass().getResource("/BuildChangeListenerRepo/project.imports");
    final org.uberfire.java.nio.file.Path nioResourcePath = fs.getPath(resourceUrl.toURI());
    final Path resourcePath = paths.convert(nioResourcePath);

    // Force full build before attempting incremental changes
    final KieProject project = projectService.resolveProject(resourcePath);
    final BuildResults buildResults = buildService.build(project);
    assertNotNull(buildResults);
    assertEquals(0, buildResults.getErrorMessages().size());
    assertEquals(1, buildResults.getInformationMessages().size());

    // Perform incremental build (Without a full Build first)
    buildChangeListener.updateResource(resourcePath);

    final IncrementalBuildResults incrementalBuildResults =
        buildResultsObserver.getIncrementalBuildResults();
    assertNull(incrementalBuildResults);
  }
  @Before
  public void setup() {
    ApplicationPreferences.setUp(new HashMap<String, String>());

    // The BuildOptions widget is manipulated in the Presenter so we need some nasty mocking
    when(view.getBuildOptionsButton()).thenReturn(buildOptions);
    when(buildOptions.getWidget(eq(0))).thenReturn(buildOptionsButton1);
    when(buildOptions.getWidget(eq(1))).thenReturn(buildOptionsMenu);
    when(buildOptionsMenu.getWidget(eq(0))).thenReturn(buildOptionsMenuButton1);
    when(buildOptionsMenu.getWidget(eq(1))).thenReturn(buildOptionsMenuButton1);

    constructProjectScreenPresenter(
        new CallerMock<BuildService>(buildService),
        new CallerMock<AssetManagementService>(assetManagementServiceMock));

    // Mock ProjectScreenService
    final POM pom = new POM(new GAV("groupId", "artifactId", "version"));
    model = new ProjectScreenModel();
    model.setPOM(pom);
    when(projectScreenService.load(any(org.uberfire.backend.vfs.Path.class))).thenReturn(model);

    // Mock BuildService
    when(buildService.buildAndDeploy(any(Project.class))).thenReturn(new BuildResults());

    // Mock LockManager initialisation
    final Path path = mock(Path.class);
    final Metadata pomMetadata = mock(Metadata.class);
    model.setPOMMetaData(pomMetadata);
    when(pomMetadata.getPath()).thenReturn(path);
    final Metadata kmoduleMetadata = mock(Metadata.class);
    model.setKModuleMetaData(kmoduleMetadata);
    when(kmoduleMetadata.getPath()).thenReturn(path);
    final Metadata importsMetadata = mock(Metadata.class);
    model.setProjectImportsMetaData(importsMetadata);
    when(importsMetadata.getPath()).thenReturn(path);

    // Mock ProjectContext
    final Repository repository = mock(Repository.class);
    when(context.getActiveRepository()).thenReturn(repository);
    when(repository.getAlias()).thenReturn("repository");
    when(repository.getCurrentBranch()).thenReturn("master");

    final Project project = mock(Project.class);
    when(project.getProjectName()).thenReturn("project");

    when(context.getActiveProject()).thenReturn(project);

    // Trigger initialisation of view. Unfortunately this is the only way to initialise a Project in
    // the Presenter
    context.onProjectContextChanged(
        new ProjectContextChangeEvent(mock(OrganizationalUnit.class), repository, project));

    verify(view, times(1)).showBusyIndicator(eq(CommonConstants.INSTANCE.Loading()));
    verify(view, times(1)).hideBusyIndicator();
  }
  @Test
  public void testResourceDeleted() throws Exception {
    final Bean buildChangeListenerBean =
        (Bean)
            beanManager
                .getBeans(org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder.class)
                .iterator()
                .next();
    final CreationalContext cc = beanManager.createCreationalContext(buildChangeListenerBean);
    final org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder buildChangeListener =
        (org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder)
            beanManager.getReference(
                buildChangeListenerBean,
                org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder.class,
                cc);

    final URL resourceUrl =
        this.getClass().getResource("/BuildChangeListenerRepo/src/main/resources/delete.drl");
    final org.uberfire.java.nio.file.Path nioResourcePath = fs.getPath(resourceUrl.toURI());
    final Path resourcePath = paths.convert(nioResourcePath);

    // Force full build before attempting incremental changes
    final KieProject project = projectService.resolveProject(resourcePath);
    final BuildResults buildResults = buildService.build(project);
    assertNotNull(buildResults);
    assertEquals(0, buildResults.getErrorMessages().size());
    assertEquals(1, buildResults.getInformationMessages().size());

    // Perform incremental build
    buildChangeListener.deleteResource(resourcePath);

    final IncrementalBuildResults incrementalBuildResults =
        buildResultsObserver.getIncrementalBuildResults();
    assertNotNull(incrementalBuildResults);
    assertEquals(0, incrementalBuildResults.getAddedMessages().size());
    assertEquals(0, incrementalBuildResults.getRemovedMessages().size());
  }
  @Test
  public void testBatchResourceChanges() throws Exception {
    final Bean buildChangeListenerBean =
        (Bean)
            beanManager
                .getBeans(org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder.class)
                .iterator()
                .next();
    final CreationalContext cc = beanManager.createCreationalContext(buildChangeListenerBean);
    final org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder buildChangeListener =
        (org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder)
            beanManager.getReference(
                buildChangeListenerBean,
                org.guvnor.common.services.builder.ResourceChangeIncrementalBuilder.class,
                cc);

    final URL resourceUrl1 =
        this.getClass().getResource("/BuildChangeListenerRepo/src/main/resources/add.drl");
    final org.uberfire.java.nio.file.Path nioResourcePath1 = fs.getPath(resourceUrl1.toURI());
    final Path resourcePath1 = paths.convert(nioResourcePath1);

    final URL resourceUrl2 =
        this.getClass().getResource("/BuildChangeListenerRepo/src/main/resources/update.drl");
    final org.uberfire.java.nio.file.Path nioResourcePath2 = fs.getPath(resourceUrl2.toURI());
    final Path resourcePath2 = paths.convert(nioResourcePath2);

    final URL resourceUrl3 =
        this.getClass().getResource("/BuildChangeListenerRepo/src/main/resources/delete.drl");
    final org.uberfire.java.nio.file.Path nioResourcePath3 = fs.getPath(resourceUrl3.toURI());
    final Path resourcePath3 = paths.convert(nioResourcePath3);

    //        final Set<ResourceChange> batch = new HashSet<ResourceChange>();
    //        batch.add( new ResourceChange( ChangeType.ADD,
    //                                       resourcePath1,
    //                                       new SessionInfoImpl( "id",
    //                                                            new IdentityImpl( "user",
    //
    // Collections.<Role>emptyList() ) ) ) );
    //        batch.add( new ResourceChange( ChangeType.UPDATE,
    //                                       resourcePath2,
    //                                       new SessionInfoImpl( "id",
    //                                                            new IdentityImpl( "user",
    //
    // Collections.<Role>emptyList() ) ) ) );
    //        batch.add( new ResourceChange( ChangeType.DELETE,
    //                                       resourcePath3,
    //                                       new SessionInfoImpl( "id",
    //                                                            new IdentityImpl( "user",
    //
    // Collections.<Role>emptyList() ) ) ) );
    final Map<Path, Collection<ResourceChange>> batch =
        new HashMap<Path, Collection<ResourceChange>>();
    batch.put(
        resourcePath1,
        new ArrayList<ResourceChange>() {
          {
            add(new ResourceAdded());
          }
        });

    batch.put(
        resourcePath2,
        new ArrayList<ResourceChange>() {
          {
            add(new ResourceUpdated());
          }
        });

    batch.put(
        resourcePath3,
        new ArrayList<ResourceChange>() {
          {
            add(new ResourceUpdated());
          }
        });

    // Force full build before attempting incremental changes
    final KieProject project = projectService.resolveProject(resourcePath1);
    final BuildResults buildResults = buildService.build(project);
    assertNotNull(buildResults);
    assertEquals(0, buildResults.getErrorMessages().size());
    assertEquals(1, buildResults.getInformationMessages().size());

    // Perform incremental build
    buildChangeListener.batchResourceChanges(batch);

    final IncrementalBuildResults incrementalBuildResults =
        buildResultsObserver.getIncrementalBuildResults();
    assertNotNull(incrementalBuildResults);
    assertEquals(0, incrementalBuildResults.getAddedMessages().size());
    assertEquals(0, incrementalBuildResults.getRemovedMessages().size());
  }