Merge "switch drools pdp image to new one"
[integration.git] / test / mocks / datafilecollector-testharness / dr-sim / dmaapDR.js
1 var http = require('http');
2 var https = require('https');
3 var ArgumentParser = require('argparse').ArgumentParser;
4 var express = require('express');
5 const stream = require('stream');
6 var app = express();
7 var fs = require('fs');
8 var path = require('path');
9 var privateKey  = fs.readFileSync('cert/private.key', 'utf8');
10 var certificate = fs.readFileSync('cert/certificate.crt', 'utf8');
11 var credentials = {key: privateKey, cert: certificate};
12 const allPublished = "allPublished";
13 const nonePublished = "nonePublished";
14
15 var parser = new ArgumentParser({
16           version: '0.0.1',
17           addHelp:true,
18           description: 'Datarouter simulator'
19         });
20
21 parser.addArgument('--tc' , { help: 'TC $NoOfTc' } );
22 parser.addArgument('--printtc' ,
23                 {
24                         help: 'Print complete usage help',
25                         action: 'storeTrue'
26                 }
27         );
28
29 var args = parser.parseArgs();
30
31 if (args.tc=="nonePublished") {
32         console.log("TC: nonePublished")
33 }
34 if (args.tc=="allPublished") {
35         console.log("TC: allPublished")
36         //preparations
37 }
38
39 if (args.printtc) {
40         console.log("TC nonePublished: no file has already been published.");
41         console.log("TC allPublished: whatever is the request, this file is considered as published.");
42         console.log("No argument passed: normal behaviour, that is publish if not already published");
43         process.exit(0);
44 }
45
46 var bodyParser = require('body-parser')
47 app.use(bodyParser.urlencoded({ extended: false }))
48
49 // parse application/json
50 app.use(bodyParser.json())
51
52 // parse application/vnd.api+json as json
53 app.use(bodyParser.json({ type: 'application/vnd.api+json' }))
54
55 // parse some custom thing into a Buffer
56 app.use(bodyParser.raw({limit:1024*1024*20, type: 'application/octet-stream' }))
57 // parse an HTML body into a string
58 app.use(bodyParser.text({ type: 'text/html' }))
59 app.get("/",function(req, res){
60         res.send("ok");
61 })
62
63
64 var published = [];
65 app.get('/feedlog/1/',function(req, res){
66         var filename = req.query.filename;
67         if(args.tc == allPublished){
68                 res.send("[" + filename + "]");
69         } else if(args.tc == nonePublished){
70                 res.send("[]");
71         } else {
72                 if (published.includes(filename)) {
73                         res.send("[" + filename + "]");
74                 } else {
75                         res.send("[]");
76                 }
77         }
78 })
79
80
81 app.put('/publish/1/', function (req, res) {
82         var filename = req.query.filename;
83         var type = req.query.type;
84         if(typeof(filename) == 'undefined'){
85                 res.status(400).send({error: 'No filename provided.'});
86         } else if(typeof(type) == 'undefined'){
87                 res.status(400).send({error: 'No type provided.'});
88         } else {
89                 if(args.tc == allPublished){
90                         res.send("[" + filename + "]");
91                 } else if(args.tc == nonePublished){
92                         res.redirect(301, 'http://127.0.0.1:3908/publish/1/'+filename);
93                 } else {
94                         if (!published.includes(filename)) {
95                                 published.push(filename);
96                                 res.redirect(301, 'http://127.0.0.1:3908/publish/1/'+filename);
97                         } else {
98                                 res.send("ok");
99                         }
100                 }
101         }
102 })
103
104
105 var httpServer = http.createServer(app);
106 var httpsServer = https.createServer(credentials, app);
107
108 var httpPort=3906;
109 var httpsPort=3907;
110 httpServer.listen(httpPort);
111 console.log("DR-simulator listening (http) at "+httpPort);
112 httpsServer.listen(httpsPort);
113 console.log("DR-simulator listening (https) at "+httpsPort);