|
| 1 | +/* |
| 2 | +Copyright IBM 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 | +package org.hyperledger.fabric.shim.impl; |
| 17 | + |
| 18 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 19 | +import static org.hamcrest.Matchers.equalTo; |
| 20 | +import static org.hamcrest.Matchers.hasProperty; |
| 21 | +import static org.hamcrest.Matchers.is; |
| 22 | +import static org.junit.Assert.assertThat; |
| 23 | + |
| 24 | +import java.util.stream.Stream; |
| 25 | + |
| 26 | +import org.hyperledger.fabric.protos.ledger.queryresult.KvQueryResult; |
| 27 | +import org.hyperledger.fabric.shim.ledger.KeyModification; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +import com.google.protobuf.ByteString; |
| 31 | +import com.google.protobuf.Timestamp; |
| 32 | + |
| 33 | +public class KeyModificationImplTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testKeyModificationImpl() { |
| 37 | + new KeyModificationImpl(KvQueryResult.KeyModification.newBuilder() |
| 38 | + .setTxId("txid") |
| 39 | + .setValue(ByteString.copyFromUtf8("value")) |
| 40 | + .setTimestamp(Timestamp.newBuilder() |
| 41 | + .setSeconds(1234567890) |
| 42 | + .setNanos(123456789)) |
| 43 | + .setIsDelete(true) |
| 44 | + .build() |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testGetTxId() { |
| 50 | + final KeyModification km = new KeyModificationImpl(KvQueryResult.KeyModification.newBuilder() |
| 51 | + .setTxId("txid") |
| 52 | + .build() |
| 53 | + ); |
| 54 | + assertThat(km.getTxId(), is(equalTo("txid"))); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testGetValue() { |
| 59 | + final KeyModification km = new KeyModificationImpl(KvQueryResult.KeyModification.newBuilder() |
| 60 | + .setValue(ByteString.copyFromUtf8("value")) |
| 61 | + .build() |
| 62 | + ); |
| 63 | + assertThat(km.getValue(), is(equalTo("value".getBytes(UTF_8)))); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testGetStringValue() { |
| 68 | + final KeyModification km = new KeyModificationImpl(KvQueryResult.KeyModification.newBuilder() |
| 69 | + .setValue(ByteString.copyFromUtf8("value")) |
| 70 | + .build() |
| 71 | + ); |
| 72 | + assertThat(km.getStringValue(), is(equalTo("value"))); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testGetTimestamp() { |
| 77 | + final KeyModification km = new KeyModificationImpl(KvQueryResult.KeyModification.newBuilder() |
| 78 | + .setTimestamp(Timestamp.newBuilder() |
| 79 | + .setSeconds(1234567890L) |
| 80 | + .setNanos(123456789)) |
| 81 | + .build() |
| 82 | + ); |
| 83 | + assertThat(km.getTimestamp(), hasProperty("epochSecond", equalTo(1234567890L))); |
| 84 | + assertThat(km.getTimestamp(), hasProperty("nano", equalTo(123456789))); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testIsDeleted() { |
| 89 | + Stream.of(true, false) |
| 90 | + .forEach(b -> { |
| 91 | + final KeyModification km = new KeyModificationImpl(KvQueryResult.KeyModification.newBuilder() |
| 92 | + .setIsDelete(b) |
| 93 | + .build() |
| 94 | + ); |
| 95 | + assertThat(km.isDeleted(), is(b)); |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments