github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/model/document_test.go (about)

     1  package model_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/pkg/resource"
     8  
     9  	"github.com/kyma-incubator/compass/components/director/internal/model"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestDocumentInput_ToDocument(t *testing.T) {
    14  	// GIVEN
    15  	bundleID := "foo"
    16  	appID := "appID"
    17  	appTemplateVerionID := "naz"
    18  	id := "bar"
    19  	kind := "fookind"
    20  	data := "foodata"
    21  	displayName := "foodisplay"
    22  	description := "foodescription"
    23  	title := "footitle"
    24  	testCases := []struct {
    25  		Name         string
    26  		Input        *model.DocumentInput
    27  		ResourceType resource.Type
    28  		ResourceID   string
    29  		Expected     *model.Document
    30  	}{
    31  		{
    32  			Name: "All properties given for App",
    33  			Input: &model.DocumentInput{
    34  				Title:       title,
    35  				DisplayName: displayName,
    36  				Description: description,
    37  				Format:      model.DocumentFormatMarkdown,
    38  				Kind:        &kind,
    39  				Data:        &data,
    40  				FetchRequest: &model.FetchRequestInput{
    41  					URL: "foo.bar",
    42  				},
    43  			},
    44  			Expected: &model.Document{
    45  				BundleID:    bundleID,
    46  				AppID:       &appID,
    47  				Title:       title,
    48  				DisplayName: displayName,
    49  				Description: description,
    50  				Format:      model.DocumentFormatMarkdown,
    51  				Kind:        &kind,
    52  				Data:        &data,
    53  				BaseEntity: &model.BaseEntity{
    54  					ID:    id,
    55  					Ready: true,
    56  				},
    57  			},
    58  			ResourceType: resource.Application,
    59  			ResourceID:   appID,
    60  		},
    61  		{
    62  			Name: "All properties given for App Template Version",
    63  			Input: &model.DocumentInput{
    64  				Title:       title,
    65  				DisplayName: displayName,
    66  				Description: description,
    67  				Format:      model.DocumentFormatMarkdown,
    68  				Kind:        &kind,
    69  				Data:        &data,
    70  				FetchRequest: &model.FetchRequestInput{
    71  					URL: "foo.bar",
    72  				},
    73  			},
    74  			Expected: &model.Document{
    75  				BundleID:                     bundleID,
    76  				ApplicationTemplateVersionID: &appTemplateVerionID,
    77  				Title:                        title,
    78  				DisplayName:                  displayName,
    79  				Description:                  description,
    80  				Format:                       model.DocumentFormatMarkdown,
    81  				Kind:                         &kind,
    82  				Data:                         &data,
    83  				BaseEntity: &model.BaseEntity{
    84  					ID:    id,
    85  					Ready: true,
    86  				},
    87  			},
    88  			ResourceType: resource.ApplicationTemplateVersion,
    89  			ResourceID:   appTemplateVerionID,
    90  		},
    91  		{
    92  			Name: "No FetchRequest",
    93  			Input: &model.DocumentInput{
    94  				Title:        title,
    95  				DisplayName:  displayName,
    96  				Description:  description,
    97  				Format:       model.DocumentFormatMarkdown,
    98  				Kind:         &kind,
    99  				Data:         &data,
   100  				FetchRequest: nil,
   101  			},
   102  			Expected: &model.Document{
   103  				BundleID:    bundleID,
   104  				AppID:       &appID,
   105  				Title:       title,
   106  				DisplayName: displayName,
   107  				Description: description,
   108  				Format:      model.DocumentFormatMarkdown,
   109  				Kind:        &kind,
   110  				Data:        &data,
   111  				BaseEntity: &model.BaseEntity{
   112  					ID:    id,
   113  					Ready: true,
   114  				},
   115  			},
   116  			ResourceType: resource.Application,
   117  			ResourceID:   appID,
   118  		},
   119  		{
   120  			Name:  "Empty",
   121  			Input: &model.DocumentInput{},
   122  			Expected: &model.Document{
   123  				BundleID: bundleID,
   124  				AppID:    &appID,
   125  				BaseEntity: &model.BaseEntity{
   126  					ID:    id,
   127  					Ready: true,
   128  				},
   129  			},
   130  			ResourceType: resource.Application,
   131  			ResourceID:   appID,
   132  		},
   133  		{
   134  			Name:     "Nil",
   135  			Input:    nil,
   136  			Expected: nil,
   137  		},
   138  	}
   139  
   140  	for i, testCase := range testCases {
   141  		t.Run(fmt.Sprintf("%d: %s", i, testCase.Name), func(t *testing.T) {
   142  			// WHEN
   143  			result := testCase.Input.ToDocumentWithinBundle(id, bundleID, testCase.ResourceType, testCase.ResourceID)
   144  
   145  			// THEN
   146  			assert.Equal(t, testCase.Expected, result)
   147  		})
   148  	}
   149  }