Skip to content

Commit 4d8dfab

Browse files
author
Baruch Sadogursky
committed
Merge pull request #27 from jbaruch/master
copy and move improvements
2 parents 7ea4f84 + fe729d3 commit 4d8dfab

File tree

5 files changed

+86
-75
lines changed

5 files changed

+86
-75
lines changed

api/src/main/java/org/jfrog/artifactory/client/ItemHandle.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jfrog.artifactory.client;
22

3+
import org.jfrog.artifactory.client.model.Item;
34
import org.jfrog.artifactory.client.model.ItemPermission;
45

56
import java.util.List;
@@ -12,7 +13,7 @@
1213
*/
1314
public interface ItemHandle {
1415

15-
<T> T info();
16+
<T extends Item> T info();
1617

1718
boolean isFolder();
1819

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

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package org.jfrog.artifactory.client.impl
22

3-
import groovyx.net.http.ContentType
43
import groovyx.net.http.HttpResponseException
54
import org.jfrog.artifactory.client.ItemHandle
65
import org.jfrog.artifactory.client.PropertiesHandler
76
import org.jfrog.artifactory.client.model.*
8-
import org.jfrog.artifactory.client.model.impl.GroupImpl
9-
import org.jfrog.artifactory.client.model.impl.ItemPermissionImpl
10-
import org.jfrog.artifactory.client.model.impl.RepoPathImpl
11-
import org.jfrog.artifactory.client.model.impl.UserImpl
7+
import org.jfrog.artifactory.client.model.impl.*
128

9+
import static groovyx.net.http.ContentType.JSON
1310
import static java.util.Collections.unmodifiableSet
1411
/**
1512
*
@@ -21,7 +18,7 @@ class ItemHandleImpl implements ItemHandle {
2118
private ArtifactoryImpl artifactory
2219
private String repo
2320
private String path
24-
private Class itemType
21+
private Class<? extends Item> itemType
2522

2623
ItemHandleImpl(ArtifactoryImpl artifactory, String repo, String path, Class itemType) {
2724
this.artifactory = artifactory
@@ -30,19 +27,19 @@ class ItemHandleImpl implements ItemHandle {
3027
this.itemType = itemType
3128
}
3229

33-
public <T> T info() {
30+
public <T extends Item> T info() {
3431
assert artifactory
3532
assert repo
3633
assert path
37-
artifactory.get("/api/storage/$repo/$path", ContentType.JSON, itemType)
34+
artifactory.get("/api/storage/$repo/$path", JSON, itemType)
3835
}
3936

4037
public Map<String, List<String>> getProperties(String... properties) {
4138
assert artifactory
4239
assert repo
4340
assert path
4441
try {
45-
artifactory.get("/api/storage/$repo/$path", [properties: properties.join(',')], ContentType.JSON, Map.class)?.properties
42+
artifactory.get("/api/storage/$repo/$path", [properties: properties.join(',')], JSON, Map.class)?.properties
4643
} catch (HttpResponseException e) {
4744
if (e.statusCode == 404) {
4845
return [:]
@@ -81,7 +78,7 @@ class ItemHandleImpl implements ItemHandle {
8178

8279
@Override
8380
Set<ItemPermission> effectivePermissions() {
84-
Map json = artifactory.get("/api/storage/${repo}/${path}", ['permissions': null], ContentType.JSON, Map) as Map
81+
Map json = artifactory.get("/api/storage/${repo}/${path}", ['permissions': null], JSON, Map) as Map
8582
unmodifiableSet(mapToItemPermissions(json.principals.users, User) + mapToItemPermissions(json.principals.groups, Group) as Set<? extends ItemPermission>) as Set<ItemPermission>
8683
}
8784

@@ -110,24 +107,21 @@ class ItemHandleImpl implements ItemHandle {
110107
}
111108
}
112109

110+
@Override
113111
ItemHandle move(String toRepo, String toPath) {
114-
moveOrCopy(toRepo, toPath, "move")
112+
moveOrCopy(toRepo, toPath, 'move')
115113
}
116114

115+
@Override
117116
ItemHandle copy(String toRepo, String toPath) {
118-
moveOrCopy(toRepo, toPath, "copy")
117+
moveOrCopy(toRepo, toPath, 'copy')
119118
}
120119

121-
ItemHandle moveOrCopy(String toRepo, String toPath, String method) {
122-
def query = ["to": toRepo + "/" + toPath]
123-
CopyMoveResultReport message = artifactory.post("/api/$method/$repo/$path", query, ContentType.JSON, CopyMoveResultReportImpl)
124-
if (!message.getMessages().get(0).getLevel().equals("INFO")) {
120+
private ItemHandle moveOrCopy(String toRepo, String toPath, String operation) {
121+
CopyMoveResultReport message = artifactory.post("/api/$operation/$repo/$path", [to: "$toRepo/$toPath"], JSON, CopyMoveResultReportImpl) as CopyMoveResultReport
122+
if (!message.getMessages().get(0).getLevel().equals('INFO')) {
125123
throw new CopyMoveException(message)
126124
}
127-
if (this.isFolder()) {
128-
artifactory.repository(toRepo).folder(toPath)
129-
} else {
130-
artifactory.repository(toRepo).file(toPath)
131-
}
125+
new ItemHandleImpl(artifactory, toRepo, toPath, folder ? FolderImpl : FileImpl)
132126
}
133127
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import static org.apache.commons.codec.binary.Base64.encodeBase64;
1515
import static org.apache.commons.lang.StringUtils.remove;
1616
import static org.jfrog.artifactory.client.ArtifactoryClient.create;
17+
import static org.testng.Assert.assertFalse;
18+
import static org.testng.Assert.assertTrue;
1719
import static org.testng.Assert.fail;
1820

1921
/**
@@ -27,6 +29,7 @@ public abstract class ArtifactoryTestsBase {
2729
protected static final String LIBS_RELEASE_VIRTUAL = "libs-release";
2830
protected static final String JCENTER = "jcenter";
2931
protected static final String JCENTER_CACHE = JCENTER + "-cache";
32+
protected static final String LIST_PATH = "api/repositories";
3033
private static final String CLIENTTESTS_ARTIFACTORY_ENV_VAR_PREFIX = "CLIENTTESTS_ARTIFACTORY_";
3134
private static final String CLIENTTESTS_ARTIFACTORY_PROPERTIES_PREFIX = "clienttests.artifactory.";
3235
protected Artifactory artifactory;
@@ -125,4 +128,19 @@ protected String textFrom(InputStream is) throws IOException {
125128
return sb.toString();
126129
}
127130
}
131+
132+
protected String deleteRepoIfExists(String repoName) throws IOException {
133+
try {
134+
String result = artifactory.repository(repoName).delete();
135+
assertTrue(result.startsWith("Repository " + repoName + " and all its content have been removed successfully."));
136+
assertFalse(curl(LIST_PATH).contains("\""+repoName+"\""));
137+
return result;
138+
} catch (Exception e) {
139+
if (e.getMessage().equals("Not Found")) { //if repo wasn't found - that's ok.
140+
return e.getMessage();
141+
} else {
142+
throw e;
143+
}
144+
}
145+
}
128146
}

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

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.jfrog.artifactory.client.model.LocalRepository;
88
import org.testng.annotations.Test;
99

10+
import java.io.IOException;
1011
import java.io.InputStream;
1112
import java.util.HashMap;
1213
import java.util.List;
@@ -127,41 +128,55 @@ public void testSetItemPropertiesOnNonExistingDirectory() throws Exception {
127128
//this test move content of directory "x" to another repo into directory "abc", than both repo's will be removed after finish
128129
@Test
129130
public void testMoveDirectory() throws Exception {
130-
prepareRepositoriesForMovingAndCoping();
131-
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_FROM).folder("x");
132-
ItemHandle newItemHandle = itemHandle.move(NEW_LOCAL_TO, "abc");
133-
checkTheEqualityOfFolders(newItemHandle);
134-
deleteAllRelatedRepos();
131+
try {
132+
prepareRepositoriesForMovingAndCoping();
133+
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_FROM).folder("x");
134+
String path = "abc";
135+
checkTheEqualityOfFolders(itemHandle.move(NEW_LOCAL_TO, path), NEW_LOCAL_TO, path);
136+
} finally {
137+
deleteAllRelatedRepos();
138+
}
135139
}
136140

137141
//this test copy content of directory "x" to another repo into directory "abc", than both repo's will be removed after finish
138142
@Test
139143
public void testCopyDirectory() throws Exception {
140-
prepareRepositoriesForMovingAndCoping();
141-
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_FROM).folder("x");
142-
ItemHandle newItemHandle = itemHandle.copy(NEW_LOCAL_TO, "abc");
143-
checkTheEqualityOfFolders(newItemHandle);
144-
deleteAllRelatedRepos();
144+
try {
145+
prepareRepositoriesForMovingAndCoping();
146+
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_FROM).folder("x");
147+
String path = "abc";
148+
checkTheEqualityOfFolders(itemHandle.copy(NEW_LOCAL_TO, path), NEW_LOCAL_TO, path);
149+
} finally {
150+
deleteAllRelatedRepos();
151+
}
145152
}
146153

147154
//this test move file "z" to the root of another repo, than both repo's will be removed after finish
148155
@Test
149156
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();
157+
try {
158+
prepareRepositoriesForMovingAndCoping();
159+
String path = "x/y/z";
160+
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_FROM).file(path);
161+
ItemHandle newItemHandle = itemHandle.move(NEW_LOCAL_TO, path);
162+
checkTheEqualityOfFiles(newItemHandle, NEW_LOCAL_TO, path);
163+
} finally {
164+
deleteAllRelatedRepos();
165+
}
155166
}
156167

157168
//this test copy file "z" to the root of another repo, than both repo's will be removed after finish
158169
@Test
159170
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();
171+
try {
172+
prepareRepositoriesForMovingAndCoping();
173+
String path = "x/y/z";
174+
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_FROM).file(path);
175+
ItemHandle newItemHandle = itemHandle.copy(NEW_LOCAL_TO, path);
176+
checkTheEqualityOfFiles(newItemHandle, NEW_LOCAL_TO, path);
177+
} finally {
178+
deleteAllRelatedRepos();
179+
}
165180
}
166181

167182
@Test
@@ -171,9 +186,10 @@ public void testExceptionOnMovingFile() throws Exception {
171186
try {
172187
itemHandle.move(NEW_LOCAL_TO, "x/y/z");
173188
} 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);
189+
assertTrue(curl("api/move/" + NEW_LOCAL_FROM + "/a/a?to=x/y/z", "POST").contains(e.getCopyMoveResultReport().getMessages().get(0).getMessage()));
190+
} finally {
191+
deleteAllRelatedRepos();
175192
}
176-
deleteAllRelatedRepos();
177193
}
178194

179195
@Test
@@ -183,23 +199,24 @@ public void testExceptionOnCopingFile() throws Exception {
183199
try {
184200
itemHandle.copy(NEW_LOCAL_TO, "x/y/z");
185201
} 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);
202+
assertTrue(curl("api/copy/" + NEW_LOCAL_FROM + "/a/a?to=x/y/z", "POST").contains(e.getCopyMoveResultReport().getMessages().get(0).getMessage()));
203+
} finally {
204+
deleteAllRelatedRepos();
187205
}
188-
deleteAllRelatedRepos();
189206
}
190207

191-
private void checkTheEqualityOfFolders(ItemHandle newItemHandle) {
192-
ItemHandle itemHandle = artifactory.repository(NEW_LOCAL_TO).folder("abc");
193-
assertEquals(itemHandle.info().equals(newItemHandle.info()), true);
208+
private void checkTheEqualityOfFolders(ItemHandle newItemHandle, String expectedRepo, String expectedPath) {
209+
ItemHandle itemHandle = artifactory.repository(expectedRepo).folder(expectedPath);
210+
assertEquals(itemHandle.info(), (newItemHandle.info()));
194211
}
195212

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);
213+
private void checkTheEqualityOfFiles(ItemHandle newItemHandle, String expectedRepo, String expectedPath) {
214+
ItemHandle itemHandle = artifactory.repository(expectedRepo).file(expectedPath);
215+
assertEquals(itemHandle.info(), (newItemHandle.info()));
199216
}
200217

201218

202-
private void prepareRepositoriesForMovingAndCoping() {
219+
private void prepareRepositoriesForMovingAndCoping() throws IOException {
203220
deleteAllRelatedRepos();
204221
setupLocalRepo(NEW_LOCAL_FROM);
205222
setupLocalRepo(NEW_LOCAL_TO);
@@ -208,18 +225,8 @@ private void prepareRepositoriesForMovingAndCoping() {
208225
artifactory.repository(NEW_LOCAL_FROM).upload("x/y/z", content).doUpload();
209226
}
210227

211-
private void deleteAllRelatedRepos() {
228+
private void deleteAllRelatedRepos() throws IOException {
212229
deleteRepoIfExists(NEW_LOCAL_FROM);
213230
deleteRepoIfExists(NEW_LOCAL_TO);
214231
}
215-
216-
private void deleteRepoIfExists(String repoName) {
217-
try {
218-
artifactory.repository(repoName).delete();
219-
} catch (Exception e) {
220-
if (!e.getMessage().equals("Not Found")) { //if repo wasn't found - that's ok. It means testCreate didn't run.
221-
throw e;
222-
}
223-
}
224-
}
225232
}

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020
public class RepositoryTests extends ArtifactoryTestsBase {
2121

22-
private static final String LIST_PATH = "api/repositories";
2322
private LocalRepository localRepository;
2423

2524
@BeforeMethod
@@ -50,16 +49,8 @@ public void testReplicationStatus() throws Exception {
5049

5150
@Test(groups = "repositoryBasics")
5251
public void testDelete() throws Exception {
53-
try {
54-
String result = artifactory.repository(NEW_LOCAL).delete();
55-
assertTrue(result
56-
.startsWith("Repository " + NEW_LOCAL + " and all its content have been removed successfully."));
57-
assertFalse(curl(LIST_PATH).contains("\""+NEW_LOCAL+"\""));
58-
} catch (HttpResponseException e) {
59-
if (!e.getMessage().equals("Not Found")) { //if repo wasn't found - that's ok. It means testCreate didn't run.
60-
throw e;
61-
}
62-
}
52+
//all the assertions are taken care of in deleteRepoIfExists
53+
deleteRepoIfExists(NEW_LOCAL);
6354
}
6455

6556
@Test

0 commit comments

Comments
 (0)