First go version of o-ru-closed-loop
[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         "reflect"
26         "testing"
27
28         "oransc.org/usecase/oruclosedloop/mocks"
29 )
30
31 func TestNewLookupServiceImpl(t *testing.T) {
32         mockCsvFileHelper := &mocks.CsvFileHelper{}
33         type args struct {
34                 fileHelper CsvFileHelper
35                 fileName   string
36         }
37         tests := []struct {
38                 name string
39                 args args
40                 want *LookupServiceImpl
41         }{
42                 {
43                         name: "Should return populated service",
44                         args: args{
45                                 fileHelper: mockCsvFileHelper,
46                                 fileName:   "test.csv",
47                         },
48                         want: &LookupServiceImpl{
49                                 csvFileHelper:   mockCsvFileHelper,
50                                 csvFileName:     "test.csv",
51                                 oRuIdToODuIdMap: map[string]string{},
52                         },
53                 },
54         }
55         for _, tt := range tests {
56                 t.Run(tt.name, func(t *testing.T) {
57                         if got := NewLookupServiceImpl(tt.args.fileHelper, tt.args.fileName); !reflect.DeepEqual(got, tt.want) {
58                                 t.Errorf("NewLookupServiceImpl() = %v, want %v", got, tt.want)
59                         }
60                 })
61         }
62 }
63
64 func TestLookupServiceImpl_Init(t *testing.T) {
65         mockCsvFileHelper := &mocks.CsvFileHelper{}
66         mockCsvFileHelper.On("GetCsvFromFile", "./map.csv").Return([][]string{{"O-RU-ID", "O-DU-ID"}}, nil).Once()
67         mockCsvFileHelper.On("GetCsvFromFile", "foo.csv").Return(nil, errors.New("Error")).Once()
68         type fields struct {
69                 csvFileHelper   CsvFileHelper
70                 csvFileName     string
71                 oRuIdToODuIdMap map[string]string
72         }
73         tests := []struct {
74                 name    string
75                 fields  fields
76                 wantErr bool
77         }{
78                 {
79                         name: "Init with proper csv file should not return error",
80                         fields: fields{
81                                 csvFileHelper:   mockCsvFileHelper,
82                                 csvFileName:     "./map.csv",
83                                 oRuIdToODuIdMap: map[string]string{}},
84                         wantErr: false,
85                 },
86                 {
87                         name: "Init with missing file should return error",
88                         fields: fields{
89                                 csvFileHelper:   mockCsvFileHelper,
90                                 csvFileName:     "foo.csv",
91                                 oRuIdToODuIdMap: map[string]string{},
92                         },
93                         wantErr: true,
94                 },
95         }
96         for _, tt := range tests {
97                 t.Run(tt.name, func(t *testing.T) {
98                         s := LookupServiceImpl{
99                                 csvFileHelper:   tt.fields.csvFileHelper,
100                                 csvFileName:     tt.fields.csvFileName,
101                                 oRuIdToODuIdMap: tt.fields.oRuIdToODuIdMap,
102                         }
103                         if err := s.Init(); (err != nil) != tt.wantErr {
104                                 t.Errorf("LookupServiceImpl.Init() error = %v, wantErr %v", err, tt.wantErr)
105                         } else if !tt.wantErr {
106                                 wantedMap := map[string]string{"O-RU-ID": "O-DU-ID"}
107                                 if !reflect.DeepEqual(wantedMap, s.oRuIdToODuIdMap) {
108                                         t.Errorf("LookupServiceImpl.Init() map not initialized, wanted map: %v, got map: %v", wantedMap, s.oRuIdToODuIdMap)
109                                 }
110                         }
111                 })
112         }
113         mockCsvFileHelper.AssertNumberOfCalls(t, "GetCsvFromFile", 2)
114 }
115
116 func TestLookupServiceImpl_GetODuID(t *testing.T) {
117         type fields struct {
118                 csvFileHelper   CsvFileHelper
119                 csvFileName     string
120                 oRuIdToODuIdMap map[string]string
121         }
122         type args struct {
123                 oRuId string
124         }
125         tests := []struct {
126                 name    string
127                 fields  fields
128                 args    args
129                 want    string
130                 wantErr error
131         }{
132                 {
133                         name: "Id mapped should return mapped id",
134                         fields: fields{
135                                 csvFileHelper:   nil,
136                                 csvFileName:     "",
137                                 oRuIdToODuIdMap: map[string]string{"O-RU-ID": "O-DU-ID"},
138                         },
139                         args: args{
140                                 oRuId: "O-RU-ID",
141                         },
142                         want:    "O-DU-ID",
143                         wantErr: nil,
144                 },
145                 {
146                         name: "Id not mapped should return IdNotMappedError",
147                         fields: fields{
148                                 csvFileHelper:   nil,
149                                 csvFileName:     "",
150                                 oRuIdToODuIdMap: map[string]string{},
151                         },
152                         args: args{
153                                 oRuId: "O-RU-ID",
154                         },
155                         want:    "",
156                         wantErr: IdNotMappedError{Id: "O-RU-ID"},
157                 },
158         }
159         for _, tt := range tests {
160                 t.Run(tt.name, func(t *testing.T) {
161                         s := LookupServiceImpl{
162                                 csvFileHelper:   tt.fields.csvFileHelper,
163                                 csvFileName:     tt.fields.csvFileName,
164                                 oRuIdToODuIdMap: tt.fields.oRuIdToODuIdMap,
165                         }
166                         got, err := s.GetODuID(tt.args.oRuId)
167                         if err != tt.wantErr {
168                                 t.Errorf("LookupServiceImpl.GetODuID() error = %v, wantErr %v", err, tt.wantErr)
169                                 return
170                         }
171                         if got != tt.want {
172                                 t.Errorf("LookupServiceImpl.GetODuID() = %v, want %v", got, tt.want)
173                         }
174                 })
175         }
176 }