16
16
* limitations under the License.
17
17
*/
18
18
19
- package idutils
19
+ package idutils_test
20
20
21
21
import (
22
22
"encoding/base64"
23
- "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/coordinate"
24
- "github.com/stretchr/testify/assert"
25
23
"strings"
26
24
"testing"
25
+
26
+ "github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/idutils"
27
+ "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/coordinate"
28
+ "github.com/stretchr/testify/assert"
27
29
)
28
30
29
31
func TestGenerateExternalIdIsStable (t * testing.T ) {
30
32
schemaId , id := "a" , "b"
31
33
32
- output1 , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {
34
+ output1 , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {
33
35
Type : schemaId ,
34
36
ConfigId : id ,
35
37
})
36
38
assert .NoError (t , err )
37
- output2 , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {
39
+ output2 , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {
38
40
Type : schemaId ,
39
41
ConfigId : id ,
40
42
})
@@ -43,11 +45,11 @@ func TestGenerateExternalIdIsStable(t *testing.T) {
43
45
}
44
46
45
47
func TestGenerateExternalIdGeneratesDifferentValuesForDifferentInput (t * testing.T ) {
46
- output1 , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : "a" , ConfigId : "a" })
48
+ output1 , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : "a" , ConfigId : "a" })
47
49
assert .NoError (t , err )
48
- output2 , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : "a" , ConfigId : "b" })
50
+ output2 , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : "a" , ConfigId : "b" })
49
51
assert .NoError (t , err )
50
- output3 , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : "b" , ConfigId : "b" })
52
+ output3 , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : "b" , ConfigId : "b" })
51
53
assert .NoError (t , err )
52
54
53
55
assert .NotEqual (t , output1 , output2 )
@@ -56,24 +58,24 @@ func TestGenerateExternalIdGeneratesDifferentValuesForDifferentInput(t *testing.
56
58
}
57
59
58
60
func TestGenerateExternalIdWithOver500CharsCutsIt (t * testing.T ) {
59
- output1 , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : strings .Repeat ("a" , 501 )})
61
+ output1 , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : strings .Repeat ("a" , 501 )})
60
62
assert .Zero (t , output1 )
61
63
assert .Error (t , err )
62
- output2 , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {ConfigId : strings .Repeat ("a" , 501 )})
64
+ output2 , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {ConfigId : strings .Repeat ("a" , 501 )})
63
65
assert .Zero (t , output2 )
64
66
assert .Error (t , err )
65
- output3 , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : strings .Repeat ("a" , 250 ), ConfigId : strings .Repeat ("a" , 251 )})
67
+ output3 , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : strings .Repeat ("a" , 250 ), ConfigId : strings .Repeat ("a" , 251 )})
66
68
assert .LessOrEqual (t , len (output3 ), 500 )
67
69
assert .NoError (t , err )
68
70
69
71
}
70
72
71
73
func TestGenerateExternalIdWithOver500CharactersProducesUniqueIDs (t * testing.T ) {
72
- uniqueID1 , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : strings .Repeat ("a" , 250 ), ConfigId : strings .Repeat ("a" , 251 )})
74
+ uniqueID1 , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : strings .Repeat ("a" , 250 ), ConfigId : strings .Repeat ("a" , 251 )})
73
75
assert .NoError (t , err )
74
- uniqueID2 , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : strings .Repeat ("a" , 250 ), ConfigId : strings .Repeat ("a" , 251 )})
76
+ uniqueID2 , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : strings .Repeat ("a" , 250 ), ConfigId : strings .Repeat ("a" , 251 )})
75
77
assert .NoError (t , err )
76
- uniqueID3 , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : strings .Repeat ("a" , 250 ), ConfigId : strings .Repeat ("a" , 300 )})
78
+ uniqueID3 , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : strings .Repeat ("a" , 250 ), ConfigId : strings .Repeat ("a" , 300 )})
77
79
assert .NoError (t , err )
78
80
79
81
assert .Equal (t , uniqueID1 , uniqueID2 )
@@ -83,28 +85,28 @@ func TestGenerateExternalIdWithOver500CharactersProducesUniqueIDs(t *testing.T)
83
85
func TestGenerateExternalIdStartsWithKnownPrefix (t * testing.T ) {
84
86
schemaId , id := "a" , "b"
85
87
86
- extId , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : schemaId , ConfigId : id })
88
+ extId , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : schemaId , ConfigId : id })
87
89
assert .NoError (t , err )
88
90
assert .True (t , strings .HasPrefix (extId , "monaco:" ))
89
91
}
90
92
91
93
func TestGenerateExternalIdWithOther500CharsStartsWithKnownPrefix (t * testing.T ) {
92
- extId , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : strings .Repeat ("a" , 250 ), ConfigId : strings .Repeat ("a" , 251 )})
94
+ extId , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : strings .Repeat ("a" , 250 ), ConfigId : strings .Repeat ("a" , 251 )})
93
95
assert .NoError (t , err )
94
96
assert .True (t , strings .HasPrefix (extId , "monaco:" ))
95
97
}
96
98
97
99
func TestGenerateExternalIdConsidersProjectName (t * testing.T ) {
98
100
expectIDWithoutProjectName := "monaco:c2NoZW1hLWlkJGNvbmZpZy1pZA=="
99
101
expectIDWithProjectName := "monaco:cHJvamVjdC1uYW1lJHNjaGVtYS1pZCRjb25maWctaWQ="
100
- id1 , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {
102
+ id1 , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {
101
103
Project : "" ,
102
104
Type : "schema-id" ,
103
105
ConfigId : "config-id" ,
104
106
})
105
107
assert .Equal (t , expectIDWithoutProjectName , id1 )
106
108
assert .NoError (t , err )
107
- id2 , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {
109
+ id2 , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {
108
110
Project : "project-name" ,
109
111
Type : "schema-id" ,
110
112
ConfigId : "config-id" ,
@@ -115,20 +117,38 @@ func TestGenerateExternalIdConsidersProjectName(t *testing.T) {
115
117
116
118
func TestGenerateExternalIdReturnsErrIfSchemaIDorConfigIDisMissing (t * testing.T ) {
117
119
118
- id , err := GenerateExternalIDForSettingsObject (coordinate.Coordinate {ConfigId : "config-id" })
120
+ id , err := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {ConfigId : "config-id" })
119
121
assert .Zero (t , id )
120
122
assert .Error (t , err )
121
123
122
- id , err = GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : "schema-id" })
124
+ id , err = idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {Type : "schema-id" })
123
125
assert .Zero (t , id )
124
126
assert .Error (t , err )
125
127
126
128
}
127
129
128
130
func TestGenerateExternalIdRawIdParts (t * testing.T ) {
129
- id , _ := GenerateExternalIDForSettingsObject (coordinate.Coordinate {Project : "project-name" , Type : "schema-id" , ConfigId : "config-id" })
131
+ id , _ := idutils . GenerateExternalIDForSettingsObject (coordinate.Coordinate {Project : "project-name" , Type : "schema-id" , ConfigId : "config-id" })
130
132
decoded , _ := base64 .StdEncoding .DecodeString (strings .TrimPrefix (id , "monaco:" ))
131
133
rawId := make ([]byte , len (decoded ))
132
134
copy (rawId , decoded )
133
135
assert .Equal (t , "project-name$schema-id$config-id" , string (decoded ))
134
136
}
137
+
138
+ func TestGenerateExternalID (t * testing.T ) {
139
+ coordinate := coordinate.Coordinate {Project : "project-name" , Type : "schema-id" , ConfigId : "config-id" }
140
+ id := idutils .GenerateExternalID (coordinate )
141
+ t .Run ("Len of id is under 50" , func (t * testing.T ) {
142
+ t .Parallel ()
143
+ len := len (id )
144
+ assert .True (t , len <= 50 )
145
+ })
146
+ t .Run ("externalID correctly encoded" , func (t * testing.T ) {
147
+ t .Parallel ()
148
+ assert .Equal (t , id , "monaco-ae5f7e18-84b6-3c05-98b9-3d8aba2c708c" )
149
+ })
150
+ t .Run ("externalID correctly prefixed" , func (t * testing.T ) {
151
+ t .Parallel ()
152
+ assert .True (t , strings .HasPrefix (id , "monaco-" ))
153
+ })
154
+ }
0 commit comments