/** * Download the required file from artifactory */ private static java.io.File downloadFile(Artifactory artifactory, String repo, String filePath, String fileDownloadToLocation) throws Exception { if (artifactory == null || StringUtils.isEmpty(repo) || StringUtils.isEmpty(filePath)){ throw new IllegalArgumentException("Arguments passed to downloadFile are not valid"); } InputStream inputStream = artifactory.repository(repo) .download(filePath) .doDownload(); java.io.File targetFile = new java.io.File(fileDownloadToLocation); FileUtils.copyInputStreamToFile(inputStream, targetFile); return targetFile; }
/** * This method receives the uploaded file source and destination, performs the upload to artifactory */ private static File uploadFile(Artifactory artifactory, String repo, String destPath, String fileNameToUpload) throws IOException { if (StringUtils.isEmpty(repo) || StringUtils.isEmpty(destPath) || StringUtils.isEmpty(fileNameToUpload) || artifactory == null){ throw new IllegalArgumentException("Arguments passed to createArtifactory are not valid"); } Path path = Paths.get(fileNameToUpload); Files.write(path, Collections.singleton("This is an example line"), Charset.forName("UTF-8")); java.io.File file = new java.io.File(fileNameToUpload); return artifactory.repository(repo).upload(destPath, file).doUpload(); }
protected String deleteRepoIfExists(String repoName) { if (isEmpty(repoName)) { return null; } try { return artifactory.repository(repoName).delete(); } catch (Exception e) { if (e instanceof HttpResponseException && ((HttpResponseException)e).getStatusCode() == 404) { //if repo wasn't found - that's ok. return e.getMessage(); } else { throw e; } } }
@Test(groups = "security") public void testEffectiveItemPermissions() throws Exception { InputStream inputStream = this.getClass().getResourceAsStream("/sample.txt"); Assert.assertNotNull(inputStream); File deployed = artifactory.repository(localRepository.getKey()).upload(PATH, inputStream).doUpload(); Assert.assertNotNull(deployed); Set<ItemPermission> itemPermissions = artifactory.repository(localRepository.getKey()).file(PATH).effectivePermissions(); assertItemPermissions(itemPermissions); }
@Test(dependsOnMethods = "testUploadWithSingleProperty") public void testUploadExplodeArchive() throws IOException { artifactory.repository(localRepository.getKey()).upload("sample/sample.zip", this.getClass().getResourceAsStream("/sample.zip")) .doUploadAndExplode(); List<Item> items = ((FolderImpl) artifactory.repository(localRepository.getKey()).folder("sample").info()).getChildren(); assertEquals(items.get(0).getUri(), "/a.txt"); assertEquals(items.get(1).getUri(), "/b.txt"); assertEquals(items.get(2).getUri(), "/c.txt"); }
@Test public void testFolderInfo() throws IOException { // Get the folder to the cache artifactory.repository(getJCenterRepoName()).download("junit/junit/4.10/junit-4.10-sources.jar").doDownload(); Folder folder = artifactory.repository(getJcenterCacheName()).folder("junit").info(); assertNotNull(folder); assertTrue(folder.isFolder()); assertEquals(folder.getChildren().size(), 1); assertEquals(folder.getRepo(), getJcenterCacheName()); assertEquals(folder.getPath(), "/junit"); }
@Test(dependsOnMethods = "testUploadWithSingleProperty") public void testUploadCopyBySha1WithSingleProperty() throws IOException { InputStream inputStream = this.getClass().getResourceAsStream("/sample.txt"); assertNotNull(inputStream); String refPath = "r/a/b/c.txt"; String sha1 = calcSha1(inputStream); File deployed = artifactory.repository(localRepository.getKey()) .copyBySha1(refPath, sha1) .withProperty("color", "red") .doUpload(); assertNotNull(deployed); inputStream = artifactory.repository(localRepository.getKey()).download(refPath).doDownload(); String actual = textFrom(inputStream); assertEquals(actual, textFrom(this.getClass().getResourceAsStream("/sample.txt"))); assertTrue(curlAndStrip("api/storage/" + localRepository.getKey() + "/" + refPath + "?properties").contains("\"color\":[\"red\"]")); }
@Test public void testFileInfo() throws IOException { // Get the file to the cache artifactory.repository(getJCenterRepoName()).download("junit/junit/4.10/junit-4.10-sources.jar").doDownload(); File file = artifactory.repository(getJcenterCacheName()).file("junit/junit/4.10/junit-4.10-sources.jar").info(); assertNotNull(file); assertFalse(file.isFolder()); assertEquals(file.getSize(), 141185); assertEquals(file.getDownloadUri(), url + getJcenterCacheName() + "/junit/junit/4.10/junit-4.10-sources.jar"); assertEquals(file.getChecksums().getMd5(), "8f17d4271b86478a2731deebdab8c846"); assertEquals(file.getChecksums().getSha1(), "6c98d6766e72d5575f96c9479d1c1d3b865c6e25"); }
@Test(dependsOnMethods = "testCreate") public void testCreateDirectoryWithIllegalName() throws IOException { try { artifactory.repository(localRepository.getKey()).folder("myFolder?").create(); } catch (Exception e) { assertTrue(e instanceof HttpResponseException); assertTrue(((HttpResponseException)e).getStatusCode() == 500); } }
private void checkTheEqualityOfFiles(ItemHandle newItemHandle, String expectedRepo, String expectedPath) { ItemHandle itemHandle = artifactory.repository(expectedRepo).file(expectedPath); assertEquals(itemHandle.info(), (Item)newItemHandle.info()); }
@Test(dependsOnMethods = "testCreate") public void testGetLocal() throws Exception { Repository repository = artifactory.repository(localRepository.getKey()).get(); assertNotNull(repository); assertTrue(LocalRepository.class.isAssignableFrom(repository.getClass())); LocalRepository localRepo = (LocalRepository) repository; assertEquals(localRepo.getKey(), localRepository.getKey()); assertEquals(localRepo.getRclass().toString(), "local"); assertEquals(localRepo.getDescription(), "new local repository"); assertEquals(localRepo.getNotes(), ""); assertEquals(localRepo.getIncludesPattern(), "**/*"); assertEquals(localRepo.getExcludesPattern(), ""); assertFalse(localRepo.isBlackedOut()); List<String> propertySets = localRepo.getPropertySets(); assertEquals(propertySets.size(), 1); assertEquals(propertySets.get(0), ("artifactory")); assertEquals(localRepo.getRepoLayoutRef(), localRepository.getRepoLayoutRef()); }
@Test(dependsOnMethods = "testCreate") public void testRepositoryExists() throws IOException { assertTrue(artifactory.repository(localRepository.getKey()).exists()); String notExistsRepoName = Long.toString(System.currentTimeMillis()); assertFalse(artifactory.repository(notExistsRepoName).exists()); }
@Test(groups = "security") public void testEffectiveRepoPermissions() { Set<ItemPermission> itemPermissions = artifactory.repository(localRepository.getKey()).effectivePermissions(); assertItemPermissions(itemPermissions); }
private void checkTheEqualityOfFolders(ItemHandle newItemHandle, String expectedRepo, String expectedPath) { ItemHandle itemHandle = artifactory.repository(expectedRepo).folder(expectedPath); assertEquals(itemHandle.info(), (Item)newItemHandle.info()); }
@Test public void testExceptionOnCopingFile() throws Exception { prepareRepositoriesForMovingAndCoping(); ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_FROM).file("a/a"); try { itemHandle.copy(NEW_LOCAL_TO, PATH); } catch (CopyMoveException e) { assertTrue(curl("api/copy/" + NEW_LOCAL_FROM + "/a/a?to=" + NEW_LOCAL_TO + "/" + PATH, "POST").contains(e.getCopyMoveResultReport().getMessages().get(0).getMessage())); } finally { deleteAllRelatedRepos(); } }
@Test(dependsOnMethods = "testCreate") public void testGetVirtual() throws Exception { Repository repository = artifactory.repository(virtualRepository.getKey()).get(); assertNotNull(repository); assertTrue(VirtualRepository.class.isAssignableFrom(repository.getClass())); VirtualRepository libsReleases = (VirtualRepository) repository; assertEquals(libsReleases.getKey(), virtualRepository.getKey()); assertEquals(libsReleases.getRclass().toString(), "virtual"); assertEquals(libsReleases.getNotes(), ""); assertEquals(libsReleases.getIncludesPattern(), "**/*"); assertEquals(libsReleases.getExcludesPattern(), ""); assertEquals(libsReleases.getRepositories(), asList(localRepository.getKey(), localRepository2.getKey(), getJCenterRepoName())); assertFalse(libsReleases.isArtifactoryRequestsCanRetrieveRemoteArtifacts()); assertTrue(libsReleases.getRepoLayoutRef() == null || libsReleases.getRepoLayoutRef().isEmpty()); }
InputStream response = client.repository(repoName).download(metadataStr).doDownload(); LOGGER.info("download complete"); Files.copy(response, tempFilePath, StandardCopyOption.REPLACE_EXISTING);
private void prepareRepositoriesForMovingAndCoping() throws IOException { deleteAllRelatedRepos(); setupLocalRepo(NEW_LOCAL_FROM); setupLocalRepo(NEW_LOCAL_TO); InputStream content = this.getClass().getResourceAsStream("/sample.txt"); assertNotNull(content); artifactory.repository(NEW_LOCAL_FROM).upload(PATH, content).doUpload(); }