Skip to content

Commit 7ea4f84

Browse files
author
Baruch Sadogursky
committed
Merge pull request #26 from Jeka1978/master
6 tests added
2 parents 84063a1 + 05bd87b commit 7ea4f84

File tree

4 files changed

+104
-20
lines changed

4 files changed

+104
-20
lines changed

services/src/main/groovy/org/jfrog/artifactory/client/impl/CopyMoveException.groovy

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import org.jfrog.artifactory.client.model.CopyMoveResultReport
66
* Created by Jeka on 05/11/13.
77
*/
88
class CopyMoveException extends RuntimeException {
9-
CopyMoveResultReport copyMoveResultReport
9+
private CopyMoveResultReport copyMoveResultReport
1010

1111
CopyMoveException(CopyMoveResultReport copyMoveResultReport) {
1212
this.copyMoveResultReport = copyMoveResultReport
1313
}
14+
15+
CopyMoveResultReport getCopyMoveResultReport() {
16+
return copyMoveResultReport
17+
}
1418
}

services/src/main/groovy/org/jfrog/artifactory/client/impl/ItemHandleImpl.groovy

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ class ItemHandleImpl implements ItemHandle {
124124
if (!message.getMessages().get(0).getLevel().equals("INFO")) {
125125
throw new CopyMoveException(message)
126126
}
127-
artifactory.repository(toRepo).file(toPath)
127+
if (this.isFolder()) {
128+
artifactory.repository(toRepo).folder(toPath)
129+
} else {
130+
artifactory.repository(toRepo).file(toPath)
131+
}
128132
}
129133
}

services/src/test/java/org/jfrog/artifactory/client/ArtifactoryTestsBase.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.IOException;
77
import java.io.InputStream;
88
import java.io.InputStreamReader;
9+
import java.net.HttpURLConnection;
910
import java.net.URL;
1011
import java.net.URLConnection;
1112
import java.util.Properties;
@@ -46,24 +47,24 @@ public void init() throws IOException {
4647
password = props.getProperty(CLIENTTESTS_ARTIFACTORY_PROPERTIES_PREFIX + "password");
4748
} else {
4849
url = System.getProperty(CLIENTTESTS_ARTIFACTORY_PROPERTIES_PREFIX + "url");
49-
if(url == null) {
50+
if (url == null) {
5051
url = System.getenv(CLIENTTESTS_ARTIFACTORY_ENV_VAR_PREFIX + "URL");
5152
}
52-
if(url == null){
53+
if (url == null) {
5354
failInit();
5455
}
5556
username = System.getProperty(CLIENTTESTS_ARTIFACTORY_PROPERTIES_PREFIX + "username");
56-
if(username == null) {
57+
if (username == null) {
5758
username = System.getenv(CLIENTTESTS_ARTIFACTORY_ENV_VAR_PREFIX + "USERNAME");
5859
}
59-
if(username == null){
60+
if (username == null) {
6061
failInit();
6162
}
6263
password = System.getProperty(CLIENTTESTS_ARTIFACTORY_PROPERTIES_PREFIX + "password");
63-
if(password == null) {
64+
if (password == null) {
6465
password = System.getenv(CLIENTTESTS_ARTIFACTORY_ENV_VAR_PREFIX + "PASSWORD");
6566
}
66-
if(password == null){
67+
if (password == null) {
6768
failInit();
6869
}
6970

@@ -85,17 +86,28 @@ public void clean() {
8586
artifactory.close();
8687
}
8788

88-
protected String curl(String path) throws IOException {
89+
protected String curl(String path, String method) throws IOException {
8990
String authStringEnc = new String(encodeBase64((username + ":" + password).getBytes()));
9091
URLConnection urlConnection = new URL(url + "/" + path).openConnection();
9192
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
93+
if (urlConnection instanceof HttpURLConnection) {
94+
((HttpURLConnection) urlConnection).setRequestMethod(method);
95+
}
9296
try (InputStream is = urlConnection.getInputStream()) {
9397
return textFrom(is);
9498
}
9599
}
96100

101+
protected String curl(String path) throws IOException {
102+
return curl(path, "GET");
103+
}
104+
97105
protected String curlAndStrip(String path) throws IOException {
98-
String result = curl(path);
106+
return curlAndStrip(path, "GET");
107+
}
108+
109+
protected String curlAndStrip(String path, String method) throws IOException {
110+
String result = curl(path, method);
99111
result = remove(result, ' ');
100112
result = remove(result, '\r');
101113
result = remove(result, '\n');

services/src/test/java/org/jfrog/artifactory/client/ItemTests.java

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jfrog.artifactory.client;
22

33
import groovyx.net.http.HttpResponseException;
4+
import org.jfrog.artifactory.client.impl.CopyMoveException;
45
import org.jfrog.artifactory.client.model.File;
56
import org.jfrog.artifactory.client.model.Folder;
67
import org.jfrog.artifactory.client.model.LocalRepository;
@@ -123,32 +124,95 @@ public void testSetItemPropertiesOnNonExistingDirectory() throws Exception {
123124
assertTrue(folder.getPropertyValues("v1").contains("b2"));
124125
}
125126

127+
//this test move content of directory "x" to another repo into directory "abc", than both repo's will be removed after finish
126128
@Test
127-
public void testMoveItem() throws Exception {
129+
public void testMoveDirectory() throws Exception {
128130
prepareRepositoriesForMovingAndCoping();
129-
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_FROM).file("x");
130-
ItemHandle newItemHandle = itemHandle.move(NEW_LOCAL_TO, "x");
131-
assertNotNull(newItemHandle);
131+
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_FROM).folder("x");
132+
ItemHandle newItemHandle = itemHandle.move(NEW_LOCAL_TO, "abc");
133+
checkTheEqualityOfFolders(newItemHandle);
134+
deleteAllRelatedRepos();
132135
}
133136

137+
//this test copy content of directory "x" to another repo into directory "abc", than both repo's will be removed after finish
134138
@Test
135-
public void testCopyItem() throws Exception {
139+
public void testCopyDirectory() throws Exception {
136140
prepareRepositoriesForMovingAndCoping();
137-
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_FROM).file("x");
138-
ItemHandle newItemHandle = itemHandle.copy(NEW_LOCAL_TO, "x");
139-
assertNotNull(newItemHandle);
141+
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_FROM).folder("x");
142+
ItemHandle newItemHandle = itemHandle.copy(NEW_LOCAL_TO, "abc");
143+
checkTheEqualityOfFolders(newItemHandle);
144+
deleteAllRelatedRepos();
140145
}
141146

147+
//this test move file "z" to the root of another repo, than both repo's will be removed after finish
148+
@Test
149+
public void testMoveFile() throws Exception {
150+
prepareRepositoriesForMovingAndCoping();
151+
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_FROM).file("x/y/z");
152+
ItemHandle newItemHandle = itemHandle.move(NEW_LOCAL_TO, "x/y/z");
153+
checkTheEqualityOfFiles(newItemHandle);
154+
deleteAllRelatedRepos();
155+
}
156+
157+
//this test copy file "z" to the root of another repo, than both repo's will be removed after finish
158+
@Test
159+
public void testCopyFile() throws Exception {
160+
prepareRepositoriesForMovingAndCoping();
161+
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_FROM).file("x/y/z");
162+
ItemHandle newItemHandle = itemHandle.copy(NEW_LOCAL_TO, "x/y/z");
163+
checkTheEqualityOfFiles(newItemHandle);
164+
deleteAllRelatedRepos();
165+
}
166+
167+
@Test
168+
public void testExceptionOnMovingFile() throws Exception {
169+
prepareRepositoriesForMovingAndCoping();
170+
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_FROM).file("a/a");
171+
try {
172+
itemHandle.move(NEW_LOCAL_TO, "x/y/z");
173+
} catch (CopyMoveException e) {
174+
assertEquals(curl("api/move/" + NEW_LOCAL_FROM + "/a/a?to=x/y/z", "POST").contains(e.getCopyMoveResultReport().getMessages().get(0).getMessage()), true);
175+
}
176+
deleteAllRelatedRepos();
177+
}
178+
179+
@Test
180+
public void testExceptionOnCopingFile() throws Exception {
181+
prepareRepositoriesForMovingAndCoping();
182+
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_FROM).file("a/a");
183+
try {
184+
itemHandle.copy(NEW_LOCAL_TO, "x/y/z");
185+
} catch (CopyMoveException e) {
186+
assertEquals(curl("api/copy/" + NEW_LOCAL_FROM + "/a/a?to=x/y/z", "POST").contains(e.getCopyMoveResultReport().getMessages().get(0).getMessage()), true);
187+
}
188+
deleteAllRelatedRepos();
189+
}
190+
191+
private void checkTheEqualityOfFolders(ItemHandle newItemHandle) {
192+
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_TO).folder("abc");
193+
assertEquals(itemHandle.info().equals(newItemHandle.info()), true);
194+
}
195+
196+
private void checkTheEqualityOfFiles(ItemHandle newItemHandle) {
197+
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_TO).file("x/y/z");
198+
assertEquals(itemHandle.info().equals(newItemHandle.info()), true);
199+
}
200+
201+
142202
private void prepareRepositoriesForMovingAndCoping() {
143-
deleteRepoIfExists(NEW_LOCAL_FROM);
144-
deleteRepoIfExists(NEW_LOCAL_TO);
203+
deleteAllRelatedRepos();
145204
setupLocalRepo(NEW_LOCAL_FROM);
146205
setupLocalRepo(NEW_LOCAL_TO);
147206
InputStream content = this.getClass().getResourceAsStream("/sample.txt");
148207
assertNotNull(content);
149208
artifactory.repository(NEW_LOCAL_FROM).upload("x/y/z", content).doUpload();
150209
}
151210

211+
private void deleteAllRelatedRepos() {
212+
deleteRepoIfExists(NEW_LOCAL_FROM);
213+
deleteRepoIfExists(NEW_LOCAL_TO);
214+
}
215+
152216
private void deleteRepoIfExists(String repoName) {
153217
try {
154218
artifactory.repository(repoName).delete();

0 commit comments

Comments
 (0)