ServerWrapper { public boolean downloadVideo(String downloadId) { boolean successful = true; HttpUrl url = new HttpUrl.Builder() .scheme(this.Scheme) .host(this.Host) .addPathSegment("download") .addQueryParameter("download_id", downloadId) .build(); Request request = new Request.Builder() .url(url) .get() .build(); try { Response response = this.okHttpClient.newCall(request).execute(); byte[] video = response.body().bytes(); this.filesystemWrapper.saveDownloadedVideo(video); } catch (IOException e) { e.printStackTrace(); successful = false; } catch (IllegalStateException e) { e.printStackTrace(); successful = false; } return successful; } @Inject  ServerWrapper(OkHttpClient okHttpClient, FileSystemWrapper filesystemWrapper); Boolean ping(); String uploadVideo(File video); String downloadYouTubeVideo(String youtubeUrl); String processVideo(String videoId, SettingsProvider settingsProvider); boolean downloadVideo(String downloadId); public boolean HasError; }
@Test public void downloadVideo_DownloadSuccessful_ReturnsTrue() { final String DownloadId = "download_id"; MockInterceptor okHttpMockInterceptor = new MockInterceptor(); okHttpMockInterceptor.addRule() .get() .urlStarts("https: .respond("I am video"); OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(okHttpMockInterceptor) .build(); FileSystemWrapper fileSystemWrapperMock = Mockito.mock(FileSystemWrapper.class); ServerWrapper server = new ServerWrapper(okHttpClient, fileSystemWrapperMock); Boolean response = server.downloadVideo(DownloadId); Assert.assertEquals(true, response); }
@Test(expected = NullPointerException.class) public void uploadVideo_FileIsNull_ThrowsNullPointerException() { ServerWrapper server = new ServerWrapper(null, null); server.downloadVideo(null); }
@Test public void downloadVideo_DownloadFailed_ReturnsFalse() { MockInterceptor okHttpMockInterceptor = new MockInterceptor(); okHttpMockInterceptor.addRule() .get() .urlStarts("https: .answer(request -> new Response.Builder().code(500)); OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(okHttpMockInterceptor) .build(); ServerWrapper server = new ServerWrapper(okHttpClient, null); Boolean response = server.downloadVideo(null); Assert.assertEquals(false, response); }