package au.gov.ga.geodesy.domain.model;

import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.transaction.annotation.Transactional;
import org.testng.annotations.Test;

import au.gov.ga.geodesy.interfaces.xml.GeodesyMLMarshaller;
import au.gov.ga.geodesy.support.spring.GeodesySupportConfig;
import au.gov.ga.geodesy.support.spring.PersistenceJpaConfig;
import au.gov.xml.icsm.geodesyml.v_0_2_2.GeodesyMLType;

@ContextConfiguration(
        classes = {GeodesySupportConfig.class, PersistenceJpaConfig.class},
        loader = AnnotationConfigContextLoader.class)

@Transactional("geodesyTransactionManager")
public class GnssSiteLogRepositoryTest extends AbstractTransactionalTestNGSpringContextTests {

    private static final Logger log = LoggerFactory.getLogger(GnssSiteLogRepositoryTest.class);

    @Autowired
    private GnssSiteLogRepository siteLogs;

    @Autowired
    private GeodesyMLMarshaller marshaller;

    private static final String sampleSiteLogsDir = "src/test/resources/sitelog/geodesyml";

    @Test
    @Rollback(false)
    public void saveAllSiteLogs() throws Exception {
        for (File f : getSiteLogFiles()) {
            GeodesyMLType geodesyML = marshaller.unmarshal(new FileReader(f)).getValue();
            new GnssSiteLogFactory(geodesyML).create()
                .forEach(siteLog -> {
                    log.info("Saving " + siteLog.getFourCharacterId());
                    siteLogs.saveAndFlush(siteLog);
                });
        }
    }

    private File[] getSiteLogFiles() throws Exception {
        return new File(sampleSiteLogsDir).listFiles(new FileFilter() {
            public boolean accept(File f) {
                return f.getName().endsWith(".xml");
            }
        });
    }
}

@@ -583,16 +583,51 @@
 tional;%0A
+import static org.testng.Assert.*;%0A
 import o
@@ -1972,16 +1972,174 @@
 %0A    %7D%0A%0A
+    @Test(dependsOnMethods = %22saveAllSiteLogs%22)%0A    public void checkNumberOfSavedLogs() throws Exception %7B%0A        assertEquals(siteLogs.count(), 1);%0A    %7D%0A%0A
     priv

Check number of created gnss site logs