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+ }
0 commit comments