github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/configuration/core_config/config_data_test.go (about)

     1  package core_config_test
     2  
     3  import (
     4  	"regexp"
     5  
     6  	. "github.com/cloudfoundry/cli/cf/configuration/core_config"
     7  	"github.com/cloudfoundry/cli/cf/models"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var exampleJSON = `
    13  {
    14  	"ConfigVersion": 3,
    15  	"Target": "api.example.com",
    16  	"ApiVersion": "3",
    17  	"AuthorizationEndpoint": "auth.example.com",
    18  	"LoggregatorEndPoint": "logs.example.com",
    19  	"UaaEndpoint": "uaa.example.com",
    20  	"AccessToken": "the-access-token",
    21  	"RefreshToken": "the-refresh-token",
    22  	"OrganizationFields": {
    23  		"Guid": "the-org-guid",
    24  		"Name": "the-org",
    25  		"QuotaDefinition": {
    26  			"name":"",
    27  			"memory_limit":0,
    28  			"instance_memory_limit":0,
    29  			"total_routes":0,
    30  			"total_services":0,
    31  			"non_basic_services_allowed": false
    32  		}
    33  	},
    34  	"SpaceFields": {
    35  		"Guid": "the-space-guid",
    36  		"Name": "the-space"
    37  	},
    38  	"SSLDisabled": true,
    39  	"AsyncTimeout": 1000,
    40  	"Trace": "path/to/some/file",
    41  	"ColorEnabled": "true",
    42  	"Locale": "fr_FR"
    43  }`
    44  
    45  var exampleData = &Data{
    46  	Target:                "api.example.com",
    47  	ApiVersion:            "3",
    48  	AuthorizationEndpoint: "auth.example.com",
    49  	LoggregatorEndPoint:   "logs.example.com",
    50  	UaaEndpoint:           "uaa.example.com",
    51  	AccessToken:           "the-access-token",
    52  	RefreshToken:          "the-refresh-token",
    53  	OrganizationFields: models.OrganizationFields{
    54  		Guid: "the-org-guid",
    55  		Name: "the-org",
    56  	},
    57  	SpaceFields: models.SpaceFields{
    58  		Guid: "the-space-guid",
    59  		Name: "the-space",
    60  	},
    61  	SSLDisabled:  true,
    62  	Trace:        "path/to/some/file",
    63  	AsyncTimeout: 1000,
    64  	ColorEnabled: "true",
    65  	Locale:       "fr_FR",
    66  }
    67  
    68  var _ = Describe("V3 Config files", func() {
    69  	Describe("serialization", func() {
    70  		It("creates a JSON string from the config object", func() {
    71  			jsonData, err := exampleData.JsonMarshalV3()
    72  
    73  			Expect(err).NotTo(HaveOccurred())
    74  			Expect(stripWhitespace(string(jsonData))).To(ContainSubstring(stripWhitespace(exampleJSON)))
    75  		})
    76  	})
    77  
    78  	Describe("parsing", func() {
    79  		It("returns an error when the JSON is invalid", func() {
    80  			configData := NewData()
    81  			err := configData.JsonUnmarshalV3([]byte(`{ "not_valid": ### }`))
    82  
    83  			Expect(err).To(HaveOccurred())
    84  		})
    85  
    86  		It("creates a config object from valid JSON", func() {
    87  			configData := NewData()
    88  			err := configData.JsonUnmarshalV3([]byte(exampleJSON))
    89  
    90  			Expect(err).NotTo(HaveOccurred())
    91  			Expect(configData).To(Equal(exampleData))
    92  		})
    93  	})
    94  })
    95  
    96  var whiteSpaceRegex = regexp.MustCompile(`\s+`)
    97  
    98  func stripWhitespace(input string) string {
    99  	return whiteSpaceRegex.ReplaceAllString(input, "")
   100  }