|
| 1 | +/* |
| 2 | + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license |
| 3 | + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template |
| 4 | + */ |
| 5 | +package org.jtbank.codeant.codeant_demo.controller; |
| 6 | + |
| 7 | +import java.util.HashMap; |
| 8 | +import org.apache.logging.log4j.Level; |
| 9 | +import org.apache.logging.log4j.LogManager; |
| 10 | +import org.apache.logging.log4j.Logger; |
| 11 | +import org.springframework.http.ResponseEntity; |
| 12 | +import org.springframework.web.bind.annotation.GetMapping; |
| 13 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 14 | +import org.springframework.web.bind.annotation.RestController; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Iterator; |
| 17 | +import java.util.Random; |
| 18 | + |
| 19 | +/** |
| 20 | + * |
| 21 | + * @author JT0020031_admin |
| 22 | + */ |
| 23 | +//Constant name should not be found |
| 24 | +//Object array passed to varargs should be found |
| 25 | +//Unnecessary String assignment instead of direct return should be found |
| 26 | +//Unnecessary throws Exception should be found |
| 27 | +//Generic exception instead of a specific one should be found |
| 28 | +//Unnecessary boxing of a boolean value should be found |
| 29 | +//Parametrized value for HashMap should be found, eg. new HashMap<>(); |
| 30 | +//Equals and to lowercase should be modified to equailsIgnoreCase call should be found |
| 31 | +//Unused imports should be found |
| 32 | +//Recommendation to switch to SecureRandom from Random should be found |
| 33 | +@RestController |
| 34 | +@RequestMapping("/codeant") |
| 35 | +public class CodeantController { |
| 36 | + private final Logger LOGGER = LogManager.getLogger(CodeantController.class); |
| 37 | + private static final HashMap<String, String> API_ENDPOINTS = new HashMap(); |
| 38 | + |
| 39 | + @GetMapping("/test-method") |
| 40 | + public String testMethod() throws Exception { |
| 41 | + LOGGER.log(Level.DEBUG, "{} {} {} {} {}{}", new Object[]{ |
| 42 | + "This", |
| 43 | + "is", |
| 44 | + "a", |
| 45 | + "test", |
| 46 | + "message", |
| 47 | + "." |
| 48 | + } |
| 49 | + ); |
| 50 | + return getTestMethodResponse(); |
| 51 | + } |
| 52 | + |
| 53 | + @GetMapping("/generic-exception") |
| 54 | + public ResponseEntity<Exception> genericException(){ |
| 55 | + try |
| 56 | + { |
| 57 | + getGenericException(); |
| 58 | + return ResponseEntity.ok().build(); |
| 59 | + } |
| 60 | + catch(Exception ex) |
| 61 | + { |
| 62 | + return ResponseEntity.internalServerError().body(ex); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @GetMapping("/evaluate-condition") |
| 67 | + public void conditionEval(){ |
| 68 | + evaluateCondition(Boolean.TRUE); |
| 69 | + } |
| 70 | + |
| 71 | + @GetMapping("/string-compare") |
| 72 | + private ResponseEntity<Boolean> compareString(){ |
| 73 | + return ResponseEntity.ok(checkStrings("a", "B")); |
| 74 | + } |
| 75 | + |
| 76 | + @GetMapping |
| 77 | + private ResponseEntity<Integer> getRandomNumber(){ |
| 78 | + return ResponseEntity.ok(randomNumber()); |
| 79 | + } |
| 80 | + |
| 81 | + private String getTestMethodResponse() throws Exception { |
| 82 | + String returnVal = ""; |
| 83 | + returnVal = "This is a test message"; |
| 84 | + return returnVal; |
| 85 | + } |
| 86 | + |
| 87 | + private void getGenericException() throws Exception { |
| 88 | + int val = 1 + 1; |
| 89 | + if(val == 2) |
| 90 | + { |
| 91 | + throw new Exception("Generic Exception"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private void evaluateCondition(Boolean condition){ |
| 96 | + if(condition) |
| 97 | + { |
| 98 | + //This log should not be found |
| 99 | + LOGGER.log(Level.DEBUG, "Condition {} is evaluated as {}", condition, condition); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private boolean checkStrings(String a, String b){ |
| 104 | + return a.toLowerCase().equals(b.toLowerCase()); |
| 105 | + } |
| 106 | + |
| 107 | + private int randomNumber(){ |
| 108 | + Random r = new Random(); |
| 109 | + return r.nextInt(); |
| 110 | + } |
| 111 | +} |
0 commit comments