Improve documentation
[nonrtric.git] / test / usecases / oruclosedlooprecovery / goversion / internal / repository / lookupservice_test.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package repository
22
23 import (
24         "errors"
25         "testing"
26
27         "github.com/stretchr/testify/require"
28         "oransc.org/usecase/oruclosedloop/mocks"
29 )
30
31 func TestIdNotMappedError(t *testing.T) {
32         assertions := require.New(t)
33
34         actualError := IdNotMappedError{
35                 Id: "1",
36         }
37         assertions.Equal("O-RU-ID: 1 not mapped.", actualError.Error())
38 }
39
40 func TestNewLookupServiceImpl(t *testing.T) {
41         assertions := require.New(t)
42         mockCsvFileHelper := &mocks.CsvFileHelper{}
43         type args struct {
44                 fileHelper CsvFileHelper
45                 fileName   string
46         }
47         tests := []struct {
48                 name string
49                 args args
50                 want *LookupServiceImpl
51         }{
52                 {
53                         name: "Should return populated service",
54                         args: args{
55                                 fileHelper: mockCsvFileHelper,
56                                 fileName:   "test.csv",
57                         },
58                         want: &LookupServiceImpl{
59                                 csvFileHelper:   mockCsvFileHelper,
60                                 csvFileName:     "test.csv",
61                                 oRuIdToODuIdMap: map[string]string{},
62                         },
63                 },
64         }
65         for _, tt := range tests {
66                 t.Run(tt.name, func(t *testing.T) {
67                         got := NewLookupServiceImpl(tt.args.fileHelper, tt.args.fileName)
68                         assertions.Equal(tt.want, got)
69                 })
70         }
71 }
72
73 func TestLookupServiceImpl_Init(t *testing.T) {
74         assertions := require.New(t)
75         type args struct {
76                 csvFileName     string
77                 mockReturn      [][]string
78                 mockReturnError error
79         }
80         tests := []struct {
81                 name                  string
82                 args                  args
83                 wantedORuIdToODuIdMap map[string]string
84                 wantErr               error
85         }{
86                 {
87                         name: "Init with proper csv file should not return error and map should be initialized",
88                         args: args{
89                                 csvFileName: "./map.csv",
90                                 mockReturn:  [][]string{{"O-RU-ID", "O-DU-ID"}},
91                         },
92                         wantedORuIdToODuIdMap: map[string]string{"O-RU-ID": "O-DU-ID"},
93                 },
94                 {
95                         name: "Init with missing file should return error and map should not be initialized",
96                         args: args{
97                                 csvFileName:     "foo.csv",
98                                 mockReturnError: errors.New("Error"),
99                         },
100                         wantedORuIdToODuIdMap: map[string]string{},
101                         wantErr:               errors.New("Error"),
102                 },
103         }
104         for _, tt := range tests {
105                 t.Run(tt.name, func(t *testing.T) {
106                         mockCsvFileHelper := &mocks.CsvFileHelper{}
107                         mockCsvFileHelper.On("GetCsvFromFile", tt.args.csvFileName).Return(tt.args.mockReturn, tt.args.mockReturnError)
108
109                         s := NewLookupServiceImpl(mockCsvFileHelper, tt.args.csvFileName)
110
111                         err := s.Init()
112
113                         assertions.Equal(tt.wantErr, err, tt.name)
114                         assertions.Equal(tt.wantedORuIdToODuIdMap, s.oRuIdToODuIdMap)
115                         mockCsvFileHelper.AssertNumberOfCalls(t, "GetCsvFromFile", 1)
116                 })
117         }
118 }
119
120 func TestLookupServiceImpl_GetODuID(t *testing.T) {
121         assertions := require.New(t)
122         type fields struct {
123                 csvFileHelper   CsvFileHelper
124                 csvFileName     string
125                 oRuIdToODuIdMap map[string]string
126         }
127         type args struct {
128                 oRuId string
129         }
130         tests := []struct {
131                 name    string
132                 fields  fields
133                 args    args
134                 want    string
135                 wantErr error
136         }{
137                 {
138                         name: "Id mapped should return mapped id",
139                         fields: fields{
140                                 csvFileHelper:   nil,
141                                 csvFileName:     "",
142                                 oRuIdToODuIdMap: map[string]string{"O-RU-ID": "O-DU-ID"},
143                         },
144                         args: args{
145                                 oRuId: "O-RU-ID",
146                         },
147                         want: "O-DU-ID",
148                 },
149                 {
150                         name: "Id not mapped should return IdNotMappedError",
151                         fields: fields{
152                                 csvFileHelper:   nil,
153                                 csvFileName:     "",
154                                 oRuIdToODuIdMap: map[string]string{},
155                         },
156                         args: args{
157                                 oRuId: "O-RU-ID",
158                         },
159                         wantErr: IdNotMappedError{Id: "O-RU-ID"},
160                 },
161         }
162         for _, tt := range tests {
163                 t.Run(tt.name, func(t *testing.T) {
164                         s := LookupServiceImpl{
165                                 csvFileHelper:   tt.fields.csvFileHelper,
166                                 csvFileName:     tt.fields.csvFileName,
167                                 oRuIdToODuIdMap: tt.fields.oRuIdToODuIdMap,
168                         }
169
170                         got, err := s.GetODuID(tt.args.oRuId)
171
172                         assertions.Equal(tt.wantErr, err, tt.name)
173                         assertions.Equal(tt.want, got, tt.name)
174                 })
175         }
176 }