[RICPLT-2165] Add rnibDataService to support retries
[ric-plt/e2mgr.git] / E2Manager / handlers / rmrmsghandlers / x2_reset_response_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         "testing"
32         "time"
33 )
34
35 func initX2ResetResponseHandlerTest(t *testing.T) (*logger.Logger, X2ResetResponseHandler, *mocks.RnibReaderMock) {
36         log, err := logger.InitLogger(logger.DebugLevel)
37         config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
38         if err!=nil{
39                 t.Errorf("#sctp_errors_notification_handler_test.TestHandleInSession - failed to initialize logger, error: %s", err)
40         }
41         readerMock :=&mocks.RnibReaderMock{}
42         rnibReaderProvider := func() reader.RNibReader {
43                 return readerMock
44         }
45         rnibDataService := services.NewRnibDataService(log, config, rnibReaderProvider, nil)
46
47         h := NewX2ResetResponseHandler(rnibDataService)
48         return log, h, readerMock
49 }
50
51 func TestX2ResetResponseSuccess(t *testing.T) {
52         log, h, readerMock := initX2ResetResponseHandlerTest(t)
53
54         payload, err := tests.BuildPackedX2ResetResponse()
55         if err != nil {
56                 t.Errorf("#x2_reset_response_handler_test.TestX2resetResponse - failed to build and pack X2ResetResponse. Error %x", err)
57         }
58
59         xaction := []byte("RanName")
60         mBuf := rmrCgo.NewMBuf(tests.MessageType, len(payload),"RanName", &payload, &xaction)
61         notificationRequest := models.NotificationRequest{RanName: mBuf.Meid, Len: mBuf.Len, Payload: *mBuf.Payload,
62                 StartTime: time.Now(), TransactionId: string(xaction)}
63         var messageChannel chan<- *models.NotificationResponse
64
65         nb := &entities.NodebInfo{RanName:mBuf.Meid, ConnectionStatus:entities.ConnectionStatus_CONNECTED_SETUP_FAILED,}
66         var rnibErr error
67         readerMock.On("GetNodeb", mBuf.Meid).Return(nb, rnibErr)
68
69         h.Handle(log, &notificationRequest, messageChannel)
70
71         //TODO:Nothing to verify
72 }
73
74 func TestX2ResetResponseReaderFailure(t *testing.T) {
75         log, h, readerMock := initX2ResetResponseHandlerTest(t)
76
77         var payload []byte
78         xaction := []byte("RanName")
79         mBuf := rmrCgo.NewMBuf(tests.MessageType, len(payload),"RanName", &payload, &xaction)
80         notificationRequest := models.NotificationRequest{RanName: mBuf.Meid, Len: mBuf.Len, Payload: *mBuf.Payload,
81                 StartTime: time.Now(), TransactionId: string(xaction)}
82         var messageChannel chan<- *models.NotificationResponse
83
84         var nb *entities.NodebInfo
85         rnibErr  := common.NewResourceNotFoundError("nodeb not found")
86         readerMock.On("GetNodeb", mBuf.Meid).Return(nb, rnibErr)
87
88         h.Handle(log, &notificationRequest, messageChannel)
89
90         //TODO:Nothing to verify
91 }
92
93 func TestX2ResetResponseUnpackFailure(t *testing.T) {
94         log, h, readerMock := initX2ResetResponseHandlerTest(t)
95
96         payload := []byte("not valid payload")
97         xaction := []byte("RanName")
98         mBuf := rmrCgo.NewMBuf(tests.MessageType, len(payload),"RanName", &payload, &xaction)
99         notificationRequest := models.NotificationRequest{RanName: mBuf.Meid, Len: mBuf.Len, Payload: *mBuf.Payload,
100                 StartTime: time.Now(), TransactionId: string(xaction)}
101         var messageChannel chan<- *models.NotificationResponse
102
103         nb := &entities.NodebInfo{RanName:mBuf.Meid, ConnectionStatus:entities.ConnectionStatus_CONNECTED_SETUP_FAILED,}
104         var rnibErr error
105         readerMock.On("GetNodeb", mBuf.Meid).Return(nb, rnibErr)
106
107         h.Handle(log, &notificationRequest, messageChannel)
108
109         //TODO:Nothing to verify
110 }