Skip to content

Commit d7c12a5

Browse files
committed
Add http level test for cleanupEmptyFolders
1 parent b2c6f74 commit d7c12a5

File tree

4 files changed

+114
-9
lines changed

4 files changed

+114
-9
lines changed

src/test/java/org/commonjava/service/storage/StorageControllerIT.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ public class StorageControllerIT extends StorageIT
3838
@Inject
3939
StorageController controller;
4040

41-
private void createEmptyDirectory(String filesystem, String dir) throws Exception {
42-
String dummyFile = dir + "/.keep";
43-
try (OutputStream out = fileManager.openOutputStream(filesystem, dummyFile)) {
44-
out.write(0);
45-
}
46-
fileManager.delete(filesystem, dummyFile);
47-
}
48-
4941
@Test
5042
public void testGetFileInfo() throws Exception
5143
{

src/test/java/org/commonjava/service/storage/StorageIT.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ protected void prepareFile( InputStream in, String filesystem, String path ) thr
8585
}
8686
}
8787

88+
protected void createEmptyDirectory(String filesystem, String dir) throws Exception {
89+
String dummyFile = dir + "/.keep";
90+
try (OutputStream out = fileManager.openOutputStream(filesystem, dummyFile)) {
91+
out.write(0);
92+
}
93+
fileManager.delete(filesystem, dummyFile);
94+
}
95+
8896
/**
8997
* Override this if your test case don't need to prepare the PATH
9098
* @return
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (C) 2021 Red Hat, Inc. (https://github.com/Commonjava/service-parent)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.service.storage;
17+
18+
import io.quarkus.test.junit.QuarkusTest;
19+
import org.commonjava.service.storage.dto.BatchDeleteRequest;
20+
import org.commonjava.service.storage.dto.BatchDeleteResult;
21+
import org.junit.jupiter.api.Test;
22+
23+
import jakarta.ws.rs.core.MediaType;
24+
import java.util.Arrays;
25+
import java.util.HashSet;
26+
import java.util.Set;
27+
28+
import static org.hamcrest.MatcherAssert.assertThat;
29+
import static org.hamcrest.Matchers.*;
30+
import static io.restassured.RestAssured.given;
31+
32+
@QuarkusTest
33+
public class StorageMaintResourceIT extends StorageIT {
34+
35+
@Override
36+
protected boolean isPrepareFile() {
37+
return false;
38+
}
39+
40+
/**
41+
* Integration test for the cleanupEmptyFolders REST endpoint.
42+
* <p>
43+
* Folder structure used in this test:
44+
* <pre>
45+
* test-root-http/
46+
* ├── a/
47+
* │ ├── b/ (not empty, contains file.txt)
48+
* │ └── file.txt (so a is not empty)
49+
* └── d/ (empty)
50+
* </pre>
51+
* <ul>
52+
* <li>Sets up a nested directory structure with both empty and non-empty folders.</li>
53+
* <li>Sends a DELETE request to /api/storage/maint/folders/empty with a batch of folders.</li>
54+
* <li>Asserts that only empty folders are deleted and non-empty folders remain.</li>
55+
* <li>Verifies the HTTP response and the BatchDeleteResult content.</li>
56+
* </ul>
57+
*/
58+
@Test
59+
public void testCleanupEmptyFolders_HttpLevel() {
60+
// Setup: create a nested directory structure as in the controller test
61+
String root = "test-root-http";
62+
String a = root + "/a";
63+
String b = a + "/b";
64+
String d = root + "/d";
65+
String fileInB = b + "/file.txt";
66+
String fileInA = a + "/file.txt";
67+
68+
// Create directories (by writing and deleting a dummy file)
69+
try {
70+
createEmptyDirectory(filesystem, b);
71+
createEmptyDirectory(filesystem, d);
72+
// Add a file to b (so b is not empty)
73+
try (java.io.OutputStream out = fileManager.openOutputStream(filesystem, fileInB)) {
74+
out.write("data".getBytes());
75+
}
76+
// Add a file to a (so a is not empty)
77+
try (java.io.OutputStream out = fileManager.openOutputStream(filesystem, fileInA)) {
78+
out.write("data".getBytes());
79+
}
80+
} catch (Exception e) {
81+
throw new RuntimeException(e);
82+
}
83+
84+
// Prepare request: try to clean up b and d
85+
BatchDeleteRequest request = new BatchDeleteRequest();
86+
request.setFilesystem(filesystem);
87+
Set<String> paths = new HashSet<>(Arrays.asList(b, d));
88+
request.setPaths(paths);
89+
90+
// Send DELETE request to the API
91+
BatchDeleteResult result =
92+
given()
93+
.contentType(MediaType.APPLICATION_JSON)
94+
.body(request)
95+
.when()
96+
.delete("/api/storage/maint/folders/empty")
97+
.then()
98+
.statusCode(200)
99+
.extract().as(BatchDeleteResult.class);
100+
101+
// d should be deleted (empty), b should not (not empty)
102+
assertThat(result.getSucceeded(), hasItem(d));
103+
assertThat(result.getSucceeded(), not(hasItem(b)));
104+
assertThat(result.getFailed(), not(hasItem(d)));
105+
}
106+
}

src/test/java/org/commonjava/service/storage/StorageResourceIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.junit.jupiter.api.Test;
2929

3030
import java.io.ByteArrayInputStream;
31-
import java.io.IOException;
3231
import java.io.InputStream;
3332
import java.util.Arrays;
3433
import java.util.HashSet;

0 commit comments

Comments
 (0)