github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/bddtests/context.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     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 bddtests
    18  
    19  import (
    20  	"fmt"
    21  	"regexp"
    22  	"strings"
    23  
    24  	"github.com/DATA-DOG/godog"
    25  	"github.com/DATA-DOG/godog/gherkin"
    26  	"github.com/hyperledger/fabric/common/util"
    27  )
    28  
    29  // BDDContext represents the current context for the executing scenario.  Commensurate concept of 'context' from behave testing.
    30  type BDDContext struct {
    31  	grpcClientPort            int
    32  	composition               *Composition
    33  	godogSuite                *godog.Suite
    34  	scenarioOrScenarioOutline interface{}
    35  	users                     map[string]*UserRegistration
    36  }
    37  
    38  func (b *BDDContext) getScenarioDefinition() *gherkin.ScenarioDefinition {
    39  	if b.scenarioOrScenarioOutline == nil {
    40  		return nil
    41  	}
    42  	switch t := b.scenarioOrScenarioOutline.(type) {
    43  	case *gherkin.Scenario:
    44  		return &(t.ScenarioDefinition)
    45  	case *gherkin.ScenarioOutline:
    46  		return &(t.ScenarioDefinition)
    47  	}
    48  	return nil
    49  }
    50  
    51  func (b *BDDContext) hasTag(tagName string) bool {
    52  	if b.scenarioOrScenarioOutline == nil {
    53  		return false
    54  	}
    55  	hasTagInner := func(tags []*gherkin.Tag) bool {
    56  		for _, t := range tags {
    57  			if t.Name == tagName {
    58  				return true
    59  			}
    60  		}
    61  		return false
    62  	}
    63  
    64  	switch t := b.scenarioOrScenarioOutline.(type) {
    65  	case *gherkin.Scenario:
    66  		return hasTagInner(t.Tags)
    67  	case *gherkin.ScenarioOutline:
    68  		return hasTagInner(t.Tags)
    69  	}
    70  	return false
    71  }
    72  
    73  // GetArgsForUser will return an arg slice of string allowing for replacement of parameterized values based upon tags for userRegistration
    74  func (b *BDDContext) GetArgsForUser(cells []*gherkin.TableCell, userRegistration *UserRegistration) (args []string, err error) {
    75  	regExp := regexp.MustCompile("\\{(.*?)\\}+")
    76  	// Loop through cells and replace with user tag values if found
    77  	for _, cell := range cells {
    78  		var arg = cell.Value
    79  		for _, tagNameToFind := range regExp.FindAllStringSubmatch(cell.Value, -1) {
    80  			println("toFind = ", tagNameToFind[0], " to replace = ", tagNameToFind[1])
    81  			var tagValue interface{}
    82  			tagValue, err = userRegistration.GetTagValue(tagNameToFind[1])
    83  			if err != nil {
    84  				return nil, fmt.Errorf("Error getting args for user '%s': %s", userRegistration.enrollID, err)
    85  			}
    86  			arg = strings.Replace(arg, tagNameToFind[0], fmt.Sprintf("%v", tagValue), 1)
    87  		}
    88  		args = append(args, arg)
    89  	}
    90  	return args, nil
    91  }
    92  
    93  func (b *BDDContext) weCompose(composeFiles string) error {
    94  	if b.composition != nil {
    95  		return fmt.Errorf("Already have composition in BDD context (%s)", b.composition.projectName)
    96  	}
    97  	// Need a unique name, but docker does not allow '-' in names
    98  	composeProjectName := strings.Replace(util.GenerateUUID(), "-", "", -1)
    99  	newComposition, err := NewComposition(composeProjectName, composeFiles)
   100  	if err != nil {
   101  		return fmt.Errorf("Error composing system in BDD context:  %s", err)
   102  	}
   103  	b.composition = newComposition
   104  	return nil
   105  }
   106  
   107  func (b *BDDContext) beforeScenario(scenarioOrScenarioOutline interface{}) {
   108  	b.scenarioOrScenarioOutline = scenarioOrScenarioOutline
   109  	//switch t := scenarioOrScenarioOutline.(type) {
   110  	//case *gherkin.Scenario:
   111  	//	fmt.Printf("Scenario received %v", t)
   112  	//case *gherkin.ScenarioOutline:
   113  	//	fmt.Printf("ScenarioOutline received %v", t)
   114  	//}
   115  }
   116  
   117  func (b *BDDContext) afterScenarioDecompose(interface{}, error) {
   118  	if b.hasTag("@doNotDecompose") == true {
   119  		fmt.Printf("Not decomposing:  %s", b.getScenarioDefinition().Name)
   120  	} else {
   121  		if b.composition != nil {
   122  			b.composition.Decompose()
   123  		}
   124  	}
   125  	// Now clear the users
   126  	b.composition = nil
   127  	b.users = make(map[string]*UserRegistration)
   128  }