Fixed most reported issues by SonarCloud.
This commit is contained in:
@ -62,7 +62,7 @@ public class RemoteService implements RemoteServiceInterface {
|
||||
*
|
||||
* @param value integer value to be multiplied.
|
||||
* @return if waitTime is less than {@link RemoteService#THRESHOLD}, it returns value * 10,
|
||||
* otherwise {@link RemoteServiceInterface#FAILURE}.
|
||||
* otherwise {@link RemoteServiceStatus#FAILURE}.
|
||||
*/
|
||||
@Override
|
||||
public long doRemoteFunction(int value) {
|
||||
@ -74,6 +74,6 @@ public class RemoteService implements RemoteServiceInterface {
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error("Thread sleep state interrupted", e);
|
||||
}
|
||||
return waitTime <= THRESHOLD ? value * 10 : FAILURE;
|
||||
return waitTime <= THRESHOLD ? value * 10 : RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue();
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,6 @@ package com.iluwatar.ambassador;
|
||||
* Interface shared by ({@link RemoteService}) and ({@link ServiceAmbassador}).
|
||||
*/
|
||||
interface RemoteServiceInterface {
|
||||
int FAILURE = -1;
|
||||
|
||||
long doRemoteFunction(int value);
|
||||
}
|
||||
|
@ -0,0 +1,23 @@
|
||||
package com.iluwatar.ambassador;
|
||||
|
||||
/**
|
||||
* Holds information regarding the status of the Remote Service.
|
||||
*
|
||||
* !Attention - This Enum replaces the integer value previously stored in {@link RemoteServiceInterface}
|
||||
* as SonarCloud was identifying it as an issue. All test cases have been checked after changes, without failures.
|
||||
*/
|
||||
|
||||
public enum RemoteServiceStatus {
|
||||
FAILURE(-1)
|
||||
;
|
||||
|
||||
private final long remoteServiceStatusValue;
|
||||
|
||||
RemoteServiceStatus(long remoteServiceStatusValue) {
|
||||
this.remoteServiceStatusValue = remoteServiceStatusValue;
|
||||
}
|
||||
|
||||
public long getRemoteServiceStatusValue() {
|
||||
return remoteServiceStatusValue;
|
||||
}
|
||||
}
|
@ -23,6 +23,7 @@
|
||||
|
||||
package com.iluwatar.ambassador;
|
||||
|
||||
import static com.iluwatar.ambassador.RemoteServiceStatus.FAILURE;
|
||||
import static java.lang.Thread.sleep;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
@ -58,14 +59,14 @@ public class ServiceAmbassador implements RemoteServiceInterface {
|
||||
|
||||
private long safeCall(int value) {
|
||||
var retries = 0;
|
||||
var result = (long) FAILURE;
|
||||
var result = FAILURE.getRemoteServiceStatusValue();
|
||||
|
||||
for (int i = 0; i < RETRIES; i++) {
|
||||
if (retries >= RETRIES) {
|
||||
return FAILURE;
|
||||
return FAILURE.getRemoteServiceStatusValue();
|
||||
}
|
||||
|
||||
if ((result = checkLatency(value)) == FAILURE) {
|
||||
if ((result = checkLatency(value)) == FAILURE.getRemoteServiceStatusValue()) {
|
||||
LOGGER.info("Failed to reach remote: (" + (i + 1) + ")");
|
||||
retries++;
|
||||
try {
|
||||
|
@ -25,13 +25,23 @@ package com.iluwatar.ambassador;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@ -37,6 +37,6 @@ class ClientTest {
|
||||
Client client = new Client();
|
||||
var result = client.useService(10);
|
||||
|
||||
assertTrue(result == 100 || result == RemoteService.FAILURE);
|
||||
assertTrue(result == 100 || result == RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue());
|
||||
}
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ class RemoteServiceTest {
|
||||
void testFailedCall() {
|
||||
var remoteService = new RemoteService(new StaticRandomProvider(0.21));
|
||||
var result = remoteService.doRemoteFunction(10);
|
||||
assertEquals(RemoteServiceInterface.FAILURE, result);
|
||||
assertEquals(RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue(), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -35,6 +35,6 @@ class ServiceAmbassadorTest {
|
||||
@Test
|
||||
void test() {
|
||||
long result = new ServiceAmbassador().doRemoteFunction(10);
|
||||
assertTrue(result == 100 || result == RemoteServiceInterface.FAILURE);
|
||||
assertTrue(result == 100 || result == RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user