Skip to content

Commit 0948234

Browse files
mbwhitejt-nti
authored andcommitted
[FABCJ-288] fix: simple key end of range
As per Node and Go, permit "" to be used to indicate the end of the range. Only for simple keys (mistake in the checkstyle rules picked up by vscode checkstyle plugin) Signed-off-by: Matthew B White <whitemat@uk.ibm.com>
1 parent 1f39d52 commit 0948234

File tree

3 files changed

+122
-7
lines changed

3 files changed

+122
-7
lines changed

ci/checkstyle/checkstyle.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
-->
1111
<module name="Checker">
1212
<property name="severity" value="error"/>
13-
<property name="basedir" value="${basedir}"/>
1413
<property name="fileExtensions" value="java, properties, xml"/>
1514
<module name="TreeWalker">
1615
<module name="SuppressWithNearbyCommentFilter">

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/InvocationStubImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ public QueryResultsIterator<KeyValue> getStateByRange(final String startKey, fin
248248
String start = startKey;
249249
String end = endKey;
250250

251-
if (startKey == null || startKey.isEmpty()) {
251+
if (startKey == null) {
252252
start = UNSPECIFIED_KEY;
253253
}
254-
if (endKey == null || endKey.isEmpty()) {
254+
if (endKey == null) {
255255
end = UNSPECIFIED_KEY;
256256
}
257257
CompositeKey.validateSimpleKeys(start, end);
@@ -293,10 +293,10 @@ public QueryResultsIteratorWithMetadata<KeyValue> getStateByRangeWithPagination(
293293
String start = startKey;
294294
String end = endKey;
295295

296-
if (startKey == null || startKey.isEmpty()) {
296+
if (startKey == null) {
297297
start = UNSPECIFIED_KEY;
298298
}
299-
if (endKey == null || endKey.isEmpty()) {
299+
if (endKey == null) {
300300
end = UNSPECIFIED_KEY;
301301
}
302302

@@ -520,10 +520,10 @@ public QueryResultsIterator<KeyValue> getPrivateDataByRange(final String collect
520520
String end = endKey;
521521

522522
validateCollection(collection);
523-
if (startKey == null || startKey.isEmpty()) {
523+
if (startKey == null) {
524524
start = UNSPECIFIED_KEY;
525525
}
526-
if (endKey == null || endKey.isEmpty()) {
526+
if (endKey == null) {
527527
end = UNSPECIFIED_KEY;
528528
}
529529
CompositeKey.validateSimpleKeys(start, end);
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)