github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/go/tests/state_test.go (about)

     1  /**
     2   * Copyright 2017 Intel Corporation
     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  
    18  package tests
    19  
    20  import (
    21  	"fmt"
    22  	"github.com/golang/mock/gomock"
    23  	"github.com/golang/protobuf/proto"
    24  	"mocks/mock_messaging"
    25  	"sawtooth_sdk/messaging"
    26  	"sawtooth_sdk/processor"
    27  	"sawtooth_sdk/protobuf/events_pb2"
    28  	"sawtooth_sdk/protobuf/state_context_pb2"
    29  	"sawtooth_sdk/protobuf/validator_pb2"
    30  	"testing"
    31  )
    32  
    33  func TestState(t *testing.T) {
    34  	mockCtrl := gomock.NewController(t)
    35  	defer mockCtrl.Finish()
    36  
    37  	var connection messaging.Connection
    38  	mock_connection := mock_messaging.NewMockConnection(mockCtrl)
    39  	connection = mock_connection
    40  	context := processor.NewContext(connection, "abc")
    41  
    42  	request := &state_context_pb2.TpStateGetRequest{
    43  		ContextId: "abc",
    44  		Addresses: []string{"abc"},
    45  	}
    46  	bytes, _ := proto.Marshal(request)
    47  
    48  	mock_connection.EXPECT().SendNewMsg(validator_pb2.Message_TP_STATE_GET_REQUEST, bytes)
    49  	mock_connection.EXPECT().RecvMsgWithId("")
    50  
    51  	context.GetState([]string{"abc"})
    52  }
    53  
    54  func TestAddEvent(t *testing.T) {
    55  	mockCtrl := gomock.NewController(t)
    56  	defer mockCtrl.Finish()
    57  
    58  	var connection messaging.Connection
    59  	mock_connection := mock_messaging.NewMockConnection(mockCtrl)
    60  	connection = mock_connection
    61  	context := processor.NewContext(connection, "asdf")
    62  
    63  	event_attributes := make([]*events_pb2.Event_Attribute, 0)
    64  	event_attributes = append(
    65  		event_attributes,
    66  		&events_pb2.Event_Attribute{
    67  			Key:   "key",
    68  			Value: "value",
    69  		},
    70  	)
    71  
    72  	event := &events_pb2.Event{
    73  		EventType:  "test",
    74  		Attributes: event_attributes,
    75  		Data:       []byte("data"),
    76  	}
    77  
    78  	request := &state_context_pb2.TpEventAddRequest{
    79  		ContextId: "asdf",
    80  		Event:     event,
    81  	}
    82  	request_bytes, _ := proto.Marshal(request)
    83  
    84  	response_bytes, err := proto.Marshal(&state_context_pb2.TpEventAddResponse{
    85  		Status: state_context_pb2.TpEventAddResponse_OK,
    86  	})
    87  
    88  	mock_connection.EXPECT().SendNewMsg(validator_pb2.Message_TP_EVENT_ADD_REQUEST, request_bytes)
    89  	mock_connection.EXPECT().RecvMsgWithId("").
    90  		Return(
    91  			"",
    92  			&validator_pb2.Message{
    93  				MessageType:   validator_pb2.Message_TP_EVENT_ADD_RESPONSE,
    94  				CorrelationId: "",
    95  				Content:       response_bytes,
    96  			},
    97  			err)
    98  
    99  	attributes := []processor.Attribute{{"key", "value"}}
   100  	add_event_err := context.AddEvent("test", attributes, []byte("data"))
   101  	if add_event_err != nil {
   102  		t.Error(fmt.Errorf("ERROR: %s", add_event_err))
   103  	}
   104  }
   105  
   106  func TestReceiptData(t *testing.T) {
   107  	mockCtrl := gomock.NewController(t)
   108  	defer mockCtrl.Finish()
   109  
   110  	var connection messaging.Connection
   111  	mock_connection := mock_messaging.NewMockConnection(mockCtrl)
   112  	connection = mock_connection
   113  	context := processor.NewContext(connection, "qwerty")
   114  
   115  	request := &state_context_pb2.TpReceiptAddDataRequest{
   116  		ContextId: "qwerty",
   117  		Data:      []byte("receiptdata"),
   118  	}
   119  	bytes, _ := proto.Marshal(request)
   120  
   121  	mock_connection.EXPECT().SendNewMsg(validator_pb2.Message_TP_RECEIPT_ADD_DATA_REQUEST, bytes)
   122  	mock_connection.EXPECT().RecvMsgWithId("")
   123  
   124  	context.AddReceiptData([]byte("receiptdata"))
   125  }