github.com/Axway/agent-sdk@v1.1.101/pkg/agent/handler/credentialrequestdefinition_test.go (about)

     1  package handler
     2  
     3  import (
     4  	"testing"
     5  
     6  	agentcache "github.com/Axway/agent-sdk/pkg/agent/cache"
     7  	apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
     8  	catalog "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/catalog/v1alpha1"
     9  	management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
    10  	"github.com/Axway/agent-sdk/pkg/config"
    11  	"github.com/Axway/agent-sdk/pkg/watchmanager/proto"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestCRDHandler(t *testing.T) {
    16  	tests := []struct {
    17  		name     string
    18  		hasError bool
    19  		resource *apiv1.ResourceInstance
    20  		action   proto.Event_Type
    21  	}{
    22  		{
    23  			name:     "should save an Credential Request Definition",
    24  			hasError: false,
    25  			action:   proto.Event_CREATED,
    26  			resource: &apiv1.ResourceInstance{
    27  				ResourceMeta: apiv1.ResourceMeta{
    28  					Name:  "name",
    29  					Title: "title",
    30  					Metadata: apiv1.Metadata{
    31  						ID: "123",
    32  					},
    33  					GroupVersionKind: apiv1.GroupVersionKind{
    34  						GroupKind: apiv1.GroupKind{
    35  							Kind: management.CredentialRequestDefinitionGVK().Kind,
    36  						},
    37  					},
    38  				},
    39  			},
    40  		},
    41  		{
    42  			name:     "should update an Credential Request Definition",
    43  			hasError: false,
    44  			action:   proto.Event_UPDATED,
    45  			resource: &apiv1.ResourceInstance{
    46  				ResourceMeta: apiv1.ResourceMeta{
    47  					Name:  "name",
    48  					Title: "title",
    49  					Metadata: apiv1.Metadata{
    50  						ID: "123",
    51  					},
    52  					GroupVersionKind: apiv1.GroupVersionKind{
    53  						GroupKind: apiv1.GroupKind{
    54  							Kind: management.CredentialRequestDefinitionGVK().Kind,
    55  						},
    56  					},
    57  				},
    58  			},
    59  		},
    60  		{
    61  			name:     "should delete an Credential Request Definition",
    62  			hasError: false,
    63  			action:   proto.Event_DELETED,
    64  			resource: &apiv1.ResourceInstance{
    65  				ResourceMeta: apiv1.ResourceMeta{
    66  					Name:  "name",
    67  					Title: "title",
    68  					Metadata: apiv1.Metadata{
    69  						ID: "123",
    70  					},
    71  					GroupVersionKind: apiv1.GroupVersionKind{
    72  						GroupKind: apiv1.GroupKind{
    73  							Kind: management.CredentialRequestDefinitionGVK().Kind,
    74  						},
    75  					},
    76  				},
    77  			},
    78  		},
    79  		{
    80  			name:     "should return nil when the kind is not an Credential Request Definition",
    81  			hasError: false,
    82  			action:   proto.Event_UPDATED,
    83  			resource: &apiv1.ResourceInstance{
    84  				ResourceMeta: apiv1.ResourceMeta{
    85  					Name:  "name",
    86  					Title: "title",
    87  					Metadata: apiv1.Metadata{
    88  						ID: "123",
    89  					},
    90  					GroupVersionKind: apiv1.GroupVersionKind{
    91  						GroupKind: apiv1.GroupKind{
    92  							Kind: catalog.CategoryGVK().Kind,
    93  						},
    94  					},
    95  				},
    96  			},
    97  		},
    98  	}
    99  
   100  	cacheManager := agentcache.NewAgentCacheManager(&config.CentralConfiguration{}, false)
   101  	for _, tc := range tests {
   102  		t.Run(tc.name, func(t *testing.T) {
   103  			handler := NewInstanceHandler(cacheManager, "")
   104  
   105  			err := handler.Handle(NewEventContext(tc.action, nil, tc.resource.Kind, tc.resource.Name), nil, tc.resource)
   106  			if tc.hasError {
   107  				assert.NotNil(t, err)
   108  			} else {
   109  				assert.Nil(t, err)
   110  			}
   111  		})
   112  	}
   113  }