|
| 1 | +/* |
| 2 | + * Copyright 2019 IBM All Rights Reserved. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +package org.hyperledger.fabric.shim.impl; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 10 | +import static org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage.Type.GET_STATE_BY_RANGE; |
| 11 | +import static org.mockito.ArgumentMatchers.any; |
| 12 | +import static org.mockito.Mockito.mock; |
| 13 | +import static org.mockito.Mockito.verify; |
| 14 | +import static org.mockito.Mockito.when; |
| 15 | + |
| 16 | +import com.google.protobuf.ByteString; |
| 17 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 18 | + |
| 19 | +import org.hyperledger.fabric.protos.peer.ChaincodeShim.ChaincodeMessage; |
| 20 | +import org.hyperledger.fabric.protos.peer.ChaincodeShim.GetStateByRange; |
| 21 | +import org.hyperledger.fabric.protos.peer.ChaincodeShim.QueryResponse; |
| 22 | +import org.hyperledger.fabric.shim.ledger.KeyValue; |
| 23 | +import org.hyperledger.fabric.shim.ledger.QueryResultsIterator; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Nested; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.mockito.ArgumentCaptor; |
| 28 | + |
| 29 | +public class InvocationStubImplTest { |
| 30 | + |
| 31 | + private final String channelId = "mychannel"; |
| 32 | + private final String txId = "0xCAFEBABE"; |
| 33 | + |
| 34 | + @Nested |
| 35 | + class GetStateByRangeTests { |
| 36 | + |
| 37 | + private InvocationStubImpl stubImpl; |
| 38 | + private ArgumentCaptor<ChaincodeMessage> chaincodeMessageCaptor; |
| 39 | + private ChaincodeInvocationTask mockHandler; |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + public void beforeEach() throws Exception { |
| 43 | + final ChaincodeMessage mockMessage = ChaincodeMessageFactory.newGetStateEventMessage(channelId, txId, "", |
| 44 | + "key"); |
| 45 | + mockHandler = mock(ChaincodeInvocationTask.class); |
| 46 | + final ByteString mockString = QueryResponse.newBuilder().build().toByteString(); |
| 47 | + |
| 48 | + chaincodeMessageCaptor = ArgumentCaptor.forClass(ChaincodeMessage.class); |
| 49 | + |
| 50 | + when(mockHandler.invoke(any())).thenReturn(mockString); |
| 51 | + stubImpl = new InvocationStubImpl(mockMessage, mockHandler); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void regular() throws InvalidProtocolBufferException { |
| 56 | + final QueryResultsIterator<KeyValue> qri = stubImpl.getStateByRange("Aardvark", "Zebra"); |
| 57 | + |
| 58 | + verify(mockHandler).invoke(chaincodeMessageCaptor.capture()); |
| 59 | + assertThat(qri).isNotNull(); |
| 60 | + |
| 61 | + final ChaincodeMessage msg = chaincodeMessageCaptor.getValue(); |
| 62 | + assertThat(msg.getTxid()).isEqualTo("0xCAFEBABE"); |
| 63 | + assertThat(msg.getType()).isEqualTo(GET_STATE_BY_RANGE); |
| 64 | + |
| 65 | + final GetStateByRange range = GetStateByRange.parseFrom(msg.getPayload()); |
| 66 | + assertThat(range.getStartKey()).isEqualTo("Aardvark"); |
| 67 | + assertThat(range.getEndKey()).isEqualTo("Zebra"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void nullvalues() throws InvalidProtocolBufferException { |
| 72 | + final QueryResultsIterator<KeyValue> qri = stubImpl.getStateByRange(null, null); |
| 73 | + |
| 74 | + verify(mockHandler).invoke(chaincodeMessageCaptor.capture()); |
| 75 | + assertThat(qri).isNotNull(); |
| 76 | + |
| 77 | + final ChaincodeMessage msg = chaincodeMessageCaptor.getValue(); |
| 78 | + assertThat(msg.getTxid()).isEqualTo("0xCAFEBABE"); |
| 79 | + assertThat(msg.getType()).isEqualTo(GET_STATE_BY_RANGE); |
| 80 | + |
| 81 | + final GetStateByRange range = GetStateByRange.parseFrom(msg.getPayload()); |
| 82 | + |
| 83 | + final String unspecifiedKey = new String(Character.toChars(0x000001)); |
| 84 | + assertThat(range.getStartKey()).isEqualTo(unspecifiedKey); |
| 85 | + assertThat(range.getEndKey()).isEqualTo(unspecifiedKey); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void unbounded() throws InvalidProtocolBufferException { |
| 90 | + final QueryResultsIterator<KeyValue> qri = stubImpl.getStateByRange("", ""); |
| 91 | + |
| 92 | + verify(mockHandler).invoke(chaincodeMessageCaptor.capture()); |
| 93 | + assertThat(qri).isNotNull(); |
| 94 | + |
| 95 | + final ChaincodeMessage msg = chaincodeMessageCaptor.getValue(); |
| 96 | + assertThat(msg.getTxid()).isEqualTo("0xCAFEBABE"); |
| 97 | + assertThat(msg.getType()).isEqualTo(GET_STATE_BY_RANGE); |
| 98 | + |
| 99 | + final GetStateByRange range = GetStateByRange.parseFrom(msg.getPayload()); |
| 100 | + |
| 101 | + assertThat(range.getStartKey()).isEqualTo(""); |
| 102 | + assertThat(range.getEndKey()).isEqualTo(""); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void simplekeys() { |
| 107 | + assertThatThrownBy(() -> { |
| 108 | + final QueryResultsIterator<KeyValue> qri = stubImpl |
| 109 | + .getStateByRange(new String(Character.toChars(Character.MIN_CODE_POINT)), ""); |
| 110 | + }).hasMessageContaining("not allowed"); |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments