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

     1  package apic
     2  
     3  import (
     4  	"testing"
     5  
     6  	management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
     7  	defs "github.com/Axway/agent-sdk/pkg/apic/definitions"
     8  	"github.com/Axway/agent-sdk/pkg/config"
     9  	"github.com/Axway/agent-sdk/pkg/util"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestUpdateAPIServiceRevisionTitle(t *testing.T) {
    14  	testCases := []struct {
    15  		name         string
    16  		format       string
    17  		apiName      string
    18  		stage        string
    19  		stageDisplay string
    20  		label        string
    21  		count        int
    22  		expected     string
    23  	}{
    24  		{
    25  			name:     "No Stage",
    26  			apiName:  "API-Name",
    27  			count:    1,
    28  			expected: "API-Name - \\d{4}/\\d{2}/\\d{2} - r 1",
    29  		},
    30  		{
    31  			name:     "Stage - default label",
    32  			apiName:  "API-Name",
    33  			stage:    "PROD",
    34  			count:    5,
    35  			expected: "API-Name \\(Stage\\: PROD\\) - \\d{4}/\\d{2}/\\d{2} - r 5",
    36  		},
    37  		{
    38  			name:     "Stage - new label",
    39  			apiName:  "API-Name",
    40  			stage:    "PROD",
    41  			label:    "Portal",
    42  			count:    3,
    43  			expected: "API-Name \\(Portal\\: PROD\\) - \\d{4}/\\d{2}/\\d{2} - r 3",
    44  		},
    45  		{
    46  			name:     "Bad Date - default",
    47  			format:   "{{.APIServiceName}} - {{.Date:YYY/MM/DD}} - r {{.Revision}}",
    48  			apiName:  "API-Name",
    49  			count:    1,
    50  			expected: "API-Name - \\d{4}/\\d{2}/\\d{2} - r 1",
    51  		},
    52  		{
    53  			name:     "New Date Format",
    54  			format:   "{{.APIServiceName}} - {{.Date:YYYY-MM-DD}}",
    55  			apiName:  "API-Name",
    56  			count:    1,
    57  			expected: "API-Name - \\d{4}-\\d{2}-\\d{2}",
    58  		},
    59  		{
    60  			name:     "Deprecated Date",
    61  			format:   "{{.APIServiceName}} - {{date}} - r {{.Revision}}",
    62  			apiName:  "API-Name",
    63  			count:    1,
    64  			expected: "API-Name - \\d{4}/\\d{2}/\\d{2} - r 1",
    65  		},
    66  		{
    67  			name:     "Bar Variable - default",
    68  			format:   "{{.APIServiceName1}} - {{date}} - r {{.Revision}}",
    69  			apiName:  "API-Name",
    70  			count:    1,
    71  			expected: "API-Name - \\d{4}/\\d{2}/\\d{2} - r 1",
    72  		},
    73  		{
    74  			name:     "Stage - new format",
    75  			format:   "{{.Stage}} - {{.APIServiceName}} - {{.Date:MM/DD/YYYY}} - r {{.Revision}}",
    76  			apiName:  "API-Name",
    77  			stage:    "MyStage",
    78  			label:    "Test",
    79  			count:    6,
    80  			expected: "MyStage - API-Name - \\d{2}/\\d{2}/\\d{4} - r 6",
    81  		},
    82  		{
    83  			name:         "Stage - new format",
    84  			format:       "{{.Stage}} - {{.APIServiceName}} - {{.Date:MM/DD/YYYY}} - r {{.Revision}}",
    85  			apiName:      "API-Name",
    86  			stage:        "e4e084b66fcf325a016fcf54677b0001",
    87  			stageDisplay: "MyStage",
    88  			label:        "Test",
    89  			count:        6,
    90  			expected:     "MyStage - API-Name - \\d{2}/\\d{2}/\\d{4} - r 6",
    91  		},
    92  	}
    93  
    94  	for _, test := range testCases {
    95  		t.Run(test.name, func(t *testing.T) {
    96  			// create the service client
    97  			c := ServiceClient{
    98  				cfg: config.NewCentralConfig(config.DiscoveryAgent),
    99  			}
   100  			c.cfg.(*config.CentralConfiguration).APIServiceRevisionPattern = test.format
   101  
   102  			s := &ServiceBody{
   103  				APIName:          test.apiName,
   104  				Stage:            test.stage,
   105  				StageDisplayName: test.stageDisplay,
   106  				serviceContext: serviceContext{
   107  					revisionCount: test.count,
   108  				},
   109  				StageDescriptor: "Stage", // default
   110  			}
   111  			if test.label != "" {
   112  				s.StageDescriptor = test.label
   113  			}
   114  
   115  			title := c.updateAPIServiceRevisionTitle(s)
   116  			assert.Regexp(t, test.expected, title)
   117  		})
   118  	}
   119  }
   120  
   121  func Test_buildAPIServiceRevision(t *testing.T) {
   122  	body := &ServiceBody{
   123  		Description:      "description",
   124  		ImageContentType: "content-type",
   125  		Image:            "image-data",
   126  		NameToPush:       "nametopush",
   127  		APIName:          "apiname",
   128  		RestAPIID:        "restapiid",
   129  		PrimaryKey:       "primarykey",
   130  		Stage:            "staging",
   131  		Version:          "v1",
   132  		Tags: map[string]interface{}{
   133  			"tag1": "value1",
   134  			"tag2": "value2",
   135  		},
   136  		CreatedBy:          "createdby",
   137  		ServiceAttributes:  map[string]string{"service_attribute": "value"},
   138  		RevisionAttributes: map[string]string{"revision_attribute": "value"},
   139  		InstanceAttributes: map[string]string{"instance_attribute": "value"},
   140  		ServiceAgentDetails: map[string]interface{}{
   141  			"subresource_svc_key": "value",
   142  		},
   143  		InstanceAgentDetails: map[string]interface{}{
   144  			"subresource_instance_key": "value",
   145  		},
   146  		RevisionAgentDetails: map[string]interface{}{
   147  			"subresource_revision_key": "value",
   148  		},
   149  		serviceContext: serviceContext{serviceName: "service-context-name"},
   150  	}
   151  
   152  	tags := []string{"tag1_value1", "tag2_value2"}
   153  
   154  	client, _ := GetTestServiceClient()
   155  	revision := client.buildAPIServiceRevision(body)
   156  
   157  	assert.Equal(t, management.APIServiceRevisionGVK(), revision.GroupVersionKind)
   158  	assert.Contains(t, revision.Title, body.APIName)
   159  	assert.Contains(t, revision.Title, body.Stage)
   160  	assert.Contains(t, revision.Tags, tags[0])
   161  	assert.Contains(t, revision.Tags, tags[1])
   162  
   163  	assert.Contains(t, revision.Attributes, "revision_attribute")
   164  	assert.NotContains(t, revision.Attributes, "service_attribute")
   165  	assert.NotContains(t, revision.Attributes, "instance_attribute")
   166  	assert.NotContains(t, revision.Attributes, defs.AttrExternalAPIStage)
   167  	assert.NotContains(t, revision.Attributes, defs.AttrExternalAPIPrimaryKey)
   168  	assert.NotContains(t, revision.Attributes, defs.AttrExternalAPIID)
   169  	assert.NotContains(t, revision.Attributes, defs.AttrExternalAPIName)
   170  	assert.NotContains(t, revision.Attributes, defs.AttrCreatedBy)
   171  
   172  	assert.Equal(t, Unstructured, revision.Spec.Definition.Type)
   173  	assert.Equal(t, body.serviceContext.serviceName, revision.Spec.ApiService)
   174  
   175  	sub := util.GetAgentDetails(revision)
   176  	assert.Equal(t, body.Stage, sub[defs.AttrExternalAPIStage])
   177  	assert.Equal(t, body.PrimaryKey, sub[defs.AttrExternalAPIPrimaryKey])
   178  	assert.Equal(t, body.RestAPIID, sub[defs.AttrExternalAPIID])
   179  	assert.Equal(t, body.APIName, sub[defs.AttrExternalAPIName])
   180  	assert.Equal(t, body.CreatedBy, sub[defs.AttrCreatedBy])
   181  	assert.Contains(t, sub, "subresource_svc_key")
   182  	assert.Contains(t, sub, "subresource_revision_key")
   183  	assert.NotContains(t, sub, "subresource_instance_key")
   184  }
   185  
   186  func Test_buildAPIServiceRevisionNilAttributes(t *testing.T) {
   187  	client, _ := GetTestServiceClient()
   188  	body := &ServiceBody{}
   189  
   190  	rev := client.buildAPIServiceRevision(body)
   191  	assert.NotNil(t, rev.Attributes)
   192  }