github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/annotations/client_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package annotations_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/api/annotations"
    11  	basetesting "github.com/juju/juju/api/base/testing"
    12  	"github.com/juju/juju/apiserver/params"
    13  	coretesting "github.com/juju/juju/testing"
    14  )
    15  
    16  type annotationsMockSuite struct {
    17  	coretesting.BaseSuite
    18  	annotationsClient *annotations.Client
    19  }
    20  
    21  var _ = gc.Suite(&annotationsMockSuite{})
    22  
    23  func (s *annotationsMockSuite) TestSetEntitiesAnnotation(c *gc.C) {
    24  	var called bool
    25  	annts := map[string]string{"annotation1": "test"}
    26  	annts2 := map[string]string{"annotation2": "test"}
    27  	setParams := map[string]map[string]string{
    28  		"charmA":   annts,
    29  		"serviceB": annts2,
    30  	}
    31  	apiCaller := basetesting.APICallerFunc(
    32  		func(objType string,
    33  			version int,
    34  			id, request string,
    35  			a, result interface{},
    36  		) error {
    37  			called = true
    38  			c.Check(objType, gc.Equals, "Annotations")
    39  			c.Check(id, gc.Equals, "")
    40  			c.Check(request, gc.Equals, "Set")
    41  
    42  			args, ok := a.(params.AnnotationsSet)
    43  			c.Assert(ok, jc.IsTrue)
    44  
    45  			for _, aParam := range args.Annotations {
    46  				// Since sometimes arrays returned on some
    47  				// architectures vary the order within params.AnnotationsSet,
    48  				// simply assert that each entity has its own annotations.
    49  				// Bug 1409141
    50  				c.Assert(aParam.Annotations, gc.DeepEquals, setParams[aParam.EntityTag])
    51  			}
    52  			return nil
    53  		})
    54  	annotationsClient := annotations.NewClient(apiCaller)
    55  	callErrs, err := annotationsClient.Set(setParams)
    56  	c.Assert(err, jc.ErrorIsNil)
    57  	c.Assert(callErrs, gc.HasLen, 0)
    58  	c.Assert(called, jc.IsTrue)
    59  }
    60  
    61  func (s *annotationsMockSuite) TestGetEntitiesAnnotations(c *gc.C) {
    62  	var called bool
    63  	apiCaller := basetesting.APICallerFunc(
    64  		func(
    65  			objType string,
    66  			version int,
    67  			id, request string,
    68  			a, response interface{}) error {
    69  			called = true
    70  			c.Check(objType, gc.Equals, "Annotations")
    71  			c.Check(id, gc.Equals, "")
    72  			c.Check(request, gc.Equals, "Get")
    73  			args, ok := a.(params.Entities)
    74  			c.Assert(ok, jc.IsTrue)
    75  			c.Assert(args.Entities, gc.HasLen, 1)
    76  			c.Assert(args.Entities[0], gc.DeepEquals, params.Entity{"charm"})
    77  			result := response.(*params.AnnotationsGetResults)
    78  			facadeAnnts := map[string]string{
    79  				"annotations": "test",
    80  			}
    81  			entitiesAnnts := params.AnnotationsGetResult{
    82  				EntityTag:   "charm",
    83  				Annotations: facadeAnnts,
    84  			}
    85  			result.Results = []params.AnnotationsGetResult{entitiesAnnts}
    86  			return nil
    87  		})
    88  	annotationsClient := annotations.NewClient(apiCaller)
    89  	found, err := annotationsClient.Get([]string{"charm"})
    90  	c.Assert(err, jc.ErrorIsNil)
    91  	c.Assert(called, jc.IsTrue)
    92  	c.Assert(found, gc.HasLen, 1)
    93  }