parent
f2937772b4
commit
2df96026bb
@ -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<String> 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<CompletableFuture<File>> futureStream = list.parallelStream().map(service::download); |
||||
|
||||
Stream<File> 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); |
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue