diff --git a/src/main/java/DNAnalyzer/adapter/StatusController.java b/src/main/java/DNAnalyzer/adapter/StatusController.java index 421662d5..e21f54c8 100644 --- a/src/main/java/DNAnalyzer/adapter/StatusController.java +++ b/src/main/java/DNAnalyzer/adapter/StatusController.java @@ -1,22 +1,22 @@ package DNAnalyzer.adapter; +import java.util.Map; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.Map; @RestController @RequestMapping("/api") @CrossOrigin(origins = "*") public class StatusController { - - @GetMapping("/status") - public ResponseEntity getStatus() { - return ResponseEntity.ok(Map.of( + + @GetMapping("/status") + public ResponseEntity getStatus() { + return ResponseEntity.ok( + Map.of( "status", "online", - "version", "1.0.0" - )); - } -} \ No newline at end of file + "version", "1.0.0")); + } +} diff --git a/src/main/java/DNAnalyzer/web/AnalyzerController.java b/src/main/java/DNAnalyzer/web/AnalyzerController.java index 92136b8f..9bef8881 100644 --- a/src/main/java/DNAnalyzer/web/AnalyzerController.java +++ b/src/main/java/DNAnalyzer/web/AnalyzerController.java @@ -1,10 +1,11 @@ package DNAnalyzer.web; +import DNAnalyzer.core.DNAAnalysis; +import DNAnalyzer.utils.core.DNATools; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.nio.file.Files; import java.nio.file.Path; - import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; @@ -14,113 +15,110 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; -import DNAnalyzer.core.DNAAnalysis; -import DNAnalyzer.utils.core.DNATools; - @RestController @RequestMapping("/api") @CrossOrigin(origins = "*") public class AnalyzerController { - @PostMapping(value = "/analyze", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - public ResponseEntity analyzeDNA( - @RequestParam("dnaFile") MultipartFile dnaFile, - @RequestParam(value = "amino", defaultValue = "M") String amino, - @RequestParam(value = "minCount", defaultValue = "1") int minCount, - @RequestParam(value = "maxCount", defaultValue = "100") int maxCount, - @RequestParam(value = "reverse", defaultValue = "false") boolean reverse, - @RequestParam(value = "rcomplement", defaultValue = "false") boolean rcomplement, - @RequestParam(value = "codons", defaultValue = "false") boolean codons, - @RequestParam(value = "coverage", defaultValue = "false") boolean coverage, - @RequestParam(value = "longest", defaultValue = "false") boolean longest, - @RequestParam(value = "format", defaultValue = "text") String format) { - - try { - // Create temporary file - Path tempFile = Files.createTempFile("dna-", ".fa"); - dnaFile.transferTo(tempFile.toFile()); - - // Capture console output - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - PrintStream ps = new PrintStream(baos); - PrintStream old = System.out; - System.setOut(ps); - - try { - // Read DNA sequence - String dnaSequence = new String(Files.readAllBytes(tempFile)); - - // Run analysis - DNAAnalysis analysis = new DNAAnalysis(new DNATools(dnaSequence), null, amino); - - if (reverse) { - analysis = analysis.reverseDna(); - } - if (rcomplement) { - analysis = analysis.reverseComplement(); - } - - if (codons) { - analysis.outPutCodons(minCount, maxCount, System.out); - } - if (coverage) { - analysis.printHighCoverageRegions(System.out); - } - if (longest) { - analysis.printLongestProtein(System.out); - } - - // Get output - String output = baos.toString(); - - // Format output - if ("json".equals(format)) { - output = formatAsJson(output); - } else if ("csv".equals(format)) { - output = formatAsCsv(output); - } - - return ResponseEntity.ok(output); - - } finally { - // Restore console output and cleanup - System.setOut(old); - Files.deleteIfExists(tempFile); - } - - } catch (Exception e) { - return ResponseEntity.badRequest().body("Error analyzing DNA: " + e.getMessage()); + @PostMapping(value = "/analyze", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity analyzeDNA( + @RequestParam("dnaFile") MultipartFile dnaFile, + @RequestParam(value = "amino", defaultValue = "M") String amino, + @RequestParam(value = "minCount", defaultValue = "1") int minCount, + @RequestParam(value = "maxCount", defaultValue = "100") int maxCount, + @RequestParam(value = "reverse", defaultValue = "false") boolean reverse, + @RequestParam(value = "rcomplement", defaultValue = "false") boolean rcomplement, + @RequestParam(value = "codons", defaultValue = "false") boolean codons, + @RequestParam(value = "coverage", defaultValue = "false") boolean coverage, + @RequestParam(value = "longest", defaultValue = "false") boolean longest, + @RequestParam(value = "format", defaultValue = "text") String format) { + + try { + // Create temporary file + Path tempFile = Files.createTempFile("dna-", ".fa"); + dnaFile.transferTo(tempFile.toFile()); + + // Capture console output + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + PrintStream old = System.out; + System.setOut(ps); + + try { + // Read DNA sequence + String dnaSequence = new String(Files.readAllBytes(tempFile)); + + // Run analysis + DNAAnalysis analysis = new DNAAnalysis(new DNATools(dnaSequence), null, amino); + + if (reverse) { + analysis = analysis.reverseDna(); + } + if (rcomplement) { + analysis = analysis.reverseComplement(); } - } - private String formatAsJson(String output) { - // Convert output to JSON format - StringBuilder json = new StringBuilder(); - json.append("{\"results\": ["); - - String[] lines = output.split("\n"); - for (int i = 0; i < lines.length; i++) { - json.append("\"").append(lines[i].replace("\"", "\\\"")).append("\""); - if (i < lines.length - 1) { - json.append(","); - } + if (codons) { + analysis.outPutCodons(minCount, maxCount, System.out); + } + if (coverage) { + analysis.printHighCoverageRegions(System.out); + } + if (longest) { + analysis.printLongestProtein(System.out); } - - json.append("]}"); - return json.toString(); - } - private String formatAsCsv(String output) { - // Convert output to CSV format - StringBuilder csv = new StringBuilder(); - - String[] lines = output.split("\n"); - for (String line : lines) { - // Replace any commas in the data with semicolons - line = line.replace(",", ";"); - csv.append(line).append(",\n"); + // Get output + String output = baos.toString(); + + // Format output + if ("json".equals(format)) { + output = formatAsJson(output); + } else if ("csv".equals(format)) { + output = formatAsCsv(output); } - - return csv.toString(); + + return ResponseEntity.ok(output); + + } finally { + // Restore console output and cleanup + System.setOut(old); + Files.deleteIfExists(tempFile); + } + + } catch (Exception e) { + return ResponseEntity.badRequest().body("Error analyzing DNA: " + e.getMessage()); + } + } + + private String formatAsJson(String output) { + // Convert output to JSON format + StringBuilder json = new StringBuilder(); + json.append("{\"results\": ["); + + String[] lines = output.split("\n"); + for (int i = 0; i < lines.length; i++) { + json.append("\"").append(lines[i].replace("\"", "\\\"")).append("\""); + if (i < lines.length - 1) { + json.append(","); + } } -} \ No newline at end of file + + json.append("]}"); + return json.toString(); + } + + private String formatAsCsv(String output) { + // Convert output to CSV format + StringBuilder csv = new StringBuilder(); + + String[] lines = output.split("\n"); + for (String line : lines) { + // Replace any commas in the data with semicolons + line = line.replace(",", ";"); + csv.append(line).append(",\n"); + } + + return csv.toString(); + } +} diff --git a/src/test/java/DNAnalyzer/core/ApiKeyServiceTest.java b/src/test/java/DNAnalyzer/core/ApiKeyServiceTest.java index 2e8b7120..d0f9f08a 100644 --- a/src/test/java/DNAnalyzer/core/ApiKeyServiceTest.java +++ b/src/test/java/DNAnalyzer/core/ApiKeyServiceTest.java @@ -1,27 +1,28 @@ package DNAnalyzer.core; import static org.junit.jupiter.api.Assertions.*; + import org.junit.jupiter.api.Test; class ApiKeyServiceTest { - private ApiKeyService apiKeyService = new ApiKeyService(); + private ApiKeyService apiKeyService = new ApiKeyService(); - @Test - void shouldSetAndGetApiKey() { - String newKey = "test-api-key"; - apiKeyService.setApiKey(newKey); - assertEquals(newKey, apiKeyService.getApiKey()); - } + @Test + void shouldSetAndGetApiKey() { + String newKey = "test-api-key"; + apiKeyService.setApiKey(newKey); + assertEquals(newKey, apiKeyService.getApiKey()); + } - @Test - void shouldReturnFalseWhenNoKeySet() { - assertFalse(apiKeyService.hasApiKey()); - } + @Test + void shouldReturnFalseWhenNoKeySet() { + assertFalse(apiKeyService.hasApiKey()); + } - @Test - void shouldReturnTrueWhenKeySet() { - apiKeyService.setApiKey("test-key"); - assertTrue(apiKeyService.hasApiKey()); - } + @Test + void shouldReturnTrueWhenKeySet() { + apiKeyService.setApiKey("test-key"); + assertTrue(apiKeyService.hasApiKey()); + } }