From 0a24f4f7be648effda17b4d09e6ba4284ae07a89 Mon Sep 17 00:00:00 2001 From: ruhan Date: Mon, 24 Nov 2025 09:28:50 +0700 Subject: [PATCH] [migration] Add a test api to create test zip --- .../archive/controller/ArchiveController.java | 23 ++++++++++ .../archive/jaxrs/ArchiveManageResources.java | 44 ++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/commonjava/indy/service/archive/controller/ArchiveController.java b/src/main/java/org/commonjava/indy/service/archive/controller/ArchiveController.java index 8a57548..af10932 100644 --- a/src/main/java/org/commonjava/indy/service/archive/controller/ArchiveController.java +++ b/src/main/java/org/commonjava/indy/service/archive/controller/ArchiveController.java @@ -338,6 +338,29 @@ public void cleanupBCWorkspace( String buildConfigId ) } } + public File createTestArchive( final String archiveName ) throws IOException + { + File targetDir = new File( archiveDir ); + targetDir.mkdirs(); + + final File archiveFile = new File( archiveDir, archiveName + ARCHIVE_SUFFIX ); + logger.info( "Creating test archive: '{}'", archiveFile.getAbsolutePath() ); + + try ( ZipOutputStream zip = new ZipOutputStream( new FileOutputStream( archiveFile ) ) ) + { + // Add a test file to the zip + ZipEntry entry = new ZipEntry( "test-file.txt" ); + zip.putNextEntry( entry ); + + String testContent = "this is a test"; + zip.write( testContent.getBytes() ); + zip.closeEntry(); + } + + logger.info( "Test archive created successfully: '{}'", archiveFile.getAbsolutePath() ); + return archiveFile; + } + public boolean statusExists( final String buildConfigId ) { return treated.containsKey( buildConfigId ); diff --git a/src/main/java/org/commonjava/indy/service/archive/jaxrs/ArchiveManageResources.java b/src/main/java/org/commonjava/indy/service/archive/jaxrs/ArchiveManageResources.java index 96a8c60..25270f6 100644 --- a/src/main/java/org/commonjava/indy/service/archive/jaxrs/ArchiveManageResources.java +++ b/src/main/java/org/commonjava/indy/service/archive/jaxrs/ArchiveManageResources.java @@ -45,9 +45,14 @@ import org.slf4j.LoggerFactory; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.Optional; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; import static jakarta.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM; @@ -213,7 +218,7 @@ public Uni deleteWithChecksum( final @PathParam( "buildConfigId" ) Str @APIResponse( responseCode = "204", description = "The workplace cleanup is finished" ) @Path( "cleanup" ) @DELETE - public Uni delete( final @Context UriInfo uriInfo ) + public Uni cleanup( final @Context UriInfo uriInfo ) { try { @@ -227,4 +232,41 @@ public Uni delete( final @Context UriInfo uriInfo ) } return Uni.createFrom().item( noContent().build() ); } + + @Operation( description = "Create a test archive with timestamp suffix for testing purposes" ) + @APIResponse( responseCode = "200", description = "Test archive created successfully" ) + @APIResponse( responseCode = "500", description = "Failed to create test archive - directory may not be writable" ) + @POST + @Path( "test" ) + @Produces( APPLICATION_JSON ) + public Uni createTestArchive( final @Context UriInfo uriInfo ) + { + try + { + String timestamp = LocalDateTime.now().format( DateTimeFormatter.ofPattern( "yyyyMMdd-HHmmss" ) ); + String testArchiveName = "test-archive-" + timestamp; + + File archiveFile = controller.createTestArchive( testArchiveName ); + + String message = String.format( "Test archive created successfully: %s.zip", testArchiveName ); + logger.info( message ); + + return Uni.createFrom().item( Response.ok() + .type( MediaType.APPLICATION_JSON ) + .entity( String.format( "{\"message\":\"%s\",\"archiveName\":\"%s.zip\"}", + message, testArchiveName ) ) + .build() ); + } + catch ( final IOException e ) + { + final String message = "Failed to create test archive: " + e.getMessage(); + logger.error( message, e ); + return Uni.createFrom().item( Response.status( Response.Status.INTERNAL_SERVER_ERROR ) + .type( MediaType.APPLICATION_JSON ) + .entity( String.format( "{\"error\":\"%s\",\"exceptionType\":\"%s\"}", + message.replace("\"", "\\\""), + e.getClass().getSimpleName() ) ) + .build() ); + } + } }