BJ-314: refactor tests

This commit is contained in:
a.karlov 2018-12-03 15:51:04 +03:00
parent 376a703605
commit 3dcc177cf9
8 changed files with 235 additions and 265 deletions

View File

@ -10,6 +10,9 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
@ -22,6 +25,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class AbstractIntegrationTest {
protected static final int TIMEOUT = 555000;
private static final String SIGNING_REGION = "RU";
private static final String AWS_ACCESS_KEY = "test";
private static final String AWS_SECRET_KEY = "test";
@ -78,4 +83,11 @@ public class AbstractIntegrationTest {
return ZoneOffset.systemDefault().getRules().getOffset(LocalDateTime.now());
}
protected HttpURLConnection getHttpURLConnection(URL url, boolean doOutput, String method) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(doOutput);
connection.setRequestMethod(method);
return connection;
}
}

View File

@ -1,13 +1,8 @@
package com.rbkmoney.file.storage;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class FileStorageApplicationTests {
public class FileStorageApplicationTests extends AbstractIntegrationTest {
@Test
public void contextLoads() {

View File

@ -0,0 +1,80 @@
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;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
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;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
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");
Path testActualFile = Files.createTempFile("", "test_actual_file");
try {
// создание нового файла
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();
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
// запись файла в хранилище через ссылку доступа
RestTemplate restTemplate = new RestTemplate();
restTemplate.postForEntity(uploadUrl, requestEntity, Void.class);
String urs = client.generateDownloadUrl(fileResult.getFileData().getFileId(), getDayInstant().toString());
URL url = new URL(urs);
HttpURLConnection urlConnection = getHttpURLConnection(url, false, "GET");
InputStream inputStream = urlConnection.getInputStream();
// чтение записанного файла из хранилища
Files.copy(inputStream, testActualFile, StandardCopyOption.REPLACE_EXISTING);
assertEquals(Files.readAllLines(testFile), Files.readAllLines(testActualFile));
} finally {
Files.deleteIfExists(testFile);
Files.deleteIfExists(testActualFile);
}
}
}

View File

@ -0,0 +1,68 @@
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;
import static org.junit.Assert.assertEquals;
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());
Thread.sleep(1000);
client.getFileData(testFile.getFileData().getFileId());
}
@Test(expected = WRuntimeException.class)
public void expiredTimeForGenerateUrlInMetadataTest() throws TException, InterruptedException {
NewFileResult testFile = client.createNewFile("test_file", Collections.emptyMap(), getSecondInstant().toString());
Thread.sleep(1000);
client.generateDownloadUrl(testFile.getFileData().getFileId(), getSecondInstant().toString());
}
@Test
public void expiredTimeForGenerateUrlConnectionInCephTest() throws TException, IOException, InterruptedException {
NewFileResult fileResult = client.createNewFile("test_file", Collections.emptyMap(), getDayInstant().toString());
URL url = storageService.generateDownloadUrl(fileResult.getFileData().getFileId(), getSecondInstant());
assertEquals(HttpStatus.OK.value(), getHttpURLConnection(url, false, "GET").getResponseCode());
Thread.sleep(2000);
assertEquals(HttpStatus.FORBIDDEN.value(), getHttpURLConnection(url, false, "GET").getResponseCode());
}
}

View File

@ -1,167 +0,0 @@
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.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.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
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;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
public class Aasd extends AbstractIntegrationTest {
private static final int TIMEOUT = 555000;
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 TException {
NewFileResult testFile = client.createNewFile("test_file", Collections.emptyMap(), getDayInstant().toString());
FileData fileData = client.getFileData(testFile.getFileData().getFileId());
assertEquals(fileData, testFile.getFileData());
}
@Test(expected = WRuntimeException.class)
public void expiredTimeForFileDataInMetadataTest() throws TException, InterruptedException {
NewFileResult testFile = client.createNewFile("test_file", Collections.emptyMap(), getSecondInstant().toString());
Thread.sleep(1000);
client.getFileData(testFile.getFileData().getFileId());
}
@Test(expected = WRuntimeException.class)
public void expiredTimeForGenerateUrlInMetadataTest() throws TException, InterruptedException {
NewFileResult testFile = client.createNewFile("test_file", Collections.emptyMap(), getSecondInstant().toString());
Thread.sleep(1000);
client.generateDownloadUrl(testFile.getFileData().getFileId(), getSecondInstant().toString());
}
@Test
public void downloadUrlTest() throws TException, IOException {
Path testFile = Files.createTempFile("", "test_file");
Files.write(testFile, new byte[0]);
Path testActualFile = Files.createTempFile("", "test_actual_file");
NewFileResult fileResult = client.createNewFile("test_file", Collections.emptyMap(), getDayInstant().toString());
String s = client.generateDownloadUrl(fileResult.getFileData().getFileId(), getDayInstant().toString());
URL url = new URL(s);
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));
}
@Test
public void expiredTimeForGenerateUrlConnectionInCephTest() throws TException, IOException, InterruptedException {
NewFileResult fileResult = client.createNewFile("test_file", Collections.emptyMap(), getDayInstant().toString());
String s = client.generateDownloadUrl(fileResult.getFileData().getFileId(), getSecondInstant().toString());
URL url = new URL(s);
assertEquals(HttpStatus.OK.value(), getHttpURLConnection(url, false, "GET").getResponseCode());
Thread.sleep(2000);
assertEquals(HttpStatus.FORBIDDEN.value(), getHttpURLConnection(url, false, "GET").getResponseCode());
}
@Test
public void name() throws IOException, TException {
NewFileResult fileResult = client.createNewFile("test_file", Collections.emptyMap(), getDayInstant().toString());
String s = fileResult.getUploadUrl();
Path testFile = Files.createTempFile("", "test_file");
Files.write(testFile, "Test".getBytes());
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", new FileSystemResource(testFile.toFile()));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
RestTemplate restTemplate = new RestTemplate();
restTemplate.postForEntity(s, requestEntity, Void.class);
Path testActualFile = Files.createTempFile("", "test_actual_file");
String urs = client.generateDownloadUrl(fileResult.getFileData().getFileId(), getDayInstant().toString());
URL url = new URL(urs);
HttpURLConnection urlConnection = getHttpURLConnection(url, false, "GET");
InputStream inputStream = urlConnection.getInputStream();
Files.copy(inputStream, testActualFile, StandardCopyOption.REPLACE_EXISTING);
assertEquals(Files.readAllLines(testFile), Files.readAllLines(testActualFile));
}
private void asd() {
/*
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);
InputStream fis = new FileInputStream(testFile.toFile());
RequestCallback requestCallback = request -> {
request.getHeaders().add("Content-type", "application/octet-stream");
IOUtils.copy(fis, request.getBody());
};
HttpMessageConverterExtractor<String> responseExtractor =
new HttpMessageConverterExtractor<>(String.class, restTemplate.getMessageConverters());
restTemplate.setRequestFactory(requestFactory);
*/
}
private HttpURLConnection getHttpURLConnection(URL url, boolean doOutput, String method) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(doOutput);
connection.setRequestMethod(method);
return connection;
}
}

View File

@ -1,20 +0,0 @@
package com.rbkmoney.file.storage.service;
import com.rbkmoney.file.storage.AbstractIntegrationTest;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.FileNotFoundException;
public class MetadataS3ObjectsTest extends AbstractIntegrationTest {
@Autowired
private StorageService storageService;
@Test
public void name() throws FileNotFoundException {
// FileData fileData = storageService.createNewFile("test_file", Collections.emptyMap(), getInstant());
// URL url = storageService.generateDownloadUrl(fileData.getFileId(), getInstant());
}
}

View File

@ -0,0 +1,46 @@
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;
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";
NewFileResult fileResult = client.createNewFile(fileName, Collections.emptyMap(), getDayInstant().toString());
assertEquals(fileResult.getFileData().getFileName(), fileName);
FileData fileData = storageService.getFileData(fileResult.getFileData().getFileId());
assertEquals(fileData, fileResult.getFileData());
}
}

View File

@ -1,90 +1,59 @@
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 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;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
public class PresignedUrlAccessRightsTest extends AbstractIntegrationTest {
@Autowired
private StorageService storageService;
@Test
public void urlForUploadTest() throws IOException {
/* String stringForWrite = "This text uploaded as an object via presigned URL.";
private FileStorageSrv.Iface client;
Path testFile = Files.createTempFile("", "test_file");
Path testActualFile = Files.createTempFile("", "test_actual_file");
try {
FileData fileData = storageService.createNewFile("test_file", Collections.emptyMap(), getInstant());
// генерация url с доступом только для выгрузки
URL url = storageService.generateUploadUrl(fileData.getFileId(), getInstant());
// ошибка при запросе по url методом get
assertEquals(HttpStatus.FORBIDDEN.value(), getHttpURLConnection(url, false, "GET").getResponseCode());
// Length Required при запросе по url методом put
assertEquals(HttpStatus.LENGTH_REQUIRED.value(), getHttpURLConnection(url, true, "PUT").getResponseCode());
// запись данных методом put
HttpURLConnection urlConnection = getHttpURLConnection(url, true, "PUT");
OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
out.write(stringForWrite);
out.close();
// чтобы завершить загрузку вызываем getResponseCode
assertEquals(HttpStatus.OK.value(), urlConnection.getResponseCode());
// файл перезаписывается и затирает метаданные.
// запись метаданных
// storageService.rewriteFileData(fileData, getInstant());
copyFromStorageToFile(fileData, testActualFile);
// testFile пустой
assertNotEquals(Files.readAllLines(testFile), Files.readAllLines(testActualFile));
Files.write(testFile, stringForWrite.getBytes());
assertEquals(Files.readAllLines(testFile), Files.readAllLines(testActualFile));
} finally {
Files.deleteIfExists(testFile);
Files.deleteIfExists(testActualFile);
}*/
@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 urlForDownloadTest() throws IOException {
/*Path testFile = Files.createTempFile("", "test_file");
Files.write(testFile, "4815162342".getBytes());
Path testActualFile = Files.createTempFile("", "test_actual_file");
public void downloadUrlTest() throws TException, IOException {
Path testFile = Files.createTempFile("", "test_file");
Files.write(testFile, new byte[0]);
try {
FileData fileData = storageService.createNewFile("test_file", Collections.emptyMap(), getInstant());
Path testActualFile = Files.createTempFile("", "test_actual_file");
// запись тестового файла в цеф
storageService.uploadFile(fileData.getFileId(), testFile);
NewFileResult fileResult = client.createNewFile("test_file", Collections.emptyMap(), getDayInstant().toString());
// генерация url с доступом только для загрузки
URL url = storageService.generateDownloadUrl(fileData.getFileId(), getInstant());
URL url = storageService.generateDownloadUrl(fileResult.getFileData().getFileId(), getDayInstant());
// ошибка при запросе по url методом put
assertEquals(HttpStatus.FORBIDDEN.value(), getHttpURLConnection(url, true, "PUT").getResponseCode());
// ок при запросе по url методом get
assertEquals(HttpStatus.OK.value(), getHttpURLConnection(url, false, "GET").getResponseCode());
// получение содержимого методом get
HttpURLConnection urlConnection = getHttpURLConnection(url, false, "GET");
InputStream inputStream = urlConnection.getInputStream();
@ -93,19 +62,6 @@ public class PresignedUrlAccessRightsTest extends AbstractIntegrationTest {
assertEquals(Files.readAllLines(testFile), Files.readAllLines(testActualFile));
} finally {
Files.deleteIfExists(testFile);
Files.deleteIfExists(testActualFile);
}*/
}
private void copyFromStorageToFile(FileData fileData, Path file) throws IOException {
/*InputStream inputStream = storageService.getFile(fileData.getFileId());
Files.copy(inputStream, file, StandardCopyOption.REPLACE_EXISTING);*/
}
private HttpURLConnection getHttpURLConnection(URL url, boolean doOutput, String method) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(doOutput);
connection.setRequestMethod(method);
return connection;
}
}
}