Repository: iluwatar/java-design-patterns
Issue: SonarCloud reports bugs
Labels: type: bug
info: good first issue
type: refactoring
epic: code quality

The project is using SonarCloud static code analysis. The latest results are showing that it has found several blocker and critical severity bugs. In this task those are fixed or marked as false positives. To mark false positives, `SuppressWarnings` annotation should be used in code as explained in the linked documentation.

Links:
[Blocker and critical severity bugs in SonarCloud](https://sonarcloud.io/project/issues?id=iluwatar_java-design-patterns&resolved=false&severities=BLOCKER%2CCRITICAL&types=BUG)
[Sonar false positives](https://docs.sonarqube.org/latest/analysis/languages/java/#JavaFAQ-HowtoremoveFalse-Positiveissues)


Issue comments:
I started taking a look at these errors and I think the first one will be a false positive. Specifically the rule: "Loops should not be infinite" in the file "api-gateway/.../java/com/iluwatar/api/gateway/ApiGateway.java" Based on the documentation in the above link, it seems like there are two options:

1. Add the following annotation: 
`@SuppressWarnings("squid:S2189")`
but this causes a Java compile warning that the squid:S2189 is not a valid warning. 
1. Mark the issue in the SonarCloud: I think this is probably what we should do, but I didn't want to act until this was discussed. 
Hmm where do you see the error after adding the annotation? Is it in the Maven output or IDE? Tried that with Maven but did not get any warnings.
I was seeing it on the IDE. (I keep stumbling on the Eclipse annoyances).
If it's not showing up in Maven, I'll just leave it as is.

On Fri, Oct 18, 2019, 15:27 Ilkka Seppälä <notifications@github.com> wrote:

> Hmm where do you see the error after adding the annotation? Is it in the
> Maven output or IDE? Tried that with Maven but did not get any warnings.
>
> —
> You are receiving this because you were assigned.
> Reply to this email directly, view it on GitHub
> <https://github.com/iluwatar/java-design-patterns/issues/1010?email_source=notifications&email_token=AAIPXDJQWZL3VOFKABBSPFDQPIE3PA5CNFSM4JA74VX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBVT6KY#issuecomment-543899435>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAIPXDOIZNQYS3KET7TOGDLQPIE3PANCNFSM4JA74VXQ>
> .
>

Ok, let's try to proceed with `SuppressWarnings`
It turns out the warning is a setting in Eclipse. Selecting Window->Preferences and navigating to the Java compiler options and changing the value of: 
`Unhandled Token in @SuppressWarnings` to ignore. By default this is warn.
![image](https://user-images.githubusercontent.com/1112973/67146172-7117b880-f256-11e9-857e-14e31bd7e0c1.png)


I just pushed #1020 to address. 
The latest scan shows two critical issues left. See the screenshot:

![image](https://user-images.githubusercontent.com/582346/67150293-6f81db00-f2be-11e9-84b3-4ce1c3b96310.png)

That's weird. I know I made those changes. I'll make sure they weren't
missed in the commit.

On Sat, Oct 19, 2019, 15:22 Ilkka Seppälä <notifications@github.com> wrote:

> The latest scan shows two critical issues left. See the screenshot:
>
> [image: image]
> <https://user-images.githubusercontent.com/582346/67150293-6f81db00-f2be-11e9-84b3-4ce1c3b96310.png>
>
> —
> You are receiving this because you were assigned.
> Reply to this email directly, view it on GitHub
> <https://github.com/iluwatar/java-design-patterns/issues/1010?email_source=notifications&email_token=AAIPXDIR7FTHU74PQWZWNFDQPNM73A5CNFSM4JA74VX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBX2YXA#issuecomment-544189532>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAIPXDKPVAIB6KTEWNZNFHDQPNM73ANCNFSM4JA74VXQ>
> .
>


Pull request review comments:

Patch that resolved the issue:
diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/sampledata/SampleData.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/sampledata/SampleData.java
index bc4e963e1808..29a9d3233889 100644
--- a/hexagonal/src/main/java/com/iluwatar/hexagonal/sampledata/SampleData.java
+++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/sampledata/SampleData.java
@@ -102,8 +102,7 @@ public static void submitTickets(LotteryService lotteryService, int numTickets)
   }
 
   private static PlayerDetails getRandomPlayerDetails() {
-    Random random = new Random();
-    int idx = random.nextInt(PLAYERS.size());
+    int idx = RANDOM.nextInt(PLAYERS.size());
     return PLAYERS.get(idx);
   }
 }
diff --git a/typeobjectpattern/src/main/java/com/iluwatar/typeobject/CellPool.java b/typeobjectpattern/src/main/java/com/iluwatar/typeobject/CellPool.java
index e04cce4f018a..6014da0cf1ce 100644
--- a/typeobjectpattern/src/main/java/com/iluwatar/typeobject/CellPool.java
+++ b/typeobjectpattern/src/main/java/com/iluwatar/typeobject/CellPool.java
@@ -71,8 +71,7 @@ Cell getNewCell() {
   }
   
   void addNewCell(Cell c) {
-    Random rand = new Random();
-    c.candy = randomCode[rand.nextInt(randomCode.length)]; //changing candytype to new
+    c.candy = randomCode[RANDOM.nextInt(randomCode.length)]; //changing candytype to new
     this.pool.add(c);
     pointer++;
   }


Test patch: