github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/graphql/version_validation_test.go (about)

     1  package graphql_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
     7  	"github.com/kyma-incubator/compass/components/director/pkg/inputvalidation/inputvalidationtest"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/str"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestVersionInput_Validate_Value(t *testing.T) {
    13  	testCases := []struct {
    14  		Name          string
    15  		Value         string
    16  		ExpectedValid bool
    17  	}{
    18  		{
    19  			Name:          "ExpectedValid",
    20  			Value:         "ExpectedValid",
    21  			ExpectedValid: true,
    22  		},
    23  		{
    24  			Name:          "Empty string",
    25  			Value:         inputvalidationtest.EmptyString,
    26  			ExpectedValid: false,
    27  		},
    28  		{
    29  			Name:          "String longer than 256 chars",
    30  			Value:         inputvalidationtest.String257Long,
    31  			ExpectedValid: false,
    32  		},
    33  	}
    34  
    35  	for _, testCase := range testCases {
    36  		t.Run(testCase.Name, func(t *testing.T) {
    37  			//GIVEN
    38  			obj := fixValidVersionInput()
    39  			obj.Value = testCase.Value
    40  			// WHEN
    41  			err := obj.Validate()
    42  			// THEN
    43  			if testCase.ExpectedValid {
    44  				require.NoError(t, err)
    45  			} else {
    46  				require.Error(t, err)
    47  			}
    48  		})
    49  	}
    50  }
    51  
    52  func TestVersionInput_Validate_Deprecated(t *testing.T) {
    53  	booleanTrue := true
    54  	booleanFalse := false
    55  
    56  	testCases := []struct {
    57  		Name          string
    58  		Value         *bool
    59  		ExpectedValid bool
    60  	}{
    61  		{
    62  			Name:          "ExpectedValid - true",
    63  			Value:         &booleanTrue,
    64  			ExpectedValid: true,
    65  		},
    66  		{
    67  			Name:          "ExpectedValid - false",
    68  			Value:         &booleanFalse,
    69  			ExpectedValid: true,
    70  		},
    71  		{
    72  			Name:          "Nil value",
    73  			Value:         nil,
    74  			ExpectedValid: false,
    75  		},
    76  	}
    77  
    78  	for _, testCase := range testCases {
    79  		t.Run(testCase.Name, func(t *testing.T) {
    80  			//GIVEN
    81  			doc := fixValidVersionInput()
    82  			doc.Deprecated = testCase.Value
    83  			// WHEN
    84  			err := doc.Validate()
    85  			// THEN
    86  			if testCase.ExpectedValid {
    87  				require.NoError(t, err)
    88  			} else {
    89  				require.Error(t, err)
    90  			}
    91  		})
    92  	}
    93  }
    94  
    95  func TestVersionInput_Validate_DeprecatedSince(t *testing.T) {
    96  	testCases := []struct {
    97  		Name          string
    98  		Value         *string
    99  		ExpectedValid bool
   100  	}{
   101  		{
   102  			Name:          "ExpectedValid",
   103  			Value:         str.Ptr("this is a valid string"),
   104  			ExpectedValid: true,
   105  		},
   106  		{
   107  			Name:          "Nil pointer",
   108  			Value:         nil,
   109  			ExpectedValid: true,
   110  		},
   111  		{
   112  			Name:          "Empty string",
   113  			Value:         str.Ptr(inputvalidationtest.EmptyString),
   114  			ExpectedValid: true,
   115  		},
   116  		{
   117  			Name:          "String longer than 256 chars",
   118  			Value:         str.Ptr(inputvalidationtest.String257Long),
   119  			ExpectedValid: false,
   120  		},
   121  	}
   122  
   123  	for _, testCase := range testCases {
   124  		t.Run(testCase.Name, func(t *testing.T) {
   125  			//GIVEN
   126  			obj := fixValidVersionInput()
   127  			obj.DeprecatedSince = testCase.Value
   128  			// WHEN
   129  			err := obj.Validate()
   130  			// THEN
   131  			if testCase.ExpectedValid {
   132  				require.NoError(t, err)
   133  			} else {
   134  				require.Error(t, err)
   135  			}
   136  		})
   137  	}
   138  }
   139  
   140  func TestVersionInput_Validate_ForRemoval(t *testing.T) {
   141  	booleanTrue := true
   142  	booleanFalse := false
   143  
   144  	testCases := []struct {
   145  		Name          string
   146  		Value         *bool
   147  		ExpectedValid bool
   148  	}{
   149  		{
   150  			Name:          "ExpectedValid - true",
   151  			Value:         &booleanTrue,
   152  			ExpectedValid: true,
   153  		},
   154  		{
   155  			Name:          "ExpectedValid - false",
   156  			Value:         &booleanFalse,
   157  			ExpectedValid: true,
   158  		},
   159  		{
   160  			Name:          "Nil value",
   161  			Value:         nil,
   162  			ExpectedValid: false,
   163  		},
   164  	}
   165  
   166  	for _, testCase := range testCases {
   167  		t.Run(testCase.Name, func(t *testing.T) {
   168  			//GIVEN
   169  			doc := fixValidVersionInput()
   170  			doc.ForRemoval = testCase.Value
   171  			// WHEN
   172  			err := doc.Validate()
   173  			// THEN
   174  			if testCase.ExpectedValid {
   175  				require.NoError(t, err)
   176  			} else {
   177  				require.Error(t, err)
   178  			}
   179  		})
   180  	}
   181  }
   182  
   183  func fixValidVersionInput() graphql.VersionInput {
   184  	boolean := true
   185  	return graphql.VersionInput{
   186  		Value: "value", Deprecated: &boolean, ForRemoval: &boolean}
   187  }