summaryrefslogtreecommitdiffstats
path: root/tests/node.d/fronius.validation.spec.js
blob: b7938d52f0cc9aaaaf46ca7aeea9bbc090ffaeeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"use strict";
// SPDX-License-Identifier: GPL-3.0-or-later

var netdata = require("../../node.d/node_modules/netdata");
// remember: subject will be a singleton!
var subject = require("../../node.d/fronius.node");

var service = netdata.service({
    name: "validation",
    module: this
});

describe("fronius response validation", function () {

    it("should do nothing if response is null", function () {
        netdata.send = jasmine.createSpy("send");

        subject.processResponse(service, null);
        var result = netdata.send.calls.count();

        expect(result).toBe(0);
    });

    it("should return null if response is null", function () {
        var result = subject.convertToJson(null);

        expect(result).toBeNull();
    });

    it("should return null and log error if response cannot be parsed", function () {
        netdata.error = jasmine.createSpy("error");

        // trailing commas are enough to create syntax exceptions
        var result = subject.convertToJson("{name,}");

        expect(result).toBeNull();
        expect(netdata.error.calls.count()).toBe(1);
    });

    it("should return true if response is valid", function () {
        var result = subject.isResponseValid({
            "Body": {
                "Data": {
                    "Site": {
                        "Mode": "meter"
                    },
                    "Inverters": {
                        "1": {}
                    }
                }
            }
        });

        expect(result).toBeTruthy();
    });

    it("should return false if response is missing data", function () {
        var result = subject.isResponseValid({
            "Body": {}
        });

        expect(result).toBeFalsy();
    });

    it("should return false if response is missing inverter", function () {
        var result = subject.isResponseValid({
            "Body": {
                "Data": {
                    "Site": {}
                }
            }
        });

        expect(result).toBeFalsy();
    });

    it("should return false if response is missing inverter", function () {
        var result = subject.isResponseValid({
            "Body": {
                "Data": {
                    "Inverters": {}
                }
            }
        });

        expect(result).toBeFalsy();
    });

});

describe("fronius configuration validation", function () {

    it("should return 0 if there are no servers configured", function () {
        var result = subject.configure({});

        expect(result).toBe(0);
    });

    it("should return 0 if the servers array is empty", function () {
        var result = subject.configure({
            "servers": []
        });

        expect(result).toBe(0);
    });

    it("should return 0 if there is one server configured incorrectly", function () {
        var result = subject.configure({
            "servers": [{}]
        });

        expect(result).toBe(0);
    });

    it("should return 1 if there is one server configured", function () {
        subject.serviceExecute = jasmine.createSpy("serviceExecute");
        var name = "solar1";
        var result = subject.configure({
            "servers": [{
                "name": name,
                "api_path": "/api/",
                "hostname": "solar1.local"
            }]
        });

        expect(result).toBe(1);
        expect(subject.serviceExecute).toHaveBeenCalledWith(name, "solar1.local/api/", 5);
    });

    it("should return 2 if there are two servers configured", function () {
        subject.serviceExecute = jasmine.createSpy("serviceExecute");
        var name1 = "solar 1";
        var name2 = "solar 2";
        var result = subject.configure({
            "servers": [
                {
                    "name": name1,
                    "api_path": "/",
                    "hostname": "solar1.local"
                },
                {
                    "name": name2,
                    "api_path": "/",
                    "hostname": "solar2.local",
                    "update_every": 3
                }
            ]
        });

        expect(result).toBe(2);
        expect(subject.serviceExecute).toHaveBeenCalledWith(name1, "solar1.local/", 5);
        expect(subject.serviceExecute).toHaveBeenCalledWith(name2, "solar2.local/", 3);
    });

});