Skip to content

Commit

Permalink
Feature/provision new api (#112)
Browse files Browse the repository at this point in the history
* Added new api provisioning functionality

* Add unit tests

* Changed the UI version in docker-compose file

* Postponed the audits

* Added documentation for provisioning new API

* Changed some docs
  • Loading branch information
vijayg10 authored Nov 12, 2020
1 parent 58ef4fb commit e4669cd
Show file tree
Hide file tree
Showing 20 changed files with 16,572 additions and 16,044 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/api-provisioning-menu-item.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32,034 changes: 16,017 additions & 16,017 deletions audit-resolve.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:
- -c
- "npm start |bunyan"
mojaloop-testing-toolkit-ui:
image: mojaloop/ml-testing-toolkit-ui:v11.3.0
image: mojaloop/ml-testing-toolkit-ui:v11.4.0
ports:
- "6060:6060"
environment:
Expand Down
88 changes: 88 additions & 0 deletions documents/User-Guide-API-Provisioning.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Mojaloop Testing Toolkit

## _API Provisioning_

**Table of Contents**

1. [Overview](#1-overview)

2. [Synchronous API](#1-synchronous-api)

3. [Asynchronous API](#2-asynchronous-api)

4. [Provisioning through UI](#3-api-provisioning-through-ui)


## 1. Overview

The Testing Toolkit is highly configurable and any API can be provisioned into TTK easily.
TTK supports two kinds of APIs

* Synchronous API
* Asynchronous API

## 2. Synchronous API

You just need to provide the **Swagger / OpenAPI** specification file of your synchronous API to the TTK.

- Please copy the specification file in yaml format into the following folder
```
spec_files/<your-api-folder-name>/api_spec.yaml
```
- Add a new entry to the property **API_DEFINITIONS** in _spec_files/system_config.json_
```
{
"type": "<your-api-name>",
"version": "<major_version.minor_version",
"folderPath": "<your-api-folder-name>"
}
```
- Restart the testing toolkit service and that's it. You are all set.

You should be able to send your API requests to TTK and the TTK will validate them against the specification. You can monitor them in the TTK UI.

And also you can go ahead to create your own response rules to get customized responses from TTK through TTK UI.

## 3. Asynchronous API

You should follow the same procedure as Synchronous API and some additional steps are there to provision an Asynchronous API.

- Please copy the specification file in yaml format into the following file
```
spec_files/<your-api-folder-name>/api_spec.yaml
```
- Add a new entry to the property **API_DEFINITIONS** in _spec_files/system_config.json_. Please observe the additional _asynchronous_ flag here.
```
{
"type": "<your-api-name>",
"version": "<major_version.minor_version",
"folderPath": "<your-api-folder-name>",
"asynchronous": true
}
```
- Additionally you should provide some information about callback mappings to associate the asynchronous callbacks to the respective requests. You can add that information into the following file. You can refer the sample files provided for FSPIOP api in the repository.
```
spec_files/<your-api-folder-name>/callback_map.json
```
- Restart the testing toolkit service and that's it. You are all set.

You should be able to send your API requests to TTK and the TTK will validate them against the specification. You can monitor them in the TTK UI.

And also you can go ahead to create your own validation rules, callback rules and response rules to get customized / mock callbacks and responses from TTK through TTK UI.

## 4. Provisioning through UI

![Opening API Management](/assets/images/api-provisioning-menu-item.png)

- You can go to _**API Management**_ navigation tab. Below is the default view of this API Management page.

![API Management Default View](/assets/images/api-provisioning-list-apis-view.png)

- Click on **Add new API** button, a popup window will be opened and you should drag / select your API specification yaml file there.

![API Management File Input Window](/assets/images/api-provisioning-file-input-window.png)

- After your file is been processed and validated by TTK, you can find the information about your API in the window. You can verify the details and change the name and other details populated there.

![API Management Add New API Confirmation](/assets/images/api-provisioning-add-new-api-confirmation.png)

4 changes: 4 additions & 0 deletions documents/User-Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ The document below covers the use-cases with the **Mojaloop Simulator** and **Mo
### Rules Engine (Advanced Users)

- [Rules Engine Documentation](/documents/RULES_ENGINE.md)

### Provisioning new API

- [API Provisioning User Guide](/documents/User-Guide-API-Provisioning.md)
98 changes: 92 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ml-testing-toolkit",
"description": "Testing Toolkit for Mojaloop implementations",
"version": "11.3.0",
"version": "11.4.0",
"license": "Apache-2.0",
"author": "Vijaya Kumar Guthi, ModusBox Inc.",
"contributors": [
Expand Down Expand Up @@ -97,6 +97,7 @@
"lodash": "^4.17.15",
"ml-testing-toolkit-shared-lib": "11.0.0",
"mongoose": "5.10.0",
"multer": "^1.4.2",
"mustache": "3.1.0",
"node-dir": "0.1.17",
"node-strings": "^1.0.2",
Expand Down
104 changes: 104 additions & 0 deletions src/lib/api-management.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*****
License
--------------
Copyright © 2017 Bill & Melinda Gates Foundation
The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Contributors
--------------
This is the official list of the Mojaloop project contributors for this file.
Names of the original copyright holders (individuals or organizations)
should be listed with a '*' in the first column. People who have
contributed from an organization can be listed under the organization
that actually holds the copyright for their contributions (see the
Gates Foundation organization for an example). Those individuals should have
their names indented and be marked with a '-'. Email address can be added
optionally within square brackets <email>.
* Gates Foundation
* ModusBox
* Vijaya Kumar Guthi <[email protected]> (Original Author)
--------------
******/

const OpenApiBackend = require('openapi-backend').default
const Utils = require('./utils')
const path = require('path')
const Config = require('./config')
const OpenApiDefinitionsModel = require('./mocking/openApiDefinitionsModel')
const OpenApiMockHandler = require('./mocking/openApiMockHandler')

const apiDefinitionsPath = 'spec_files/api_definitions/'

const validateDefinition = async (apiFilePath) => {
const newApi = new OpenApiBackend({
definition: path.join(apiFilePath),
strict: true,
quick: true
})
await newApi.loadDocument()
newApi.validateDefinition()
return newApi.document
}

const addDefinition = async (apiFilePath, name, version, asynchronous) => {
// Validate the definition first
await validateDefinition(apiFilePath)
// Create a folder with api name and copy the uploaded file there
await Utils.makeDirectoryAsync(apiDefinitionsPath + name)
await Utils.renameFileAsync(apiFilePath, apiDefinitionsPath + name + '/api_spec.yaml')
// Add the entry to system config
const apiDefinitions = Config.getSystemConfig().API_DEFINITIONS
apiDefinitions.push({
type: name,
version,
folderPath: name,
asynchronous: asynchronous === 'true' || asynchronous === 'TRUE',
additionalApi: true
})
await Config.setSystemConfig({ API_DEFINITIONS: apiDefinitions })

// Reload the opneapi definitions model
await OpenApiDefinitionsModel.refreshApiDefinitions()

// Reload the openapimock handlers
OpenApiMockHandler.initilizeMockHandler()
}

const deleteDefinition = async (name, version) => {
// Make sure the requested API is not an inbuilt API
const apiDefinitions = Config.getSystemConfig().API_DEFINITIONS
const matchedApiIndex = apiDefinitions.findIndex(item => {
return item.type === name && item.version === version
})

if (matchedApiIndex === -1) {
throw new Error('Requested API is not found')
}
const matchedApi = apiDefinitions[matchedApiIndex]
if (!matchedApi.additionalApi) {
throw new Error('Requested API is an inbuilt API and can not be deleted')
}

// Remove entry in system config file
apiDefinitions.splice(matchedApiIndex, 1)
await Config.setSystemConfig({ API_DEFINITIONS: apiDefinitions })

// Remove the folder from spec_files
await Utils.rmdirAsync(apiDefinitionsPath + name, { recursive: true })

// Refresh openapi Definitions
await OpenApiDefinitionsModel.refreshApiDefinitions()

// Reload the openapimock handlers
OpenApiMockHandler.initilizeMockHandler()

return true
}

module.exports = {
validateDefinition,
addDefinition,
deleteDefinition
}
Loading

0 comments on commit e4669cd

Please sign in to comment.