ea56b0090b82a036daa8f17b8c70d6bdbb8f74f9
[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/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", "my-app")
36         assert.Nil(t, err, "init failed")
37         assert.Equal(t, false, a == nil)
38
39         alarmer = a
40 }
41
42 func TestAlarmRaiseSuccess(t *testing.T) {
43         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
44
45         err := alarmer.Raise(a)
46         assert.Nil(t, err, "raise failed")
47 }
48
49 func TestAlarmClearSuccess(t *testing.T) {
50         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
51
52         err := alarmer.Clear(a)
53         assert.Nil(t, err, "clear failed")
54 }
55
56 func TestAlarmReraiseSuccess(t *testing.T) {
57         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
58
59         err := alarmer.Reraise(a)
60         assert.Nil(t, err, "re-raise failed")
61 }
62
63 func TestAlarmClearAllSuccess(t *testing.T) {
64         err := alarmer.ClearAll()
65         assert.Nil(t, err, "clearAll failed")
66 }
67
68 func TestAlarmSendSuccess(t *testing.T) {
69         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
70
71         consumer := func(m alarm.AlarmMessage) {
72                 assert.Equal(t, m.ManagedObjectId, a.ManagedObjectId)
73                 assert.Equal(t, m.ApplicationId, a.ApplicationId)
74                 assert.Equal(t, m.SpecificProblem, a.SpecificProblem)
75                 assert.Equal(t, m.PerceivedSeverity, a.PerceivedSeverity)
76                 assert.Equal(t, m.AdditionalInfo, a.AdditionalInfo)
77                 assert.Equal(t, m.IdentifyingInfo, a.IdentifyingInfo)
78                 assert.Equal(t, m.AlarmAction, alarm.AlarmActionRaise)
79         }
80
81         go alarmer.ReceiveMessage(consumer)
82         time.Sleep(time.Duration(1 * time.Second))
83
84         m := alarmer.NewAlarmMessage(a, alarm.AlarmActionRaise)
85         err := alarmer.SendMessage(m)
86         assert.Nil(t, err, "send failed")
87 }