package au.gov.ga.geodesy.port.adapter.geodesyml;

import au.gov.ga.xmlschemer.Violation;
import org.springframework.util.ResourceUtils;
import org.testng.annotations.Test;

import javax.xml.transform.stream.StreamSource;
import java.util.List;

import static org.testng.Assert.assertEquals;

public class GeodesyMLValidatorTest {

    // TODO fix the constructor so that the unit tests will pass
    private GeodesyMLValidator validator = new GeodesyMLValidator(null, "");

    @Test
    public void testSchemaValidation() throws Exception {
        StreamSource xml = new StreamSource(ResourceUtils.getFile("classpath:sitelog/geodesyml/MOBS.xml"));
        List<Violation> violations = validator.validate(xml);
        assertNoViolations(violations);
    }

    private void assertNoViolations(List<Violation> violations) throws AssertionError {
        assertViolations(violations, 0);
    }

    private void assertViolations(List<Violation> violations, int n) throws AssertionError {
        if (n != violations.size()) {
            violations.forEach(System.out::println);
        }
        assertEquals(violations.size(), n);
    }
}

@@ -114,26 +114,188 @@
 ork.
-util.ResourceUtils
+context.support.ClassPathXmlApplicationContext;%0Aimport org.springframework.core.io.Resource;%0Aimport org.springframework.util.ResourceUtils;%0Aimport org.testng.annotations.BeforeTest
 ;%0Aim
@@ -325,24 +325,59 @@
 ions.Test;%0A%0A
+import javax.xml.transform.Source;%0A
 import javax
@@ -404,32 +404,60 @@
 m.StreamSource;%0A
+import java.io.IOException;%0A
 import java.util
@@ -558,145 +558,1075 @@
     
-// TODO fix the constructor so that the unit tests will pass%0A    private GeodesyMLValidator validator = new GeodesyMLValidator(null, %22%22);
+private GeodesyMLValidator geodesyMLValidator = null;%0A%0A    @BeforeTest%0A    public void setup() %7B%0A        ClassPathXmlApplicationContext context = null;%0A        try %7B%0A            context = new ClassPathXmlApplicationContext();%0A            Resource catalogResource = context.getResource(%22classpath:xsd/geodesyml-1.0.0-SNAPSHOT/catalog.xml%22);%0A            Resource xsdResource = context.getResource(%22classpath:xsd/geodesyml-1.0.0-SNAPSHOT/geodesyML.xsd%22);%0A            Source xsdSource = new StreamSource(xsdResource.getInputStream());%0A            String catalogPath = catalogResource.getFile().getAbsolutePath();%0A%0A            geodesyMLValidator = new GeodesyMLValidator(xsdSource, catalogPath);%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void testSchemaValidation() throws Exception %7B%0A        StreamSource xml = new StreamSource(ResourceUtils.getFile(%22classpath:sitelog/geodesyml/MOBS.xml%22));%0A        List%3CViolation%3E violations = geodesyMLValidator.validate(xml);%0A        assertNoViolations(violations);%0A    %7D
 %0A%0A  
@@ -1661,32 +1661,39 @@
 SchemaValidation
+Failure
 () throws Except
@@ -1690,32 +1690,86 @@
 ows Exception %7B%0A
+        // load a file with 2 known schema violations%0A
         StreamSo
@@ -1843,32 +1843,47 @@
 g/geodesyml/MOBS
+-invalid-schema
 .xml%22));%0A       
@@ -1912,17 +1912,26 @@
 tions = 
-v
+geodesyMLV
 alidator
@@ -1952,34 +1952,32 @@
 ;%0A        assert
-No
 Violations(viola
@@ -1973,32 +1973,35 @@
 tions(violations
+, 2
 );%0A    %7D%0A%0A    pr

fix unit test