/* * Copyright (C) 2017 Centro de Computacao Cientifica e Software Livre * Departamento de Informatica - Universidade Federal do Parana * * This file is part of blendb. * * blendb is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * blendb is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with blendb. If not, see . */ import * as request from "supertest"; import { expect } from "chai"; import * as server from "../../main"; import { Adapter } from "../../core/adapter"; import { ConfigParser } from "../../util/configParser"; import { Fixture as FixPostgres } from "../../../test/postgres/fixture"; import { Fixture as FixMonet } from "../../../test/monet/fixture"; import { MonetAdapter, MonetConfig } from "../../adapter/monet"; import { PostgresAdapter } from "../../adapter/postgres"; describe("API collect controller", () => { // Initializing let config: any; let adapter: Adapter; let fixture; before(function (done): void { // Arrow function not used to get acces to this and skip the test const configPath = process.env.BLENDB_SCHEMA_FILE; config = ConfigParser.parse(configPath); if (config.adapter === "postgres") { fixture = new FixPostgres(config.connection); fixture.loadSource(config.sources, (err) => { if (err) { throw err; } adapter = new PostgresAdapter(config.connection); done(); }); } else if (config.adapter === "monet") { fixture = new FixMonet(config.connection); fixture.loadSource(config.sources, (err) => { if (err) { throw err; } let parsedConfig: MonetConfig = { user: config.connection.user, dbname: config.connection.database, password: config.connection.password, host: config.connection.host, port: config.connection.port }; adapter = new MonetAdapter(parsedConfig); done(); }); } else { this.skip(); } }); it("should respond 500 when req.params.class does not exist on Sources", (done) => { request(server) .post("/v1/collect/thisisjustatest") .send({"fields:1": 1, "fields:2": 2}) .expect(500) .expect((res: any) => { const message = "Query execution failed: " + "Could not construct query with the given parameters."; const error = "The source named 'thisisjustatest' was not found"; expect(res.body).to.be.an("object"); expect(res.body).to.have.property("message"); expect(res.body.message).to.be.eql(message); expect(res.body.error).to.be.eql(error); }) .end(done); }); it("should respond 500 when fields:0 does not exist on Source", (done) => { request(server) .post("/v1/collect/source_0") .send({"fields:1": 1, "fields:2": 2}) .expect(500) .expect((res: any) => { const message = "Query execution failed: " + "Could not construct query with the given parameters."; const error = "The 'fields:0' wasn't informed on json"; expect(res.body).to.be.an("object"); expect(res.body).to.have.property("message"); expect(res.body.message).to.be.eql(message); expect(res.body.error).to.be.eql(error); }) .end(done); }); it("should respond 200 when data has been stored on source_0", (done) => { request(server) .post("/v1/collect/source_0") .send({"fields:0": "teste", "fields:1": "test1", "fields:2": "teste2"}) .expect(200) .expect((res: any) => { const message = "Data has been successfully received and stored by the server"; expect(res.body).to.be.an("object"); expect(res.body).to.have.property("message"); expect(res.body.message).to.be.eql(message); }) .end(done); }); it("should respond 500 when value isn't defined on enumtype:0 ", (done) => { request(server) .post("/v1/collect/source_1") .send({"fields:0": 1, "fields:1": 2}) .expect(500) .expect((res: any) => { const message = "Query execution failed: " + "Could not construct query with the given parameters."; const error = "The value '1' from 'fields:0' isn't listed on enumtype:0"; expect(res.body).to.be.an("object"); expect(res.body).to.have.property("message"); expect(res.body.message).to.be.eql(message); expect(res.body.error).to.be.eql(error); }) .end(done); }); it("should respond 200 when data has been stored on source_1", (done) => { request(server) .post("/v1/collect/source_1") .send({"fields:0": "male", "fields:1": "test1"}) .expect(200) .expect((res: any) => { const message = "Data has been successfully received and stored by the server"; expect(res.body).to.be.an("object"); expect(res.body).to.have.property("message"); expect(res.body.message).to.be.eql(message); }) .end(done); }); it("should respond 500 when dataType from fields:0 isn't integer", (done) => { request(server) .post("/v1/collect/source_4") .send({"fields:0" : "nope", "fields:1" : 95.5 , "fields:2" : "justabacon" , "fields:3" : "1991-25-03" , "fields:4" : 1}) .expect(500) .expect((res: any) => { const message = "Query execution failed: " + "Could not construct query with the given parameters."; const error = "The value 'nope' from 'fields:0' isn't a type integer"; expect(res.body).to.be.an("object"); expect(res.body).to.have.property("message"); expect(res.body.message).to.be.eql(message); expect(res.body.error).to.be.eql(error); }) .end(done); }); it("should respond 500 when dataType from fields:1 isn't float", (done) => { request(server) .post("/v1/collect/source_4") .send({"fields:0" : 1 , "fields:1" : "notafloat" , "fields:2" : "justabacon" , "fields:3" : "1991-25-03" , "fields:4" : 1}) .expect(500) .expect((res: any) => { const message = "Query execution failed: " + "Could not construct query with the given parameters."; const error = "The value 'notafloat' from 'fields:1' isn't a type float"; expect(res.body).to.be.an("object"); expect(res.body).to.have.property("message"); expect(res.body.message).to.be.eql(message); expect(res.body.error).to.be.eql(error); }) .end(done); }); it("should respond 500 when dataType from fields:2 isn't string", (done) => { request(server) .post("/v1/collect/source_4") .send({"fields:0" : 1 , "fields:1" : 95.5 , "fields:2" : 1 , "fields:3" : "1991-25-03" , "fields:4" : 1}) .expect(500) .expect((res: any) => { const message = "Query execution failed: " + "Could not construct query with the given parameters."; const error = "The value '1' from 'fields:2' isn't a type string"; expect(res.body).to.be.an("object"); expect(res.body).to.have.property("message"); expect(res.body.message).to.be.eql(message); expect(res.body.error).to.be.eql(error); }) .end(done); }); it("should respond 500 when dataType from fields:3 isn't boolean", (done) => { request(server) .post("/v1/collect/source_4") .send({"fields:0" : 1 , "fields:1" : 95.5 , "fields:2" : "teste" , "fields:3" : "notaboolean" , "fields:4" : "1999-10-10"}) .expect(500) .expect((res: any) => { const message = "Query execution failed: " + "Could not construct query with the given parameters."; const error = "The value 'notaboolean' from 'fields:3' isn't a type boolean"; expect(res.body).to.be.an("object"); expect(res.body).to.have.property("message"); expect(res.body.message).to.be.eql(message); expect(res.body.error).to.be.eql(error); }) .end(done); }); it("should respond 500 when the first dataType from fields:4 isn't date", (done) => { request(server) .post("/v1/collect/source_4") .send({"fields:0" : 1 , "fields:1" : 95.5 , "fields:2" : "teste" , "fields:3" : "true" , "fields:4" : "1999-25-25"}) .expect(500) .expect((res: any) => { const message = "Query execution failed: " + "Could not construct query with the given parameters."; const error = "The value '1999-25-25' from 'fields:4' isn't a type date"; expect(res.body).to.be.an("object"); expect(res.body).to.have.property("message"); expect(res.body.message).to.be.eql(message); expect(res.body.error).to.be.eql(error); }) .end(done); }); it("should respond 200 when sucessfull insert data on source_4", (done) => { request(server) .post("/v1/collect/source_4") .send({"fields:0" : 1 , "fields:1" : 95.5 , "fields:2" : "teste" , "fields:3" : "true" , "fields:4" : "1999-10-10"}) .expect(200) .expect((res: any) => { const message = "Data has been successfully received and stored by the server"; expect(res.body).to.be.an("object"); expect(res.body).to.have.property("message"); expect(res.body.message).to.be.eql(message); }) .end(done); }); });