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

     1  /*
     2   * Copyright 2020 The Compass Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package normalizer_test
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/kyma-incubator/compass/components/director/pkg/normalizer"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestNormalizer(t *testing.T) {
    28  	const expectedName = "mp-test-application"
    29  
    30  	// given
    31  	testCases := []struct {
    32  		Name     string
    33  		Input    string
    34  		Expected string
    35  	}{
    36  		{
    37  			Name:     "Contains upper case characters",
    38  			Input:    "tEsT-ApPlIcAtIoN",
    39  			Expected: expectedName,
    40  		},
    41  		{
    42  			Name:     "Contains dot non-alphanumeric character in the middle",
    43  			Input:    "test.application",
    44  			Expected: expectedName,
    45  		},
    46  		{
    47  			Name:     "Contains $ non-alphanumeric character in the middle",
    48  			Input:    "test$application",
    49  			Expected: expectedName,
    50  		},
    51  		{
    52  			Name:     "Contains & non-alphanumeric character in the middle",
    53  			Input:    "test&application",
    54  			Expected: expectedName,
    55  		},
    56  		{
    57  			Name:     "Contains @ non-alphanumeric character in the middle",
    58  			Input:    "test@application",
    59  			Expected: expectedName,
    60  		},
    61  		{
    62  			Name:     "Contains a sequence of non-alphanumeric character in the middle",
    63  			Input:    "test%.-&application",
    64  			Expected: expectedName,
    65  		},
    66  		{
    67  			Name:     "Contains non-alphanumeric character at the beginning",
    68  			Input:    "!test-application",
    69  			Expected: expectedName,
    70  		},
    71  		{
    72  			Name:     "Contains sequence of non-alphanumeric character at the beginning",
    73  			Input:    "!@#$%test-application",
    74  			Expected: expectedName,
    75  		},
    76  		{
    77  			Name:     "Contains non-alphanumeric character at the end",
    78  			Input:    "test-application%",
    79  			Expected: expectedName,
    80  		},
    81  		{
    82  			Name:     "Contains sequence of non-alphanumeric character at the end",
    83  			Input:    "test-application&*^%",
    84  			Expected: expectedName,
    85  		},
    86  		{
    87  			Name:     "Contains non-alphanumeric character at the beginning, end and in the middle",
    88  			Input:    "[test@application]",
    89  			Expected: expectedName,
    90  		},
    91  		{
    92  			Name:     "Contains multiple dash characters in the middle",
    93  			Input:    "test---application",
    94  			Expected: expectedName,
    95  		},
    96  		{
    97  			Name:     "Contains multiple dash characters at the beginning",
    98  			Input:    "---test-application",
    99  			Expected: expectedName,
   100  		},
   101  		{
   102  			Name:     "Contains multiple dash characters at the end",
   103  			Input:    "test-application---",
   104  			Expected: expectedName,
   105  		},
   106  		{
   107  			Name:     "Contains multiple dash characters at the beginning, end and in the middle",
   108  			Input:    "---test---application---",
   109  			Expected: expectedName,
   110  		},
   111  		{
   112  			Name:     "Contains a single dash character at the end",
   113  			Input:    "test-application-",
   114  			Expected: expectedName,
   115  		},
   116  		{
   117  			Name:     "Contains a combination of dashes and non-dash non-alphanumeric characters at the end",
   118  			Input:    "test-application&--*^-%",
   119  			Expected: expectedName,
   120  		},
   121  		{
   122  			Name:     "Contains upper case characters, non-alphanumeric characters and multiple dashes",
   123  			Input:    "---tEsT@ApPlIcAtIoN.!$",
   124  			Expected: expectedName,
   125  		},
   126  		{
   127  			Name:     "Doesn't contain upper case characters, non-alphanumeric characters or any redundant dashes",
   128  			Input:    "test-application",
   129  			Expected: expectedName,
   130  		},
   131  		{
   132  			Name:     "Already normalized",
   133  			Input:    expectedName,
   134  			Expected: expectedName,
   135  		},
   136  	}
   137  
   138  	for i, testCase := range testCases {
   139  		t.Run(fmt.Sprintf("%d: %s", i, testCase.Name), func(t *testing.T) {
   140  			defaultNormalizer := normalizer.DefaultNormalizator{}
   141  
   142  			// when
   143  			normalizedResult := defaultNormalizer.Normalize(testCase.Input)
   144  
   145  			// then
   146  			assert.Equal(t, testCase.Expected, normalizedResult)
   147  		})
   148  	}
   149  }