github.com/jdolitsky/cnab-go@v0.7.1-beta1/bundle/parameters_test.go (about) 1 package bundle 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 func TestCanReadParameterNames(t *testing.T) { 9 json := `{ 10 "parameters": { 11 "foo": { }, 12 "bar": { } 13 } 14 }` 15 definitions, err := Unmarshal([]byte(json)) 16 if err != nil { 17 t.Fatal(err) 18 } 19 if len(definitions.Parameters) != 2 { 20 t.Fatalf("Expected 2 parameter definitons, got %d", len(definitions.Parameters)) 21 } 22 if _, ok := definitions.Parameters["foo"]; !ok { 23 t.Errorf("Expected an entry with name 'foo' but didn't get one") 24 } 25 if _, ok := definitions.Parameters["bar"]; !ok { 26 t.Errorf("Expected an entry with name 'bar' but didn't get one") 27 } 28 } 29 30 func TestCanReadParameterDefinition(t *testing.T) { 31 definition := "cooldef" 32 description := "some description" 33 action0 := "action0" 34 action1 := "action1" 35 destinationEnvValue := "BACKEND_PORT" 36 destinationPathValue := "/some/path" 37 38 json := fmt.Sprintf(`{ 39 "parameters": { 40 "test": { 41 "definition": "%s", 42 "destination": { 43 "env": "%s", 44 "path": "%s" 45 }, 46 "description": "%s", 47 "applyTo": [ "%s", "%s" ], 48 "required": true 49 } 50 } 51 }`, 52 definition, destinationEnvValue, destinationPathValue, 53 description, action0, action1) 54 55 definitions, err := Unmarshal([]byte(json)) 56 if err != nil { 57 t.Fatal(err) 58 } 59 60 p := definitions.Parameters["test"] 61 if p.Definition != definition { 62 t.Errorf("Expected definition'%s' but got '%s'", definition, p.Definition) 63 } 64 if p.Destination.EnvironmentVariable != destinationEnvValue { 65 t.Errorf("Expected destination environment value '%s' but got '%s'", destinationEnvValue, p.Destination.EnvironmentVariable) 66 } 67 if p.Destination.Path != destinationPathValue { 68 t.Errorf("Expected destination path value '%s' but got '%s'", destinationPathValue, p.Destination.Path) 69 } 70 if p.Description != description { 71 t.Errorf("Expected description '%s' but got '%s'", description, p.Description) 72 } 73 if len(p.ApplyTo) != 2 { 74 t.Errorf("Expected 2 applyTo actions but got %d", len(p.ApplyTo)) 75 } 76 if p.ApplyTo[0] != action0 { 77 t.Errorf("Expected action '%s' but got '%s'", action0, p.ApplyTo[0]) 78 } 79 if p.ApplyTo[1] != action1 { 80 t.Errorf("Expected action '%s' but got '%s'", action1, p.ApplyTo[1]) 81 } 82 if !p.Required { 83 t.Errorf("Expected parameter to be required") 84 } 85 }