github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/peer/chaincode/instantiate_test.go (about)

     1  /*
     2   Copyright IBM Corp. 2017 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 chaincode
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestInstantiateCmd(t *testing.T) {
    26  	InitMSP()
    27  
    28  	mockCF, err := getMockChaincodeCmdFactory()
    29  	assert.NoError(t, err, "Error getting mock chaincode command factory")
    30  
    31  	// basic function tests
    32  	var tests = []struct {
    33  		name          string
    34  		args          []string
    35  		errorExpected bool
    36  		errMsg        string
    37  	}{
    38  		{
    39  			name:          "successful",
    40  			args:          []string{"-n", "example02", "-v", "anotherversion", "-c", "{\"Args\": [\"init\",\"a\",\"100\",\"b\",\"200\"]}"},
    41  			errorExpected: false,
    42  			errMsg:        "Run chaincode instantiate cmd error",
    43  		},
    44  		{
    45  			name:          "no option",
    46  			args:          []string{},
    47  			errorExpected: true,
    48  			errMsg:        "Expected error executing instantiate command without required options",
    49  		},
    50  		{
    51  			name:          "missing version",
    52  			args:          []string{"-n", "example02", "-c", "{\"Args\": [\"init\",\"a\",\"100\",\"b\",\"200\"]}"},
    53  			errorExpected: true,
    54  			errMsg:        "Expected error executing instantiate command without the -v option",
    55  		},
    56  		{
    57  			name:          "missing name",
    58  			args:          []string{"-v", "anotherversion", "-c", "{\"Args\": [\"init\",\"a\",\"100\",\"b\",\"200\"]}"},
    59  			errorExpected: true,
    60  			errMsg:        "Expected error executing instantiate command without the -n option",
    61  		},
    62  		{
    63  			name:          "missing ctor",
    64  			args:          []string{"-n", "example02", "-v", "anotherversion"},
    65  			errorExpected: true,
    66  			errMsg:        "Expected error executing instantiate command without the -c option",
    67  		},
    68  	}
    69  	for _, test := range tests {
    70  		t.Run(test.name, func(t *testing.T) {
    71  			resetFlags()
    72  			cmd := instantiateCmd(mockCF)
    73  			addFlags(cmd)
    74  			cmd.SetArgs(test.args)
    75  			err = cmd.Execute()
    76  			checkError(t, err, test.errorExpected, test.errMsg)
    77  		})
    78  	}
    79  }
    80  
    81  func checkError(t *testing.T, err error, expectedError bool, msg string) {
    82  	if expectedError {
    83  		assert.Error(t, err, msg)
    84  	} else {
    85  		assert.NoError(t, err, msg)
    86  	}
    87  }