Skip to content

Commit 9e7a500

Browse files
npathaiiluwatar
authored andcommitted
Refactoring changes in Ambassador Pattern (iluwatar#805)
* 1) Updated test cases to use Junit Assert method as compared to assert keyword 2) Proper testing of RemoteService using RandomProvider interface. Introduced RandomProvider interface so that randomness can be controlled from test cases. 3) For readability used constant for representing FAILURE * Addressing review comments, Deleting unintentional file and used FAILURE constant in ClientTest as well
1 parent 21a149e commit 9e7a500

File tree

7 files changed

+65
-14
lines changed

7 files changed

+65
-14
lines changed

ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
package com.iluwatar.ambassador;
2424

25+
import com.iluwatar.ambassador.util.RandomProvider;
2526
import org.slf4j.Logger;
2627
import org.slf4j.LoggerFactory;
2728

@@ -31,9 +32,10 @@
3132
* A remote legacy application represented by a Singleton implementation.
3233
*/
3334
public class RemoteService implements RemoteServiceInterface {
34-
35+
static final int THRESHOLD = 200;
3536
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
3637
private static RemoteService service = null;
38+
private final RandomProvider randomProvider;
3739

3840
static synchronized RemoteService getRemoteService() {
3941
if (service == null) {
@@ -42,24 +44,33 @@ static synchronized RemoteService getRemoteService() {
4244
return service;
4345
}
4446

45-
private RemoteService() {}
47+
private RemoteService() {
48+
this(Math::random);
49+
}
4650

51+
/**
52+
* This constuctor is used for testing purposes only.
53+
*/
54+
RemoteService(RandomProvider randomProvider) {
55+
this.randomProvider = randomProvider;
56+
}
4757
/**
4858
* Remote function takes a value and multiplies it by 10 taking a random amount of time.
4959
* Will sometimes return -1. This imitates connectivity issues a client might have to account for.
5060
* @param value integer value to be multiplied.
51-
* @return if waitTime is more than 200ms, it returns value * 10, otherwise -1.
61+
* @return if waitTime is less than {@link RemoteService#THRESHOLD}, it returns value * 10,
62+
* otherwise {@link RemoteServiceInterface#FAILURE}.
5263
*/
5364
@Override
5465
public long doRemoteFunction(int value) {
5566

56-
long waitTime = (long) Math.floor(Math.random() * 1000);
67+
long waitTime = (long) Math.floor(randomProvider.random() * 1000);
5768

5869
try {
5970
sleep(waitTime);
6071
} catch (InterruptedException e) {
6172
LOGGER.error("Thread sleep state interrupted", e);
6273
}
63-
return waitTime >= 200 ? value * 10 : -1;
74+
return waitTime <= THRESHOLD ? value * 10 : FAILURE;
6475
}
6576
}

ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceInterface.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* Interface shared by ({@link RemoteService}) and ({@link ServiceAmbassador}).
2727
*/
2828
interface RemoteServiceInterface {
29-
29+
int FAILURE = -1;
30+
3031
long doRemoteFunction(int value) throws Exception;
3132
}

ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ private long checkLatency(int value) {
5959
private long safeCall(int value) {
6060

6161
int retries = 0;
62-
long result = -1;
62+
long result = FAILURE;
6363

6464
for (int i = 0; i < RETRIES; i++) {
6565

6666
if (retries >= RETRIES) {
67-
return -1;
67+
return FAILURE;
6868
}
6969

70-
if ((result = checkLatency(value)) == -1) {
70+
if ((result = checkLatency(value)) == FAILURE) {
7171
LOGGER.info("Failed to reach remote: (" + (i + 1) + ")");
7272
retries++;
7373
try {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.iluwatar.ambassador.util;
2+
3+
/**
4+
* An interface for randomness. Useful for testing purposes.
5+
*/
6+
public interface RandomProvider {
7+
double random();
8+
}

ambassador/src/test/java/com/iluwatar/ambassador/ClientTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import org.junit.jupiter.api.Test;
2626

27+
import static org.junit.jupiter.api.Assertions.assertTrue;
28+
2729
/**
2830
* Test for {@link Client}
2931
*/
@@ -35,6 +37,6 @@ public void test() {
3537
Client client = new Client();
3638
long result = client.useService(10);
3739

38-
assert result == 100 || result == -1;
40+
assertTrue(result == 100 || result == RemoteService.FAILURE);
3941
}
4042
}

ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,43 @@
2222
*/
2323
package com.iluwatar.ambassador;
2424

25+
import com.iluwatar.ambassador.util.RandomProvider;
2526
import org.junit.jupiter.api.Test;
2627

28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
2731
/**
2832
* Test for {@link RemoteService}
2933
*/
3034
public class RemoteServiceTest {
3135

3236
@Test
33-
public void test() {
34-
long result = RemoteService.getRemoteService().doRemoteFunction(10);
35-
assert result == 100 || result == -1;
37+
public void testFailedCall() {
38+
RemoteService remoteService = new RemoteService(
39+
new StaticRandomProvider(0.21));
40+
long result = remoteService.doRemoteFunction(10);
41+
assertEquals(RemoteServiceInterface.FAILURE, result);
42+
}
43+
44+
@Test
45+
public void testSuccessfulCall() {
46+
RemoteService remoteService = new RemoteService(
47+
new StaticRandomProvider(0.2));
48+
long result = remoteService.doRemoteFunction(10);
49+
assertEquals(100, result);
50+
}
51+
52+
private class StaticRandomProvider implements RandomProvider {
53+
private double value;
54+
55+
StaticRandomProvider(double value) {
56+
this.value = value;
57+
}
58+
59+
@Override
60+
public double random() {
61+
return value;
62+
}
3663
}
3764
}

ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import org.junit.jupiter.api.Test;
2626

27+
import static org.junit.jupiter.api.Assertions.assertTrue;
28+
2729
/**
2830
* Test for {@link ServiceAmbassador}
2931
*/
@@ -32,6 +34,6 @@ public class ServiceAmbassadorTest {
3234
@Test
3335
public void test() {
3436
long result = new ServiceAmbassador().doRemoteFunction(10);
35-
assert result == 100 || result == -1;
37+
assertTrue(result == 100 || result == RemoteServiceInterface.FAILURE);
3638
}
3739
}

0 commit comments

Comments
 (0)