blob: 326e4f87fc6c3ede350547fe67317d4f2c714b40 [file] [log] [blame]
Chip Boling610117d2021-09-09 11:24:34 -05001/*
2 * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
3 * Copyright 2020-present Open Networking Foundation
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 * NOTE: This file was generated, manual edits will be overwritten!
19 *
20 * Generated by 'goCodeGenerator.py':
21 * https://github.com/cboling/OMCI-parser/README.md
22 */
23
24package generated
25
26import (
Chip Bolingceca0012022-01-25 15:13:31 -060027 "encoding/json"
28 "fmt"
Chip Boling610117d2021-09-09 11:24:34 -050029 "github.com/stretchr/testify/assert"
Chip Bolingceca0012022-01-25 15:13:31 -060030 "io/ioutil"
31 "os"
32 "strconv"
33 "strings"
Chip Boling610117d2021-09-09 11:24:34 -050034 "testing"
35)
36
37func TestClassIDMap(t *testing.T) {
38 for classID, function := range classToManagedEntityMap {
39 managedEntity, omciError := function()
40
41 assert.NotNil(t, managedEntity)
42 assert.Equal(t, omciError.StatusCode(), Success)
43
44 assert.Equal(t, classID, managedEntity.GetClassID())
45 assert.NotNil(t, managedEntity.GetManagedEntityDefinition())
46 }
47}
Chip Bolingceca0012022-01-25 15:13:31 -060048
49func TestAllAttributeNamesMatch(t *testing.T) {
50 // Load test JSON Class and Attribute Name file
51 // NOTE: This JSON file should never be updated as it is used to verify
52 // that attribute name constants never change between versions
53 //
54 type attributeEntry struct {
55 Name string `json:"Name"`
56 CamelCase string `json:"CamelCase"`
57 Final string `json:"Final"`
58 Index uint `json:"Index"`
59 }
60 type classEntry struct {
61 Name string `json:"Name"`
62 Filename string `json:"Filename"`
63 CamelCase string `json:"CamelCase"`
64 ClassID uint16 `json:"ClassID"`
65 Attributes map[string]attributeEntry `json:"Attributes"`
66 }
67 //cwd, getErr := os.Getwd()
68 //assert.NoError(t, getErr)
69 //assert.NotNil(t, cwd)
70 inputFile := "./attrNames_test.json"
71 input, err := os.Open(inputFile)
72 assert.NoError(t, err)
73 assert.NotNil(t, input)
74 defer input.Close()
75
76 inputJson, err2 := ioutil.ReadAll(input)
77 assert.NoError(t, err2)
78 assert.NotNil(t, inputJson)
79
80 var classEntries map[string]classEntry
81
82 err = json.Unmarshal([]byte(inputJson), &classEntries)
83 assert.NoError(t, err)
84 // Make sure every Managed Entity is represented in the map
85
86 for classID, function := range classToManagedEntityMap {
87 // Get attribute string definition
88 classAttrEntry, ok := classEntries[strconv.Itoa(int(classID))]
89 assert.True(t, ok)
90 assert.NotNil(t, classAttrEntry)
91
92 // Get an example class
93 managedEntity, omciError := function()
94
95 assert.NotNil(t, managedEntity)
96 assert.Equal(t, omciError.StatusCode(), Success)
97 assert.Equal(t, classID, managedEntity.GetClassID())
98
99 meDef := managedEntity.GetManagedEntityDefinition()
100 assert.NotNil(t, meDef)
101 assert.Equal(t, meDef.Name, classAttrEntry.CamelCase)
102
103 // Open the Golang class file and make sure the attribute name constants exist
104 golangFile, goFileErr := os.Open(classAttrEntry.Filename)
105 assert.NoError(t, goFileErr)
106 assert.NotNil(t, golangFile)
107 defer golangFile.Close()
108
109 goFileBytes, txtErr := ioutil.ReadAll(golangFile)
110 assert.NoError(t, txtErr)
111 assert.NotNil(t, goFileBytes)
112
113 goFileText := string(goFileBytes)
114 assert.NotNil(t, goFileText)
115 assert.NotEmpty(t, goFileText)
116
117 if meDef.Name == classAttrEntry.CamelCase {
118 attrDefs := meDef.GetAttributeDefinitions()
119 assert.NotNil(t, attrDefs)
120
121 // Walk attribute definitions and see if they match
122 for attrIndex, attr := range attrDefs {
123 assert.NotNil(t, attr)
124
125 attrEntry, found := classAttrEntry.Attributes[strconv.Itoa(int(attrIndex))]
126 assert.True(t, found)
127 assert.NotNil(t, attrEntry)
128
129 assert.Equal(t, attr.Name, attrEntry.CamelCase)
130 if attr.Name == attrEntry.CamelCase {
131 // ManagedEntityId constant common in all files and defined elsewhere
132 var expectedLine string
133 if attrEntry.Index != 0 {
134 expectedLine = fmt.Sprintf("(\"%v\", ", attrEntry.CamelCase)
135 // NOTE: Following will be enabled when the constant attribute names are
136 // added to the golang generated code and the previous removed
137 //format := "const %v = %v"
138 //expectedLine = fmt.Sprintf(format, attrEntry.Final, attrEntry.CamelCase)
139 } else {
140 expectedLine = "(\"ManagedEntityId\", "
141 // NOTE: Following will be enabled when the constant attribute names are
142 // added to the golang generated code
143 //format := "const ManagedEntityId = %v"
144 //expectedLine = fmt.Sprintf(format, attrEntry.CamelCase)
145 }
146 found := strings.Contains(goFileText, expectedLine)
147 assert.True(t, found)
148
149 if found {
150 // Scrub it from the attribute list
151 delete(classAttrEntry.Attributes, strconv.Itoa(int(attrIndex)))
152 }
153 }
154 }
155 }
156 // If all attributes have been scrubbed, delete the class entry
157 assert.Zero(t, len(classAttrEntry.Attributes))
158 if len(classAttrEntry.Attributes) == 0 {
159 delete(classEntries, strconv.Itoa(int(classID)))
160 }
161 }
162 // Unmarshalled JSON map should be empty if all attributes and classes
163 // match the code-generated file information.
164 assert.Zero(t, len(classEntries))
165 if len(classEntries) != 0 {
166 fmt.Printf("These entries remained after test:\n%+v\n", classEntries)
167 }
168}