virtual_network_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package cfapi
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "testing"
  6. "time"
  7. "github.com/google/uuid"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestVirtualNetworkJsonRoundtrip(t *testing.T) {
  11. data := `{
  12. "id":"74fce949-351b-4752-b261-81a56cfd3130",
  13. "comment":"New York DC1",
  14. "name":"us-east-1",
  15. "is_default_network":true,
  16. "created_at":"2021-11-26T14:40:02.600673Z",
  17. "deleted_at":"2021-12-01T10:23:13.102645Z"
  18. }`
  19. var v VirtualNetwork
  20. err := json.Unmarshal([]byte(data), &v)
  21. require.NoError(t, err)
  22. require.Equal(t, uuid.MustParse("74fce949-351b-4752-b261-81a56cfd3130"), v.ID)
  23. require.Equal(t, "us-east-1", v.Name)
  24. require.Equal(t, "New York DC1", v.Comment)
  25. require.Equal(t, true, v.IsDefault)
  26. bytes, err := json.Marshal(v)
  27. require.NoError(t, err)
  28. obtainedJson := string(bytes)
  29. data = strings.Replace(data, "\t", "", -1)
  30. data = strings.Replace(data, "\n", "", -1)
  31. require.Equal(t, data, obtainedJson)
  32. }
  33. func TestMarshalNewVnet(t *testing.T) {
  34. newVnet := NewVirtualNetwork{
  35. Name: "eu-west-1",
  36. Comment: "London office",
  37. IsDefault: true,
  38. }
  39. serialized, err := json.Marshal(newVnet)
  40. require.NoError(t, err)
  41. require.True(t, strings.Contains(string(serialized), newVnet.Name))
  42. }
  43. func TestMarshalUpdateVnet(t *testing.T) {
  44. newName := "bulgaria-1"
  45. updates := UpdateVirtualNetwork{
  46. Name: &newName,
  47. }
  48. // Test where receiver is struct
  49. serialized, err := json.Marshal(updates)
  50. require.NoError(t, err)
  51. require.True(t, strings.Contains(string(serialized), newName))
  52. }
  53. func TestVnetTableString(t *testing.T) {
  54. virtualNet := VirtualNetwork{
  55. ID: uuid.New(),
  56. Name: "us-east-1",
  57. Comment: "New York DC1",
  58. IsDefault: true,
  59. CreatedAt: time.Now(),
  60. DeletedAt: time.Time{},
  61. }
  62. row := virtualNet.TableString()
  63. require.True(t, strings.HasPrefix(row, virtualNet.ID.String()))
  64. require.True(t, strings.Contains(row, virtualNet.Name))
  65. require.True(t, strings.Contains(row, virtualNet.Comment))
  66. require.True(t, strings.Contains(row, "true"))
  67. require.True(t, strings.HasSuffix(row, "-\t"))
  68. }