Giter Site home page Giter Site logo

simple-state-machine's Introduction

simple-state-machine

A refactored versin of this exists in the branch.

A simple state machine for Spring Boot projects

After importing into an IDE like STS can be run as Spring Boot application.

This project contains a framework and an illustration of the usage of the framework for a sample project like online order processing.

Usage Workflow

Initial State Pre-event Processor Post-event Final State
DEFAULT -> submit -> orderProcessor() -> orderCreated -> PMTPENDING
PMTPENDING -> pay -> paymentProcessor() -> paymentError -> PMTPENDING
PMTPENDING -> pay -> paymentProcessor() -> paymentSuccess -> COMPLETED
  1. To use this framework first create a state transitions table like above.

  2. Then implement the interfaces ProcessState and ProcessEvent. See OrderState and OrderEvent classes for examples

  3. Identify a primary key for the process. For the order process it would be orderId, for a time sheet application it would be userId-week-ending-date etc.

  4. Implement the StateTransitionsManager. See the OrderStateTransitionsManager class for an example.

  5. Implement the Processor class. See the OrderProcessor and the PaymentProcessor classes for examples.

  6. Create a controller class. See the OrderController for an example.

Unit Testing

Unit tests can be run using the "\gradlew test" command.

Integration Testing

The application can be run using the ".\gradlew bootRun" command. For the order sample considered in this project, the following two APIs are created to test the order process:

  1. User request to create an order. This API is implemented as GET so it can be tested quickly in the browser. http://localhost:8080/order << creates an order and returns an orderId. Selected product ids are not included in this demo example >>

  2. User makes a payment. This API is also implemented as GET so it can be tested quickly in the browser. http://localhost:8080/order/cart?payment=123&orderId=123 << where orderId is the UUID returned by the first API. Payment value less than 1.00 is considered for the error transition >>

<< for quick testing in a browser both of the above are implemented as GET APIs >> When the above APIs are called the console log displays the state transitions that reflect the above table. (Note: payment=0 is used to mock payment error in this example)

Related

The ideas used in this project can also be adopted in Angular projects. See, for instance, https://github.com/mapteb/framework-for-angular-development

simple-state-machine's People

Contributors

mapteb avatar rasta341 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.