BAEL-4856 Remove BinaryFileDownloadApplication.java and use correct indentation
This commit is contained in:
@@ -27,54 +27,48 @@ import static org.mockito.Mockito.when;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class BinaryFileDownloaderUnitTest {
|
||||
|
||||
@Mock
|
||||
private OkHttpClient client;
|
||||
@Mock
|
||||
private BinaryFileWriter writer;
|
||||
@InjectMocks
|
||||
private BinaryFileDownloader tested;
|
||||
@Mock
|
||||
private OkHttpClient client;
|
||||
@Mock
|
||||
private BinaryFileWriter writer;
|
||||
@InjectMocks
|
||||
private BinaryFileDownloader tested;
|
||||
|
||||
@Test
|
||||
public void givenUrlAndResponse_whenDownload_thenExpectFileWritten() throws Exception {
|
||||
String url = "http://example.com/file";
|
||||
Call call = mock(Call.class);
|
||||
when(client.newCall(any(Request.class))).thenReturn(call);
|
||||
ResponseBody body = ResponseBody.create("BODY", MediaType.get("application/text"));
|
||||
Response response = createResponse(url, body);
|
||||
when(call.execute()).thenReturn(response);
|
||||
when(writer.write(any())).thenReturn(1L);
|
||||
@Test
|
||||
public void givenUrlAndResponse_whenDownload_thenExpectFileWritten() throws Exception {
|
||||
String url = "http://example.com/file";
|
||||
Call call = mock(Call.class);
|
||||
when(client.newCall(any(Request.class))).thenReturn(call);
|
||||
ResponseBody body = ResponseBody.create("BODY", MediaType.get("application/text"));
|
||||
Response response = createResponse(url, body);
|
||||
when(call.execute()).thenReturn(response);
|
||||
when(writer.write(any())).thenReturn(1L);
|
||||
|
||||
try (BinaryFileDownloader tested = new BinaryFileDownloader(client, writer)) {
|
||||
long size = tested.download(url);
|
||||
assertEquals(1L, size);
|
||||
verify(writer).write(any(InputStream.class));
|
||||
try (BinaryFileDownloader tested = new BinaryFileDownloader(client, writer)) {
|
||||
long size = tested.download(url);
|
||||
assertEquals(1L, size);
|
||||
verify(writer).write(any(InputStream.class));
|
||||
}
|
||||
verify(writer).close();
|
||||
}
|
||||
verify(writer).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrlAndResponseWithNullBody_whenDownload_thenExpectIllegalStateException() throws Exception {
|
||||
String url = "http://example.com/file";
|
||||
Call call = mock(Call.class);
|
||||
when(client.newCall(any(Request.class))).thenReturn(call);
|
||||
Response response = createResponse(url, null);
|
||||
when(call.execute()).thenReturn(response);
|
||||
@Test
|
||||
public void givenUrlAndResponseWithNullBody_whenDownload_thenExpectIllegalStateException() throws Exception {
|
||||
String url = "http://example.com/file";
|
||||
Call call = mock(Call.class);
|
||||
when(client.newCall(any(Request.class))).thenReturn(call);
|
||||
Response response = createResponse(url, null);
|
||||
when(call.execute()).thenReturn(response);
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> tested.download(url));
|
||||
assertThrows(IllegalStateException.class, () -> tested.download(url));
|
||||
|
||||
verify(writer, times(0)).write(any(InputStream.class));
|
||||
}
|
||||
verify(writer, times(0)).write(any(InputStream.class));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Response createResponse(String url, ResponseBody body) {
|
||||
Request request = new Request.Builder().url(url).build();
|
||||
return new Response.Builder()
|
||||
.code(200)
|
||||
.request(request)
|
||||
.protocol(Protocol.HTTP_2)
|
||||
.message("Message")
|
||||
.body(body)
|
||||
.build();
|
||||
}
|
||||
@NotNull
|
||||
private Response createResponse(String url, ResponseBody body) {
|
||||
Request request = new Request.Builder().url(url).build();
|
||||
return new Response.Builder().code(200).request(request).protocol(Protocol.HTTP_2).message("Message").body(body).build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,36 +20,36 @@ import static org.mockito.Mockito.when;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class BinaryFileWriterUnitTest {
|
||||
|
||||
@Mock
|
||||
private OutputStream outputStream;
|
||||
@Mock
|
||||
private OutputStream outputStream;
|
||||
|
||||
@Test
|
||||
public void givenInputStream_whenWrite_thenExpectWritten() throws Exception {
|
||||
InputStream inputStream = mock(InputStream.class);
|
||||
when(inputStream.read(any(), anyInt(), anyInt())).thenReturn(10, -1);
|
||||
@Test
|
||||
public void givenInputStream_whenWrite_thenExpectWritten() throws Exception {
|
||||
InputStream inputStream = mock(InputStream.class);
|
||||
when(inputStream.read(any(), anyInt(), anyInt())).thenReturn(10, -1);
|
||||
|
||||
try (BinaryFileWriter tested = new BinaryFileWriter(outputStream)) {
|
||||
long result = tested.write(inputStream);
|
||||
try (BinaryFileWriter tested = new BinaryFileWriter(outputStream)) {
|
||||
long result = tested.write(inputStream);
|
||||
|
||||
assertEquals(10, result);
|
||||
verify(outputStream).write(any(), eq(0), eq(10));
|
||||
verify(inputStream).close();
|
||||
assertEquals(10, result);
|
||||
verify(outputStream).write(any(), eq(0), eq(10));
|
||||
verify(inputStream).close();
|
||||
}
|
||||
verify(outputStream).close();
|
||||
}
|
||||
verify(outputStream).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInputStreamEmpty_whenWrite_thenExpectNotWritten() throws Exception {
|
||||
InputStream inputStream = mock(InputStream.class);
|
||||
@Test
|
||||
public void givenInputStreamEmpty_whenWrite_thenExpectNotWritten() throws Exception {
|
||||
InputStream inputStream = mock(InputStream.class);
|
||||
|
||||
try (BinaryFileWriter tested = new BinaryFileWriter(outputStream)) {
|
||||
long result = tested.write(inputStream);
|
||||
try (BinaryFileWriter tested = new BinaryFileWriter(outputStream)) {
|
||||
long result = tested.write(inputStream);
|
||||
|
||||
assertEquals(0, result);
|
||||
verify(outputStream, times(0)).write(any(), anyInt(), anyInt());
|
||||
verify(inputStream).close();
|
||||
assertEquals(0, result);
|
||||
verify(outputStream, times(0)).write(any(), anyInt(), anyInt());
|
||||
verify(inputStream).close();
|
||||
}
|
||||
verify(outputStream).close();
|
||||
}
|
||||
verify(outputStream).close();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user