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