2017-04-10 18:54:12 +02:00
|
|
|
package meshviewer
|
2016-03-20 20:00:02 +01:00
|
|
|
|
|
|
|
import (
|
2016-06-16 20:59:58 +02:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2016-03-20 20:00:02 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-03-03 16:19:35 +01:00
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/data"
|
2017-04-10 18:54:12 +02:00
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
2016-03-20 20:00:02 +01:00
|
|
|
)
|
|
|
|
|
2016-06-16 20:59:58 +02:00
|
|
|
type TestNode struct {
|
|
|
|
Nodeinfo *data.NodeInfo `json:"nodeinfo"`
|
|
|
|
Neighbours *data.Neighbours `json:"neighbours"`
|
|
|
|
}
|
|
|
|
|
2016-03-20 20:00:02 +01:00
|
|
|
func TestGenerateGraph(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2017-03-06 09:52:39 +01:00
|
|
|
nodes := testGetNodesByFile("node1.json", "node2.json", "node3.json", "node4.json")
|
2016-06-16 20:59:58 +02:00
|
|
|
|
2017-04-10 18:54:12 +02:00
|
|
|
graph := BuildGraph(nodes)
|
2016-06-16 20:59:58 +02:00
|
|
|
assert.NotNil(graph)
|
|
|
|
assert.Equal(1, graph.Version, "Wrong Version")
|
|
|
|
assert.NotNil(graph.Batadv, "no Batadv")
|
|
|
|
assert.Equal(false, graph.Batadv.Directed, "directed batadv")
|
2017-03-06 09:52:39 +01:00
|
|
|
assert.Len(graph.Batadv.Links, 3, "wrong Links count")
|
|
|
|
assert.Equal(4, testNodesCountWithLinks(graph.Batadv.Links), "wrong unneed nodes in graph")
|
|
|
|
assert.Len(graph.Batadv.Nodes, 4, "wrong Nodes count")
|
2016-06-16 20:59:58 +02:00
|
|
|
// TODO more tests required
|
|
|
|
}
|
|
|
|
|
2017-04-10 18:54:12 +02:00
|
|
|
func testGetNodesByFile(files ...string) *runtime.Nodes {
|
2016-03-20 20:00:02 +01:00
|
|
|
|
2017-04-10 18:54:12 +02:00
|
|
|
nodes := &runtime.Nodes{
|
|
|
|
List: make(map[string]*runtime.Node),
|
2016-03-20 20:00:02 +01:00
|
|
|
}
|
|
|
|
|
2016-06-16 20:59:58 +02:00
|
|
|
for _, file := range files {
|
2016-10-08 11:41:08 +02:00
|
|
|
node := testGetNodeByFile(file)
|
|
|
|
nodes.Update(file, &data.ResponseData{
|
|
|
|
NodeInfo: node.Nodeinfo,
|
|
|
|
Neighbours: node.Neighbours,
|
|
|
|
})
|
2016-06-16 20:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2017-04-10 18:54:12 +02:00
|
|
|
func testGetNodeByFile(filename string) *runtime.Node {
|
2016-06-16 20:59:58 +02:00
|
|
|
testnode := &TestNode{}
|
|
|
|
testfile(filename, testnode)
|
2017-04-10 18:54:12 +02:00
|
|
|
return &runtime.Node{
|
2016-06-16 20:59:58 +02:00
|
|
|
Nodeinfo: testnode.Nodeinfo,
|
|
|
|
Neighbours: testnode.Neighbours,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testfile(name string, obj interface{}) {
|
2017-05-20 14:46:29 +02:00
|
|
|
file, err := ioutil.ReadFile("../../runtime/testdata/" + name)
|
2016-06-16 20:59:58 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(file, obj); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-03-20 20:00:02 +01:00
|
|
|
}
|
2017-03-06 09:52:39 +01:00
|
|
|
|
|
|
|
func testNodesCountWithLinks(links []*GraphLink) int {
|
|
|
|
indexMap := make(map[int]bool)
|
|
|
|
for _, l := range links {
|
|
|
|
indexMap[l.Source] = true
|
|
|
|
indexMap[l.Target] = true
|
|
|
|
}
|
|
|
|
return len(indexMap)
|
|
|
|
}
|