github.com/openshift-online/ocm-sdk-go@v0.1.473/metrics/path_tree_test.go (about) 1 /* 2 Copyright (c) 2021 Red Hat, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // This file contains tests for the URL path tree. 18 19 package metrics 20 21 import ( 22 "encoding/json" 23 24 . "github.com/onsi/ginkgo/v2/dsl/table" // nolint 25 . "github.com/onsi/gomega" // nolint 26 ) 27 28 var _ = DescribeTable( 29 "Add", 30 func(original string, paths []string, expected string) { 31 var tree *pathTree 32 err := json.Unmarshal([]byte(original), &tree) 33 Expect(err).ToNot(HaveOccurred()) 34 for _, path := range paths { 35 tree.add(path) 36 } 37 actual, err := json.Marshal(tree) 38 Expect(err).ToNot(HaveOccurred()) 39 Expect(actual).To(MatchJSON(expected)) 40 }, 41 Entry( 42 "Empty path", 43 `{}`, 44 []string{ 45 ``, 46 }, 47 `{}`, 48 ), 49 Entry( 50 "Non existing path with one segment", 51 `{}`, 52 []string{ 53 `/api`, 54 }, 55 `{ 56 "api": null 57 }`, 58 ), 59 Entry( 60 "Non existing path with two segments", 61 `{}`, 62 []string{ 63 `/api/clusters_mgmt`, 64 }, 65 `{ 66 "api": { 67 "clusters_mgmt": null 68 } 69 }`, 70 ), 71 Entry( 72 "Non existing path with three segments", 73 `{}`, 74 []string{ 75 `/api/clusters_mgmt/v1`, 76 }, 77 `{ 78 "api": { 79 "clusters_mgmt": { 80 "v1": null 81 } 82 } 83 }`, 84 ), 85 Entry( 86 "Existing path with one segment", 87 `{ 88 "api": null 89 }`, 90 []string{ 91 `/api`, 92 }, 93 `{ 94 "api": null 95 }`, 96 ), 97 Entry( 98 "Existing path with two segments", 99 `{ 100 "api": { 101 "clusters_mgmt": null 102 } 103 }`, 104 []string{ 105 `/api/clusters_mgmt`, 106 }, 107 `{ 108 "api": { 109 "clusters_mgmt": null 110 } 111 }`, 112 ), 113 Entry( 114 "Existing path with three segments", 115 `{ 116 "api": { 117 "clusters_mgmt": { 118 "v1": null 119 } 120 } 121 }`, 122 []string{ 123 `/api/clusters_mgmt/v1`, 124 }, 125 `{ 126 "api": { 127 "clusters_mgmt": { 128 "v1": null 129 } 130 } 131 }`, 132 ), 133 Entry( 134 "Appends to partially existing path", 135 `{ 136 "api": null 137 }`, 138 []string{ 139 `/api/clusters_mgmt`, 140 }, 141 `{ 142 "api": { 143 "clusters_mgmt": null 144 } 145 }`, 146 ), 147 Entry( 148 "Adds default token URL", 149 `{ 150 "api": { 151 "clusters_mgmt": null 152 } 153 }`, 154 []string{ 155 `/auth/realms/redhat-external/protocol/openid-connect/token`, 156 }, 157 `{ 158 "api": { 159 "clusters_mgmt": null 160 }, 161 "auth": { 162 "realms": { 163 "redhat-external": { 164 "protocol": { 165 "openid-connect": { 166 "token": null 167 } 168 } 169 } 170 } 171 } 172 }`, 173 ), 174 Entry( 175 "Merges prefix", 176 `{ 177 "api": { 178 "clusters_mgmt": null 179 } 180 }`, 181 []string{ 182 `/api/clusters_mgmt`, 183 `/api/accounts_mgmt`, 184 `/api/service_logs`, 185 }, 186 `{ 187 "api": { 188 "clusters_mgmt": null, 189 "accounts_mgmt": null, 190 "service_logs": null 191 } 192 }`, 193 ), 194 )