Change mock generation for tests
[nonrtric/rapp/orufhrecovery.git] / goversion / internal / repository / lookupservice.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         "fmt"
25 )
26
27 type IdNotMappedError struct {
28         Id string
29 }
30
31 func (inme IdNotMappedError) Error() string {
32         return fmt.Sprintf("O-RU-ID: %v not mapped.", inme.Id)
33 }
34
35 //go:generate mockery --name LookupService
36 type LookupService interface {
37         Init() error
38         GetODuID(oRuId string) (string, error)
39 }
40
41 type LookupServiceImpl struct {
42         csvFileHelper CsvFileHelper
43         csvFileName   string
44
45         oRuIdToODuIdMap map[string]string
46 }
47
48 func NewLookupServiceImpl(fileHelper CsvFileHelper, fileName string) *LookupServiceImpl {
49         s := LookupServiceImpl{
50                 csvFileHelper: fileHelper,
51                 csvFileName:   fileName,
52         }
53         s.oRuIdToODuIdMap = make(map[string]string)
54         return &s
55 }
56
57 func (s LookupServiceImpl) Init() error {
58         if csvData, err := s.csvFileHelper.GetCsvFromFile(s.csvFileName); err == nil {
59                 for _, each := range csvData {
60                         s.oRuIdToODuIdMap[each[0]] = each[1]
61                 }
62                 return nil
63         } else {
64                 return err
65         }
66 }
67
68 func (s LookupServiceImpl) GetODuID(oRuId string) (string, error) {
69         if oDuId, ok := s.oRuIdToODuIdMap[oRuId]; ok {
70                 return oDuId, nil
71         } else {
72                 return "", IdNotMappedError{Id: oRuId}
73         }
74 }