[RICPLT-2165] Add rnibDataService to support retries
[ric-plt/e2mgr.git] / E2Manager / handlers / rmrmsghandlers / x2_reset_request_notification_handler_test.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 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
18 package rmrmsghandlers
19
20 import (
21         "e2mgr/configuration"
22         "e2mgr/logger"
23         "e2mgr/mocks"
24         "e2mgr/models"
25         "e2mgr/rmrCgo"
26         "e2mgr/services"
27         "e2mgr/tests"
28         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
29         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
30         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
31         "github.com/stretchr/testify/assert"
32         "testing"
33         "time"
34 )
35
36 func initX2ResetRequestNotifHandlerTest(t *testing.T) (*logger.Logger, X2ResetRequestNotificationHandler, *mocks.RnibReaderMock) {
37         log := initLog(t)
38         config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
39         readerMock := &mocks.RnibReaderMock{}
40         readerProvider := func() reader.RNibReader {
41                 return readerMock
42         }
43         rnibDataService := services.NewRnibDataService(log, config, readerProvider, nil)
44
45         h := NewX2ResetRequestNotificationHandler(rnibDataService)
46         return log, h, readerMock
47 }
48
49 func TestX2ResetRequestNotifSuccess(t *testing.T) {
50         log, h, readerMock := initX2ResetRequestNotifHandlerTest(t)
51
52         payload := []byte("payload")
53
54         xaction := []byte("RanName")
55         mBuf := rmrCgo.NewMBuf(tests.MessageType, len(payload), "RanName", &payload, &xaction)
56         notificationRequest := models.NotificationRequest{RanName: mBuf.Meid, Len: mBuf.Len, Payload: *mBuf.Payload,
57                 StartTime: time.Now(), TransactionId: string(xaction)}
58
59         nb := &entities.NodebInfo{RanName: mBuf.Meid, ConnectionStatus: entities.ConnectionStatus_CONNECTED,}
60         var rnibErr error
61         readerMock.On("GetNodeb", mBuf.Meid).Return(nb, rnibErr)
62
63         messageChannel := make(chan *models.NotificationResponse)
64
65         go h.Handle(log, &notificationRequest, messageChannel)
66
67         result := <-messageChannel
68         assert.Equal(t, result.RanName, mBuf.Meid)
69         assert.Equal(t, result.MgsType, rmrCgo.RIC_X2_RESET_RESP)
70 }
71
72 func TestHandleX2ResetRequestNotifShuttingDownStatus(t *testing.T) {
73         log, h, readerMock := initX2ResetRequestNotifHandlerTest(t)
74         var payload []byte
75
76         xaction := []byte("RanName")
77         mBuf := rmrCgo.NewMBuf(tests.MessageType, len(payload), "RanName", &payload, &xaction)
78         notificationRequest := models.NotificationRequest{RanName: mBuf.Meid, Len: mBuf.Len, Payload: *mBuf.Payload,
79                 StartTime: time.Now(), TransactionId: string(xaction)}
80
81         nb := &entities.NodebInfo{RanName: mBuf.Meid, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN,}
82         var rnibErr error
83
84         readerMock.On("GetNodeb", mBuf.Meid).Return(nb, rnibErr)
85
86         h.Handle(log, &notificationRequest, nil)
87 }
88
89 func TestHandleX2ResetRequestNotifDisconnectStatus(t *testing.T) {
90         log, h, readerMock := initX2ResetRequestNotifHandlerTest(t)
91         var payload []byte
92
93         xaction := []byte("RanName")
94         mBuf := rmrCgo.NewMBuf(tests.MessageType, len(payload), "RanName", &payload, &xaction)
95         notificationRequest := models.NotificationRequest{RanName: mBuf.Meid, Len: mBuf.Len, Payload: *mBuf.Payload,
96                 StartTime: time.Now(), TransactionId: string(xaction)}
97
98         nb := &entities.NodebInfo{RanName: mBuf.Meid, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED,}
99         var rnibErr error
100
101         readerMock.On("GetNodeb", mBuf.Meid).Return(nb, rnibErr)
102
103         h.Handle(log, &notificationRequest, nil)
104 }
105
106 func TestHandleX2ResetRequestNotifGetNodebFailed(t *testing.T) {
107
108         log, h, readerMock := initX2ResetRequestNotifHandlerTest(t)
109         var payload []byte
110         xaction := []byte("RanName")
111         mBuf := rmrCgo.NewMBuf(tests.MessageType, len(payload), "RanName", &payload, &xaction)
112         notificationRequest := models.NotificationRequest{RanName: mBuf.Meid, Len: mBuf.Len, Payload: *mBuf.Payload,
113                 StartTime: time.Now(), TransactionId: string(xaction)}
114
115         var nb *entities.NodebInfo
116         rnibErr := &common.ResourceNotFoundError{}
117         readerMock.On("GetNodeb", mBuf.Meid).Return(nb, rnibErr)
118
119         h.Handle(log, &notificationRequest, nil)
120 }