fa9e5b133fb1c3c2c68f26e0c7c48c7b4b8f2edb
[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         "encoding/json"
25         "github.com/stretchr/testify/assert"
26         "net"
27         "net/http"
28         "net/http/httptest"
29         "os"
30         "testing"
31         "time"
32
33         "gerrit.o-ran-sc.org/r/ric-plt/alarm-go.git/alarm"
34 )
35
36 var alarmer *alarm.RICAlarm
37 var managerSim *httptest.Server
38
39 // Test cases
40 func TestAlarmInitSuccess(t *testing.T) {
41         os.Setenv("ALARM_MANAGER_URL", "http://localhost:8080")
42         managerSim = CreateAlarmManagerSim(t, "POST", "/ric/v1/alarms", http.StatusOK, nil)
43
44         a, err := alarm.InitAlarm("my-pod-lib", "my-app")
45         assert.Nil(t, err, "init failed")
46         assert.Equal(t, false, a == nil)
47
48         alarmer = a
49         time.Sleep(time.Duration(5 * time.Second))
50 }
51
52 func TestAlarmRaiseSuccess(t *testing.T) {
53         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
54
55         err := alarmer.Raise(a)
56         assert.Nil(t, err, "raise failed")
57 }
58
59 func TestAlarmClearSuccess(t *testing.T) {
60         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
61
62         err := alarmer.Clear(a)
63         assert.Nil(t, err, "clear failed")
64 }
65
66 func TestAlarmReraiseSuccess(t *testing.T) {
67         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
68
69         err := alarmer.Reraise(a)
70         assert.Nil(t, err, "re-raise failed")
71 }
72
73 func TestAlarmClearAllSuccess(t *testing.T) {
74         err := alarmer.ClearAll()
75         assert.Nil(t, err, "clearAll failed")
76 }
77
78 func TestAlarmSendSuccess(t *testing.T) {
79         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
80
81         consumer := func(m alarm.AlarmMessage) {
82                 assert.Equal(t, m.ManagedObjectId, a.ManagedObjectId)
83                 assert.Equal(t, m.ApplicationId, a.ApplicationId)
84                 assert.Equal(t, m.SpecificProblem, a.SpecificProblem)
85                 assert.Equal(t, m.PerceivedSeverity, a.PerceivedSeverity)
86                 assert.Equal(t, m.AdditionalInfo, a.AdditionalInfo)
87                 assert.Equal(t, m.IdentifyingInfo, a.IdentifyingInfo)
88                 assert.Equal(t, m.AlarmAction, alarm.AlarmActionRaise)
89         }
90
91         go alarmer.ReceiveMessage(consumer)
92         time.Sleep(time.Duration(1 * time.Second))
93
94         err := alarmer.Raise(a)
95         assert.Nil(t, err, "send failed")
96 }
97
98 func TestSetManagedObjectIdSuccess(t *testing.T) {
99         alarmer.SetManagedObjectId("new-pod")
100
101         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
102         assert.Equal(t, a.ManagedObjectId, "new-pod")
103 }
104
105 func TestSetApplicationIdSuccess(t *testing.T) {
106         alarmer.SetApplicationId("new-app")
107
108         a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
109         assert.Equal(t, a.ApplicationId, "new-app")
110 }
111
112 func TestTeardown(t *testing.T) {
113         managerSim.Close()
114 }
115
116 func CreateAlarmManagerSim(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server {
117         l, err := net.Listen("tcp", "localhost:8080")
118         if err != nil {
119                 t.Error("Failed to create listener: " + err.Error())
120         }
121         ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
122                 assert.Equal(t, r.Method, method)
123                 assert.Equal(t, r.URL.String(), url)
124
125                 w.Header().Add("Content-Type", "application/json")
126                 w.WriteHeader(status)
127                 b, _ := json.Marshal(respData)
128                 w.Write(b)
129         }))
130         ts.Listener.Close()
131         ts.Listener = l
132
133         ts.Start()
134
135         return ts
136 }