collect.spec.ts 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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 <http://www.gnu.org/licenses/>.
 */

import * as request from "supertest";
import { expect } from "chai";
import * as server from "../../main";
Rafael Dias's avatar
Rafael Dias committed
24 25 26 27 28 29
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";
30 31 32

describe("API collect controller", () => {

Rafael Dias's avatar
Rafael Dias committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    // 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
        config = ConfigParser.parse("config/test.yaml");
        if (config.adapter === "postgres") {
            fixture = new FixPostgres(config.connection);
            fixture.LoadSource(config.sources, config.struct.create, (err) => {
                if (err) {
                    throw err;
                }
                adapter = new PostgresAdapter(config.connection);
                done();
            });
        }
        else if (config.adapter === "monet") {
            fixture = new FixMonet(config.connection);
            fixture.load(config.sources, config.struct.create, (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: " +
79
                "Could not construct query with the parameters given.";
Rafael Dias's avatar
Rafael Dias committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
                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: " +
96
                "Could not construct query with the parameters given.";
Rafael Dias's avatar
Rafael Dias committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
                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) => {
121
        request(server)
Rafael Dias's avatar
Rafael Dias committed
122 123 124
            .post("/v1/collect/source_1")
            .send({"fields:0": 1, "fields:1": 2})
            .expect(500)
125
            .expect((res: any) => {
Rafael Dias's avatar
Rafael Dias committed
126
                const message = "Query execution failed: " +
127
                "Could not construct query with the parameters given.";
Rafael Dias's avatar
Rafael Dias committed
128
                const error = "The value '1' from 'fields:0' isn't listed on enumtype:0";
129 130 131
                expect(res.body).to.be.an("object");
                expect(res.body).to.have.property("message");
                expect(res.body.message).to.be.eql(message);
Rafael Dias's avatar
Rafael Dias committed
132 133
                expect(res.body.error).to.be.eql(error);

134 135 136
            })
            .end(done);
    });
Rafael Dias's avatar
Rafael Dias committed
137 138 139 140 141 142 143 144 145 146
    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);
147

Rafael Dias's avatar
Rafael Dias committed
148 149 150 151
            })
            .end(done);
    });
    it("should respond 500 when dataType from fields:0 isn't integer", (done) => {
152
        request(server)
Rafael Dias's avatar
Rafael Dias committed
153 154 155
            .post("/v1/collect/source_4")
            .send({"fields:0" : "nope", "fields:1" : 95.5 , "fields:2" : "justabacon"
            , "fields:3" : "1991-25-03" , "fields:4" : 1})
156 157
            .expect(500)
            .expect((res: any) => {
Rafael Dias's avatar
Rafael Dias committed
158 159

                const message = "Query execution failed: " +
160
                "Could not construct query with the parameters given.";
Rafael Dias's avatar
Rafael Dias committed
161
                const error = "The value 'nope' from 'fields:0' isn't a type integer";
162 163 164
                expect(res.body).to.be.an("object");
                expect(res.body).to.have.property("message");
                expect(res.body.message).to.be.eql(message);
Rafael Dias's avatar
Rafael Dias committed
165 166
                expect(res.body.error).to.be.eql(error);

167 168 169
            })
            .end(done);
    });
Rafael Dias's avatar
Rafael Dias committed
170 171 172 173 174 175 176
    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) => {
177

Rafael Dias's avatar
Rafael Dias committed
178
                const message = "Query execution failed: " +
179
                "Could not construct query with the parameters given.";
Rafael Dias's avatar
Rafael Dias committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
                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: " +
198
                "Could not construct query with the parameters given.";
Rafael Dias's avatar
Rafael Dias committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
                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: " +
217
                "Could not construct query with the parameters given.";
Rafael Dias's avatar
Rafael Dias committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
                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: " +
236
                "Could not construct query with the parameters given.";
Rafael Dias's avatar
Rafael Dias committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
                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);
    });
261
});