Monday, March 18, 2024

What are the best practices for securing communication between microservices in a Java ecosystem

 

Securing communication between microservices in a Java ecosystem is crucial to protect sensitive data, prevent unauthorized access, and ensure the integrity and confidentiality of communication. Here are some best practices for achieving secure communication between microservices:






1. Transport Layer Security (TLS):

Use TLS/SSL for encrypting data transmitted over the network.

Enable HTTPS for RESTful APIs to ensure data confidentiality and integrity.

Configure mutual TLS (mTLS) for two-way authentication between services, where both client and server authenticate each other using certificates.

2. Service-to-Service Authentication:

Implement secure authentication mechanisms between microservices.

Use tokens (like JWT) or OAuth tokens for authentication and authorization.

Validate incoming tokens for each request to ensure only authorized services can access endpoints.

3. Role-Based Access Control (RBAC):

Implement RBAC to control access to microservice endpoints.

Define roles and permissions for each microservice, allowing only authorized users or services to perform specific actions.

4. API Gateway:

Use an API gateway to centralize security concerns and provide a single entry point for microservices.

Implement authentication, authorization, rate limiting, and request validation at the API gateway level.

5. Secure Service Discovery:

When using service discovery mechanisms like Eureka or Consul, ensure that service registration and discovery are secure.

Use authentication and encryption for communication between service registry and microservices.

6. Secure Configuration Management:

Store sensitive configuration properties (such as passwords, API keys) securely.

Use tools like Spring Cloud Config Server with encryption to manage and distribute configuration securely.

7. Secure Logging and Monitoring:

Implement secure logging practices to avoid logging sensitive information.

Use log encryption and centralized log management tools to monitor and detect security incidents.

8. Implement Content Validation:

Validate and sanitize input data to prevent injection attacks (e.g., SQL injection, XSS).

Use input validation libraries like Hibernate Validator or Bean Validation.

9. Container Security:

If deploying microservices in containers, ensure container images are scanned for vulnerabilities.

Implement least privilege principles for container permissions and avoid running containers with unnecessary privileges.

10. Use of Secure Protocols:

Avoid using insecure protocols such as HTTP and use HTTPS/TLS for secure communication.

Use protocols with strong security features like OAuth 2.0 for authentication and authorization.

11. Secure Message Queues:

If using message brokers (like RabbitMQ, Apache Kafka), ensure they are secured.

Use TLS/SSL for communication with the message broker.

Implement message encryption for sensitive data.

12. Data Encryption:

Encrypt sensitive data at rest and in transit.

Use libraries like Java Cryptography Architecture (JCA) or Bouncy Castle for encryption/decryption.

13. API Versioning and Deprecation:

Implement API versioning to manage changes in microservices.

Securely deprecate and remove old APIs to prevent security vulnerabilities in outdated endpoints.

14. Regular Security Audits and Penetration Testing:

Conduct regular security audits and vulnerability scans of microservices.

Perform penetration testing to identify potential security weaknesses and address them proactively.

15. Continuous Security Monitoring:

Implement continuous security monitoring using tools like Prometheus, Grafana, or ELK stack.

Monitor for suspicious activities, abnormal behaviors, or unauthorized access attempts.

Implement OAuth 2.0 Authorization Server and Resource Server in your microservices architecture.

Use JWT tokens for authentication and authorization between microservices.

Secure endpoints based on roles and scopes defined in JWT tokens.

Configure OAuth 2.0 clients for microservices to request and validate tokens.

Use Spring Cloud Gateway or Zuul as an API Gateway for centralized security enforcement.

Explain the SOLID principles and how they influence the design of Java applications.

 




The SOLID principles are a set of five design principles for writing clean, maintainable, and extensible object-oriented code. 

They were introduced by Robert C. Martin (also known as Uncle Bob) to guide developers in creating software that is easier to understand, modify, and scale. 

Here's an explanation of each principle and how they influence the design of Java applications:

1. Single Responsibility Principle (SRP):

The SRP states that a class should have only one reason to change, meaning it should have only one job or responsibility. 

This principle aims to keep classes focused and avoid bloated, tightly-coupled designs.

Influence on Java Design:

Helps create smaller, focused classes that are easier to understand and maintain.

Encourages separating concerns, such as separating business logic from data access or user interface.

Promotes the use of interfaces and abstractions to define contracts between components.

2. Open/Closed Principle (OCP):

The OCP states that software entities (classes, modules, functions, etc.) should be open for extension but closed for modification. 

This means that the behavior of a module can be extended without modifying its source code.

Influence on Java Design:

Encourages the use of interfaces and abstract classes to define contracts.

Allows developers to add new functionality by creating new classes that implement existing interfaces or extend abstract classes.

Promotes the use of design patterns like Strategy, Decorator, and Factory to achieve extensibility without modifying existing code.

3. Liskov Substitution Principle (LSP):

The LSP states that objects of a superclass should be replaceable with objects of its subclasses without affecting the correctness of the program. In other words, subclasses should be substitutable for their base classes.

Influence on Java Design:

Encourages adherence to contracts defined by interfaces or base classes.

Promotes polymorphism and inheritance in a way that maintains consistency and behavior across classes.

Helps prevent unexpected behavior when using subclasses in place of their base classes.

4. Interface Segregation Principle (ISP):

The ISP states that clients should not be forced to depend on interfaces they do not use. It suggests that large interfaces should be broken down into smaller, more specific interfaces so that clients only need to know about the methods that are of interest to them.

Influence on Java Design:

Encourages the creation of cohesive and focused interfaces.

Helps avoid "fat" interfaces that require implementing unnecessary methods.

Facilitates easier implementation of interfaces by focusing on specific functionalities.

5. Dependency Inversion Principle (DIP):

The DIP states that high-level modules should not depend on low-level modules. Both should depend on abstractions. Additionally, abstractions should not depend on details; details should depend on abstractions.

Influence on Java Design:

Encourages the use of interfaces or abstract classes to define contracts between components.

Promotes loose coupling between classes by depending on abstractions rather than concrete implementations.

Facilitates easier unit testing and the ability to swap implementations without affecting the higher-level modules.

Influence on Java Applications:

Modularity: Applying SOLID principles helps create modular Java applications with smaller, more focused components.

Flexibility: Designing with SOLID principles allows for easier changes and extensions to the system without risking unintended side effects.

Readability and Maintainability: By promoting clean, well-structured code, SOLID principles make it easier for developers to understand and maintain Java applications.

Testability: Code designed with SOLID principles is typically easier to unit test, as it often results in classes that are more isolated and decoupled from dependencies.


In Java applications, adherence to the SOLID principles often leads to the use of design patterns such as Factory, Strategy, Decorator, and others. 

These patterns help implement the principles effectively, resulting in code that is more robust, flexible, and easier to maintain over time.

Saturday, March 9, 2024

what are different types of design patterns used in microservices

When designing microservices, there are several architectural patterns that can be used to achieve various goals such as scalability, fault tolerance, maintainability, and ease of deployment. Here are some common patterns used in microservices architecture:


1. Single Service Instance Pattern

Each microservice instance runs as a single instance. This is the simplest form of microservices architecture, where each service is deployed independently.

2. Service Instance per Container Pattern

Each microservice runs in its own container. Containers provide lightweight, isolated runtime environments for applications, allowing them to run consistently across different environments.

3. Service Instance per Virtual Machine Pattern

Each microservice runs in its own virtual machine (VM). This pattern provides a higher level of isolation compared to containers but comes with the overhead of managing VMs.

4. Shared Database Pattern

Multiple microservices share a common database. While this can simplify some aspects of development, it can also lead to tight coupling between services and make it difficult to evolve the system over time.

5. Database per Service Pattern

Each microservice has its own database. This pattern promotes loose coupling between services but requires careful coordination when data needs to be shared between services.

6. API Gateway Pattern

An API Gateway acts as a single entry point for clients to interact with multiple microservices. It can handle routing, authentication, and other cross-cutting concerns.

7. Aggregator Pattern

Aggregates data from multiple microservices into a single response for the client. This can reduce the number of client-server round trips and improve performance.

8. Saga Pattern

Manages distributed transactions across multiple microservices. A saga is a sequence of local transactions where each local transaction updates the database and publishes a message or event to trigger the next transaction.

9. Event Sourcing Pattern

Each microservice persists events as a log of changes to the system's state. This enables replaying events to rebuild state, auditing, and decoupling between services.

10. CQRS (Command Query Responsibility Segregation) Pattern

Separates read and write operations for a microservice. This pattern can improve scalability by allowing separate optimization for read and write operations.

11. Bulkhead Pattern

Isolates components of a system into separate pools to prevent failures in one component from affecting others. This helps improve fault tolerance and resilience.

12. Circuit Breaker Pattern

Monitors for failures and prevents cascading failures by temporarily blocking requests to a failing service. This pattern helps improve system stability.

13. Sidecar Pattern

Attaches a helper service, known as a "sidecar," to a microservice to provide additional functionality such as monitoring, logging, or security.

14. Strangler Pattern

Gradually replaces a monolithic application with microservices by "strangling" parts of the monolith with new microservices over time.

15. Choreography vs. Orchestration

In microservices, you often need to decide between choreography (decentralized coordination through events) and orchestration (centralized coordination through a service). This decision impacts how services communicate and coordinate their actions.

These patterns can be used individually or in combination to design a microservices architecture that meets the specific requirements of your application. It's essential to consider factors such as scalability, maintainability, fault tolerance, and team expertise when choosing the appropriate patterns for your system.





 

Wednesday, December 27, 2023

Getting started with Generative AI prompt engineer Step By Step Guide

 Generative AI prompt engineering involves crafting effective prompts to elicit desired responses from generative models.

Whether you're working with any models, the key is to provide clear and specific instructions. Here's a step-by-step guide to get started:

  1. Understand the Model's Capabilities:

    • Familiarize yourself with the capabilities and limitations of the generative model you're using. Understand the types of tasks it can perform and the formats it accepts.
  2. Define Your Goal:

    • Clearly define the goal of your prompt. Are you looking for creative writing, programming code, problem-solving, or something else? The specificity of your goal will guide your prompt creation.
  3. Start with a Clear Instruction:

    • Begin your prompt with a clear and concise instruction. Be specific about the type of output you're expecting. For example, if you want a creative story, you might start with "Write a short story about..."
  4. Provide Context or Constraints:

    • If necessary, provide additional context or constraints to guide the model. This can include setting, characters, tone, or any specific requirements. Constraints help to narrow down the output and make it more relevant to your needs.
  5. Experiment with Temperature and Max Tokens:

    • Generative models often come with parameters like "temperature" and "max tokens." Temperature controls the randomness of the output, and max tokens limit the length of the response. Experiment with these parameters to fine-tune the model's behavior.
  6. Iterate and Refine:

    • Don't be afraid to iterate and refine your prompts. Experiment with different instructions, wording, and structures to achieve the desired output. Analyze the model's responses and adjust your prompts accordingly.
  7. Use System and User Messages:

    • For interactive conversations with the model, you can use both system and user messages. System messages set the behavior of the assistant, while user messages simulate the user's input. This can be useful for multi-turn interactions.
  8. Handle Ambiguity:

    • If your prompt is ambiguous, the model might produce unexpected or undesired results. Clarify your instructions to reduce ambiguity and improve the likelihood of getting the desired output.
  9. Consider Prompt Engineering Libraries:

    • Some platforms provide prompt engineering libraries that simplify the process of crafting effective prompts. For example, OpenAI's Playground or other third-party libraries may offer useful tools and examples.
  10. Stay Ethical:

    • Be mindful of ethical considerations when generating content. Avoid prompts that may lead to harmful or inappropriate outputs. Review and filter the generated content to ensure it aligns with ethical guidelines.

Prompt engineering often involves a trial-and-error process. As you experiment and become familiar with the model's behavior, you'll improve your ability to craft effective prompts for generative AI.

Friday, December 8, 2023

API rate limiting strategies for Spring Boot applications

 


API Rate Limiting

 Rate limiting is a strategy to limit access to APIs. 

 It restricts the number of API calls that a client can make within a certain time frame. 

 This helps defend the API against overuse, both unintentional and malicious.


API rate limiting is crucial for maintaining the performance, stability, and security of Spring Boot applications. Here are several rate limiting strategies you can employ:


1. Fixed Window Counter:

In this strategy, you set a fixed window of time (e.g., 1 minute), and you allow a fixed number of requests within that window. If a client exceeds the limit, further requests are rejected until the window resets. This approach is simple but can be prone to bursts of traffic.


2. Sliding Window Counter:

A sliding window counter tracks the number of requests within a moving window of time. This allows for a more fine-grained rate limiting mechanism that considers recent activity. You can implement this using a data structure like a sliding window or a queue to track request timestamps.


3. Token Bucket Algorithm:

The token bucket algorithm issues tokens at a fixed rate. Each token represents permission to make one request. Clients consume tokens for each request, and requests are only allowed if there are available tokens. Google's Guava library provides a RateLimiter class that implements this algorithm.


4. Leaky Bucket Algorithm:

Similar to the token bucket, the leaky bucket algorithm releases tokens at a constant rate. However, in the leaky bucket, the bucket has a leak, allowing it to empty at a constant rate. Requests are processed as long as there are tokens available. This strategy can help smooth out bursts of traffic.

5. Distributed Rate Limiting with Redis or Memcached:

If your Spring Boot application is distributed, you can use a distributed caching system like Redis or Memcached to store and share rate limiting information among different instances of your application.


6. Spring Cloud Gateway Rate Limiting:

If you're using Spring Cloud Gateway, it provides built-in support for rate limiting. You can configure rate limiting policies based on various criteria such as the number of requests per second, per user, or per IP address.


7. User-based Rate Limiting:

Instead of limiting based on the number of requests, you can implement rate limiting on a per-user basis. This is useful for scenarios where different users may have different rate limits based on their subscription level or user type.


8. Adaptive Rate Limiting:

Implement adaptive rate limiting that dynamically adjusts rate limits based on factors such as server load, response times, or the health of the application. This approach can help handle variations in traffic.


9.Response Code-based Rate Limiting:

Consider rate limiting based on response codes. For example, if a client is generating a high rate of error responses, you might want to impose stricter rate limits on that client.


10. API Key-based Rate Limiting:

Tie rate limits to API keys, allowing you to set different limits for different clients or users. This approach is common in scenarios where you have third-party developers using your API.

Thursday, June 15, 2023

How to install Kong Gateway using Docker

To install Kong Gateway, you can follow these steps: 

 Step 1: Choose the installation method: 
  
     Kong Gateway offers different installation methods depending on your operating system and
     requirements. 

    You can choose from Docker, package managers (e.g., Homebrew, Yum, Apt), or manual installation.

     For simplicity, let's go with the Docker installation method.

 Step 2: Install Docker: If you don't have Docker installed, visit the Docker website
              (https://www.docker.com/) and follow the instructions to install Docker for your specific
               operating system. 

 Step 3: Pull the Kong Gateway Docker image: 
 
             Open a terminal or command prompt. Run the following command to pull the Kong Gateway
              Docker image from Docker Hub:
docker pull kong/kong-gateway

Step 4: Run Kong Gateway container: Once the image is pulled, run the following command to start a
             Kong Gateway
docker run -d --name kong-gateway \
  -e "KONG_DATABASE=off" \
  -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
  -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
  -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
  -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
  -e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
  -e "KONG_PROXY_LISTEN=0.0.0.0:8000" \
  -p 8000:8000 \
  -p 8001:8001 \
  kong/kong-gateway

This command starts a Kong Gateway container named "kong-gateway" with the necessary environment variables and port mappings. 

 The -p option maps the container's ports to the host machine, allowing access to Kong Gateway's admin API (port 8001) and proxy API (port 8000). 

 The -e options set various environment variables like the database type (KONG_DATABASE=off disables the database), log configurations, and listen addresses.

 Step 5: Verify Kong Gateway installation: After running the container, wait for a few moments to allow
              Kong Gateway to initialize. 


You can check the logs of the container using the following command:
docker logs kong-gateway

Look for any error messages or indications that Kong Gateway has started successfully. 


 Step 6: Access Kong Gateway admin API: 

 Once Kong Gateway is running, you can access its admin API to configure and manage your Kong Gateway instance. 

Open a web browser and go to http://localhost:8001. You should see the Kong Gateway admin API homepage if everything is working correctly.

 Congratulations! You have successfully installed Kong Gateway using Docker. 

You can now proceed with configuring Kong Gateway and integrating it with your applications as needed

Monday, May 1, 2023

How to Implement Image classification using TensorFlow maven and Java

Here is an example of using TensorFlow with Java and Maven to perform image classification: 

 1.Create a new Maven project in your favorite IDE. 

 2. Add the TensorFlow Java dependency to your project by adding the following to your pom.xml file:

    
      <dependencies>
    <dependency>
        <groupId>org.tensorflow</groupId>
        <artifactId>tensorflow</artifactId>
        <version>2.7.0</version>
    </dependency>
</dependencies>
    

3. Create a new class, for example ImageClassifier.java, and add the following code:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.tensorflow.DataType;
import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.TensorFlow;

public class ImageClassifier {
    private static byte[] loadImage(String path) throws IOException {
        BufferedImage img = ImageIO.read(new File(path));
        int height = img.getHeight();
        int width = img.getWidth();
        int channels = 3;
        byte[] data = new byte[height * width * channels];
        int pixel = 0;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int rgb = img.getRGB(j, i);
                data[pixel++] = (byte) ((rgb >> 16) & 0xFF);
                data[pixel++] = (byte) ((rgb >> 8) & 0xFF);
                data[pixel++] = (byte) (rgb & 0xFF);
            }
        }
        return data;
    }

    public static void main(String[] args) throws Exception {
        // Load the TensorFlow library
        try (Graph g = new Graph()) {
           byte[] graphBytes = TensorFlowModelLoader.load("path/to/model.pb");
            g.importGraphDef(graphBytes);

            // Create a new session to run the graph
            try (Session s = new Session(g)) {
                // Load the image data
                String imagePath = "path/to/image.jpg";
                byte[] imageBytes = loadImage(imagePath);

                // Create a tensor from the image data
                Tensor inputTensor = Tensor.create(new long[]
                                   {1, imageBytes.length}, ByteBuffer.wrap(imageBytes));

                // Run the graph on the input tensor
                Tensor outputTensor = s.runner()
                        .feed("input", inputTensor)
                        .fetch("output")
                        .run()
                        .get(0);

                // Print the predicted label
                DataType outputDataType = outputTensor.dataType();
                long[] outputShape = outputTensor.shape();
                Object[] output = new Object[outputTensor.numElements()];
                outputTensor.copyTo(output);
                System.out.println("Prediction: " + output[0]);
            }
        }
    }
}
4. Replace the path/to/model.pb and path/to/image.jpg with the actual paths to your model and image files. 

 5. Run the ImageClassifier class, and it should print out the predicted label for the input image.

Thursday, April 13, 2023

How to create key cloak authentication server and spring boot

To create a Keycloak authentication server, you need to follow these steps: 

 1. Download and Install Keycloak: You can download Keycloak from the official website     

 Follow the installation instructions provided in the documentation. 

 2. Configure Keycloak: Once installed, you need to configure Keycloak by creating a new realm. 
     A realm is a container for all the users, roles, and groups in your application.

    To create a new realm, log in to the Keycloak admin console using the default credentials
      (admin/admin), then follow these steps:

      Click on the "Add Realm" button and provide a name for your realm. 

      Configure your realm settings, including themes, email settings, and login settings. 

      Create users and groups within your realm and assign roles to them. 

 3. Set Up Your Spring Boot Application: You can use the Keycloak Spring Boot Starter dependency to
      add Keycloak authentication to your Spring Boot application.

      Add the following dependency to your Maven or Gradle build file:

<dependency>
  <groupId>org.keycloak</groupId>
  <artifactId>keycloak-spring-boot-starter</artifactId>
</dependency>


4. Configure Your Spring Boot Application: You need to configure your Spring Boot application to
     connect to the Keycloak server. 

     You can do this by adding the following properties to your application.properties or application.yml file:
keycloak.auth-server-url=<keycloak-server-url>
keycloak.realm=<keycloak-realm>
keycloak.resource=<keycloak-client-id>
keycloak.credentials.secret=<keycloak-client-secret>


   Replace <keycloak-server-url>, <keycloak-realm>, <keycloak-client-id>, 
    and <keycloak-client-secret> with the appropriate values for your Keycloak instance.

 5.  Secure Your Spring Boot Application: You can secure your Spring Boot application by adding the
      Keycloak configuration to your Spring Security configuration. 

      You can do this by creating a new class that extends KeycloakWebSecurityConfigurerAdapter and
      override the configure method. 

Here's an example:
@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(keycloakAuthenticationProvider());
    }

    @Bean
    public KeycloakSpringBootConfigResolver keycloakConfigResolver() {
        return new KeycloakSpringBootConfigResolver();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http.authorizeRequests().antMatchers("/admin/**").hasRole("admin")
          .antMatchers("/user/**").hasAnyRole("user", "admin")
          .anyRequest().permitAll();
    }
}
    This configuration class enables Keycloak authentication and authorization for specific URLs in the
     application.

 6. Test Your Application: You can test your application by running it and accessing the protected URLs.
     When a user tries to access a protected resource, they will be redirected to the Keycloak login page.
      Once they successfully authenticate, they will be redirected back to the original resource. 

That's it! we have created a Keycloak authentication server and secured your Spring Boot application with it.

Wednesday, April 12, 2023

How to build video conference using spring boot

To build a video conference application using Spring Boot, you can follow these steps: 

 1. Choose a video conferencing API: There are several video conferencing APIs available such as Twilio, Agora, Zoom, Jitsi, etc. Choose an API that best fits your requirements. 

 2. Set up a Spring Boot project: Create a new Spring Boot project using your preferred IDE or by using Spring Initializr. 

 3. Add the video conferencing API dependencies: Add the necessary dependencies for the chosen API in your project's pom.xml file.

 4. Configure the video conferencing API: Configure the video conferencing API with the required credentials and other settings in your Spring Boot application's configuration file.

 5. Implement the video conferencing features: Use the API's SDK to implement the video conferencing features such as creating a conference room, joining a room, leaving a room, etc. 

 6. Integrate the video conferencing features with your application: Add the necessary controllers and views to your Spring Boot application to integrate the video conferencing features with your application. 

 7. Test the application: Test the application thoroughly to ensure that the video conferencing features are working as expected. 

 Here's a sample code for a video conference application using Spring Boot and the Twilio Video API:
 1. Add the Twilio Video API dependency to your pom.xml file:

 

<dependency>
    <groupId>com.twilio.sdk</groupId>
    <artifactId>twilio</artifactId>
    <version>7.54.0</version>
</dependency>



2. Add the required Twilio credentials and settings to your Spring Boot application's application.properties file
twilio.account.sid=your_account_sid
twilio.auth.token=your_auth_token
twilio.api.key=your_api_key
twilio.api.secret=your_api_secret
twilio.video.room.max-participants=4
3. Implement a controller for creating and joining a video conference room:
@RestController
public class VideoConferenceController {

    @Autowired
    private TwilioConfig twilioConfig;

    @PostMapping("/room")
    public ResponseEntity createRoom() throws Exception {
        RoomCreator roomCreator = Room.creator()
                .setUniqueName(UUID.randomUUID().toString())
                .setType(RoomType.PEER_TO_PEER)
                .setStatusCallback(twilioConfig.getStatusCallback())
                .setMaxParticipants(twilioConfig.getMaxParticipants());
        Room room = roomCreator.create();
        RoomResponse response = new RoomResponse();
        response.setRoomId(room.getSid());
        response.setRoomName(room.getUniqueName());
        response.setToken(createAccessToken(room.getSid()));
        return ResponseEntity.ok(response);
    }

    @GetMapping("/room/{roomId}/token")
    public ResponseEntity 
                        getRoomToken(@PathVariable("roomId") String roomId) {
        String accessToken = createAccessToken(roomId);
        return ResponseEntity.ok(accessToken);
    }

    private String createAccessToken(String roomId) {
        VideoGrant grant = new VideoGrant();
        grant.setRoom(roomId);
        AccessToken token = new AccessToken.Builder(
                twilioConfig.getAccountSid(),
                twilioConfig.getApiKey(),
                twilioConfig.getApiSecret()
        ).identity(UUID.randomUUID().toString())
                .grant(grant)
                .build();
        return token.toJwt();
    }

}
4.Define a configuration class for the Twilio settings:
@ConfigurationProperties(prefix = "twilio")
@Data
public class TwilioConfig {

    private String accountSid;
    private String authToken;
    private String apiKey;
    private String apiSecret;
    private String statusCallback;
    private int maxParticipants;


}


5.Configure the Twilio settings in your Spring Boot application's application.yml file:
twilio:
  account-sid: your_account_sid
  auth-token: your_auth_token
  api-key: your_api_key
  api-secret: your_api_secret
  status-callback: http://localhost:8080/callback
  max-participants: 4
  
  
6. Run your Spring Boot application and test the video conference feature by creating and joining a room using the API endpoints.

Tuesday, March 14, 2023

Oracle Cloud Infrastructure- Object storage example

This post will explain, how we can create bucket and configure notification and rules and object storage like create/update object. Login into OCI using login credentials . if you don't have a account please create the same using this link https://www.oracle.com/cloud/sign-in.html Once you create account and successful login, Now we need to create a bucket. search for bucket and create a bucket
After creating bucket now we can create a topic
Now we can create notification, after uploading file , we should get email
Create a Rule to process this
Once we create all then , once we upload file in object storage , then we should get email like below
This shows we can send email, but we can configure with different ways like queues. Here we will get the details in mail . But if we want complete object details, then we can use java code to retrieve the object details by calling the API which required namespace details ,bucket name and object name.
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Paths;
import java.util.List;

import com.oracle.bmc.objectstorage.ObjectStorage;
import com.oracle.bmc.objectstorage.ObjectStorageClient;
import com.oracle.bmc.objectstorage.model.GetObjectRequest;
import com.oracle.bmc.objectstorage.model.ObjectStream;
import com.oracle.bmc.objectstorage.model.Range;
import com.oracle.bmc.objectstorage.requests.GetObjectRequest;
import com.oracle.bmc.objectstorage.responses.GetObjectResponse;

public class LogFileRetriever {
    public static void main(String[] args) {
        String namespaceName = "mynamespace";
        String bucketName = "mybucket";
        String objectName = "mylogfile.txt";

        // Create a new Object Storage client
        ObjectStorage objectStorageClient = ObjectStorageClient.builder()
                .build(objectStorageConfig);

        GetObjectRequest request = GetObjectRequest.builder()
                .namespaceName(namespaceName)
                .bucketName(bucketName)
                .objectName(objectName)
                .build();

        GetObjectResponse response = objectStorageClient.getObject(request);

        // Get the InputStream from the response
        InputStream logFileInputStream = response.getInputStream();

        // Write the InputStream to a local file
        OutputStream logFileOutputStream = 
            new FileOutputStream(Paths.get("logfile.txt").toFile());
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = logFileInputStream.read(buffer)) != -1) {
            logFileOutputStream.write(buffer, 0, bytesRead);
        }
        logFileOutputStream.close();
    }
}

Monday, March 14, 2022

How to call REST API Using Spring Webflux WebClient set proxy with authorization while calling the external site and Generate base 64 authentication header spring boot

This post will explain us, how can we call REST API using Webclient. How to set Proxy username and password to call the external site. Step 1: Add folllowing dependency in your pom.xml
   
	 org.springframework.boot
	 spring-boot-starter-webflux
	
Step 2:
import lombok.extern.slf4j.XSlf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.transport.ProxyProvider;

import java.net.URI;
import java.util.Base64;
import java.util.function.Consumer;
import java.util.function.Function;
 public static void main(String[] args) {
  try {
  //Generate HttP client to set proxy details
  HttpClient httpClient = HttpClient.create()
            .proxy(proxy-> proxy.type(ProxyProvider.Proxy.HTTP)
            .host("javaguruonline.com").port(1328)
            .username("siva")
            .password(new Function() {
                        @Override
                        public String apply(String s) {
                            return "test@12!";
                        }
                    }));
  ClientHttpConnector clientHttpConnector = 
           new ReactorClientHttpConnector(httpClient);
  WebClient webClient = WebClient.builder().build();
  String plainCredentials = "username" + ":" + "password";
  String base64Credentials = new String(Base64.getEncoder()
                           .encode(plainCredentials.getBytes()));
  String authorizationHeader = "Basic " + base64Credentials;
  //input json
  String json = "{\n" +
                    "  \"user\": \"siva\"\n" +
                    "}";
            //set http headers
  Consumer httpHeaders = new Consumer() {
     @Override
     public void accept(HttpHeaders httpHeaders) {
       httpHeaders.add("Authorization", authorizationHeader);
       httpHeaders.add("Content-Type", "application/json");
       httpHeaders.add("Proxy-Authorization", authorizationHeader);
     }};
   //post call to external URL and get ResponseEntity as response like body 
   //and http status code etc..
    ResponseEntity result = webClient.mutate()
                    .clientConnector(clientHttpConnector)
                    .build()
                    .post()
                    .uri(new URI("https://localhost:8080/test/details"))
                    .bodyValue(json)
                    .accept(MediaType.APPLICATION_JSON)
                    .retrieve()
                    .toEntity(String.class)
                    .block();
            System.out.println(result);
        }
        catch(Exception ex){
        }
Hope this post will help some one, while working on the REST API calls

Thursday, March 11, 2021

How to mock ResultSet , NamedParameterJdbcTemplate , RowMapper

package com.siva.springboot.javaguruonline.repository;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import com.siva.springboot.javaguruonline.model.EmployeeDetails;
@RunWith(SpringRunner.class)
/**
 * 
 * @author Siva
 *
 */
//@SpringBootTest(classes = SpringbootJpaSwaggerSonarqubeApplication.class)
public class EmployeeDaoImplTest {
	
	@InjectMocks
	public EmployeeDaoImpl employeeDao;
	
	@Mock
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
	@SuppressWarnings("unchecked")
	@Test
	public void testGetAllEmployeeDetails(){
		List list = new ArrayList();
		EmployeeDetails employeeDetails = new EmployeeDetails();
		employeeDetails.setEmpId(1);
		employeeDetails.setEmpName("siva");
		list.add(employeeDetails);
		Mockito.when(namedParameterJdbcTemplate.query(Mockito.anyString(),
        Mockito.any(RowMapper.class)))
	    .thenAnswer(new Answer>() {
		
	        @Override
	public List answer(InvocationOnMock invocation) 
                                          throws Throwable {
	            // Fetch the method arguments
	          Object[] args = invocation.getArguments();
	          

	            // Fetch the row mapper instance from the arguments
	          RowMapper rm = (RowMapper) args[1];

	          // Create a mock result set and setup an expectation on it
	          ResultSet rs = Mockito.mock(ResultSet.class);
	          Mockito.when(rs.getInt("emp_id")).thenReturn(employeeDetails.getEmpId());
	            
	          Mockito.when(rs.getString("emp_name")).thenReturn(employeeDetails.getEmpName());
               
	            // Invoke the row mapper
	          EmployeeDetails actual = rm.mapRow(rs, 0);
	            // Assert the result of the row mapper execution
	          Assert.assertEquals(employeeDetails.getEmpName(), actual.getEmpName());

	            // Return your created list for the template#query call
	            return list;
	        }
	    });
		
		
		
		
		 employeeDao.getEmployeeDetails();
		Assert.assertNotNull(list);
	}

}

Thursday, August 27, 2020

Getting started with Chatbot using java and response in text to speech -- Java simple chatbot and text to speech

This will explain you about , how we can write simple chatbot using java and read the bot response into speech 

 Step 1: Download sample program-ab from the below archive folder Program-ab 

 Step 2: Unzip the downloaded folder. 

 Step 3: Create java maven project using any IDE or console application

 Step 4: Copy Ab.jar (Which is there in the unzipped folder lib in step 2) add to the classpath 

 Step 5: Copy bots folder (which is available in the unzipped folder) it has all the aiml files, which bot act upon our request and give the response

 Step 6: Now we need to give bot response in speech

 Step 7: Download freetss from the given link FREETTS 

 Step 8: Unzip the downloaded folder and go to \freetts-1.2.2-bin\freetts-1.2\lib folder 

 Step 9: Run the jsapi.exe file- It will generate multiple jars

 Step 10: copy that jars and place it in the classpath of the java project 

 Step 11: Once above steps completed then, we can write simple java program as follows
package com.siva;

import java.io.File;
import java.util.Locale;
import java.util.Scanner;

import javax.speech.Central;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;

import org.alicebot.ab.AB;
import org.alicebot.ab.Bot;
import org.alicebot.ab.Chat;
import org.alicebot.ab.MagicBooleans;
import org.alicebot.ab.utils.IOUtils;

public class TestChatbot {

    private static final boolean TRACE_MODE = false;
    static String botName = "super";
 
    public static void main(String[] args) {
        try {
 
            String aimlResourcesPath = getResourcesPath();
            System.out.println(aimlResourcesPath);
            MagicBooleans.trace_mode = TRACE_MODE;
            Bot bot = new Bot("super", aimlResourcesPath);
            Chat chatSession = new Chat(bot);
	        AB.ab(bot);
	        Scanner sc=new Scanner(System.in);

	        String request = "Hai";
	        do{

	        request = IOUtils.readInputTextLine();
	        String response = chatSession.multisentenceRespond(request); 
	        
	        
	        
	            // Set property as Kevin Dictionary 
	            System.setProperty( 
	                "freetts.voices", 
	                "com.sun.speech.freetts.en.us"
	                    + ".cmu_us_kal.KevinVoiceDirectory"); 
	  
	            // Register Engine 
	            Central.registerEngineCentral( 
	                "com.sun.speech.freetts"
	                + ".jsapi.FreeTTSEngineCentral"); 
	  
	            // Create a Synthesizer 
	            Synthesizer synthesizer 
	                = Central.createSynthesizer( 
	                    new SynthesizerModeDesc(Locale.US)); 
	  
	            // Allocate synthesizer 
	            synthesizer.allocate(); 
	  
	            // Resume Synthesizer 
	            synthesizer.resume(); 
	  
	            // Speaks the given text 
	            // until the queue is empty. 
	            synthesizer.speakPlainText( 
	            		response, null); 
	            synthesizer.waitEngineState( 
	                Synthesizer.QUEUE_EMPTY); 
	  
	        System.out.println(response); 
	        }while(!request.equals("exit"));

 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private static String getResourcesPath() {
        File currDir = new File(".");
        String path = currDir.getAbsolutePath();
        path = path.substring(0, path.length() - 2);
        System.out.println(path);
        String resourcesPath = path + File.separator + "src" 
        + File.separator + "main" + File.separator + "resources";
        return resourcesPath;
    }
}


Step 12: Once code completed , then run the java program , type the input like Hai, hello and date, there are so many QA mentioned in imal files. and we can also write our own aiml file, for reference, you can verify under boats/aiml folder 

Step 13: output will print in console and it will read the boat response.. 

 Step 14: Out put will be like below image. 


 Thanks for viewing this post.

Friday, July 24, 2020

Angular with Spring boot Getting started with Angular and Spring boot


This post will explain , how we can integrate spring boot rest api in angular js

Step 1 : We need to install angular js

Follow the steps to mention in the angular web site Angular set up

Basic commands used in angular application
npm install -g @angular/cli

Create a new angular js application

ng new employee-crud-client


Once application created then, now we need to create components, services, model classes to integrate with spring boot

Step 2: to create component - Go to src/app folder in command prompt


ng g c employee-list

We can write our entire crud operations logic inside employee-list component or we can create different components for different operations.

Once we create component , there were 4 files created

1. employee-list.component.ts - logic related to how to form request and after getting the response from service- spring boot api, how to massage and send it to ui
2. employee-list.component.html - plain html with angular script to display/action with response or user input
3. employee-list.component.spec
4. employee-list.component.css

Step 3: Once the above files created and now we need to configure these with angular components


1. app.module.ts - will have the components, which we created
2. app-routing.module.ts- will have the path details, to which path has to call the which component
3. app.component.html - will have the router-outlet, which internally call the routing-module and which will call the app.modules.ts

Step 4: This is important step, service to be created, which calls the spring boot api

ng g s employee

employee.service file will be created

this service consists of the logic , how we can call the rest api methods, This is called from the employee-list.component.ts


Step 5: We will create model class, it should be same like our pojo class in java

ng g class employee


Step 6: Once all the steps completed, then now we need to run the angular client.


ng serve
ng serve --open If there is no errors, then page directly will open in browser http://localhost:4200


Step 7 : For spring boot api application, see my previous employee crud operations with spring boot post.


Step 8: This way we can integrate our spring boot application with angular.

Thanks for viewing this post, if you have any difficulty while integration, please comment the same.

Convert Date to Number and Number to Date in Oracle



How we can convert date to number in oracle

select to_number(to_char(add_months(sysdate,-0),'yyyymm')) from dual;

How we can convert number to date

select to_char(to_date(202007,'yyyymm'),'dd-mon-yyyy') from dual;


Sunday, March 22, 2020

Spring Boot with Swagger UI , JPA , MYSQL , Mockito, Integration Test and Sonar Qube



This post will expain you about
1. How we will integrate spring boot and swagger api
2. Spring boot with JPA and MySQL (Crud Repository and NamedJdbcTemplate)
3 Spring mockMvc test for controller
4. Integration test for Service and DAO classes
5. Usage of Mockito
6. Sonarqube Code coverage

Step 1 : create a maven project called - springboot-jpa-swagger-mysql-sonarqube in eclipse
Step 2 : provide groupId,artifactId,version,jar,name and description
Step 3: Replace below pom.xml into your local system
pom.xml will have dependencies related to spring boot,swagger,jdbc,mysql,mockito,sonarqube



 4.0.0

 com.siva.springboot.javaguruonline
 springboot-jpa-swagger-mysql-sonarqube
 0.0.1-SNAPSHOT
 jar

 springboot-jpa-swagger-mysql-sonarqube
 Demo project for Spring Boot PJA MySQL AND Sonarqube

 
  org.springframework.boot
  spring-boot-starter-parent
  2.0.5.RELEASE
  
 

 
  UTF-8
  UTF-8
  1.8
 

 
  
   org.springframework.boot
   spring-boot-starter-data-jpa
  
  
   org.springframework.boot
   spring-boot-starter-web
  

  
   org.springframework.boot
   spring-boot-starter-test
   test
  

  
   mysql
   mysql-connector-java
   runtime
  
  
  
   io.springfox
   springfox-swagger2
   2.8.0
  
  
   io.springfox
   springfox-swagger-ui
   2.8.0
  
  
   io.springfox
   springfox-bean-validators
   2.8.0
  
  
   javax.xml
   jaxb-api
   2.1
  
  
   org.projectlombok
   lombok
   1.18.12
   provided
  

  
  
   org.sonarsource.scanner.maven
   sonar-maven-plugin
   3.0.2
  
 

 
  
   
    org.springframework.boot
    spring-boot-maven-plugin
   
   
    org.codehaus.mojo
    sonar-maven-plugin
    3.0.2
   
   
    org.jacoco
    jacoco-maven-plugin
    0.8.0
    
     
      default-prepare-agent
      
       prepare-agent
      
     
     
      default-report
      prepare-package
      
       report
      
     
    
   
  
 


Step 4: Now create a Springboot application, which is the starting point to run the Application.

Step 5: create appliaction.properties under resources folder, add the db related details
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/employee
spring.datasource.username = root
spring.datasource.password = root


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

logging.level.root = DEBUG

spring.main.banner-mode=off
spring.datasource.platform=h2






Step 5: Create a SwaggerConfig class, which will have the details , what is controller package and other details
Learn more about Swagger API swagger-ui

package com.siva.springboot.javaguruonline.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {
 @Bean
 public Docket api() {
  return new Docket(DocumentationType.SWAGGER_2).select()
    .apis(RequestHandlerSelectors
      .basePackage("com.siva.springboot.javaguruonline.controller"))
    .paths(PathSelectors.regex("/.*"))
    .build().apiInfo(apiEndPointsInfo());
 }

 private ApiInfo apiEndPointsInfo() {

  return new ApiInfoBuilder().title("Spring Boot REST API")
    .description("Employee Management REST API")
    .contact(new Contact("Siva Raju", "http://www.javaguruonline.com", "siva82k@gmail.com"))
    .license("Apache 2.0")
    .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
    .version("1.0.0")
    .build();
 }
}



Step 6 : This project is related to employee management system - like Employee CRUD operations

Step 7 : Write Model class called Employee and EmployeeDetails

package com.siva.springboot.javaguruonline.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.EqualsAndHashCode;
import lombok.ToString;

@Entity
@Table(name = "employee")
@ApiModel(description="All details about the Employee. ")
@ToString
@EqualsAndHashCode
public class Employee implements Serializable{

 /**
  * 
  */
 private static final long serialVersionUID = 7407317371057056536L;

 @ApiModelProperty(notes = "The database generated employee ID")
 private int id;

 @ApiModelProperty(notes = "The employee name")
 private String name;

 @ApiModelProperty(notes = "The employee age")
 private int age;

 public Employee() {

 }

 public Employee(String name, int age) {
  this.name = name;
  this.age = age;
 }

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "emp_id", nullable = false)
 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 @Column(name = "emp_name", nullable = false)
 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
 @Column(name = "email_age", nullable = false)
 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

}

package com.siva.springboot.javaguruonline.model;

import lombok.EqualsAndHashCode;
import lombok.ToString;

@ToString
@EqualsAndHashCode
public class EmployeeDetails {
 
 private int empId;
 public int getEmpId() {
  return empId;
 }
 public void setEmpId(int empId) {
  this.empId = empId;
 }
 public String getEmpName() {
  return empName;
 }
 public void setEmpName(String empName) {
  this.empName = empName;
 }
 private String empName;

}


Step 8: Step 8: service/serviceimpl and repository classes

In the repository class, It is extending the JpaRepository, which will get all the default methods related to that Model Object.
In the DAO class, NamedParameterJdbcTemplate , to work with native query and how to map using rowmapper

package com.siva.springboot.javaguruonline.repository;

import java.util.List;

import com.siva.springboot.javaguruonline.model.EmployeeDetails;

public interface EmployeeDao {
 
 public List getEmployeeDetails();
 

}

package com.siva.springboot.javaguruonline.repository;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.siva.springboot.javaguruonline.mapper.EmployeeDetailsMapper;
import com.siva.springboot.javaguruonline.model.EmployeeDetails;

@Repository
public class EmployeeDaoImpl implements EmployeeDao{
 String sqlQuery="select emp_id,emp_name from employee";
 @Autowired
 private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

 @Override
 public List getEmployeeDetails() {
  return namedParameterJdbcTemplate.query(sqlQuery, new EmployeeDetailsMapper());
 }

}


package com.siva.springboot.javaguruonline.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.siva.springboot.javaguruonline.model.EmployeeDetails;

public class EmployeeDetailsMapper implements RowMapper {
 @Override
 public EmployeeDetails mapRow(ResultSet resultset, int count) throws SQLException {
  EmployeeDetails employeeDetails = new EmployeeDetails();
  employeeDetails.setEmpId(resultset.getInt("emp_id"));
  employeeDetails.setEmpName(resultset.getString("emp_name"));
  return employeeDetails;
 }
 

}


package com.siva.springboot.javaguruonline.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.siva.springboot.javaguruonline.model.Employee;

@Repository
public interface EmployeeRepository extends JpaRepository{

}



Step 9: Write Controller class, which will have the all the crud operations
package com.siva.springboot.javaguruonline.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.siva.springboot.javaguruonline.exception.ResourceNotFoundException;
import com.siva.springboot.javaguruonline.model.Employee;
import com.siva.springboot.javaguruonline.model.EmployeeDetails;
import com.siva.springboot.javaguruonline.repository.EmployeeDao;
import com.siva.springboot.javaguruonline.repository.EmployeeRepository;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;


@RestController
@RequestMapping("/api/v1")
@Api(value="Employee Management System")
public class EmployeeController {
 private static final String EMPLOYEE_NOT_FOUND_FOR_THIS_ID = "Employee not found for this id :: ";

 @Autowired
 private EmployeeRepository employeeRepository;
 
 @Autowired
 private EmployeeDao employeeDao;

 @ApiOperation(value = "View a list of available employees", response = List.class)
 @ApiResponses(value = { @ApiResponse(code = 200, message = "Successfully retrieved list"),
   @ApiResponse(code = 401, message = "You are not authorized to view the resource"),
   @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
   @ApiResponse(code = 404, message = "The resource you were trying to reach is not found") })
 @GetMapping("/employees")
 public List getAllEmployees() {
  return employeeRepository.findAll();
 }
 
 @ApiOperation(value = "View a list of available employee details", response = List.class)
 @ApiResponses(value = { @ApiResponse(code = 200, message = "Successfully retrieved list"),
   @ApiResponse(code = 401, message = "You are not authorized to view the resource"),
   @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
   @ApiResponse(code = 404, message = "The resource you were trying to reach is not found") })
 @GetMapping("/employeedetails")
 public List getAllEmployeeDetails() {
  return employeeDao.getEmployeeDetails();
 }

 @ApiOperation(value = "Get an employee by Id")
 @GetMapping("/employees/{id}")
 public ResponseEntity getEmployeeById(
   @ApiParam(value = "Employee id from which employee object will retrieve", required = true)
   @PathVariable(value = "id") Long employeeId)
   throws ResourceNotFoundException {
  Employee employee = employeeRepository.findById(employeeId)
    .orElseThrow(() -> new ResourceNotFoundException(EMPLOYEE_NOT_FOUND_FOR_THIS_ID + employeeId));
  return ResponseEntity.ok().body(employee);
 }

 @ApiOperation(value = "Add an employee")
 @PostMapping("/employees")
 public Employee createEmployee(
   @ApiParam(value = "Employee object store in database table", required = true)
   @Valid @RequestBody Employee employee) {
  return employeeRepository.save(employee);
 }

 @ApiOperation(value = "Update an employee")
 @PutMapping("/employees/{id}")
 public ResponseEntity updateEmployee(
   @ApiParam(value = "Employee Id to update employee object", required = true)
   @PathVariable(value = "id") Long employeeId,
   @ApiParam(value = "Update employee object", required = true)
   @Valid @RequestBody Employee employeeDetails) throws ResourceNotFoundException {
  Employee employee = employeeRepository.findById(employeeId)
    .orElseThrow(() -> new ResourceNotFoundException(EMPLOYEE_NOT_FOUND_FOR_THIS_ID + employeeId));

  employee.setName(employeeDetails.getName());
  employee.setAge(employeeDetails.getAge());
  final Employee updatedEmployee = employeeRepository.save(employee);
  return ResponseEntity.ok(updatedEmployee);
 }

 @ApiOperation(value = "Delete an employee")
 @DeleteMapping("/employees/{id}")
 public Map deleteEmployee(
   @ApiParam(value = "Employee Id from which employee object will delete from database table", required = true)
   @PathVariable(value = "id") Long employeeId)
   throws ResourceNotFoundException {
  Employee employee = employeeRepository.findById(employeeId)
    .orElseThrow(() -> new ResourceNotFoundException(EMPLOYEE_NOT_FOUND_FOR_THIS_ID + employeeId));

  employeeRepository.delete(employee);
  Map response = new HashMap<>();
  response.put("deleted", Boolean.TRUE);
  return response;
 }
}



Step 10: Once above code has been completed, then you can run the application by right clicking on the Springboot application.
Step 11. Once the application executed successfully, then we need to test this application

One way is either Using- SOAP UI, Postman - these needs to be installed on our machine, else we can't test the rest service.
To test the REST API, we already configured Swagger UI. So just go to your web browser, then click, http://localhost:8080/swagger-ui.html - it will display all the operations, which is availabe in controller class.


Now you can test those API's by giving request parameters



Step 12: Once done, we need to write Junit test cases for the all the classes

package com.siva.springboot.javaguruonline;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootJpaSwaggerSonarqubeApplicationTest {

 @Test
 public void contextLoads() {
 }

}


Controller class test

package com.siva.springboot.javaguruonline.controller;

import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.meta.When;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.siva.springboot.javaguruonline.SpringbootJpaSwaggerSonarqubeApplication;
import com.siva.springboot.javaguruonline.model.Employee;
import com.siva.springboot.javaguruonline.model.EmployeeDetails;
import com.siva.springboot.javaguruonline.repository.EmployeeDaoImpl;
import com.siva.springboot.javaguruonline.repository.EmployeeRepository;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,classes=SpringbootJpaSwaggerSonarqubeApplication.class)
@AutoConfigureMockMvc
public class EmployeeControllerTest {
 @Autowired
 private MockMvc mockMvc;
 
 @Autowired
 private EmployeeRepository employeeRepository;
 @Mock
 private EmployeeDaoImpl employeeDao;
 
 
 @Test
 public void testGetAllEmployees() throws Exception{
  List employeeList  = employeeRepository.findAll();
  ObjectMapper mapper = new ObjectMapper();
  String jsonString = mapper.writeValueAsString(employeeList);
  FileWriter file = new FileWriter(new File("employee.json"));
  file.write(jsonString);
  file.close();
  this.mockMvc.perform(get("/api/v1/employees")).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
  .andExpect(jsonPath("$", hasSize(4)));

 } 
 
 @Test
 public void testGetAllEmployeeDetails()  throws Exception {
  EmployeeDetails employee = new EmployeeDetails();
  employee.setEmpId(123);
  employee.setEmpName("Siva");
  List employeeList = new ArrayList();
  employeeList.add(employee);
  when(employeeDao.getEmployeeDetails()).thenReturn(employeeList);
  this.mockMvc.perform(get("/api/v1/employeedetails")).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
  .andExpect(jsonPath("$", hasSize(4)));
  
 }

}


DaoImpl Test

package com.siva.springboot.javaguruonline.repository;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.siva.springboot.javaguruonline.SpringbootJpaSwaggerSonarqubeApplication;
import com.siva.springboot.javaguruonline.model.EmployeeDetails;
import com.siva.springboot.javaguruonline.repository.EmployeeDao;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootJpaSwaggerSonarqubeApplication.class)
public class EmployeeDaoImplTest {
 
 @Autowired
 public EmployeeDao employeeDao;
 
 @Test
 public void testGetAllEmployeeDetails(){
  List employeeDetails = employeeDao.getEmployeeDetails();
  Assert.assertNotNull(employeeDetails);
 }

}


Repository Test

package com.siva.springboot.javaguruonline.repository;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.siva.springboot.javaguruonline.SpringbootJpaSwaggerSonarqubeApplication;
import com.siva.springboot.javaguruonline.model.Employee;
import com.siva.springboot.javaguruonline.repository.EmployeeRepository;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootJpaSwaggerSonarqubeApplication.class)
public class EmployeeRepositoryTest {
 @Autowired
 EmployeeRepository employeeRepository;
 
 @Test
 public void getEmployeeDetails(){
  List employeeList = employeeRepository.findAll();
  Assert.assertNotNull("EmployeeListNotEmpty", employeeList);;
 }
 

}



Step 13: We need to check the code coverage tool called Sonarqube, required dependencies added in pom.xml

Step 14: Download Sonarqube from SonarQube - and look for Historical Downloads, then download , which ever the version you want to work on.


Step 15 : Unzip that file and go to bin folder- StartSonar.bat file

Step 16 : Once Sonar is up, then go to browser and try http://localhost:9000 , login with username -admin and password- admin

Step 17: Once Sonarqube is up and running, we need to run the code through either sonar scanner or maven

Before Scanning the code, we need to create properties file called - sonar-project.properties place the inside of the your project
and need to mention src code path , test path, java version.. etc

# must be unique in a given SonarQube instance
sonar.projectKey=springboot-jpa-swagger-mysql-sonarqube
# this is the name displayed in the SonarQube UI
sonar.projectName=springboot-jpa-swagger-mysql-sonarqube
sonar.projectVersion=1.0


# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set. 
# If not set, SonarQube starts looking for source code from the directory containing 
# the sonar-project.properties file.
sonar.sources=/src/main/java/
 
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

sonar.junit.reportPaths=./target/surefire-reports
# Generate sonar issues report in html and console
sonar.issuesReport.html.enable=true
sonar.issuesReport.console.enable=true

# Display Jacoco report into SonarQube dashboard
# Comma-separated paths to directories with tests (optional)
sonar.tests=/src/test/java/
# This name depends on the configuration in pom.xml. In this example we have ${project.build.directory}/coverage-reports/jacoco-ut.exec entry in our pom.xml
sonar.jacoco.reportPath=target/surefire-reports/jacoco-ut.exec
sonar.dynamicAnalysis=reuseReports
sonar.java.coveragePlugin=jacoco
sonar.jacoco.reportMissing.force.zero=true
sonar.java.binaries=/target/classes/
sonar.coverage.exclusions=**/*Employee.java,**/*EmployeeDetails.java,**/*ErrorDetails.java,**/*ErrorDetails.java,**/*ResourceNotFoundException.java

Step 18 : Use the below command to run the code coverage- make sure before running the sonar, sonarqube should up and running
Go to your project location in the command prompt.
/>mvn clean install sonar:sonar



AddToAny

Contact Form

Name

Email *

Message *