github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/config/validation/validation_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package validation 5 6 import ( 7 "github.com/stretchr/testify/assert" 8 "testing" 9 ) 10 11 type Connection struct { 12 Endpoint string 13 User string 14 Password string 15 } 16 17 type Dummy struct { 18 Connection Connection 19 Prop1 string 20 Prop2 string 21 Prop3 string 22 Bool1 bool 23 Int1 int64 24 List []string 25 } 26 27 func TestWeProvideNotAStruct(t *testing.T) { 28 _, err := FindEmptyStringsInConfigStruct("Hello World") 29 assert.EqualError(t, err, "'Hello World' (string) is not a struct") 30 } 31 32 func TestUnsupportedType(t *testing.T) { 33 34 type DummyWithUnsupportedType struct { 35 Dummy 36 NotExpected float32 37 } 38 39 _, err := FindEmptyStringsInConfigStruct(DummyWithUnsupportedType{}) 40 assert.EqualError(t, err, "unexpected type 'float32' of field: 'NotExpected', value: '0'") 41 42 } 43 44 func TestFindEmptyStringsInConfig(t *testing.T) { 45 myStruct := Dummy{ 46 Connection: Connection{ 47 Endpoint: "<set>", 48 User: "", 49 Password: "<set>", 50 }, 51 Prop1: "<set>", 52 Prop2: "", // this is empty 53 // Prop3: "this is missing intentionally" 54 Bool1: false, 55 Int1: 42, 56 List: []string{"1", "2"}, 57 } 58 emptyStrings, err := FindEmptyStringsInConfigStruct(myStruct) 59 if assert.NoError(t, err) { 60 assert.Len(t, emptyStrings, 3) 61 assert.Subset(t, emptyStrings, []string{ 62 "Connection.User", // empty value, nested 63 "Prop2", // empty value 64 "Prop3", // missing value 65 }) 66 } 67 }