github.com/jbking/gohan@v0.0.0-20151217002006-b41ccf1c2a96/util/util_test.go (about)

     1  // Copyright (C) 2015 NTT Innovation Institute, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package util
    17  
    18  import (
    19  	"os"
    20  
    21  	. "github.com/onsi/ginkgo"
    22  	. "github.com/onsi/gomega"
    23  )
    24  
    25  var _ = Describe("Util functions", func() {
    26  
    27  	Describe("SaveFile and LoadFile", func() {
    28  		It("should handle json file properly", func() {
    29  			testFileUtil("./util_test.json")
    30  		})
    31  
    32  		It("should handle yaml file properly", func() {
    33  			testFileUtil("./util_test.yaml")
    34  		})
    35  	})
    36  
    37  	Describe("TempFile", func() {
    38  		It("should create temporary file properly", func() {
    39  			file, err := TempFile("./", "util_", "_test")
    40  			Expect(err).ToNot(HaveOccurred())
    41  			Expect(file).ToNot(BeNil())
    42  			_, err = os.Stat(file.Name())
    43  			Expect(err).ToNot(HaveOccurred())
    44  			Expect(os.Remove(file.Name())).To(Succeed())
    45  		})
    46  	})
    47  
    48  	Describe("GetContents", func() {
    49  		It("should get contents form http", func() {
    50  			_, err := GetContent("http://www.google.com")
    51  			Expect(err).ToNot(HaveOccurred())
    52  		})
    53  
    54  		It("should get contents form file", func() {
    55  			_, err := GetContent("file://../tests/test_schema.yaml")
    56  			Expect(err).ToNot(HaveOccurred())
    57  		})
    58  	})
    59  
    60  	Describe("GetSortedKeys", func() {
    61  		It("should return sorted map[string]interface{} keys", func() {
    62  			input := map[string]interface{}{
    63  				"Lorem": "v1",
    64  				"ipsum": "v2",
    65  				"dolor": "v3",
    66  				"sit":   "v4",
    67  			}
    68  			result := GetSortedKeys(input)
    69  			Expect(result).To(Equal([]string{"dolor", "ipsum", "Lorem", "sit"}))
    70  		})
    71  	})
    72  })
    73  
    74  func testFileUtil(file string) {
    75  	os.Remove(file)
    76  	data := map[string]interface{}{
    77  		"test": "data",
    78  	}
    79  	Expect(SaveFile(file, data)).To(Succeed())
    80  	defer os.Remove(file)
    81  	loadedData, err := LoadFile(file)
    82  	Expect(err).ToNot(HaveOccurred())
    83  	Expect(loadedData["test"]).To(Equal(data["test"]))
    84  }