|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 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 | + * https://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 com.google.cloud.bigtable.data.v2.internal; |
| 17 | + |
| 18 | +import static com.google.common.truth.Truth.assertThat; |
| 19 | + |
| 20 | +import com.google.bigtable.v2.ReadRowsResponse; |
| 21 | +import com.google.bigtable.v2.ReadRowsResponse.CellChunk; |
| 22 | +import com.google.cloud.bigtable.data.v2.models.Row; |
| 23 | +import com.google.cloud.bigtable.data.v2.models.RowCell; |
| 24 | +import com.google.common.collect.ImmutableList; |
| 25 | +import com.google.protobuf.ByteString; |
| 26 | +import com.google.protobuf.BytesValue; |
| 27 | +import com.google.protobuf.StringValue; |
| 28 | +import java.util.List; |
| 29 | +import junit.framework.TestCase; |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.Test; |
| 32 | +import org.junit.runner.RunWith; |
| 33 | +import org.junit.runners.JUnit4; |
| 34 | + |
| 35 | +@RunWith(JUnit4.class) |
| 36 | +public class RowMergerUtilTest extends TestCase { |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testEmpty() { |
| 40 | + try (RowMergerUtil util = new RowMergerUtil()) {} |
| 41 | + |
| 42 | + try (RowMergerUtil util = new RowMergerUtil()) { |
| 43 | + util.parseReadRowsResponses(ImmutableList.of()); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testSingle() { |
| 49 | + List<ReadRowsResponse> responses = |
| 50 | + ImmutableList.of( |
| 51 | + ReadRowsResponse.newBuilder() |
| 52 | + .addChunks( |
| 53 | + CellChunk.newBuilder() |
| 54 | + .setRowKey(ByteString.copyFromUtf8("key")) |
| 55 | + .setFamilyName(StringValue.newBuilder().setValue("family")) |
| 56 | + .setQualifier( |
| 57 | + BytesValue.newBuilder().setValue(ByteString.copyFromUtf8("qualifier"))) |
| 58 | + .setTimestampMicros(1000) |
| 59 | + .setValue(ByteString.copyFromUtf8("value")) |
| 60 | + .setCommitRow(true)) |
| 61 | + .build()); |
| 62 | + try (RowMergerUtil util = new RowMergerUtil()) { |
| 63 | + List<Row> rows = util.parseReadRowsResponses(responses); |
| 64 | + assertThat(rows) |
| 65 | + .containsExactly( |
| 66 | + Row.create( |
| 67 | + ByteString.copyFromUtf8("key"), |
| 68 | + ImmutableList.of( |
| 69 | + RowCell.create( |
| 70 | + "family", |
| 71 | + ByteString.copyFromUtf8("qualifier"), |
| 72 | + 1000, |
| 73 | + ImmutableList.of(), |
| 74 | + ByteString.copyFromUtf8("value"))))); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testMultiple() { |
| 80 | + List<ReadRowsResponse> responses = |
| 81 | + ImmutableList.of( |
| 82 | + ReadRowsResponse.newBuilder() |
| 83 | + .addChunks( |
| 84 | + CellChunk.newBuilder() |
| 85 | + .setRowKey(ByteString.copyFromUtf8("key")) |
| 86 | + .setFamilyName(StringValue.newBuilder().setValue("family")) |
| 87 | + .setQualifier( |
| 88 | + BytesValue.newBuilder().setValue(ByteString.copyFromUtf8("qualifier"))) |
| 89 | + .setTimestampMicros(1000) |
| 90 | + .setValue(ByteString.copyFromUtf8("value")) |
| 91 | + .setCommitRow(true)) |
| 92 | + .build(), |
| 93 | + ReadRowsResponse.newBuilder() |
| 94 | + .addChunks( |
| 95 | + CellChunk.newBuilder() |
| 96 | + .setRowKey(ByteString.copyFromUtf8("key2")) |
| 97 | + .setFamilyName(StringValue.newBuilder().setValue("family")) |
| 98 | + .setQualifier( |
| 99 | + BytesValue.newBuilder().setValue(ByteString.copyFromUtf8("qualifier"))) |
| 100 | + .setTimestampMicros(1000) |
| 101 | + .setValue(ByteString.copyFromUtf8("value")) |
| 102 | + .setCommitRow(true)) |
| 103 | + .build()); |
| 104 | + try (RowMergerUtil util = new RowMergerUtil()) { |
| 105 | + assertThat(util.parseReadRowsResponses(responses)) |
| 106 | + .containsExactly( |
| 107 | + Row.create( |
| 108 | + ByteString.copyFromUtf8("key"), |
| 109 | + ImmutableList.of( |
| 110 | + RowCell.create( |
| 111 | + "family", |
| 112 | + ByteString.copyFromUtf8("qualifier"), |
| 113 | + 1000, |
| 114 | + ImmutableList.of(), |
| 115 | + ByteString.copyFromUtf8("value")))), |
| 116 | + Row.create( |
| 117 | + ByteString.copyFromUtf8("key2"), |
| 118 | + ImmutableList.of( |
| 119 | + RowCell.create( |
| 120 | + "family", |
| 121 | + ByteString.copyFromUtf8("qualifier"), |
| 122 | + 1000, |
| 123 | + ImmutableList.of(), |
| 124 | + ByteString.copyFromUtf8("value"))))); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testPartial() { |
| 130 | + List<ReadRowsResponse> responses = |
| 131 | + ImmutableList.of( |
| 132 | + ReadRowsResponse.newBuilder() |
| 133 | + .addChunks( |
| 134 | + CellChunk.newBuilder() |
| 135 | + .setRowKey(ByteString.copyFromUtf8("key")) |
| 136 | + .setFamilyName(StringValue.newBuilder().setValue("family")) |
| 137 | + .setQualifier( |
| 138 | + BytesValue.newBuilder().setValue(ByteString.copyFromUtf8("qualifier"))) |
| 139 | + .setTimestampMicros(1000) |
| 140 | + .setValue(ByteString.copyFromUtf8("value")) |
| 141 | + .setCommitRow(false)) |
| 142 | + .build()); |
| 143 | + |
| 144 | + RowMergerUtil util = new RowMergerUtil(); |
| 145 | + util.parseReadRowsResponses(responses); |
| 146 | + Assert.assertThrows(IllegalStateException.class, util::close); |
| 147 | + } |
| 148 | +} |
0 commit comments