|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 IBM, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy ofthe License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specificlanguage governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package mod |
| 19 | + |
| 20 | +import ( |
| 21 | + "encoding/json" |
| 22 | + "reflect" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/spf13/viper" |
| 26 | + |
| 27 | + "github.com/skydive-project/skydive/contrib/exporters/core" |
| 28 | +) |
| 29 | + |
| 30 | +func areEqualJSON(buf1, buf2 []byte) (bool, error) { |
| 31 | + var o1 interface{} |
| 32 | + var o2 interface{} |
| 33 | + |
| 34 | + var err error |
| 35 | + err = json.Unmarshal(buf1, &o1) |
| 36 | + if err != nil { |
| 37 | + return false, err |
| 38 | + } |
| 39 | + err = json.Unmarshal(buf2, &o2) |
| 40 | + if err != nil { |
| 41 | + return false, err |
| 42 | + } |
| 43 | + return reflect.DeepEqual(o1, o2), nil |
| 44 | +} |
| 45 | + |
| 46 | +func getEncoder(t *testing.T) core.Encoder { |
| 47 | + cfg := viper.New() |
| 48 | + encoder, err := NewEncode(cfg) |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("NewEncode returned unexpected error: %v", err) |
| 51 | + } |
| 52 | + return encoder.(core.Encoder) |
| 53 | +} |
| 54 | + |
| 55 | +func Test_Encode_empty_flows_array(t *testing.T) { |
| 56 | + in := make([]interface{}, 0) |
| 57 | + result, err := getEncoder(t).Encode(in) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("Encode returned unexpected error: %v", err) |
| 60 | + } |
| 61 | + expected := []byte( |
| 62 | + `{ |
| 63 | + "data": [] |
| 64 | + }`) |
| 65 | + equal, err := areEqualJSON(expected, result) |
| 66 | + if err != nil { |
| 67 | + t.Fatalf("Error parsing JSON: %v", err) |
| 68 | + } |
| 69 | + if !equal { |
| 70 | + t.Fatalf("Objects not identical") |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func Test_Encode_flows_array_with_objects(t *testing.T) { |
| 75 | + in := []interface{}{ |
| 76 | + &SecurityAdvisorFlow{ |
| 77 | + Status: "STARTED", |
| 78 | + Start: 1234, |
| 79 | + }, |
| 80 | + &SecurityAdvisorFlow{ |
| 81 | + Status: "ENDED", |
| 82 | + Last: 5678, |
| 83 | + }, |
| 84 | + } |
| 85 | + result, err := getEncoder(t).Encode(in) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("Encode returned unexpected error: %v", err) |
| 88 | + } |
| 89 | + expected := []byte( |
| 90 | + `{ |
| 91 | + "data": [ |
| 92 | + { |
| 93 | + "Status": "STARTED", |
| 94 | + "Start": 1234, |
| 95 | + "Last": 0, |
| 96 | + "UpdateCount": 0 |
| 97 | + }, |
| 98 | + { |
| 99 | + "Status": "ENDED", |
| 100 | + "Start": 0, |
| 101 | + "Last": 5678, |
| 102 | + "UpdateCount": 0 |
| 103 | + } |
| 104 | + ] |
| 105 | + }`) |
| 106 | + equal, err := areEqualJSON(expected, result) |
| 107 | + if err != nil { |
| 108 | + t.Fatalf("Error parsing JSON: %v", err) |
| 109 | + } |
| 110 | + if !equal { |
| 111 | + t.Fatalf("Objects not identical") |
| 112 | + } |
| 113 | +} |
0 commit comments