/*
 * Copyright (c) 2012-2018 Red Hat, Inc.
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.che.api.languageserver;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableSet;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.lsp4j.services.LanguageServer;
import org.eclipse.lsp4j.services.WorkspaceService;
import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

/**
 * Test for {@link LanguageServerUpdateFileWatcher}, {@link LanguageServerCreateFileWatcher}, {@link
 * LanguageServerDeleteFileWatcher}
 */
@Listeners(MockitoTestNGListener.class)
public class LanguageServerAbstractFileWatcherTest {

  private static final String ID = "id";

  private EventService eventService;
  private RegistryContainer registryContainer;
  private LanguageServerAbstractFileWatcher fileWatcher;

  @Mock private LanguageServer languageServer;
  @Mock private WorkspaceService workspaceService;
  @Mock private LanguageServerInitializedEvent event;
  @Mock private PathMatcher pathMatcher;

  private Path tmpFilePath;

  @BeforeMethod
  public void setUp() throws IOException {
    tmpFilePath = Files.createTempFile(null, null);

    registryContainer = new RegistryContainer();
    eventService = new EventService();

    fileWatcher = new LanguageServerCreateFileWatcher(eventService, registryContainer);
    fileWatcher.subscribe();

    when(event.getId()).thenReturn(ID);
    when(event.getLanguageServer()).thenReturn(languageServer);
    when(languageServer.getWorkspaceService()).thenReturn(workspaceService);
    when(pathMatcher.matches(tmpFilePath)).thenReturn(true);
  }

  @Test
  public void shouldSubscribeForEvent() throws Exception {
    eventService.publish(event);

    verify(event).getId();
    verify(event).getLanguageServer();
  }

  @Test
  public void shouldSendChangesWhenCalledAcceptMethodIfPathMatches() {
    registryContainer.pathMatcherRegistry.add(ID, ImmutableSet.of(pathMatcher));
    eventService.publish(event);

    fileWatcher.accept(tmpFilePath);

    verify(languageServer).getWorkspaceService();
    verify(workspaceService).didChangeWatchedFiles(any());
  }

  @Test
  public void shouldNotSendChangesWhenCalledAcceptMethodIfPathDoesNotMatches() {
    registryContainer.pathMatcherRegistry.add(ID, ImmutableSet.of(pathMatcher));
    eventService.publish(event);

    fileWatcher.accept(tmpFilePath.resolve("other"));

    verify(languageServer, never()).getWorkspaceService();
    verify(workspaceService, never()).didChangeWatchedFiles(any());
  }
}

@@ -2155,134 +2155,318 @@
 hen(
-languageServer.getWorkspaceService()).thenReturn(workspaceService);%0A    when(pathMatcher.matches(tmpFilePath)).thenR
+pathMatcher.matches(any(Path.class)))%0A        .thenAnswer(%0A            invocation -%3E %7B%0A              Object argument = invocation.getArguments()%5B0%5D;%0A              if (argument.equals(tmpFilePath)) %7B%0A                r
 eturn
-(
+ 
 true
+;%0A              %7D else %7B%0A                return false;%0A              %7D%0A            %7D
 );%0A 
@@ -2826,32 +2826,108 @@
 publish(event);%0A
+    when(languageServer.getWorkspaceService()).thenReturn(workspaceService);
 %0A    fileWatcher

Fix build (invalid stubbing)