BJ-314: fix after review

This commit is contained in:
a.karlov 2018-12-03 20:12:27 +03:00
parent f6e190c6eb
commit 35388610b0
7 changed files with 29 additions and 66 deletions

View File

@ -16,7 +16,7 @@ import java.net.URL;
import java.time.Instant;
import java.util.Map;
import static com.rbkmoney.file.storage.util.CheckerUtil.checkRegexString;
import static com.rbkmoney.file.storage.util.CheckerUtil.checkFileName;
import static com.rbkmoney.file.storage.util.CheckerUtil.checkString;
@RequiredArgsConstructor
@ -47,7 +47,7 @@ public class FileStorageHandler implements FileStorageSrv.Iface {
try {
log.info("Request createNewFile fileName: {}, metadata: {}, expiresAt: {}", fileName, metadata, expiresAt);
checkString(fileName, "Bad request parameter, fileName required and not empty arg");
checkRegexString(fileName, "Bad request parameter, enter the correct fileName");
checkFileName(fileName, "Bad request parameter, enter the correct fileName");
checkString(expiresAt, "Bad request parameter, expiresAt required and not empty arg");
Instant instant = TypeUtil.stringToInstant(expiresAt);
NewFileResult newFile = storageService.createNewFile(fileName, metadata, instant);

View File

@ -8,7 +8,7 @@ import java.util.regex.Pattern;
public class CheckerUtil {
private static final Pattern PATTERN = Pattern.compile("^[a-zA-Z0-9 ,-_.]*$");
private static final Pattern FILE_NAME_PATTERN = Pattern.compile("^[a-zA-Z0-9 ,-_.]*$");
public static void checkString(String string, String exMessage) throws TException {
if (Strings.isNullOrEmpty(string)) {
@ -16,15 +16,15 @@ public class CheckerUtil {
}
}
public static void checkRegexString(String string, String exMessage) throws TException {
if (!PATTERN.matcher(string).matches()) {
public static void checkFileName(String string, String exMessage) throws TException {
if (!FILE_NAME_PATTERN.matcher(string).matches()) {
throw new TException(exMessage);
}
}
public static void checkFile(MultipartFile file, String exMessage) throws TException {
if (file.isEmpty() || file.getSize() == 0) {
if (file.isEmpty()) {
throw new TException(exMessage);
}
}

View File

@ -1,5 +1,7 @@
package com.rbkmoney.file.storage;
import com.rbkmoney.woody.thrift.impl.http.THSpawnClientBuilder;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
@ -15,6 +17,8 @@ import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
@ -41,6 +45,8 @@ public abstract class AbstractIntegrationTest {
@LocalServerPort
protected int port;
protected FileStorageSrv.Iface client;
@ClassRule
public static GenericContainer cephContainer = new GenericContainer("dr.rbkmoney.com/ceph-demo:latest")
.withEnv("RGW_NAME", "localhost")
@ -76,6 +82,14 @@ public abstract class AbstractIntegrationTest {
}
}
@Before
public void before() throws URISyntaxException {
client = new THSpawnClientBuilder()
.withAddress(new URI("http://localhost:" + port + "/file_storage"))
.withNetworkTimeout(TIMEOUT)
.build(FileStorageSrv.Iface.class);
}
protected Instant getDayInstant() {
return LocalDateTime.now().plusDays(1).toInstant(getZoneOffset());
}

View File

@ -1,8 +1,6 @@
package com.rbkmoney.file.storage;
import com.rbkmoney.woody.thrift.impl.http.THSpawnClientBuilder;
import org.apache.thrift.TException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
@ -14,8 +12,6 @@ import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
@ -26,16 +22,6 @@ import static org.junit.Assert.assertEquals;
public class FileStorageTest extends AbstractIntegrationTest {
private FileStorageSrv.Iface client;
@Before
public void before() throws URISyntaxException {
client = new THSpawnClientBuilder()
.withAddress(new URI("http://localhost:" + port + "/file_storage"))
.withNetworkTimeout(TIMEOUT)
.build(FileStorageSrv.Iface.class);
}
@Test
public void uploadAndDownloadFileFromStorageTest() throws IOException, TException {
Path testFile = Files.createTempFile("", "test_file");
@ -47,9 +33,11 @@ public class FileStorageTest extends AbstractIntegrationTest {
NewFileResult fileResult = client.createNewFile("test_file", Collections.emptyMap(), getDayInstant().toString());
String uploadUrl = fileResult.getUploadUrl();
// запись данных в файл
Files.write(testFile, "Test".getBytes());
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
// запись файла в тело запроса
body.add("file", new FileSystemResource(testFile.toFile()));
HttpHeaders headers = new HttpHeaders();
@ -60,7 +48,7 @@ public class FileStorageTest extends AbstractIntegrationTest {
RestTemplate restTemplate = new RestTemplate();
restTemplate.postForEntity(uploadUrl, requestEntity, Void.class);
// генерация url с доступом только для загрузки
String urs = client.generateDownloadUrl(fileResult.getFileData().getFileId(), getDayInstant().toString());
URL url = new URL(urs);

View File

@ -1,20 +1,15 @@
package com.rbkmoney.file.storage.handler;
import com.rbkmoney.file.storage.AbstractIntegrationTest;
import com.rbkmoney.file.storage.FileStorageSrv;
import com.rbkmoney.file.storage.NewFileResult;
import com.rbkmoney.file.storage.service.StorageService;
import com.rbkmoney.woody.api.flow.error.WRuntimeException;
import com.rbkmoney.woody.thrift.impl.http.THSpawnClientBuilder;
import org.apache.thrift.TException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;
@ -25,16 +20,6 @@ public class ExpirationTimeStorageObjectTest extends AbstractIntegrationTest {
@Autowired
private StorageService storageService;
private FileStorageSrv.Iface client;
@Before
public void before() throws URISyntaxException {
client = new THSpawnClientBuilder()
.withAddress(new URI("http://localhost:" + port + "/file_storage"))
.withNetworkTimeout(TIMEOUT)
.build(FileStorageSrv.Iface.class);
}
@Test(expected = WRuntimeException.class)
public void expiredTimeForFileDataInMetadataTest() throws TException, InterruptedException {
NewFileResult testFile = client.createNewFile("test_file", Collections.emptyMap(), getSecondInstant().toString());

View File

@ -2,17 +2,12 @@ package com.rbkmoney.file.storage.service;
import com.rbkmoney.file.storage.AbstractIntegrationTest;
import com.rbkmoney.file.storage.FileData;
import com.rbkmoney.file.storage.FileStorageSrv;
import com.rbkmoney.file.storage.NewFileResult;
import com.rbkmoney.woody.thrift.impl.http.THSpawnClientBuilder;
import org.apache.thrift.TException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.FileNotFoundException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
@ -22,16 +17,6 @@ public class MetadataStorageObjectTest extends AbstractIntegrationTest {
@Autowired
private StorageService storageService;
private FileStorageSrv.Iface client;
@Before
public void before() throws URISyntaxException {
client = new THSpawnClientBuilder()
.withAddress(new URI("http://localhost:" + port + "/file_storage"))
.withNetworkTimeout(TIMEOUT)
.build(FileStorageSrv.Iface.class);
}
@Test
public void extractMetadataTest() throws FileNotFoundException, TException {
String fileName = "test_file";

View File

@ -1,11 +1,8 @@
package com.rbkmoney.file.storage.service;
import com.rbkmoney.file.storage.AbstractIntegrationTest;
import com.rbkmoney.file.storage.FileStorageSrv;
import com.rbkmoney.file.storage.NewFileResult;
import com.rbkmoney.woody.thrift.impl.http.THSpawnClientBuilder;
import org.apache.thrift.TException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@ -13,8 +10,6 @@ import org.springframework.http.HttpStatus;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
@ -28,16 +23,6 @@ public class PresignedUrlAccessRightsTest extends AbstractIntegrationTest {
@Autowired
private StorageService storageService;
private FileStorageSrv.Iface client;
@Before
public void before() throws URISyntaxException {
client = new THSpawnClientBuilder()
.withAddress(new URI("http://localhost:" + port + "/file_storage"))
.withNetworkTimeout(TIMEOUT)
.build(FileStorageSrv.Iface.class);
}
@Test
public void downloadUrlTest() throws TException, IOException {
Path testFile = Files.createTempFile("", "test_file");
@ -47,17 +32,23 @@ public class PresignedUrlAccessRightsTest extends AbstractIntegrationTest {
try {
Files.write(testFile, new byte[0]);
// создание нового файла
NewFileResult fileResult = client.createNewFile("test_file", Collections.emptyMap(), getDayInstant().toString());
// генерация url с доступом только для загрузки
URL url = storageService.generateDownloadUrl(fileResult.getFileData().getFileId(), getDayInstant());
// с данной ссылкой нельзя записывать
assertEquals(HttpStatus.FORBIDDEN.value(), getHttpURLConnection(url, true, "PUT").getResponseCode());
// можно читать
assertEquals(HttpStatus.OK.value(), getHttpURLConnection(url, false, "GET").getResponseCode());
// чтение данных
HttpURLConnection urlConnection = getHttpURLConnection(url, false, "GET");
InputStream inputStream = urlConnection.getInputStream();
// чтение записанного файла из хранилища
Files.copy(inputStream, testActualFile, StandardCopyOption.REPLACE_EXISTING);
assertEquals(Files.readAllLines(testFile), Files.readAllLines(testActualFile));