Skip to content

Commit 8d1dfa3

Browse files
authored
DURACLOUD-1102: updates createSpace method to check for existence of space before try… (duracloud#195)
* udpates createSpace method to check for existence of space before trying to create it * fixes testCreateSpace()
1 parent 86ce05e commit 8d1dfa3

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

storeclient/src/main/java/org/duracloud/client/ContentStoreImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,13 @@ public void createSpace(final String spaceId)
400400
execute(new Retriable() {
401401
@Override
402402
public Boolean retry() throws ContentStoreException {
403-
// The actual method being executed
404-
doCreateSpace(spaceId);
403+
// First check if space exists
404+
try {
405+
getSpace(spaceId, null, 0, null);
406+
} catch (NotFoundException e) {
407+
// Try creating the space if it doesn't exist
408+
doCreateSpace(spaceId);
409+
}
405410
return true;
406411
}
407412
});

storeclient/src/test/java/org/duracloud/client/ContentStoreImplTest.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,16 @@ public class ContentStoreImplTest {
7575
private String spaceId = "myspace";
7676
private String contentId = "mycontent";
7777

78+
private int maxRetries = 3;
79+
7880
@Before
7981
public void setUp() throws Exception {
8082
restHelper = EasyMock.createMock("RestHttpHelper",
8183
RestHttpHelper.class);
8284
response = EasyMock.createMock("HttpResponse",
8385
RestHttpHelper.HttpResponse.class);
8486

85-
contentStore = new ContentStoreImpl(baseURL, type, storeId, false, restHelper);
87+
contentStore = new ContentStoreImpl(baseURL, type, storeId, false, restHelper, maxRetries);
8688
}
8789

8890
@After
@@ -228,9 +230,17 @@ public void testGetAuditLog() throws Exception {
228230

229231
@Test
230232
public void testCreateSpace() throws Exception {
231-
String fullURL = baseURL + "/" + spaceId + "?storeID=" + storeId;
233+
// First call is to check if space exists
234+
String existsFullURL = baseURL + "/" + spaceId +
235+
"?storeID=" + storeId;
236+
EasyMock.expect(response.getStatusCode()).andReturn(404).times(4);
237+
EasyMock.expect(response.getResponseBody()).andReturn("").times(4);
238+
EasyMock.expect(restHelper.get(existsFullURL)).andReturn(response).times(4);
239+
240+
// Second call is to create space
241+
String createFullURL = baseURL + "/" + spaceId + "?storeID=" + storeId;
232242
EasyMock.expect(response.getStatusCode()).andReturn(201);
233-
EasyMock.expect(restHelper.put(fullURL, null, null)).andReturn(response);
243+
EasyMock.expect(restHelper.put(createFullURL, null, null)).andReturn(response);
234244

235245
replayMocks();
236246

0 commit comments

Comments
 (0)