github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/model_test.go (about)

     1  package models
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  	"time"
     7  
     8  	m "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
     9  
    10  	v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  // should handle marshaling and unmarshalling for an apiserver resource with a custom sub resource
    15  func TestAPIServiceMarshal(t *testing.T) {
    16  	svc1 := &m.APIService{
    17  		ResourceMeta: v1.ResourceMeta{
    18  			GroupVersionKind: v1.GroupVersionKind{
    19  				GroupKind:  v1.GroupKind{Group: "management", Kind: "APIService"},
    20  				APIVersion: "v1",
    21  			},
    22  			Name:  "name",
    23  			Title: "title",
    24  			Metadata: v1.Metadata{
    25  				ID:              "123",
    26  				Audit:           v1.AuditMetadata{},
    27  				ResourceVersion: "1",
    28  				SelfLink:        "/self/link",
    29  				State:           "state",
    30  			},
    31  			Attributes: map[string]string{
    32  				"attr1": "val1",
    33  				"attr2": "val2",
    34  			},
    35  			Tags: []string{"tag1", "tag2"},
    36  			Finalizers: []v1.Finalizer{
    37  				{Name: "finalizer1"},
    38  				{Name: "finalizer2"},
    39  			},
    40  			SubResources: map[string]interface{}{
    41  				"x-agent-details": map[string]interface{}{
    42  					"x-agent-id": "123",
    43  				},
    44  			},
    45  		},
    46  		Owner: &v1.Owner{
    47  			Type: v1.TeamOwner,
    48  			ID:   "233",
    49  		},
    50  		Spec: m.ApiServiceSpec{
    51  			Description: "desc",
    52  			Icon: m.ApiServiceSpecIcon{
    53  				ContentType: "image/png",
    54  				Data:        "data",
    55  			},
    56  		},
    57  		Status: &v1.ResourceStatus{
    58  			Level: "Error",
    59  			Reasons: []v1.ResourceStatusReason{
    60  				{
    61  					Type:      "Error",
    62  					Detail:    "",
    63  					Timestamp: getTimestamp(),
    64  				},
    65  			},
    66  		},
    67  	}
    68  
    69  	bts, err := json.Marshal(svc1)
    70  	assert.Nil(t, err)
    71  	assert.NotNil(t, bts)
    72  
    73  	svc2 := &m.APIService{}
    74  
    75  	err = json.Unmarshal(bts, svc2)
    76  	assert.Nil(t, err)
    77  
    78  	// override the audit metadata to easily assert the two structs are equal
    79  	svc1.Metadata.Audit = v1.AuditMetadata{}
    80  	svc2.Metadata.Audit = v1.AuditMetadata{}
    81  	assert.Equal(t, svc1, svc2)
    82  }
    83  
    84  // should unmarshal when owner is not set
    85  func TestAPIServiceMarshalNoOwner(t *testing.T) {
    86  	svc1 := &m.APIService{
    87  		ResourceMeta: v1.ResourceMeta{
    88  			GroupVersionKind: v1.GroupVersionKind{
    89  				GroupKind:  v1.GroupKind{Group: "management", Kind: "APIService"},
    90  				APIVersion: "v1",
    91  			},
    92  			Name:  "name",
    93  			Title: "title",
    94  			Metadata: v1.Metadata{
    95  				ID: "123",
    96  			},
    97  			Finalizers: []v1.Finalizer{
    98  				{Name: "finalizer1"},
    99  				{Name: "finalizer2"},
   100  			},
   101  			SubResources: map[string]interface{}{
   102  				"x-agent-details": map[string]interface{}{
   103  					"x-agent-id": "123",
   104  				},
   105  			},
   106  		},
   107  		Spec: m.ApiServiceSpec{
   108  			Description: "desc",
   109  		},
   110  		Status: &v1.ResourceStatus{
   111  			Level: "Error",
   112  			Reasons: []v1.ResourceStatusReason{
   113  				{
   114  					Type:      "Error",
   115  					Detail:    "",
   116  					Timestamp: getTimestamp(),
   117  				},
   118  			},
   119  		},
   120  	}
   121  
   122  	bts, err := json.Marshal(svc1)
   123  	assert.Nil(t, err)
   124  	assert.NotNil(t, bts)
   125  
   126  	svc2 := &m.APIService{}
   127  
   128  	err = json.Unmarshal(bts, svc2)
   129  	assert.Nil(t, err)
   130  
   131  	// override the audit metadata to easily assert the two structs are equal
   132  	svc1.Metadata.Audit = v1.AuditMetadata{}
   133  	svc2.Metadata.Audit = v1.AuditMetadata{}
   134  	assert.Equal(t, svc1, svc2)
   135  }
   136  
   137  // should convert an APIService to a ResourceInstance
   138  func TestAPIServiceAsInstance(t *testing.T) {
   139  	newTime := getTimestamp()
   140  	svc := &m.APIService{
   141  		ResourceMeta: v1.ResourceMeta{
   142  			GroupVersionKind: v1.GroupVersionKind{
   143  				GroupKind:  v1.GroupKind{Group: "management", Kind: "APIService"},
   144  				APIVersion: "v1",
   145  			},
   146  			Name:  "name",
   147  			Title: "title",
   148  			Metadata: v1.Metadata{
   149  				ID:              "123",
   150  				Audit:           v1.AuditMetadata{},
   151  				ResourceVersion: "1",
   152  				SelfLink:        "/self/link",
   153  				State:           "state",
   154  			},
   155  			Attributes: map[string]string{
   156  				"attr1": "val1",
   157  				"attr2": "val2",
   158  			},
   159  			Tags:       []string{"tag1", "tag2"},
   160  			Finalizers: nil,
   161  			SubResources: map[string]interface{}{
   162  				"details": map[string]interface{}{},
   163  				"x-agent-details": map[string]interface{}{
   164  					"x-agent-id": "123",
   165  				},
   166  				"status": map[string]interface{}{
   167  					"level": "Error",
   168  					"reasons": []interface{}{
   169  						map[string]interface{}{
   170  							"type":      "Error",
   171  							"detail":    "error",
   172  							"timestamp": time.Time(newTime).Format(v1.APIServerTimeFormat),
   173  						},
   174  					},
   175  				},
   176  				"references": map[string]interface{}{},
   177  			},
   178  		},
   179  		Owner: &v1.Owner{
   180  			Type: v1.TeamOwner,
   181  			ID:   "233",
   182  		},
   183  		Spec: m.ApiServiceSpec{
   184  			Description: "desc",
   185  			Icon: m.ApiServiceSpecIcon{
   186  				ContentType: "image/png",
   187  				Data:        "data",
   188  			},
   189  		},
   190  		Status: &v1.ResourceStatus{
   191  			Level: "Error",
   192  			Reasons: []v1.ResourceStatusReason{
   193  				{
   194  					Type:      "Error",
   195  					Detail:    "error",
   196  					Timestamp: newTime,
   197  				},
   198  			},
   199  		},
   200  	}
   201  
   202  	ri, err := svc.AsInstance()
   203  	assert.Nil(t, err)
   204  
   205  	// override the audit metadata to easily assert the two structs are equal
   206  	svc.Metadata.Audit = v1.AuditMetadata{}
   207  	ri.Metadata.Audit = v1.AuditMetadata{}
   208  
   209  	// marshal the instance spec to bytes, then convert it to an ApiServiceSpec
   210  	// to see if it matches the svc.Spec field
   211  	bts, err := json.Marshal(ri.Spec)
   212  	assert.Nil(t, err)
   213  
   214  	instSpec := &m.ApiServiceSpec{}
   215  	err = json.Unmarshal(bts, instSpec)
   216  	assert.Nil(t, err)
   217  
   218  	assert.Equal(t, svc.Spec, *instSpec)
   219  	assert.Equal(t, svc.Owner, ri.Owner)
   220  	assert.Equal(t, svc.ResourceMeta, ri.ResourceMeta)
   221  
   222  	svcBytes, err := json.Marshal(svc)
   223  	assert.Nil(t, err)
   224  
   225  	assert.Equal(t, json.RawMessage(svcBytes), ri.GetRawResource())
   226  }
   227  
   228  // Should create an APIService from a ResourceInstance
   229  func TestAPIServiceFromInstance(t *testing.T) {
   230  	// convert a service to an instance
   231  	svc1 := &m.APIService{
   232  		ResourceMeta: v1.ResourceMeta{
   233  			GroupVersionKind: v1.GroupVersionKind{
   234  				GroupKind:  v1.GroupKind{Group: "management", Kind: "APIService"},
   235  				APIVersion: "v1",
   236  			},
   237  			Name:  "name",
   238  			Title: "title",
   239  			Metadata: v1.Metadata{
   240  				ID:              "123",
   241  				Audit:           v1.AuditMetadata{},
   242  				ResourceVersion: "1",
   243  				SelfLink:        "/self/link",
   244  				State:           "state",
   245  			},
   246  			Attributes: map[string]string{
   247  				"attr1": "val1",
   248  				"attr2": "val2",
   249  			},
   250  			Tags:       []string{"tag1", "tag2"},
   251  			Finalizers: nil,
   252  			SubResources: map[string]interface{}{
   253  				"x-agent-details": map[string]interface{}{
   254  					"x-agent-id": "123",
   255  				},
   256  			},
   257  		},
   258  		Owner: &v1.Owner{
   259  			Type: v1.TeamOwner,
   260  			ID:   "233",
   261  		},
   262  		Spec: m.ApiServiceSpec{
   263  			Description: "desc",
   264  			Icon: m.ApiServiceSpecIcon{
   265  				ContentType: "image/png",
   266  				Data:        "data",
   267  			},
   268  		},
   269  		Status: &v1.ResourceStatus{
   270  			Level: "Success",
   271  			Reasons: []v1.ResourceStatusReason{
   272  				{
   273  					Type:      "Error",
   274  					Detail:    "",
   275  					Timestamp: getTimestamp(),
   276  				},
   277  			},
   278  		},
   279  	}
   280  	ri1, err := svc1.AsInstance()
   281  	assert.Nil(t, err)
   282  
   283  	// call FromInstance using the first service, which should fill all the fields of svc2 from svc1
   284  	svc2 := &m.APIService{}
   285  	err = svc2.FromInstance(ri1)
   286  	assert.Nil(t, err)
   287  
   288  	// the api services should be equal, and their resource instances should be equal
   289  	ri2, err := svc2.AsInstance()
   290  	assert.Nil(t, err)
   291  	assert.Equal(t, ri1, ri2)
   292  
   293  	svc1.Metadata.Audit = v1.AuditMetadata{}
   294  	svc2.Metadata.Audit = v1.AuditMetadata{}
   295  	assert.Equal(t, svc1, svc2)
   296  }
   297  
   298  // getTimestamp - Returns current timestamp formatted for API Server
   299  func getTimestamp() v1.Time {
   300  	activityTime := time.Now()
   301  	newV1Time := v1.Time(activityTime)
   302  
   303  	// marshall the time in and out of JSON to get same format
   304  	timeBytes, _ := newV1Time.MarshalJSON()
   305  	newV1Time.UnmarshalJSON(timeBytes)
   306  	return newV1Time
   307  }