package org.zaproxy.zap.model;

import org.junit.Test;
import org.parosproxy.paros.Constant;

import java.nio.file.Path;
import java.nio.file.Paths;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class SessionUtilsUnitTest {

    private static final String ZAP_HOME = "/zap/";
    private static final String ZAP_SESSION_DIR = ZAP_HOME + Constant.FOLDER_SESSION_DEFAULT;

    @Test
    public void shouldRetrieveExistingSessionFileFromAbsolutePath() throws Exception {
        // Given
        String session = "/test.session";
        // When
        Path sessionPath = SessionUtils.getSessionPath(session);
        // Then
        assertThat(sessionPath, is(equalTo(Paths.get("/test.session"))));
    }

    @Test
    public void shouldAppendSessionFiletypeAndRetrieveSessionFileFromAbsolutePath() throws Exception {
        // Given
        String session = "/test";
        // When
        Path sessionPath = SessionUtils.getSessionPath(session);
        // Then
        assertThat(sessionPath, is(equalTo(Paths.get("/test.session"))));
    }

    @Test
    public void shouldRetrieveExistingSessionFileFromRelativePath() throws Exception {
        // Given
        Constant.setZapHome(ZAP_HOME);
        String session = "test.session";
        // When
        Path sessionPath = SessionUtils.getSessionPath(session);
        // Then
        assertThat(sessionPath, is(equalTo(Paths.get(ZAP_SESSION_DIR, "test.session"))));
    }

    @Test
    public void shouldAppendSessionFiletypeAndRetrieveSessionFileFromRelativePath() throws Exception {
        // Given
        Constant.setZapHome(ZAP_HOME);
        String session = "test";
        // When
        Path sessionPath = SessionUtils.getSessionPath(session);
        // Then
        assertThat(sessionPath, is(equalTo(Paths.get(ZAP_SESSION_DIR, "test.session"))));
    }

    @Test(expected = NullPointerException.class)
    public void shouldFailOnNullForSessionInput() throws Exception {
        SessionUtils.getSessionPath(null);
    }

}
@@ -44,20 +44,111 @@
 g.junit.
-Test
+Before;%0D%0Aimport org.junit.Rule;%0D%0Aimport org.junit.Test;%0D%0Aimport org.junit.rules.TemporaryFolder
 ;%0D%0Aimpor
@@ -179,24 +179,53 @@
 onstant;%0D%0A%0D%0A
+import java.io.IOException;%0D%0A
 import java.
@@ -459,150 +459,173 @@
     
-private static final String ZAP_HOME = %22/zap/%22;%0D%0A    private static final String ZAP_SESSION_DIR = ZAP_HOME + Constant.FOLDER_SESSION_DEFAULT;
+@Rule%0D%0A    public TemporaryFolder tempFolder = new TemporaryFolder();%0D%0A%0D%0A    @Before%0D%0A    public void setUp() throws Exception %7B%0D%0A        tempFolder.create();%0D%0A    %7D
 %0D%0A%0D%0A
@@ -733,32 +733,78 @@
       // Given%0D%0A
+        Path path = newFile(%22test.session%22);%0D%0A
         String s
@@ -812,31 +812,31 @@
 ssion = 
-%22/test.session%22
+path.toString()
 ;%0D%0A     
@@ -977,34 +977,12 @@
 lTo(
-P
+p
 ath
-s.get(%22/test.session%22)
 )));
@@ -1137,32 +1137,111 @@
     
-String session = %22/test%22
+Path path = newFile(%22test.session%22);%0D%0A        String session = path.toString().replace(%22.session$%22, %22%22)
 ;%0D%0A 
@@ -1386,34 +1386,12 @@
 lTo(
-P
+p
 ath
-s.get(%22/test.session%22)
 )));
@@ -1530,36 +1530,39 @@
     
-Constant.setZapHome(ZAP_HOME
+String zapHome = createZapHome(
 );%0D%0A
@@ -1742,37 +1742,57 @@
  is(equalTo(
-Paths.get(ZAP
+pathWith(zapHome, Constant.FOLDER
 _SESSION_DIR
@@ -1781,34 +1781,38 @@
 FOLDER_SESSION_D
-IR
+EFAULT
 , %22test.session%22
@@ -1972,36 +1972,39 @@
     
-Constant.setZapHome(ZAP_HOME
+String zapHome = createZapHome(
 );%0D%0A
@@ -2184,21 +2184,41 @@
 lTo(
-Paths.get(ZAP
+pathWith(zapHome, Constant.FOLDER
 _SES
@@ -2227,10 +2227,14 @@
 ON_D
-IR
+EFAULT
 , %22t
@@ -2373,32 +2373,32 @@
 ws Exception %7B%0D%0A
-
         SessionU
@@ -2434,9 +2434,448 @@
    %7D%0D%0A%0D%0A
+    private Path newFile(String name) throws IOException %7B%0D%0A        return tempFolder.newFile(name).toPath();%0D%0A    %7D%0D%0A%0D%0A    private Path pathWith(String baseDir, String... paths) %7B%0D%0A        return Paths.get(baseDir, paths);%0D%0A    %7D%0D%0A%0D%0A    private String createZapHome() throws IOException %7B%0D%0A        String zapHome = tempFolder.newFolder(%22zap%22).toPath().toString();%0D%0A        Constant.setZapHome(zapHome);%0D%0A        return zapHome;%0D%0A    %7D%0D%0A%0D%0A
 %7D

Fix tests failing in Windows