feat: Added Microservices UI Client side composition #2698 (#3062)

* Added Microservices client side ui composition to the repo.
Added ClientSideCompositionTest, ApiGateway, ClientSideIntegrator, FrontendComponent, CartFrontend, ProductFrontend, updated pom.xml, ReadME.md.

* Improved some checkstyle changes.

* Added Random variable to re-use the random instead of creating it everytime in FrontendComponent.

* changed the Pom.xml and upadted the ReadME.md

* Changes in the README.md File as per requirement, name of the file changed from ReadME.md to README.md

---------

Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
This commit is contained in:
Tarun Vishwakarma
2025-01-07 00:25:08 +05:30
committed by GitHub
parent c06bd2c5a1
commit ab59dfe770
10 changed files with 442 additions and 0 deletions
@@ -0,0 +1,46 @@
package com.iluwatar.clientsideuicomposition;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* ClientSideCompositionTest contains unit tests to validate dynamic route registration and UI composition.
*/
class ClientSideCompositionTest {
/**
* Tests dynamic registration of frontend components and dynamic composition of UI.
*/
@Test
void testClientSideUIComposition() {
// Create API Gateway and dynamically register frontend components
ApiGateway apiGateway = new ApiGateway();
apiGateway.registerRoute("/products", new ProductFrontend());
apiGateway.registerRoute("/cart", new CartFrontend());
// Create the Client-Side Integrator
ClientSideIntegrator integrator = new ClientSideIntegrator(apiGateway);
// Dynamically pass parameters for data fetching
Map<String, String> productParams = new HashMap<>();
productParams.put("category", "electronics");
// Compose UI for products and cart with dynamic params
integrator.composeUi("/products", productParams);
Map<String, String> cartParams = new HashMap<>();
cartParams.put("userId", "user123");
integrator.composeUi("/cart", cartParams);
// Validate the dynamically fetched data
String productData = apiGateway.handleRequest("/products", productParams);
String cartData = apiGateway.handleRequest("/cart", cartParams);
assertTrue(productData.contains("Product List for category 'electronics'"));
assertTrue(cartData.contains("Shopping Cart for user 'user123'"));
}
}