Skip to content

Commit b3f30d6

Browse files
authored
Merge pull request #61 from ruhan1/main-testarchive
[migration] Add a test api to create test zip
2 parents 178eca7 + 0a24f4f commit b3f30d6

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/main/java/org/commonjava/indy/service/archive/controller/ArchiveController.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,29 @@ public void cleanupBCWorkspace( String buildConfigId )
338338
}
339339
}
340340

341+
public File createTestArchive( final String archiveName ) throws IOException
342+
{
343+
File targetDir = new File( archiveDir );
344+
targetDir.mkdirs();
345+
346+
final File archiveFile = new File( archiveDir, archiveName + ARCHIVE_SUFFIX );
347+
logger.info( "Creating test archive: '{}'", archiveFile.getAbsolutePath() );
348+
349+
try ( ZipOutputStream zip = new ZipOutputStream( new FileOutputStream( archiveFile ) ) )
350+
{
351+
// Add a test file to the zip
352+
ZipEntry entry = new ZipEntry( "test-file.txt" );
353+
zip.putNextEntry( entry );
354+
355+
String testContent = "this is a test";
356+
zip.write( testContent.getBytes() );
357+
zip.closeEntry();
358+
}
359+
360+
logger.info( "Test archive created successfully: '{}'", archiveFile.getAbsolutePath() );
361+
return archiveFile;
362+
}
363+
341364
public boolean statusExists( final String buildConfigId )
342365
{
343366
return treated.containsKey( buildConfigId );

src/main/java/org/commonjava/indy/service/archive/jaxrs/ArchiveManageResources.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@
4545
import org.slf4j.LoggerFactory;
4646

4747
import java.io.File;
48+
import java.io.FileOutputStream;
4849
import java.io.IOException;
4950
import java.io.InputStream;
51+
import java.time.LocalDateTime;
52+
import java.time.format.DateTimeFormatter;
5053
import java.util.Optional;
54+
import java.util.zip.ZipEntry;
55+
import java.util.zip.ZipOutputStream;
5156

5257
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
5358
import static jakarta.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM;
@@ -213,7 +218,7 @@ public Uni<Response> deleteWithChecksum( final @PathParam( "buildConfigId" ) Str
213218
@APIResponse( responseCode = "204", description = "The workplace cleanup is finished" )
214219
@Path( "cleanup" )
215220
@DELETE
216-
public Uni<Response> delete( final @Context UriInfo uriInfo )
221+
public Uni<Response> cleanup( final @Context UriInfo uriInfo )
217222
{
218223
try
219224
{
@@ -227,4 +232,41 @@ public Uni<Response> delete( final @Context UriInfo uriInfo )
227232
}
228233
return Uni.createFrom().item( noContent().build() );
229234
}
235+
236+
@Operation( description = "Create a test archive with timestamp suffix for testing purposes" )
237+
@APIResponse( responseCode = "200", description = "Test archive created successfully" )
238+
@APIResponse( responseCode = "500", description = "Failed to create test archive - directory may not be writable" )
239+
@POST
240+
@Path( "test" )
241+
@Produces( APPLICATION_JSON )
242+
public Uni<Response> createTestArchive( final @Context UriInfo uriInfo )
243+
{
244+
try
245+
{
246+
String timestamp = LocalDateTime.now().format( DateTimeFormatter.ofPattern( "yyyyMMdd-HHmmss" ) );
247+
String testArchiveName = "test-archive-" + timestamp;
248+
249+
File archiveFile = controller.createTestArchive( testArchiveName );
250+
251+
String message = String.format( "Test archive created successfully: %s.zip", testArchiveName );
252+
logger.info( message );
253+
254+
return Uni.createFrom().item( Response.ok()
255+
.type( MediaType.APPLICATION_JSON )
256+
.entity( String.format( "{\"message\":\"%s\",\"archiveName\":\"%s.zip\"}",
257+
message, testArchiveName ) )
258+
.build() );
259+
}
260+
catch ( final IOException e )
261+
{
262+
final String message = "Failed to create test archive: " + e.getMessage();
263+
logger.error( message, e );
264+
return Uni.createFrom().item( Response.status( Response.Status.INTERNAL_SERVER_ERROR )
265+
.type( MediaType.APPLICATION_JSON )
266+
.entity( String.format( "{\"error\":\"%s\",\"exceptionType\":\"%s\"}",
267+
message.replace("\"", "\\\""),
268+
e.getClass().getSimpleName() ) )
269+
.build() );
270+
}
271+
}
230272
}

0 commit comments

Comments
 (0)