HttpRequester extends IArtifactRequester { @Override public Optional<File> requestFile(Coordinate mavenCoordinate, Path targetDirectory, ClassifierInformation classifierInformation) { String jarBaseName = getExpectedJarBaseName(mavenCoordinate, classifierInformation); File localJarFile = targetDirectory.resolve(jarBaseName).toFile(); if (localJarFile.exists()) { LOGGER.debug("The file " + localJarFile + " already exists and won't be downloaded again"); return Optional.of(localJarFile); } Optional<File> downloadedFile = downloadFileFromUserUrl(mavenCoordinate, targetDirectory, jarBaseName); if (!downloadedFile.isPresent()) { String mavenCentralJarUrl = getJarUrl(mavenCoordinate, jarBaseName, MAVEN_CENTRAL_URL); return tryFileDownload(mavenCentralJarUrl, targetDirectory, jarBaseName); } return downloadedFile; } HttpRequester(HttpClient httpClient, URL sourceRepositoryUrl);  HttpRequester(HttpClient httpClient); @Override Optional<File> requestFile(Coordinate mavenCoordinate, Path targetDirectory, ClassifierInformation classifierInformation);  }
@Test public void requestFileUsesTheCorrectUrl() throws Exception { Path targetDirectory = temporaryFolder.newFolder("target").toPath(); hr.requestFile(mavenCoordinate, targetDirectory, classifierInformation); String filename = "artifactId-version" + (classifierInformation.isSource ? "-sources" : "") + ".jar"; verify(httpHelperMock).downloadFile("http: }
@Test public void requestFileDealsWithExceptionReturningAnEmptyRequest() throws Exception { Path targetDirectory = temporaryFolder.newFolder("target").toPath(); when(httpHelperMock.downloadFile(anyString(), eq(targetDirectory), anyString())).thenThrow(new IOException("Failed to download")); Optional<File> file = hr.requestFile(mavenCoordinate, targetDirectory, classifierInformation); assertThat(file).isEmpty(); }
@Test public void requestFileDoesNotDownloadIfFileExists() throws Exception { Path targetDirectory = temporaryFolder.newFolder("target").toPath(); String filename = "artifactId-version" + (classifierInformation.isSource ? "-sources" : "") + ".jar"; File expectedFile = new File(targetDirectory.toFile(), filename); new FileOutputStream(expectedFile).close(); hr.requestFile(mavenCoordinate, targetDirectory, classifierInformation); verify(httpHelperMock, never()).downloadFile(anyString(), eq(targetDirectory), anyString()); }