package au.gov.ga.geodesy.support;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

/**
 * A collection of static methods to access regression test data.
 */
public class TestResources {

    private static final PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();

    /**
     * Don't instantiate, use the static methods.
     */
    private TestResources() {
    }

    /**
     * Given a site id, return a SOPAC site log test file.
     */
    public static File sopacSiteLog(String siteId) throws IOException {
        return resourceAsFile(sopacSiteLogResourceName(siteId));
    }

    /**
     * Given a site id, return a SOPAC site log test file reader.
     */
    public static Reader sopacSiteLogReader(String siteId) throws IOException {
        return new FileReader(resourceAsFile(sopacSiteLogResourceName(siteId)));
    }

    /**
     * Return all SOPAC site log test files.
     */
    public static List<File> sopacSiteLogs() throws IOException {
        return sopacSiteLogs("*");
    }

    /**
     * Given a site id pattern return all matching SOPAC site log test files.
     */
    public static List<File> sopacSiteLogs(String siteIdPattern) throws IOException {
        Resource[] resources = resourceResolver.getResources("classpath:" + sopacSiteLogsDirectory() + siteIdPattern + ".xml");
        List<File> files = new ArrayList<>(resources.length);
        for (Resource r : resources) {
            files.add(r.getFile());
        }
        return files;
    }

    /**
     * SOPAC site logs directory relative to classpath root.
     */
    private static String sopacSiteLogsDirectory() {
        return "/sitelog/sopac/";
    }

    /**
     * SOPAC site log resource name relative to classpath root.
     */
    private static String sopacSiteLogResourceName(String id) {
        return sopacSiteLogsDirectory() + id + ".xml";
    }

    /**
     * Find a resource relative to classpath root.
     */
    private static File resourceAsFile(String resourceName) throws IOException {
        Resource resource = new PathMatchingResourcePatternResolver().getResource("classpath:" + resourceName);
        if (resource == null) {
            throw new IllegalArgumentException("Resource " + resourceName + " must exist.");
        }
        return resource.getFile();
    }
}

@@ -629,32 +629,386 @@
 rces() %7B%0A    %7D%0A%0A
+    /**%0A     * SOPAC site logs directory relative to classpath root.%0A     */%0A    private static String sopacSiteLogsDirectory() %7B%0A        return %22/sitelog/sopac/%22;%0A    %7D%0A%0A    /**%0A     * GeodesyML site logs directory relative to classpath root.%0A     */%0A    private static String geodesyMLSiteLogsDirectory() %7B%0A        return %22/sitelog/geodesyml/%22;%0A    %7D%0A%0A
     /**%0A     * G
@@ -1452,32 +1452,294 @@
 teId)));%0A    %7D%0A%0A
+    /**%0A     * Given a site id, return a GeodesyML site log test file reader.%0A     */%0A    public static Reader geodesyMLSiteLogReader(String siteId) throws IOException %7B%0A        return new FileReader(resourceAsFile(geodesyMLSiteLogResourceName(siteId)));%0A    %7D%0A%0A
     /**%0A     * R
@@ -2400,35 +2400,38 @@
 PAC site log
-s directory
+ resource name
  relative to
@@ -2489,35 +2489,46 @@
 sopacSiteLog
-sDirectory(
+ResourceName(String id
 ) %7B%0A        
@@ -2534,32 +2534,53 @@
  return 
-%22/sitelog/sopac/
+sopacSiteLogsDirectory() + id + %22.xml
 %22;%0A    %7D
@@ -2588,37 +2588,41 @@
 %0A    /**%0A     * 
-SOPAC
+GeodesyML
  site log resour
@@ -2683,37 +2683,41 @@
 e static String 
-sopac
+geodesyML
 SiteLogResourceN
@@ -2740,37 +2740,41 @@
 %0A        return 
-sopac
+geodesyML
 SiteLogsDirector

Add loading of GeodesyML test resources