github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/util/json/json_parser_test.go (about)

     1  package json_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	"code.cloudfoundry.org/cli/cf/util/json"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("JSON Parser", func() {
    13  	Describe("ParseJSONArray", func() {
    14  		var filename string
    15  		var tmpFile *os.File
    16  
    17  		Context("when everything is proper", func() {
    18  			BeforeEach(func() {
    19  				tmpFile, _ = ioutil.TempFile("", "WONDERFULFILEWHOSENAMEISHARDTOREADBUTCONTAINSVALIDJSON")
    20  				filename = tmpFile.Name()
    21  				ioutil.WriteFile(filename, []byte("[{\"akey\": \"avalue\"}]"), 0644)
    22  			})
    23  
    24  			AfterEach(func() {
    25  				tmpFile.Close()
    26  				os.Remove(tmpFile.Name())
    27  			})
    28  
    29  			It("converts a json file into an unmarshalled slice of string->string map objects", func() {
    30  				stringMaps, err := json.ParseJSONArray(filename)
    31  				Expect(err).To(BeNil())
    32  				Expect(stringMaps[0]["akey"]).To(Equal("avalue"))
    33  			})
    34  		})
    35  
    36  		Context("when the JSON is invalid", func() {
    37  			BeforeEach(func() {
    38  				tmpFile, _ = ioutil.TempFile("", "TERRIBLEFILECONTAININGINVALIDJSONWHICHMAKESEVERYTHINGTERRIBLEANDSTILLHASANAMETHATSHARDTOREAD")
    39  				filename = tmpFile.Name()
    40  				ioutil.WriteFile(filename, []byte("SCARY NOISES}"), 0644)
    41  			})
    42  
    43  			AfterEach(func() {
    44  				tmpFile.Close()
    45  				os.Remove(tmpFile.Name())
    46  			})
    47  
    48  			It("tries to convert the json file but fails because it was given something it didn't like", func() {
    49  				_, err := json.ParseJSONArray(filename)
    50  				Expect(err).To(MatchError("Incorrect json format: invalid character 'S' looking for beginning of value"))
    51  			})
    52  		})
    53  	})
    54  
    55  	Describe("ParseJSONFromFileOrString", func() {
    56  		Context("when the input is empty", func() {
    57  			It("returns nil", func() {
    58  				result, err := json.ParseJSONFromFileOrString("")
    59  
    60  				Expect(result).To(BeNil())
    61  				Expect(err).To(BeNil())
    62  			})
    63  		})
    64  
    65  		Context("when the input is a file", func() {
    66  			var jsonFile *os.File
    67  			var fileContent string
    68  
    69  			AfterEach(func() {
    70  				if jsonFile != nil {
    71  					jsonFile.Close()
    72  					os.Remove(jsonFile.Name())
    73  				}
    74  			})
    75  
    76  			BeforeEach(func() {
    77  				fileContent = `{"foo": "bar"}`
    78  			})
    79  
    80  			JustBeforeEach(func() {
    81  				var err error
    82  				jsonFile, err = ioutil.TempFile("", "")
    83  				Expect(err).ToNot(HaveOccurred())
    84  
    85  				err = ioutil.WriteFile(jsonFile.Name(), []byte(fileContent), os.ModePerm)
    86  				Expect(err).NotTo(HaveOccurred())
    87  			})
    88  
    89  			It("returns the parsed json from the file", func() {
    90  				result, err := json.ParseJSONFromFileOrString(jsonFile.Name())
    91  				Expect(err).NotTo(HaveOccurred())
    92  
    93  				Expect(result).To(Equal(map[string]interface{}{"foo": "bar"}))
    94  			})
    95  
    96  			Context("when the file contains invalid json", func() {
    97  				BeforeEach(func() {
    98  					fileContent = `badtimes`
    99  				})
   100  
   101  				It("returns an error", func() {
   102  					_, err := json.ParseJSONFromFileOrString(jsonFile.Name())
   103  					Expect(err).To(MatchError("Incorrect json format: invalid character 'b' looking for beginning of value"))
   104  				})
   105  			})
   106  		})
   107  
   108  		Context("when the input is considered a json string (when it is not a file path)", func() {
   109  			var jsonString string
   110  
   111  			BeforeEach(func() {
   112  				jsonString = `{"foo": "bar"}`
   113  			})
   114  
   115  			It("returns the parsed json", func() {
   116  				result, err := json.ParseJSONFromFileOrString(jsonString)
   117  				Expect(err).NotTo(HaveOccurred())
   118  
   119  				Expect(result).To(Equal(map[string]interface{}{"foo": "bar"}))
   120  			})
   121  
   122  			Context("when the JSON is invalid", func() {
   123  				BeforeEach(func() {
   124  					jsonString = "SOMETHING IS WRONG"
   125  				})
   126  
   127  				It("returns a json parse error", func() {
   128  					_, err := json.ParseJSONFromFileOrString(jsonString)
   129  					Expect(err).To(MatchError("Incorrect json format: invalid character 'S' looking for beginning of value"))
   130  				})
   131  			})
   132  		})
   133  
   134  		Context("when the input is neither a file nor a json string", func() {
   135  			var invalidInput string
   136  
   137  			BeforeEach(func() {
   138  				invalidInput = "boo"
   139  			})
   140  
   141  			It("returns an error", func() {
   142  				_, err := json.ParseJSONFromFileOrString(invalidInput)
   143  				Expect(err).To(HaveOccurred())
   144  			})
   145  		})
   146  	})
   147  })