Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net) |
| 3 | * Copyright 2020-present Open Networking Foundation |
Chip Boling | 8cdd639 | 2022-01-27 08:43:37 -0600 | [diff] [blame] | 4 | * |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 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 |
Chip Boling | 8cdd639 | 2022-01-27 08:43:37 -0600 | [diff] [blame] | 8 | * |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Chip Boling | 8cdd639 | 2022-01-27 08:43:37 -0600 | [diff] [blame] | 10 | * |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 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 | |
| 24 | package generated |
| 25 | |
| 26 | import ( |
Chip Boling | ceca001 | 2022-01-25 15:13:31 -0600 | [diff] [blame] | 27 | "encoding/json" |
| 28 | "fmt" |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 29 | "github.com/stretchr/testify/assert" |
Chip Boling | ceca001 | 2022-01-25 15:13:31 -0600 | [diff] [blame] | 30 | "io/ioutil" |
| 31 | "os" |
| 32 | "strconv" |
| 33 | "strings" |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 34 | "testing" |
| 35 | ) |
| 36 | |
| 37 | func 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 Boling | ceca001 | 2022-01-25 15:13:31 -0600 | [diff] [blame] | 48 | |
| 49 | func 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 { |
Chip Boling | 8cdd639 | 2022-01-27 08:43:37 -0600 | [diff] [blame] | 134 | format := "const %v = \"%v\"" |
| 135 | expectedLine = fmt.Sprintf(format, attrEntry.Final, attrEntry.CamelCase) |
Chip Boling | ceca001 | 2022-01-25 15:13:31 -0600 | [diff] [blame] | 136 | } else { |
Chip Boling | 8cdd639 | 2022-01-27 08:43:37 -0600 | [diff] [blame] | 137 | // Match Entity ID by looking at use in structure initialization |
| 138 | expectedLine = "Uint16Field(ManagedEntityID, PointerAttributeType" |
Chip Boling | ceca001 | 2022-01-25 15:13:31 -0600 | [diff] [blame] | 139 | } |
| 140 | found := strings.Contains(goFileText, expectedLine) |
| 141 | assert.True(t, found) |
| 142 | |
| 143 | if found { |
| 144 | // Scrub it from the attribute list |
| 145 | delete(classAttrEntry.Attributes, strconv.Itoa(int(attrIndex))) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | // If all attributes have been scrubbed, delete the class entry |
| 151 | assert.Zero(t, len(classAttrEntry.Attributes)) |
| 152 | if len(classAttrEntry.Attributes) == 0 { |
| 153 | delete(classEntries, strconv.Itoa(int(classID))) |
| 154 | } |
| 155 | } |
| 156 | // Unmarshalled JSON map should be empty if all attributes and classes |
| 157 | // match the code-generated file information. |
| 158 | assert.Zero(t, len(classEntries)) |
| 159 | if len(classEntries) != 0 { |
| 160 | fmt.Printf("These entries remained after test:\n%+v\n", classEntries) |
| 161 | } |
| 162 | } |