Skip to content

3.1.2 SUBMOD.001.002 : Upload AML File

NamidM edited this page May 20, 2021 · 24 revisions

Contents

Changelog

Version Date Author Comment
0.1 17.01.2021 Namid Marxen und Nils-Christopher Wiesenauer created
1.0 14.05.2021 Namid Marxen und Nils-Christopher Wiesenauer finished

1. Scope

The Module Documentations (MODs) describes the architecture, the interfaces and the main features of the module. It also describes the module/component test including the results. It can also serve as a programming or integration manual for the module. If there are some risks related to the module itself, they shall be noted and commented within this document.

2. Definitions

AML: Automation Markup Language

CRUD: Create Read Update Delete

GUI: Graphical User Interface

JSend: A specification that lays down some rules for how JSON responses from web servers should be formatted

SAS: System Architecture Specification

SRS: System Requirements Specification

3 Module requirements

This submodule contains the endpoint for uploading an AML file to the database.

This REST API is the most important module because it contains the logic for converting the AML file to JSON. It also includes the authorization to the database. When a file is uploaded to the server it is converted from an AML format to a JSON format so that it can be saved in the MongoDB database. The content of the files are saved as a string and then converted into one JSON object, so that each file has their own database entry, with the Base64 encoded text as the content.

3.1 User view

The submodule describes that the user has the ability to upload files from his computer to the database. He does this by uploading the file in a dialog in the GUI and the GUI sends a POST request to the REST API.

3.2 Requirements

This submodule contains the requirement LF20 which specifies that the user can upload files to the database. The REST API processes the uploaded data and sends a request to the database to upload the file.

3.3 Module Context

The submodule to upload AML files that are sent from the GUI provides the functionality to upload files to the database. The user send a file with the GUI and sends a POST request. When the file is converted, the file is uploaded to the database.

Component REST API Submodule 2

4. Analysis

This submodule provides the user with the ability to upload files via the GUI. The uploaded file is sent to the REST API with a POST request and the API sends a create request to the database. This function is important, because it provides the application with data and without it, the user would not have the ability to upload files.

5. Design

We included all /file routes into our server.js where the FILE schema is imported to have every endpoint together and clear.

5.1 Risks

When the function to upload is not available the user can not upload files from his computer anymore. When a file needs to be shared, this would not be possible anymore.

6. Implementation

Link to sourcecode: server.js

6.1 Get documents from database

The needed /file POST endpoint is defined below. Here we are using the created FILE model to store file documents from MongoDB by using ´FILE.addFile()´. The file must be sent in the request body. (body: { file: ... }).

const FILE = require('./models/file');
...
app.post('/file', (req, res) => {
  const file = req.body.file;
  >> 6.2 Validation check
});
...

6.2 Validation check

In this codeblock we are checking the posted request body. The file in our req.body needs to have base64, name and the size. After this check, we are checking the file typ, if its allowed. process.env.ALLOWED_FILES gives us all allowed file types. If the provided data is not allowed to be saved, we are sending a JSend with status 'error' and a message back. In the last step we are calculation our file size by using Buffer.

...
if (file && file.base64 && file.name && file.size) {
  const availableFileTypes = process.env.ALLOWED_FILES.split(',');
  if(!file.type) {
    file.type = 'application/xml';
  }
  if (availableFileTypes.includes(`.${file.name.split('.').pop().toLowerCase()}`)) {
    file.size = Buffer.from(file.base64.split(',')[1], 'base64').length;
    file.base64 = file.base64.split(',')[1];
    FILE.addFile(file, (err, savedFile) => {
      if (!err && savedFile) {
        savedFile.base64 = null;
        res.status(200).send({ status: 'success', data: savedFile });
      } else {
        console.log(err.message)
        res.status(200).send({ status: 'error', message: 'Unable to add file' });
      }
    });
  } else {
    res.status(200).send({ status: 'error', message: 'File type is not allowed' });
  }
} else if (file && file.content && file.name) {  
  file.base64 = new Buffer(file.content).toString('base64');
  delete file.content;
  file.date = new Date();
  file.size = Buffer.from(file.base64, 'base64').length;
  file.type = 'application/xml';  
  file.name += '.aml';  
  FILE.addFile(
    file,
    (err, file) => {
      if (!err && file) {
        res.status(200).send({ status: 'success', data: file });
      } else {
        res.status(200).send({ status: 'error', message: 'Unable to save new file' });
      }
    }
  );
} else {
  res.status(200).send({ status: 'error', message: 'No file was uploaded. Please try again' });
}
...

6.3 Storage of file

After validation and modifying we are able to store the file in our database.

FILE.addFile(file, (err, savedFile) => {
  if (!err && savedFile) {
    savedFile.base64 = null;
    res.status(200).send({ status: 'success', data: savedFile });
  } else {
    console.log(err.message)
    res.status(200).send({ status: 'error', message: 'Unable to add file' });
  }
});

7. Module Test

Module Test Documentation

8. Summary

The endpoint/submodule to upload an AML file is neccessary to provide the user with the ability to upload AML files. It is triggered by a request from the GUI when the user wants to upload a local AML file from his computer. The request is processed by the REST API and another request is sent to the database.

The implementation of the module is descriped with SwaggerUI.

9. Appendix

9.1 References

  1. SRS (System Requirements Specification)
  2. SAS (System Architecuture Specification)
  3. Systemtestplan

9.2 Sourcecode

The source code of this module can be found here: aml-database-management-api/server.js

9.3 Module Test Cases

Testcase <TC.REST.002.F> (POST call)

Testcase ID: TC.REST.002.F
Testcase Name: POST call
Requirement ID(s): LF110, LF100, LF30
Description: The test case verifies that the REST API POST call works as documented in the SRS

Test Steps:

Step Action Expected Result
1 Open Postman and send the following POST request: localhost:3000/file and use the Testdata below as body Verify that you receive a JSON response containing "status": "success", as well as the just object that hast just been added
2 Send the following GET request: localhost:3000/file Verify that the file is now present in the database

Testdata:

{
    "file": {
    "base64": "data:application/xml;base64,ZnVlciBiYWxsdWY=",
    "name": "test-file.xml",
    "size": 560,
    "type": "text/xml"
    }
}
Clone this wiki locally