github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/operation/operation_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 operation_test 18 19 import ( 20 "context" 21 "fmt" 22 "testing" 23 24 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 25 "github.com/kyma-incubator/compass/components/director/pkg/operation" 26 "github.com/kyma-incubator/compass/components/director/pkg/resource" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestFromContext(t *testing.T) { 31 op1 := &operation.Operation{ 32 OperationType: operation.OperationTypeCreate, 33 OperationCategory: "registerApplication", 34 ResourceType: resource.Application, 35 } 36 37 op2 := &operation.Operation{ 38 OperationType: operation.OperationTypeCreate, 39 OperationCategory: "registerApplication", 40 ResourceType: resource.Application, 41 } 42 43 initOperations := &[]*operation.Operation{op1} 44 45 testCases := []struct { 46 Name string 47 Context context.Context 48 OperationsToAdd *[]*operation.Operation 49 50 ExpectedResult *[]*operation.Operation 51 Exists bool 52 }{ 53 { 54 Name: "When Operation slice is set should append to it", 55 OperationsToAdd: &[]*operation.Operation{op2}, 56 Context: context.WithValue(context.TODO(), operation.OpCtxKey, initOperations), 57 ExpectedResult: &[]*operation.Operation{op1, op2}, 58 Exists: true, 59 }, 60 { 61 Name: "When Operation slice is not set save it in the context", 62 OperationsToAdd: initOperations, 63 Context: context.TODO(), 64 ExpectedResult: initOperations, 65 Exists: true, 66 }, 67 { 68 Name: "When Operation slice is not set it in the context at all", 69 OperationsToAdd: nil, 70 Context: context.TODO(), 71 ExpectedResult: nil, 72 Exists: false, 73 }, 74 } 75 76 for i, testCase := range testCases { 77 t.Run(fmt.Sprintf("%d: %s", i, testCase.Name), func(t *testing.T) { 78 testCtx := operation.SaveToContext(testCase.Context, testCase.OperationsToAdd) 79 80 // when 81 result, exists := operation.FromCtx(testCtx) 82 83 // then 84 assert.Equal(t, testCase.ExpectedResult, result) 85 assert.Equal(t, testCase.Exists, exists) 86 }) 87 } 88 } 89 90 func TestSaveToContext(t *testing.T) { 91 // given 92 ctx := context.TODO() 93 94 op := &operation.Operation{ 95 OperationType: operation.OperationTypeCreate, 96 OperationCategory: "registerApplication", 97 ResourceType: resource.Application, 98 } 99 operations := &[]*operation.Operation{op} 100 101 // when 102 result := operation.SaveToContext(ctx, operations) 103 104 // then 105 assert.Equal(t, operations, result.Value(operation.OpCtxKey)) 106 } 107 108 func TestModeFromContext(t *testing.T) { 109 testCases := []struct { 110 Name string 111 Context context.Context 112 113 ExpectedResult graphql.OperationMode 114 }{ 115 { 116 Name: "When Operation Mode is explicitly set should return it", 117 Context: context.WithValue(context.TODO(), operation.OpModeKey, graphql.OperationModeAsync), 118 ExpectedResult: graphql.OperationModeAsync, 119 }, 120 { 121 Name: "When Operation Mode is not explicitly set, should return default (SYNC)", 122 Context: context.TODO(), 123 ExpectedResult: graphql.OperationModeSync, 124 }, 125 } 126 127 for i, testCase := range testCases { 128 t.Run(fmt.Sprintf("%d: %s", i, testCase.Name), func(t *testing.T) { 129 // when 130 result := operation.ModeFromCtx(testCase.Context) 131 132 // then 133 assert.Equal(t, testCase.ExpectedResult, result) 134 }) 135 } 136 } 137 138 func TestSaveModeToContext(t *testing.T) { 139 // given 140 ctx := context.TODO() 141 142 // when 143 result := operation.SaveModeToContext(ctx, graphql.OperationModeAsync) 144 145 // then 146 assert.Equal(t, graphql.OperationModeAsync, result.Value(operation.OpModeKey)) 147 }