|
1 | 1 | # TDEI-python-osw-validation |
2 | | -Python service to Validate the OSW file that is uploaded. |
| 2 | + |
| 3 | +## Introduction |
| 4 | +Service to Validate the OSW files that is uploaded. At the moment, the service does the following: |
| 5 | +- Listens to the topic which is mentioned in `.env` file for any new message (that is triggered when a file is uploaded), example `UPLOAD_TOPIC=osw-upload` |
| 6 | +- Consumes the message and perform following checks - |
| 7 | + - Download the file locally |
| 8 | + - File location is in the message `data.meta.file_upload_path` |
| 9 | + - Uses `python-osw-validation` to validate the file |
| 10 | + - Adds the `isValid` and `validationMessage` keys to the original message |
| 11 | +- Publishes the result to the topic mentioned in `.env` file, example `VALIDATION_TOPIC=osw-validation` |
| 12 | + |
| 13 | +## Getting Started |
| 14 | +The project is built on Python with FastAPI framework. All the regular nuances for a Python project are valid for this. |
| 15 | + |
| 16 | +### System requirements |
| 17 | +| Software | Version | |
| 18 | +|------------|---------| |
| 19 | +| Python | 3.10.x | |
| 20 | + |
| 21 | + |
| 22 | +### Connectivity to cloud |
| 23 | +- Connecting this to cloud will need the following in the `.env` file |
| 24 | + |
| 25 | +```bash |
| 26 | +QUEUECONNECTION=xxxx |
| 27 | +STORAGECONNECTION=xxxx |
| 28 | +VALIDATION_REQ_TOPIC=xxxx |
| 29 | +VALIDATION_REQ_SUB=xxxx |
| 30 | +VALIDATION_RES_TOPIC=xxxx |
| 31 | +CONTAINER_NAME=xxxx |
| 32 | +AUTH_PERMISSION_URL=xxx |
| 33 | + |
| 34 | +``` |
| 35 | + |
| 36 | +The application connect with the `STORAGECONNECTION` string provided in `.env` file and validates downloaded zipfile using `python-osw-validation` package. |
| 37 | +`QUEUECONNECTION` is used to send out the messages and listen to messages. |
| 38 | + |
| 39 | + |
| 40 | +### How to Set up and Build |
| 41 | +Follow the steps to install the python packages required for both building and running the application |
| 42 | + |
| 43 | +1. Setup virtual environment |
| 44 | + ``` |
| 45 | + python3.10 -m venv .venv |
| 46 | + source .venv/bin/activate |
| 47 | + ``` |
| 48 | +
|
| 49 | +2. Install the dependencies. Run the following command in terminal on the same directory as `requirements.txt` |
| 50 | + ``` |
| 51 | + # Installing requirements |
| 52 | + pip install -r requirements.txt |
| 53 | + ``` |
| 54 | +### How to Run the Server/APIs |
| 55 | +
|
| 56 | +1. The http server by default starts with `8000` port |
| 57 | +2. Run server |
| 58 | + ``` |
| 59 | + uvicorn src.main:app --reload |
| 60 | + ``` |
| 61 | +3. By default `get` call on `localhost:8000/health` gives a sample response |
| 62 | +4. Other routes include a `ping` with get and post. Make `get` or `post` request to `http://localhost:8000/health/ping` |
| 63 | +5. Once the server starts, it will start to listening the subscriber(`VALIDATION_REQ_SUB` should be in env file) |
| 64 | +
|
| 65 | +
|
| 66 | +#### Request Format |
| 67 | + |
| 68 | +```json |
| 69 | + { |
| 70 | + "messageId": "tdei_record_id", |
| 71 | + "messageType": "workflow_identifier", |
| 72 | + "data": { |
| 73 | + "file_upload_path": "file_upload_path", |
| 74 | + "user_id": "user_id", |
| 75 | + "tdei_project_group_id": "tdei_project_group_id" |
| 76 | + } |
| 77 | + } |
| 78 | +``` |
| 79 | + |
| 80 | +#### Response Format |
| 81 | + |
| 82 | +```json |
| 83 | + { |
| 84 | + "messageId": "tdei_record_id", |
| 85 | + "messageType": "workflow_identifier", |
| 86 | + "data": { |
| 87 | + "file_upload_path": "file_upload_path", |
| 88 | + "user_id": "user_id", |
| 89 | + "tdei_project_group_id": "tdei_project_group_id", |
| 90 | + "success": true/false, |
| 91 | + "message": "message" // if false the error string else empty string |
| 92 | + }, |
| 93 | + "publishedDate": "published date" |
| 94 | + } |
| 95 | +``` |
| 96 | + |
| 97 | + |
| 98 | +### How to Set up and run the Tests |
| 99 | + |
| 100 | +Make sure you have set up the project properly before running the tests, see above for `How to Setup and Build`. |
| 101 | + |
| 102 | +#### How to run test harness |
| 103 | +1. Add the new set of test inside `tests/test_harness/tests.json` file like - |
| 104 | + ``` |
| 105 | + { |
| 106 | + "Name": "Test Name", |
| 107 | + "Input_file": "test_files/osw_test_case1.json", // Input file path which you want to provide to the test |
| 108 | + "Result": true/false // Defining the test output |
| 109 | + } |
| 110 | + ``` |
| 111 | +2. Test Harness would require a valid `.env` file. |
| 112 | +3. To run the test harness `python tests/test_harness/run_tests.py` |
| 113 | +#### How to run unit test cases |
| 114 | +1. `.env` file is not required for Unit test cases. |
| 115 | +2. To run the unit test cases |
| 116 | + 1. `python test_report.py` |
| 117 | + 2. Above command will run all test cases and generate the html report, in `reports` folder at the root level. |
| 118 | +3. To run the coverage |
| 119 | + 1. `python -m coverage run --source=src -m unittest discover -s tests/unit_tests` |
| 120 | + 2. Above command will run all the unit test cases. |
| 121 | + 3. To generate the coverage report in console |
| 122 | + 1. `coverage report` |
| 123 | + 2. Above command will generate the code coverage report in terminal. |
| 124 | + 4. To generate the coverage report in html. |
| 125 | + 1. `coverage html` |
| 126 | + 2. Above command will generate the html report, and generated html would be in `htmlcov` directory at the root level. |
| 127 | + 5. _NOTE :_ To run the `html` or `report` coverage, 3.i) command is mandatory |
| 128 | +
|
| 129 | +#### How to run integration test cases |
| 130 | +1. `.env` file is required for Unit test cases. |
| 131 | +2. To run the integration test cases, run the below command |
| 132 | + 1. `python test_integration.py` |
| 133 | + 2. Above command will run all integration test cases and generate the html report, in `reports` folder at the root level. |
| 134 | +
|
| 135 | +
|
| 136 | +### Messaging |
| 137 | +
|
| 138 | +This microservice deals with two topics/queues. |
| 139 | +- upload queue from osw-upload |
| 140 | +- validation queue from osw-validation |
| 141 | +
|
| 142 | +
|
| 143 | +#### Incoming |
| 144 | +The incoming messages will be from the upload queue `osw-upload`. |
| 145 | +The format is mentioned in [osw-upload.json](./src/assets/osw-upload.json) |
| 146 | +
|
| 147 | +#### Outgoing |
| 148 | +The outgoing messages will be to the `osw-validation` topic. |
| 149 | +The format of the message is at [osw-validation.json](./src/assets/osw-validation.json) |
| 150 | +
|
| 151 | +
|
0 commit comments