package cc.blynk.server.workers;

import cc.blynk.server.handlers.workflow.BaseSimpleChannelInboundHandler;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

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

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doThrow;

/**
 * The Blynk Project.
 * Created by Dmitriy Dumanskiy.
 * Created on 2/26/2015.
 */
@RunWith(MockitoJUnitRunner.class)
//todo this is ugly test but I don't have time to make it better/simpler/easier
public class PropertiesChangeWatcherWorkerTest {

    @Mock
    private BaseSimpleChannelInboundHandler fakeHandler;

    private PropertiesChangeWatcherWorker propertiesChangeWatcherWorker;

    @Test(expected = RuntimeException.class)
    public void testPropertiesChanged() throws IOException {
        Path tmpFile = Files.createTempFile("", "");

        doThrow(new RuntimeException()).when(fakeHandler).updateProperties(any());

        System.setProperty("user.dir", System.getProperty("java.io.tmpdir"));
        propertiesChangeWatcherWorker = new PropertiesChangeWatcherWorker(tmpFile.getFileName().toString(), fakeHandler);
        new Thread(new TmpFileChanger(tmpFile)).start();
        propertiesChangeWatcherWorker.run();

    }

    private class TmpFileChanger implements Runnable {
        private Path tmpFilePath;
        private TmpFileChanger(Path tmpFilePath) {
            this.tmpFilePath = tmpFilePath;
        }

        @Override
        public void run() {
            try (OutputStream outputStream = Files.newOutputStream(tmpFilePath)) {
                Thread.sleep(100);
                outputStream.write(new byte[1]);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

@@ -346,47 +346,36 @@
 th;%0A
-%0A
 import 
-static org.mockito.Mockito.any;
+java.nio.file.Paths;%0A
 %0Aimp
@@ -405,23 +405,17 @@
 Mockito.
-doThrow
+*
 ;%0A%0A/**%0A 
@@ -536,88 +536,8 @@
 ss)%0A
-//todo this is ugly test but I don't have time to make it better/simpler/easier%0A
 publ
@@ -658,122 +658,13 @@
     
-private PropertiesChangeWatcherWorker propertiesChangeWatcherWorker;%0A%0A    @Test(expected = RuntimeException.class)
+@Test
 %0A   
@@ -735,22 +735,22 @@
   Path t
+e
 mp
-File
+Dir
  = Files
@@ -764,31 +764,34 @@
 Temp
-File(%22%22, %22
+Directory(%22tmp
 %22);%0A
-%0A
         
 doTh
@@ -790,84 +790,86 @@
     
-doThrow(new RuntimeException()).when(fakeHandler).updateProperties(any(
+Path tmpFile = Files.createFile(Paths.get(tempDir.toString(), %22temp.txt%22
 ));%0A%0A
+%0A
     
@@ -907,86 +907,48 @@
 r%22, 
-System.getProperty(%22java.io.tmpdir%22));%0A        propertiesChangeWatcherWorker =
+tempDir.toString());%0A        new Thread(
  new
@@ -1028,56 +1028,8 @@
 ler)
-;%0A        new Thread(new TmpFileChanger(tmpFile)
 ).st
@@ -1035,24 +1035,26 @@
 tart();%0A
+%0A%0A
         
 properti
@@ -1049,298 +1049,100 @@
     
-propertiesChangeWatcherWorker.run();%0A
+try %7B
 %0A    
-%7D%0A%0A
     
-private class TmpFileChanger implements Runnable %7B%0A        private P
+    Thread.sleep(1000);%0A        %7D c
 at
+c
 h 
-tmpFilePath;%0A        private TmpFileChanger(Path tmpFilePath) %7B%0A            this.tmpFilePath = tmpFilePath;%0A        %7D%0A%0A        @Override%0A        public void run() %7B%0A    
+(InterruptedException e) %7B%0A        %7D%0A%0A
     
@@ -1211,56 +1211,13 @@
 File
-Path
 )) %7B%0A
-                Thread.sleep(100);%0A    
     
@@ -1261,28 +1261,24 @@
 %5D);%0A        
-    
 %7D catch (Exc
@@ -1301,20 +1301,16 @@
         
-   
- 
 e.printS
@@ -1334,23 +1334,139 @@
     
+%7D%0A%0A
     
-%7D%0A        %7D
+    //todo find out why it called 2 times%0A        verify(fakeHandler, timeout(5000).atLeastOnce()).updateProperties(any());%0A
 %0A   

Test fix