|
| 1 | +/* |
| 2 | + * Copyright 2019 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.admin.v2.models; |
| 17 | + |
| 18 | +import static com.google.cloud.bigtable.admin.v2.models.GCRules.GCRULES; |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import com.google.bigtable.admin.v2.ColumnFamily; |
| 22 | +import com.google.bigtable.admin.v2.GcRule; |
| 23 | +import com.google.bigtable.admin.v2.Table; |
| 24 | +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; |
| 25 | +import com.google.protobuf.ByteString; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | +import org.junit.Test; |
| 28 | +import org.junit.runner.RunWith; |
| 29 | +import org.junit.runners.JUnit4; |
| 30 | +import org.threeten.bp.Duration; |
| 31 | + |
| 32 | +@RunWith(JUnit4.class) |
| 33 | +public class CreateTableRequestTest { |
| 34 | + |
| 35 | + private static final String TABLE_ID = "my-table"; |
| 36 | + private static final String PROJECT_ID = "my-project"; |
| 37 | + private static final String INSTANCE_ID = "my-instance"; |
| 38 | + private static final ByteString splitKey = ByteString.copyFromUtf8("first-split"); |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testToProto() { |
| 42 | + ByteString secondSplitKey = ByteString.copyFromUtf8("second-split"); |
| 43 | + |
| 44 | + CreateTableRequest request = |
| 45 | + CreateTableRequest.of(TABLE_ID) |
| 46 | + .addFamily("family-id") |
| 47 | + .addFamily("another-family", GCRULES.maxAge(100, TimeUnit.HOURS)) |
| 48 | + .addSplit(splitKey) |
| 49 | + .addSplit(secondSplitKey); |
| 50 | + |
| 51 | + com.google.bigtable.admin.v2.CreateTableRequest requestProto = |
| 52 | + com.google.bigtable.admin.v2.CreateTableRequest.newBuilder() |
| 53 | + .setTableId(TABLE_ID) |
| 54 | + .setTable( |
| 55 | + Table.newBuilder() |
| 56 | + .putColumnFamilies("family-id", ColumnFamily.getDefaultInstance()) |
| 57 | + .putColumnFamilies( |
| 58 | + "another-family", |
| 59 | + ColumnFamily.newBuilder() |
| 60 | + .setGcRule( |
| 61 | + GcRule.newBuilder() |
| 62 | + .setMaxAge( |
| 63 | + com.google.protobuf.Duration.newBuilder() |
| 64 | + .setSeconds(100 * 60 * 60)) |
| 65 | + .build()) |
| 66 | + .build())) |
| 67 | + .setParent(NameUtil.formatInstanceName(PROJECT_ID, INSTANCE_ID)) |
| 68 | + .addInitialSplits( |
| 69 | + com.google.bigtable.admin.v2.CreateTableRequest.Split.newBuilder().setKey(splitKey)) |
| 70 | + .addInitialSplits( |
| 71 | + com.google.bigtable.admin.v2.CreateTableRequest.Split.newBuilder() |
| 72 | + .setKey(secondSplitKey)) |
| 73 | + .build(); |
| 74 | + assertThat(request.toProto(PROJECT_ID, INSTANCE_ID)).isEqualTo(requestProto); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testEquality() { |
| 79 | + CreateTableRequest request = |
| 80 | + CreateTableRequest.of(TABLE_ID) |
| 81 | + .addFamily("family-id") |
| 82 | + .addFamily("another-family", GCRULES.maxAge(100, TimeUnit.HOURS)) |
| 83 | + .addSplit(splitKey); |
| 84 | + |
| 85 | + assertThat(request) |
| 86 | + .isEqualTo( |
| 87 | + CreateTableRequest.of(TABLE_ID) |
| 88 | + .addFamily("family-id") |
| 89 | + .addFamily("another-family", GCRULES.maxAge(Duration.ofHours(100))) |
| 90 | + .addSplit(splitKey)); |
| 91 | + |
| 92 | + assertThat(request) |
| 93 | + .isNotEqualTo( |
| 94 | + CreateTableRequest.of(TABLE_ID) |
| 95 | + .addFamily("family-id") |
| 96 | + .addFamily("another-family") |
| 97 | + .addSplit(splitKey)); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testHashCode() { |
| 102 | + CreateTableRequest request = |
| 103 | + CreateTableRequest.of(TABLE_ID).addFamily("family-id").addSplit(splitKey); |
| 104 | + |
| 105 | + assertThat(request.hashCode()) |
| 106 | + .isEqualTo( |
| 107 | + CreateTableRequest.of(TABLE_ID).addFamily("family-id").addSplit(splitKey).hashCode()); |
| 108 | + |
| 109 | + assertThat(request.hashCode()) |
| 110 | + .isNotEqualTo(CreateTableRequest.of(TABLE_ID).addFamily("other-family").hashCode()); |
| 111 | + } |
| 112 | +} |
0 commit comments