mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-09-07 18:17:09 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -28,8 +28,8 @@ package com.iluwatar.ambassador;
|
||||
* The ambassador pattern creates a helper service that sends network requests on behalf of a
|
||||
* client. It is often used in cloud-based applications to offload features of a remote service.
|
||||
*
|
||||
* <p>An ambassador service can be thought of as an out-of-process proxy that is co-located with
|
||||
* the client. Similar to the proxy design pattern, the ambassador service provides an interface for
|
||||
* <p>An ambassador service can be thought of as an out-of-process proxy that is co-located with the
|
||||
* client. Similar to the proxy design pattern, the ambassador service provides an interface for
|
||||
* another remote service. In addition to the interface, the ambassador provides extra functionality
|
||||
* and features, specifically offloaded common connectivity tasks. This usually consists of
|
||||
* monitoring, logging, routing, security etc. This is extremely useful in legacy applications where
|
||||
@@ -37,14 +37,11 @@ package com.iluwatar.ambassador;
|
||||
* capabilities.
|
||||
*
|
||||
* <p>In this example, we will the ({@link ServiceAmbassador}) class represents the ambassador while
|
||||
* the
|
||||
* ({@link RemoteService}) class represents a remote application.
|
||||
* the ({@link RemoteService}) class represents a remote application.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Entry point.
|
||||
*/
|
||||
/** Entry point. */
|
||||
public static void main(String[] args) {
|
||||
var host1 = new Client();
|
||||
var host2 = new Client();
|
||||
|
||||
@@ -26,9 +26,7 @@ package com.iluwatar.ambassador;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* A simple Client.
|
||||
*/
|
||||
/** A simple Client. */
|
||||
@Slf4j
|
||||
public class Client {
|
||||
|
||||
|
||||
@@ -29,9 +29,7 @@ import static java.lang.Thread.sleep;
|
||||
import com.iluwatar.ambassador.util.RandomProvider;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* A remote legacy application represented by a Singleton implementation.
|
||||
*/
|
||||
/** A remote legacy application represented by a Singleton implementation. */
|
||||
@Slf4j
|
||||
public class RemoteService implements RemoteServiceInterface {
|
||||
private static final int THRESHOLD = 200;
|
||||
@@ -49,9 +47,7 @@ public class RemoteService implements RemoteServiceInterface {
|
||||
this(Math::random);
|
||||
}
|
||||
|
||||
/**
|
||||
* This constructor is used for testing purposes only.
|
||||
*/
|
||||
/** This constructor is used for testing purposes only. */
|
||||
RemoteService(RandomProvider randomProvider) {
|
||||
this.randomProvider = randomProvider;
|
||||
}
|
||||
@@ -75,7 +71,8 @@ public class RemoteService implements RemoteServiceInterface {
|
||||
LOGGER.error("Thread sleep state interrupted", e);
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
return waitTime <= THRESHOLD ? value * 10
|
||||
return waitTime <= THRESHOLD
|
||||
? value * 10
|
||||
: RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.ambassador;
|
||||
|
||||
/**
|
||||
* Interface shared by ({@link RemoteService}) and ({@link ServiceAmbassador}).
|
||||
*/
|
||||
/** Interface shared by ({@link RemoteService}) and ({@link ServiceAmbassador}). */
|
||||
interface RemoteServiceInterface {
|
||||
|
||||
long doRemoteFunction(int value);
|
||||
|
||||
@@ -29,17 +29,14 @@ import lombok.Getter;
|
||||
/**
|
||||
* Holds information regarding the status of the Remote Service.
|
||||
*
|
||||
* <p> 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. </p>
|
||||
* <p>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);
|
||||
|
||||
@Getter
|
||||
private final long remoteServiceStatusValue;
|
||||
@Getter private final long remoteServiceStatusValue;
|
||||
|
||||
RemoteServiceStatus(long remoteServiceStatusValue) {
|
||||
this.remoteServiceStatusValue = remoteServiceStatusValue;
|
||||
|
||||
@@ -40,8 +40,7 @@ public class ServiceAmbassador implements RemoteServiceInterface {
|
||||
private static final int RETRIES = 3;
|
||||
private static final int DELAY_MS = 3000;
|
||||
|
||||
ServiceAmbassador() {
|
||||
}
|
||||
ServiceAmbassador() {}
|
||||
|
||||
@Override
|
||||
public long doRemoteFunction(int value) {
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.ambassador.util;
|
||||
|
||||
/**
|
||||
* An interface for randomness. Useful for testing purposes.
|
||||
*/
|
||||
/** An interface for randomness. Useful for testing purposes. */
|
||||
public interface RandomProvider {
|
||||
double random();
|
||||
}
|
||||
|
||||
@@ -24,25 +24,22 @@
|
||||
*/
|
||||
package com.iluwatar.ambassador;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** 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.
|
||||
* <p>Solution: Inserted assertion to check whether the execution of the main method in {@link
|
||||
* App} throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,13 +24,11 @@
|
||||
*/
|
||||
package com.iluwatar.ambassador;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Test for {@link Client}
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Test for {@link Client} */
|
||||
class ClientTest {
|
||||
|
||||
@Test
|
||||
@@ -38,6 +36,7 @@ class ClientTest {
|
||||
Client client = new Client();
|
||||
var result = client.useService(10);
|
||||
|
||||
assertTrue(result == 100 || result == RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue());
|
||||
assertTrue(
|
||||
result == 100 || result == RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,9 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import com.iluwatar.ambassador.util.RandomProvider;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test for {@link RemoteService}
|
||||
*/
|
||||
/** Test for {@link RemoteService} */
|
||||
class RemoteServiceTest {
|
||||
|
||||
@Test
|
||||
|
||||
@@ -24,18 +24,17 @@
|
||||
*/
|
||||
package com.iluwatar.ambassador;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Test for {@link ServiceAmbassador}
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Test for {@link ServiceAmbassador} */
|
||||
class ServiceAmbassadorTest {
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
long result = new ServiceAmbassador().doRemoteFunction(10);
|
||||
assertTrue(result == 100 || result == RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue());
|
||||
assertTrue(
|
||||
result == 100 || result == RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user