github.com/kyma-project/kyma/components/asset-store-controller-manager@v0.0.0-20191203152857-3792b5df17c5/internal/assethook/mutation_engine_test.go (about)

     1  package assethook_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/kyma-project/kyma/components/asset-store-controller-manager/internal/assethook"
     9  	"github.com/kyma-project/kyma/components/asset-store-controller-manager/internal/assethook/automock"
    10  	"github.com/kyma-project/kyma/components/asset-store-controller-manager/pkg/apis/assetstore/v1alpha2"
    11  	"github.com/onsi/gomega"
    12  )
    13  
    14  func TestMutationEngine_Mutate(t *testing.T) {
    15  	for testName, testCase := range map[string]struct {
    16  		err      error
    17  		messages map[string][]assethook.Message
    18  	}{
    19  		"success": {},
    20  		"error": {
    21  			err: fmt.Errorf("test"),
    22  		},
    23  		"fail": {
    24  			messages: map[string][]assethook.Message{
    25  				"test": {
    26  					{Filename: "test", Message: "test"},
    27  				},
    28  			},
    29  		},
    30  	} {
    31  		t.Run(testName, func(t *testing.T) {
    32  			// Given
    33  			g := gomega.NewGomegaWithT(t)
    34  
    35  			processor := automock.NewHttpProcessor()
    36  			defer processor.AssertExpectations(t)
    37  			ctx := context.TODO()
    38  			files := []string{}
    39  			services := []v1alpha2.AssetWebhookService{}
    40  
    41  			processor.On("Do", ctx, "", files, services).Return(testCase.messages, testCase.err).Once()
    42  			mutator := assethook.NewTestMutator(processor)
    43  
    44  			// When
    45  			result, err := mutator.Mutate(ctx, "", files, services)
    46  
    47  			// Then
    48  			if testCase.err == nil {
    49  				g.Expect(err).ToNot(gomega.HaveOccurred())
    50  			} else {
    51  				g.Expect(err).To(gomega.HaveOccurred())
    52  			}
    53  
    54  			if len(testCase.messages) == 0 && err == nil {
    55  				g.Expect(result.Success).To(gomega.BeTrue())
    56  			} else {
    57  				g.Expect(result.Success).To(gomega.BeFalse())
    58  			}
    59  		})
    60  	}
    61  }