2 * Copyright (c) 2020 AT&T Intellectual Property.
3 * Copyright (c) 2020 Nokia.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
25 "github.com/stretchr/testify/assert"
33 "gerrit.o-ran-sc.org/r/ric-plt/alarm-go.git/alarm"
36 var alarmer *alarm.RICAlarm
37 var managerSim *httptest.Server
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)
44 a, err := alarm.InitAlarm("my-pod-lib", "my-app")
45 assert.Nil(t, err, "init failed")
46 assert.Equal(t, false, a == nil)
49 time.Sleep(time.Duration(5 * time.Second))
52 func TestAlarmRaiseSuccess(t *testing.T) {
53 a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
55 err := alarmer.Raise(a)
56 assert.Nil(t, err, "raise failed")
59 func TestAlarmClearSuccess(t *testing.T) {
60 a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
62 err := alarmer.Clear(a)
63 assert.Nil(t, err, "clear failed")
66 func TestAlarmReraiseSuccess(t *testing.T) {
67 a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
69 err := alarmer.Reraise(a)
70 assert.Nil(t, err, "re-raise failed")
73 func TestAlarmClearAllSuccess(t *testing.T) {
74 err := alarmer.ClearAll()
75 assert.Nil(t, err, "clearAll failed")
78 func TestAlarmSendSuccess(t *testing.T) {
79 a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
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)
91 go alarmer.ReceiveMessage(consumer)
92 time.Sleep(time.Duration(1 * time.Second))
94 err := alarmer.Raise(a)
95 assert.Nil(t, err, "send failed")
98 func TestSetManagedObjectIdSuccess(t *testing.T) {
99 alarmer.SetManagedObjectId("new-pod")
101 a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
102 assert.Equal(t, a.ManagedObjectId, "new-pod")
105 func TestSetApplicationIdSuccess(t *testing.T) {
106 alarmer.SetApplicationId("new-app")
108 a := alarmer.NewAlarm(1234, alarm.SeverityMajor, "Some App data", "eth 0 1")
109 assert.Equal(t, a.ApplicationId, "new-app")
112 func TestTeardown(t *testing.T) {
116 func CreateAlarmManagerSim(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server {
117 l, err := net.Listen("tcp", "localhost:8080")
119 t.Error("Failed to create listener: " + err.Error())
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)
125 w.Header().Add("Content-Type", "application/json")
126 w.WriteHeader(status)
127 b, _ := json.Marshal(respData)