summaryrefslogtreecommitdiffstats
path: root/pnda-ztt-app/src/test/scala/com/cisco/ztt/cisco/telemetry/xr/JsonParserTest.scala
blob: c08ae2f596d115fee7f455936d7aad393f961f5d (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
/*
 * Copyright (c) 2018 Cisco Systems. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cisco.ztt.cisco.telemetry.xr

import org.scalatest.FlatSpec
import org.scalatest.Matchers
import org.json4s.JsonAST.JArray
import com.cisco.ztt.cisco.xr.telemetry.JsonParser

class JsonParserTest extends FlatSpec with Matchers {
  val cpu_json = """
{
    "Source": "172.16.1.157:27059",
    "Telemetry": {
        "node_id_str": "IOS-XRv9k-edge-1",
        "subscription_id_str": "cpu",
        "encoding_path": "Cisco-IOS-XR-wdsysmon-fd-oper:system-monitoring/cpu-utilization",
        "collection_id": 265673,
        "collection_start_time": 1505905434090,
        "msg_timestamp": 1505905434090,
        "collection_end_time": 1505905434103
    },
    "Rows": [
        {
            "Timestamp": 1505905434099,
            "Keys": {
                "node-name": "0/RP0/CPU0"
            },
            "Content": {
                "process-cpu_PIPELINE_EDIT": [
                    {
                        "process-cpu-fifteen-minute": 0,
                        "process-cpu-five-minute": 0,
                        "process-cpu-one-minute": 0,
                        "process-id": 1,
                        "process-name": "init"
                    },
                    {
                        "process-cpu-fifteen-minute": 0,
                        "process-cpu-five-minute": 0,
                        "process-cpu-one-minute": 0,
                        "process-id": 1544,
                        "process-name": "bash"
                    },
                    {
                        "process-cpu-fifteen-minute": 0,
                        "process-cpu-five-minute": 0,
                        "process-cpu-one-minute": 0,
                        "process-id": 26436,
                        "process-name": "sleep"
                    }
                ],
                "total-cpu-fifteen-minute": 6,
                "total-cpu-five-minute": 6,
                "total-cpu-one-minute": 6
            }
        }
    ]
}
    """

    "JsonParser" should "successfully parse cpu telemetry JSON" in {
        val event = JsonParser.parse(cpu_json)

        event.Telemetry.subscription_id_str should be ("cpu")
        event.Rows(0).Keys.size should be (1)

        val subrows = event.Rows(0).Content("process-cpu_PIPELINE_EDIT")
        val extracted = JsonParser.array(subrows)

        extracted.size should be (3)
        extracted(0).size should be (5)
    }

  val null_keys = """
{
  "Source": "172.16.1.157:49227",
  "Telemetry": {
    "node_id_str": "IOS-XRv9k-edge-1",
    "subscription_id_str": "Logging",
    "encoding_path": "Cisco-IOS-XR-infra-syslog-oper:syslog/logging-statistics",
    "collection_id": 925712,
    "collection_start_time": 1507552918199,
    "msg_timestamp": 1507552918199,
    "collection_end_time": 1507552918203
  },
  "Rows": [
    {
      "Timestamp": 1507552918201,
      "Keys": null,
      "Content": {
        "buffer-logging-stats": {
          "buffer-size": 2097152,
          "is-log-enabled": "true",
          "message-count": 221,
          "severity": "message-severity-debug"
        }
      }
    }
  ]
}
  """

  it should "successfully parse JSON with null keys" in {
      val event = JsonParser.parse(null_keys)

      event.Rows(0).Keys.size should be (0)
  }
}