github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/environs/simplestreams/json_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package simplestreams_test
     5  
     6  import (
     7  	"encoding/json"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/environs/simplestreams"
    13  )
    14  
    15  type jsonSuite struct{}
    16  
    17  func (s *jsonSuite) TestItemCollectionMarshalling(c *gc.C) {
    18  	// Ensure that unmarshalling a simplestreams.ItemCollection
    19  	// directly (not through ParseCloudMetadata) doesn't
    20  	// cause any surprises.
    21  	var m simplestreams.ItemCollection
    22  	m.Items = make(map[string]interface{})
    23  	err := json.Unmarshal([]byte(`{
    24          "items": {
    25              "a": "b",
    26              "c": 123 
    27          }
    28      }`), &m)
    29  	c.Assert(err, jc.ErrorIsNil)
    30  	c.Assert(m.Items, gc.DeepEquals, map[string]interface{}{
    31  		"a": "b",
    32  		"c": float64(123),
    33  	})
    34  	// Ensure marshalling works as expected, too.
    35  	b, err := json.Marshal(&m)
    36  	c.Assert(err, jc.ErrorIsNil)
    37  	c.Assert(string(b), gc.Equals, `{"items":{"a":"b","c":123}}`)
    38  }