package com.adaptris.interlok.resolver;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.nio.file.attribute.PosixFilePermission;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class FileResolverTest
{
  @Test
  public void testResolveString()
  {
    FileResolver resolver = new FileResolver();

    String s = resolver.resolve("%file{./build.gradle:%size}");
    long size = Long.parseLong(s);
    assertTrue(size > 0);

    String d = resolver.resolve("%file{./build.gradle:%data}");
    assertEquals(d.length(), size);

    String type = resolver.resolve("%file{./build.gradle:%type}");
    assertEquals(FileResolver.Type.FILE.name(), type);

    String permissions = resolver.resolve("%file{./build.gradle:%permissions}");
    assertTrue(permissions.contains(PosixFilePermission.OWNER_READ.name()));

    String combined = resolver.resolve("%file{./build.gradle:%size%type%permissions}");
    String expected = s + "," + type + "," + permissions;
    assertEquals(expected, combined);

    d = resolver.resolve("%file{./build.gradle:%date_create}");
    assertTrue(d.length() > 0);
    d = resolver.resolve("%file{./build.gradle:%date_modify}");
    assertTrue(d.length() > 0);
    d = resolver.resolve("%file{./build.gradle:%date_access}");
    assertTrue(d.length() > 0);

    // not meaningful to get size or data from a directory (so just the type is resolved)
    type = resolver.resolve("%file{./src:%size%type%data}");
    assertEquals(FileResolver.Type.DIRECTORY.name(), type);

    // non-existent path
    assertTrue(StringUtils.isEmpty(resolver.resolve("%file{./unknown:%type%permissions}")));

    assertNull(resolver.resolve(null));
  }

  @Test(expected = UnresolvableException.class)
  public void testException() throws UnresolvableException
  {
    FileResolver resolver = new FileResolver();

    // unknown resolvable
    resolver.resolve("%file{wrong:%unknown}");
  }

  @Test
  public void testCanHandle()
  {
    FileResolver resolver = new FileResolver();

    assertFalse(resolver.canHandle("hello"));
    assertFalse(resolver.canHandle("%sysprop{java.version}"));

    assertTrue(resolver.canHandle("%file{/some/path:%size}"));
    assertTrue(resolver.canHandle("%file{/some/path:%data}"));
    assertTrue(resolver.canHandle("%file{/some/path:%size%data}"));
  }
}

@@ -330,16 +330,53 @@
 ertTrue;
+%0Aimport static org.junit.Assert.fail;
 %0A%0Apublic
@@ -1695,126 +1695,8 @@
 );%0A%0A
-    // non-existent path%0A    assertTrue(StringUtils.isEmpty(resolver.resolve(%22%25file%7B./unknown:%25type%25permissions%7D%22)));%0A
 %0A   
@@ -1748,48 +1748,8 @@
 Test
-(expected = UnresolvableException.class)
 %0A  p
@@ -1778,37 +1778,8 @@
 on()
- throws UnresolvableException
 %0A  %7B
@@ -1824,24 +1824,40 @@
 esolver();%0A%0A
+    try%0A    %7B%0A  
     // unkno
@@ -1870,16 +1870,18 @@
 olvable%0A
+  
     reso
@@ -1904,26 +1904,311 @@
 ile%7B
-wrong:%25unknown%7D%22);
+./build.gradle:%25unknown%7D%22);%0A      fail();%0A    %7D%0A    catch (UnresolvableException e)%0A    %7B%0A      // expected%0A    %7D%0A%0A    try%0A    %7B%0A      // non-existent path%0A      resolver.resolve(%22%25file%7B./unknown:%25type%25permissions%7D%22);%0A      fail();%0A    %7D%0A    catch (UnresolvableException e)%0A    %7B%0A      // expected%0A    %7D
 %0A  %7D

Update unit tests