Static RT for alarm
[ric-plt/alarm-go.git] / alarm / alarm_test.go
1 /*
2  *  Copyright (c) 2020 AT&T Intellectual Property.
3  *  Copyright (c) 2020 Nokia.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18  * platform project (RICP).
19  */
20
21 package alarm_test
22
23 import (
24         "github.com/stretchr/testify/assert"
25         "testing"
26         "time"
27
28         "gerrit.o-ran-sc.org/r/ric-plt/alarm-go.git/alarm"
29 )
30
31 var alarmer *alarm.RICAlarm
32
33 // Test cases
34 func TestAlarmInitSuccess(t *testing.T) {
35         a, err := alarm.InitAlarm("my-pod-lib", "my-app")
36         assert.Nil(t, err, "init failed")
37         assert.Equal(t, false, a == nil)
38
39         alarmer = a
40         time.Sleep(time.Duration(5 * time.Second))
41 }
42
43 func TestAlarmRaiseSuccess(t *testing.T) {
44         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
45
46         err := alarmer.Raise(a)
47         assert.Nil(t, err, "raise failed")
48 }
49
50 func TestAlarmClearSuccess(t *testing.T) {
51         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
52
53         err := alarmer.Clear(a)
54         assert.Nil(t, err, "clear failed")
55 }
56
57 func TestAlarmReraiseSuccess(t *testing.T) {
58         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
59
60         err := alarmer.Reraise(a)
61         assert.Nil(t, err, "re-raise failed")
62 }
63
64 func TestAlarmClearAllSuccess(t *testing.T) {
65         err := alarmer.ClearAll()
66         assert.Nil(t, err, "clearAll failed")
67 }
68
69 func TestAlarmSendSuccess(t *testing.T) {
70         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
71
72         consumer := func(m alarm.AlarmMessage) {
73                 assert.Equal(t, m.ManagedObjectId, a.ManagedObjectId)
74                 assert.Equal(t, m.ApplicationId, a.ApplicationId)
75                 assert.Equal(t, m.SpecificProblem, a.SpecificProblem)
76                 assert.Equal(t, m.PerceivedSeverity, a.PerceivedSeverity)
77                 assert.Equal(t, m.AdditionalInfo, a.AdditionalInfo)
78                 assert.Equal(t, m.IdentifyingInfo, a.IdentifyingInfo)
79                 assert.Equal(t, m.AlarmAction, alarm.AlarmActionRaise)
80         }
81
82         go alarmer.ReceiveMessage(consumer)
83         time.Sleep(time.Duration(1 * time.Second))
84
85         err := alarmer.Raise(a)
86         assert.Nil(t, err, "send failed")
87 }
88
89 func TestSetManagedObjectIdSuccess(t *testing.T) {
90         alarmer.SetManagedObjectId("new-pod")
91
92         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
93         assert.Equal(t, a.ManagedObjectId, "new-pod")
94 }
95
96 func TestSetApplicationIdSuccess(t *testing.T) {
97         alarmer.SetApplicationId("new-app")
98
99         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
100         assert.Equal(t, a.ApplicationId, "new-app")
101 }