Skip to content

Commit

Permalink
Fix ConfigDispatcherFileWatcher (openhab#3384)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <github@klug.nrw>
  • Loading branch information
J-N-K authored Feb 19, 2023
1 parent 0da4a22 commit 1b18831
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
package org.openhab.core.config.dispatch.internal;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -65,22 +64,22 @@ public void deactivate() {

@Override
public void processWatchEvent(WatchService.Kind kind, Path path) {
Path fullPath = watchService.getWatchPath().resolve(path);
try {
if (kind == WatchService.Kind.CREATE || kind == WatchService.Kind.MODIFY) {
if (!Files.isHidden(path) && path.toString().endsWith(".cfg")) {
configDispatcher.processConfigFile(path.toFile());
if (!Files.isHidden(fullPath) && fullPath.toString().endsWith(".cfg")) {
configDispatcher.processConfigFile(fullPath.toFile());
}
} else if (kind == WatchService.Kind.DELETE) {
// Detect if a service specific configuration file was removed. We want to
// notify the service in this case with an updated empty configuration.
File configFile = path.toFile();
if (Files.isHidden(path) || Files.isDirectory(path) || !path.toString().endsWith(".cfg")) {
if (Files.isHidden(fullPath) || Files.isDirectory(fullPath) || !fullPath.toString().endsWith(".cfg")) {
return;
}
configDispatcher.fileRemoved(configFile.getAbsolutePath());
configDispatcher.fileRemoved(fullPath.toString());
}
} catch (IOException e) {
logger.error("Failed to process watch event {} for {}", kind, path);
logger.error("Failed to process watch event {} for {}", kind, path, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@

import static org.mockito.Mockito.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openhab.core.service.WatchService;
Expand All @@ -36,57 +39,58 @@ public class ConfigDispatcherFileWatcherTest {
private @Mock @NonNullByDefault({}) ConfigDispatcher configDispatcherMock;
private @Mock @NonNullByDefault({}) WatchService watchService;

private @TempDir @NonNullByDefault({}) Path tempDir;

private @NonNullByDefault({}) Path cfgPath;
private @NonNullByDefault({}) Path nonCfgPath;

@BeforeEach
public void setUp() {
public void setUp() throws IOException {
configDispatcherFileWatcher = new ConfigDispatcherFileWatcher(configDispatcherMock, watchService);
verify(configDispatcherMock).processConfigFile(any());
}

@Test
public void configurationFileCreated() {
Path path = Path.of("myPath.cfg");
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.CREATE, path);
when(watchService.getWatchPath()).thenReturn(tempDir.toAbsolutePath());

cfgPath = tempDir.resolve("myPath.cfg");
nonCfgPath = tempDir.resolve("myPath");

verify(configDispatcherMock).processConfigFile(path.toFile());
Files.createFile(cfgPath);
Files.createFile(nonCfgPath);
}

@Test
public void configurationFileModified() {
Path path = Path.of("myPath.cfg");
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.MODIFY, path);
public void configurationFileCreated() throws IOException {
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.CREATE, cfgPath);
verify(configDispatcherMock).processConfigFile(cfgPath.toAbsolutePath().toFile());
}

verify(configDispatcherMock).processConfigFile(path.toFile());
@Test
public void configurationFileModified() throws IOException {
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.MODIFY, cfgPath);
verify(configDispatcherMock).processConfigFile(cfgPath.toAbsolutePath().toFile());
}

@Test
public void nonConfigurationFileCreated() {
Path path = Path.of("myPath");
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.CREATE, path);

configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.CREATE, nonCfgPath);
verifyNoMoreInteractions(configDispatcherMock);
}

@Test
public void nonConfigurationFileModified() {
Path path = Path.of("myPath");
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.MODIFY, path);

configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.MODIFY, nonCfgPath);
verifyNoMoreInteractions(configDispatcherMock);
}

@Test
public void configurationFileRemoved() {
Path path = Path.of("myPath.cfg");
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.DELETE, path);

verify(configDispatcherMock).fileRemoved(path.toAbsolutePath().toString());
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.DELETE, cfgPath);
verify(configDispatcherMock).fileRemoved(cfgPath.toAbsolutePath().toString());
}

@Test
public void nonConfigurationFileRemoved() {
Path path = Path.of("myPath");
configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.DELETE, path);

configDispatcherFileWatcher.processWatchEvent(WatchService.Kind.DELETE, nonCfgPath);
verifyNoMoreInteractions(configDispatcherMock);
}
}

0 comments on commit 1b18831

Please sign in to comment.