Compare commits

...

2 Commits

Author SHA1 Message Date
Terekhin Alexander 2df96026bb Test for DownloaderImpl 5 years ago
Terekhin Alexander f2937772b4 Spring version upgrade, DownloaderImpl complete (need some tests!) 5 years ago
  1. 12
      pom.xml
  2. 2
      src/main/java/me/bearns/fias/config/ApplicationConfig.java
  3. 6
      src/main/java/me/bearns/fias/service/Downloader.java
  4. 59
      src/main/java/me/bearns/fias/service/DownloaderImpl.java
  5. 4
      src/main/java/me/bearns/fias/service/FiasUpdater.java
  6. 57
      src/test/java/me/bearns/fias/DownloaderTest.java

@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>me.bearns</groupId>
@ -52,6 +52,16 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

@ -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

@ -1,9 +1,9 @@
package me.bearns.fias.service;
import java.io.InputStream;
import java.util.concurrent.Future;
import java.io.File;
import java.util.concurrent.CompletableFuture;
public interface Downloader {
public Future<InputStream> download(String url);
public CompletableFuture<File> download(String url);
}

@ -1,28 +1,65 @@
package me.bearns.fias.service;
import java.io.InputStream;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.springframework.stereotype.Component;
import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.concurrent.*;
@Component
public class DownloaderImpl implements Downloader {
private static final ExecutorService pool = Executors.newCachedThreadPool();
private static final String BASE_DIRECTORY = "/var/opt/fias";
private static final ConcurrentMap<String,CompletableFuture<File>> locks = new ConcurrentHashMap();
@Override
public Future<InputStream> download(String url) {
public CompletableFuture<File> download(String url) {
CompletableFuture<File> start = new CompletableFuture<>();
//syncronized
final CompletableFuture<File> feature = locks.putIfAbsent(url, start);
CompletableFuture<InputStream> completableFuture
= new CompletableFuture<>();
//return earlier runned feature
if(feature != start) return feature;
pool.submit(() -> {
InputStream is = null;
//todo
completableFuture.complete(is);
final URL conn;
final Path path;
try{
conn = new URL(url);
path = Paths.get(BASE_DIRECTORY, conn.getFile());
} catch (IOException e) {
feature.obtrudeException(e);
return;
}
final File file = path.toFile();
if(!file.exists()) {
try (BufferedInputStream in = new BufferedInputStream(conn.openStream())) {
Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// handle exception
feature.obtrudeException(e);
}
}
feature.complete(file);
});
return completableFuture;
return feature;
}
}

@ -5,7 +5,7 @@ import me.bearns.fias.repository.FiasVersionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.io.File;
import java.util.List;
import java.util.concurrent.Future;
import java.util.stream.Stream;
@ -63,7 +63,7 @@ public class FiasUpdater implements Updater {
updates.sort((o1, o2) -> Math.toIntExact(o1.getVersionId() - o2.getVersionId()));
//just for test
final Stream<Future<InputStream>> stream = updates.stream().map(u -> downloader.download(reloadFlag ? u.getFiasCompleteXmlUrl() : u.getFiasDeltaXmlUrl()));
final Stream<Future<File>> stream = updates.stream().map(u -> downloader.download(reloadFlag ? u.getFiasCompleteXmlUrl() : u.getFiasDeltaXmlUrl()));
}

@ -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…
Cancel
Save