LIGHT

  • News
  • Docs
  • Community
  • Reddit
  • GitHub
Star

Debug with Unit Test

When developing light-portal services, it is crucial to ensure that each endpoint works before loading them from the hybrid server. The best approach is to debug and test with JUnit tests within each service. In this tutorial, we are going to walk through the process with light-portal user-command and user-query services.

This approach has its limitations. It requires that the endpoint tested is working independently without access to other services. If it requires additional services, you need to start one of the hybrid-command or hybrid-query servers. For example, when you are running updateUserById on the user-command service through JUnit, you might need to start the hybrid-query server with user-query service loaded for the query. Another example is when debugging the streams processing on the user-query, you might need to either run a JUnit test case from user-command to publish an event to Kafka or start a hybrid-command server and push an event from curl command line or postman.

Environment

Confluent Platform

In different combinations, we need to ensure the local environment is ready for debugging or testing.

Almost all services in light-portal need Kafka for event sourcing and CQRS. We need to start the Confluent platform locally. I have it installed in my tool folder.

cd ~/tool/confluent-6.1.0/bin
confluent local services start

To check the status.

confluent local services status

To stop the Confluent platform.

confluent local services stop
Consul

When you need two or more servers to interact with each other, it is better to have a Consul docker instance running locally for flexibility. Of course, you can use the DirectRegistry in the service.yml to pin each server to a specific IP and port number.

To start a Consul instance with Docker.

cd ~/networknt/light-docker
docker-compose -f docker-compose-consul.yml up -d
Light-oauth2

If there are any interactions between the command side and the query side, we need the light-oauth2 services running locally to provide client credentials tokens. For all portal services, we have security enabled by default, as most of them need the user_id and roles in the JWT token to complete the logic. For command side services, we need the user_id in JWT to generate the eventId object to indicate the owner of the events.

To start the light-oauth2 and register them to the Consul.

cd ~/networknt/light-config-test/light-oauth2/local-consul
docker-compose up -d

Independent Endpoint

With most of the user services depending on the user_id and roles from the JWT token, please set hybrid-security.yml enableVerifyJwt to true. It requires all tests must contain authorization header with a long-lived token.

Here is a valid token for testing.

eyJraWQiOiIxMDAiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJ1cm46Y29tOm5ldHdvcmtudDpvYXV0aDI6djEiLCJhdWQiOiJ1cm46Y29tLm5ldHdvcmtudCIsImV4cCI6MTkwMDE3MDQ3OSwianRpIjoiZ2R2OUNsOGJyR0dUQWhVbjVsZnhfUSIsImlhdCI6MTU4NDgxMDQ3OSwibmJmIjoxNTg0ODEwMzU5LCJ2ZXJzaW9uIjoiMS4wIiwidXNlcl9pZCI6InN0ZXZlaHVAZ21haWwuY29tIiwidXNlcl90eXBlIjoiRU1QTE9ZRUUiLCJjbGllbnRfaWQiOiJmN2Q0MjM0OC1jNjQ3LTRlZmItYTUyZC00YzU3ODc0MjFlNzIiLCJyb2xlcyI6InVzZXIgbGlnaHRhcGkubmV0IGFkbWluIiwic2NvcGUiOlsicG9ydGFsLnIiLCJwb3J0YWwudyJdfQ.rZOj27je8e6HEb1XvIm34zDjVUUaBUHXqvTyQWnw2va1Xbe2_6nX0hk7VqNpdIWUL5-4tcdyQ4iVVGtEpZWIhY9s2vtoL2Zsc1e5WxViqZ8cJ00zyTU1aeOuXjVWyOc2HC754xLl4WS0se6TEpi8Xs5gVrczaNvuvnrVns1KC2q_0cN574EGmT8Fbi3EP3j_kMVXf_m7NdxaV2PREbY_bejlgqsH6bWE0CSIgt4olAlgIZUHifZ7mFElXdek7MZ2ywaAitmoyMWYgUhPzoX_EYcCgrusU2KrOFfm-9CGwZ1Wf2LGawfbcnBDRjW28pzzBaHINADXrYOV8cy7NWGEZg

From the jwt.io site, you can see the claims of the token.

{
  "iss": "urn:com:networknt:oauth2:v1",
  "aud": "urn:com.networknt",
  "exp": 1900170479,
  "jti": "gdv9Cl8brGGTAhUn5lfx_Q",
  "iat": 1584810479,
  "nbf": 1584810359,
  "version": "1.0",
  "user_id": "[email protected]",
  "user_type": "EMPLOYEE",
  "client_id": "f7d42348-c647-4efb-a52d-4c5787421e72",
  "roles": "user lightapi.net admin",
  "scope": [
    "portal.r",
    "portal.w"
  ]
}

Debug

Most query side endpoints should work independently. We are going to use the user-query service for this demo. The detail steps to set up the Run/Debug configuration is documented here

The following is a video walkthrough.

  • About Light
    • Overview
    • Testimonials
    • What is Light
    • Features
    • Principles
    • Benefits
    • Roadmap
    • Community
    • Articles
    • Videos
    • License
    • Why Light Platform
  • Getting Started
    • Get Started Overview
    • Environment
    • Light Codegen Tool
    • Light Rest 4j
    • Light Tram 4j
    • Light Graphql 4j
    • Light Hybrid 4j
    • Light Eventuate 4j
    • Light Oauth2
    • Light Portal Service
    • Light Proxy Server
    • Light Router Server
    • Light Config Server
    • Light Saga 4j
    • Light Session 4j
    • Webserver
    • Websocket
    • Spring Boot Servlet
  • Architecture
    • Architecture Overview
    • API Category
    • API Gateway
    • Architecture Patterns
    • CQRS
    • Eco System
    • Event Sourcing
    • Fail Fast vs Fail Slow
    • Integration Patterns
    • JavaEE declining
    • Key Distribution
    • Microservices Architecture
    • Microservices Monitoring
    • Microservices Security
    • Microservices Traceability
    • Modular Monolith
    • Platform Ecosystem
    • Plugin Architecture
    • Scalability and Performance
    • Serverless
    • Service Collaboration
    • Service Mesh
    • SOA
    • Spring is bloated
    • Stages of API Adoption
    • Transaction Management
    • Microservices Cross-cutting Concerns Options
    • Service Mesh Plus
    • Service Discovery
  • Design
    • Design Overview
    • Design First vs Code First
    • Desgin Pattern
    • Service Evolution
    • Consumer Contract and Consumer Driven Contract
    • Handling Partial Failure
    • Idempotency
    • Server Life Cycle
    • Environment Segregation
    • Database
    • Decomposition Patterns
    • Http2
    • Test Driven
    • Multi-Tenancy
    • Why check token expiration
    • WebServices to Microservices
  • Cross-Cutting Concerns
    • Concerns Overview
  • API Styles
    • Light-4j for absolute performance
    • Style Overview
    • Distributed session on IMDG
    • Hybrid Serverless Modularized Monolithic
    • Kafka - Event Sourcing and CQRS
    • REST - Representational state transfer
    • Web Server with Light
    • Websocket with Light
    • Spring Boot Integration
    • Single Page Application
    • GraphQL - A query language for your API
    • Light IBM MQ
    • Light AWS Lambda
    • Chaos Monkey
  • Infrastructure Services
    • Service Overview
    • Light Proxy
    • Light Mesh
    • Light Router
    • Light Portal
    • Messaging Infrastructure
    • Centralized Logging
    • COVID-19
    • Light OAuth2
    • Metrics and Alerts
    • Config Server
    • Tokenization
    • Light Controller
  • Tool Chain
    • Tool Chain Overview
  • Utility Library
  • Service Consumer
    • Service Consumer
  • Development
    • Development Overview
  • Deployment
    • Deployment Overview
    • Frontend Backend
    • Linux Service
    • Windows Service
    • Install Eventuate on Windows
    • Secure API
    • Client vs light-router
    • Memory Limit
    • Deploy to Kubernetes
  • Benchmark
    • Benchmark Overview
  • Tutorial
    • Tutorial Overview
  • Troubleshooting
    • Troubleshoot
  • FAQ
    • FAQ Overview
  • Milestones
  • Contribute
    • Contribute to Light
    • Development
    • Documentation
    • Example
    • Tutorial
“Debug with Unit Test” was last updated: July 15, 2021: update the link for the pfx certificate doc (cc85743)
Improve this page
  • News
  • Docs
  • Community
  • Reddit
  • GitHub
  • About Light
  • Getting Started
  • Architecture
  • Design
  • Cross-Cutting Concerns
  • API Styles
  • Infrastructure Services
  • Tool Chain
  • Utility Library
  • Service Consumer
  • Development
  • Deployment
  • Benchmark
  • Tutorial
  • Troubleshooting
  • FAQ
  • Milestones
  • Contribute