github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/core/chaincode/shim/mockstub_test.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 shim
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"reflect"
    23  	"testing"
    24  
    25  	"github.com/spf13/viper"
    26  )
    27  
    28  func TestMockStateRangeQueryIterator(t *testing.T) {
    29  	stub := NewMockStub("rangeTest", nil)
    30  	stub.MockTransactionStart("init")
    31  	stub.PutState("1", []byte{61})
    32  	stub.PutState("0", []byte{62})
    33  	stub.PutState("5", []byte{65})
    34  	stub.PutState("3", []byte{63})
    35  	stub.PutState("4", []byte{64})
    36  	stub.PutState("6", []byte{66})
    37  	stub.MockTransactionEnd("init")
    38  
    39  	expectKeys := []string{"3", "4"}
    40  	expectValues := [][]byte{{63}, {64}}
    41  
    42  	rqi := NewMockStateRangeQueryIterator(stub, "2", "4")
    43  
    44  	fmt.Println("Running loop")
    45  	for i := 0; i < 2; i++ {
    46  		key, value, err := rqi.Next()
    47  		fmt.Println("Loop", i, "got", key, value, err)
    48  		if expectKeys[i] != key {
    49  			fmt.Println("Expected key", expectKeys[i], "got", key)
    50  			t.FailNow()
    51  		}
    52  		if expectValues[i][0] != value[0] {
    53  			fmt.Println("Expected value", expectValues[i], "got", value)
    54  		}
    55  	}
    56  }
    57  
    58  // TestMockStateRangeQueryIterator_openEnded tests running an open-ended query
    59  // for all keys on the MockStateRangeQueryIterator
    60  func TestMockStateRangeQueryIterator_openEnded(t *testing.T) {
    61  	stub := NewMockStub("rangeTest", nil)
    62  	stub.MockTransactionStart("init")
    63  	stub.PutState("1", []byte{61})
    64  	stub.PutState("0", []byte{62})
    65  	stub.PutState("5", []byte{65})
    66  	stub.PutState("3", []byte{63})
    67  	stub.PutState("4", []byte{64})
    68  	stub.PutState("6", []byte{66})
    69  	stub.MockTransactionEnd("init")
    70  
    71  	rqi := NewMockStateRangeQueryIterator(stub, "", "")
    72  
    73  	count := 0
    74  	for rqi.HasNext() {
    75  		rqi.Next()
    76  		count++
    77  	}
    78  
    79  	if count != rqi.Stub.Keys.Len() {
    80  		t.FailNow()
    81  	}
    82  }
    83  
    84  // TestSetChaincodeLoggingLevel uses the utlity function defined in chaincode.go to
    85  // set the chaincodeLogger's logging level
    86  func TestSetChaincodeLoggingLevel(t *testing.T) {
    87  	// set log level to a non-default level
    88  	testLogLevelString := "debug"
    89  	viper.Set("logging.chaincode", testLogLevelString)
    90  
    91  	SetChaincodeLoggingLevel()
    92  
    93  	if !IsEnabledForLogLevel(testLogLevelString) {
    94  		t.FailNow()
    95  	}
    96  }
    97  
    98  type Marble struct {
    99  	ObjectType string `json:"docType"` //docType is used to distinguish the various types of objects in state database
   100  	Name       string `json:"name"`    //the fieldtags are needed to keep case from bouncing around
   101  	Color      string `json:"color"`
   102  	Size       int    `json:"size"`
   103  	Owner      string `json:"owner"`
   104  }
   105  
   106  // JSONBytesEqual compares the JSON in two byte slices.
   107  func jsonBytesEqual(expected []byte, actual []byte) bool {
   108  	var infExpected, infActual interface{}
   109  	if err := json.Unmarshal(expected, &infExpected); err != nil {
   110  		return false
   111  	}
   112  	if err := json.Unmarshal(actual, &infActual); err != nil {
   113  		return false
   114  	}
   115  	return reflect.DeepEqual(infActual, infExpected)
   116  }
   117  
   118  func TestGetStateByPartialCompositeKey(t *testing.T) {
   119  	stub := NewMockStub("GetStateByPartialCompositeKeyTest", nil)
   120  	stub.MockTransactionStart("init")
   121  
   122  	marble1 := &Marble{"marble", "set-1", "red", 5, "tom"}
   123  	// Convert marble1 to JSON with Color and Name as composite key
   124  	compositeKey1, _ := stub.CreateCompositeKey(marble1.ObjectType, []string{marble1.Name, marble1.Color})
   125  	marbleJSONBytes1, _ := json.Marshal(marble1)
   126  	// Add marble1 JSON to state
   127  	stub.PutState(compositeKey1, marbleJSONBytes1)
   128  
   129  	marble2 := &Marble{"marble", "set-1", "blue", 5, "jerry"}
   130  	compositeKey2, _ := stub.CreateCompositeKey(marble2.ObjectType, []string{marble2.Name, marble2.Color})
   131  	marbleJSONBytes2, _ := json.Marshal(marble2)
   132  	stub.PutState(compositeKey2, marbleJSONBytes2)
   133  
   134  	marble3 := &Marble{"marble", "set-2", "red", 5, "tom-jerry"}
   135  	compositeKey3, _ := stub.CreateCompositeKey(marble3.ObjectType, []string{marble3.Name, marble3.Color})
   136  	marbleJSONBytes3, _ := json.Marshal(marble3)
   137  	stub.PutState(compositeKey3, marbleJSONBytes3)
   138  
   139  	stub.MockTransactionEnd("init")
   140  	// should return in sorted order of attributes
   141  	expectKeys := []string{compositeKey2, compositeKey1}
   142  	expectKeysAttributes := [][]string{{"set-1", "blue"}, {"set-1", "red"}}
   143  	expectValues := [][]byte{marbleJSONBytes2, marbleJSONBytes1}
   144  
   145  	rqi, _ := stub.GetStateByPartialCompositeKey("marble", []string{"set-1"})
   146  	fmt.Println("Running loop")
   147  	for i := 0; i < 2; i++ {
   148  		key, value, err := rqi.Next()
   149  		fmt.Println("Loop", i, "got", key, value, err)
   150  		if expectKeys[i] != key {
   151  			fmt.Println("Expected key", expectKeys[i], "got", key)
   152  			t.FailNow()
   153  		}
   154  		objectType, attributes, _ := stub.SplitCompositeKey(key)
   155  		if objectType != "marble" {
   156  			fmt.Println("Expected objectType", "marble", "got", objectType)
   157  			t.FailNow()
   158  		}
   159  		fmt.Println(attributes)
   160  		for index, attr := range attributes {
   161  			if expectKeysAttributes[i][index] != attr {
   162  				fmt.Println("Expected keys attribute", expectKeysAttributes[index][i], "got", attr)
   163  				t.FailNow()
   164  			}
   165  		}
   166  		if jsonBytesEqual(expectValues[i], value) != true {
   167  			fmt.Println("Expected value", expectValues[i], "got", value)
   168  			t.FailNow()
   169  		}
   170  	}
   171  }
   172  
   173  func TestGetStateByPartialCompositeKeyCollision(t *testing.T) {
   174  	stub := NewMockStub("GetStateByPartialCompositeKeyCollisionTest", nil)
   175  	stub.MockTransactionStart("init")
   176  
   177  	vehicle1Bytes := []byte("vehicle1")
   178  	compositeKeyVehicle1, _ := stub.CreateCompositeKey("Vehicle", []string{"VIN_1234"})
   179  	stub.PutState(compositeKeyVehicle1, vehicle1Bytes)
   180  
   181  	vehicleListing1Bytes := []byte("vehicleListing1")
   182  	compositeKeyVehicleListing1, _ := stub.CreateCompositeKey("VehicleListing", []string{"LIST_1234"})
   183  	stub.PutState(compositeKeyVehicleListing1, vehicleListing1Bytes)
   184  
   185  	stub.MockTransactionEnd("init")
   186  
   187  	// Only the single "Vehicle" object should be returned, not the "VehicleListing" object
   188  	rqi, _ := stub.GetStateByPartialCompositeKey("Vehicle", []string{})
   189  	i := 0
   190  	fmt.Println("Running loop")
   191  	for rqi.HasNext() {
   192  		i++
   193  		key, value, err := rqi.Next()
   194  		fmt.Println("Loop", i, "got", key, value, err)
   195  	}
   196  	// Only the single "Vehicle" object should be returned, not the "VehicleListing" object
   197  	if i != 1 {
   198  		fmt.Println("Expected 1, got", i)
   199  		t.FailNow()
   200  	}
   201  }