diff --git a/pom.xml b/pom.xml index d3d81da..fb83473 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,16 @@ + + org.springframework.boot + spring-boot-test-autoconfigure + ${spring-boot.version} + + + junit + junit + test + diff --git a/src/main/java/me/bearns/fias/config/ApplicationConfig.java b/src/main/java/me/bearns/fias/config/ApplicationConfig.java index 5a07903..93bc9ad 100644 --- a/src/main/java/me/bearns/fias/config/ApplicationConfig.java +++ b/src/main/java/me/bearns/fias/config/ApplicationConfig.java @@ -20,7 +20,7 @@ import javax.sql.DataSource; @Configuration @EnableJpaRepositories("me.bearns.fias.repository") @EnableTransactionManagement -@PropertySource("application.properties") +@PropertySource(value = "application.properties", ignoreResourceNotFound = true) class ApplicationConfig { @Autowired diff --git a/src/test/java/me/bearns/fias/DownloaderTest.java b/src/test/java/me/bearns/fias/DownloaderTest.java new file mode 100644 index 0000000..0499db2 --- /dev/null +++ b/src/test/java/me/bearns/fias/DownloaderTest.java @@ -0,0 +1,57 @@ +package me.bearns.fias; + + +import me.bearns.fias.service.Downloader; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import org.springframework.test.context.junit4.SpringRunner; + +import java.io.File; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Stream; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class DownloaderTest { + + @Autowired + private Downloader service; + + @Test + public void testDownload() { + List list = Arrays.asList( + "http://fias-file.nalog.ru/DownloadUpdates?file=fias_delta_xml.zip&version=20200811", + "http://fias-file.nalog.ru/DownloadUpdates?file=fias_delta_xml.zip&version=20200807", + "http://fias-file.nalog.ru/DownloadUpdates?file=fias_delta_xml.zip&version=20200804", + "http://fias-file.nalog.ru/DownloadUpdates?file=fias_delta_xml.zip&version=20200807" //repeat + ); + + assert service != null; + + //start download + final Stream> futureStream = list.parallelStream().map(service::download); + + Stream fileStream = futureStream.map(fileCompletableFuture -> { + try { + return fileCompletableFuture.get(); + } catch (Exception e) { + return null; //on error + } + }); + + assert fileStream.allMatch(Objects::nonNull); + + assert fileStream.count() == 3; + + assert fileStream.allMatch(File::exists); + + assert fileStream.allMatch(file -> file.length() > 0); + + } +}