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

     1  package viewer_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/internal/domain/viewer"
     7  
     8  	"github.com/google/uuid"
     9  	"github.com/kyma-incubator/compass/components/director/pkg/consumer"
    10  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestToViewer(t *testing.T) {
    16  	id := uuid.New().String()
    17  	testCases := []struct {
    18  		Name        string
    19  		Input       consumer.Consumer
    20  		Expected    graphql.Viewer
    21  		ExpectedErr bool
    22  	}{
    23  		{
    24  			Name:     "Convert to Runtime",
    25  			Input:    consumer.Consumer{ConsumerID: id, ConsumerType: consumer.Runtime},
    26  			Expected: graphql.Viewer{ID: id, Type: graphql.ViewerTypeRuntime},
    27  		},
    28  		{
    29  			Name:     "Convert To Application",
    30  			Input:    consumer.Consumer{ConsumerID: id, ConsumerType: consumer.Application},
    31  			Expected: graphql.Viewer{ID: id, Type: graphql.ViewerTypeApplication},
    32  		},
    33  		{
    34  			Name:     "Convert To Integration System",
    35  			Input:    consumer.Consumer{ConsumerID: id, ConsumerType: consumer.IntegrationSystem},
    36  			Expected: graphql.Viewer{ID: id, Type: graphql.ViewerTypeIntegrationSystem},
    37  		},
    38  		{
    39  			Name:     "Convert To User",
    40  			Input:    consumer.Consumer{ConsumerID: id, ConsumerType: consumer.User},
    41  			Expected: graphql.Viewer{ID: id, Type: graphql.ViewerTypeUser},
    42  		},
    43  		{
    44  			Name:        "Error while converting",
    45  			Input:       consumer.Consumer{ConsumerType: "Janusz"},
    46  			ExpectedErr: true,
    47  		},
    48  	}
    49  
    50  	for _, tc := range testCases {
    51  		t.Run(tc.Name, func(t *testing.T) {
    52  			// WHEN
    53  			viewer, err := viewer.ToViewer(tc.Input)
    54  			// THEN
    55  			if tc.ExpectedErr {
    56  				require.Error(t, err)
    57  			} else {
    58  				require.NotNil(t, viewer)
    59  				assert.Equal(t, tc.Expected, *viewer)
    60  			}
    61  		})
    62  	}
    63  }