JSON-RPC || BDD-Serenity


 JSON-RPC (Remote Procedure Call) is a lightweight remote procedure call protocol encoded in JSON (JavaScript Object Notation). It allows communication between a client and a server over a network by invoking methods or procedures on the server and receiving the results.

JSON-RPC is a stateless protocol that operates over HTTP or other transport protocols. It is designed to be simple and language-agnostic, making it easy to implement and use in various programming languages.

Here are some key aspects of JSON-RPC:

Request and Response Format: JSON-RPC messages are structured as JSON objects. The request message includes the method name to be invoked and any required parameters. The response message contains the result of the method execution or an error if applicable.

Methods and Procedures: JSON-RPC supports remote procedure calls, where the client invokes methods or procedures on the server. The methods are identified by a string name and can accept parameters. The server executes the requested method and returns the result to the client.

Transport Protocol: JSON-RPC can be used over various transport protocols such as HTTP, WebSockets, or TCP. It uses the underlying transport to send the JSON-RPC requests and receive responses.

Batch Processing: JSON-RPC supports batching, where multiple requests can be sent together as an array. The server processes the requests in the order received and returns an array of responses corresponding to each request.

Error Handling: JSON-RPC provides a standardized way to handle errors. If an error occurs during method execution, the server returns an error response with a specific error code and error message.

Notification Calls: JSON-RPC also supports notification calls, where the client can send a request to the server without expecting a response. Notifications are one-way messages used for events or notifications that do not require a reply.

JSON-RPC is widely used in various scenarios, including web APIs, microservices, and distributed systems. It offers a lightweight and language-agnostic approach for remote procedure calls, allowing systems built on different technologies to communicate seamlessly.



To use JSON-RPC in Java, you can follow these steps:

  1. Include Required Dependencies: Add the necessary dependencies to your Java project. One commonly used library for JSON-RPC in Java is jsonrpc4j. You can include it in your project by adding the appropriate Maven or Gradle dependency to your build configuration.

  2. Create Interface: Define an interface that represents the methods available for remote procedure calls. Each method in the interface should have corresponding annotations to specify the method name and parameters.

    public interface MyService {
    @JsonRpcMethod("method_name")
    int add(int a, int b);
    }

    1. Implement the Interface: Create a class that implements the interface defined in the previous step. This class will contain the actual implementation of the methods.
    public class MyServiceImpl implements MyService {
    @Override
    public int add(int a, int b) {
    return a + b;
    }
    }

  1. Expose the Service: Set up an HTTP server to expose the service and handle incoming JSON-RPC requests. You can use a library like com.sun.net.httpserver.HttpServer or a framework like Spring Boot.
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/jsonrpc", new MyServiceHandler(new MyServiceImpl()));
server.start();
  1. Implement the Handler: Implement a handler class that processes incoming JSON-RPC requests and delegates the method calls to the corresponding service implementation.

public class MyServiceHandler implements HttpHandler {
private final MyService myService;

public MyServiceHandler(MyService myService) {
this.myService = myService;
}

@Override
public void handle(HttpExchange exchange) throws IOException {
JSONRPC2Request request = JSONRPC2Request.parse(exchange.getRequestBody());

String methodName = request.getMethod();
List<Object> params = request.getPositionalParams();

Object result = null;
if (methodName.equals("method_name")) {
int a = (int) params.get(0);
int b = (int) params.get(1);
result = myService.add(a, b);
}

JSONRPC2Response response = new JSONRPC2Response(result, request.getID());
exchange.sendResponseHeaders(200, 0);
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(response.toJSONString().getBytes());
outputStream.close();
}
}
  1. Make JSON-RPC Requests: On the client-side, you can use a JSON-RPC library to make requests to the server. For example, using jsonrpc4j, you can create a proxy to invoke the remote methods.
JSONRPC2Session client = new JSONRPC2Session(new URL("http://localhost:8080/jsonrpc"));
MyService service = client.open(MyService.class);
int result = service.add(5, 3);
System.out.println(result); // Output: 8

These steps demonstrate a basic setup for using JSON-RPC in Java. You can customize and extend the implementation based on your specific requirements.


BDD-SERENITY:-

Git-Hub:-
https://github.com/serenity-bdd

In Serenity, requirements are organized into three levels:

  • capabilities
  • features
  • stories

Each test tells a simple user story, which is carried out using certain Step.

To run our Serenity tests with JUnit, we need to @RunWith the SerenityRunner, test runner. SerenityRunner instruments the step libraries and ensures that the test results will be recorded and reported on by the Serenity reporters.

3.1. Maven Dependencies

To make use of Serenity with JUnit, we should include serenity-core and serenity-junit in the pom.xml:

<dependency>
    <groupId>net.serenity-bdd</groupId>
    <artifactId>serenity-core</artifactId>
    <version>1.9.0</version>
</dependency>
<dependency>
    <groupId>net.serenity-bdd</groupId>
    <artifactId>serenity-junit</artifactId>
    <version>1.9.0</version>
</dependency>

We also need serenity-maven-plugin to have reports aggregated from test results:

<plugin>
    <groupId>net.serenity-bdd.maven.plugins</groupId>
    <artifactId>serenity-maven-plugin</artifactId>
    <version>3.6.12</version>
    <executions>
        <execution>
            <id>serenity-reports</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>aggregate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

If we want Serenity to generate reports even if there's a test failure, add the following to the pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20</version>
    <configuration>
        <testFailureIgnore>true</testFailureIgnore>
    </configuration>
</plugin>

https://www.baeldung.com/serenity-bdd











Comments

Popular posts from this blog

DataPipeline-with-DocumentAI-Form-Parser