|
| 1 | +/* |
| 2 | + * Copyright 2016 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 | + * 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 com.google.cloud; |
| 17 | + |
| 18 | +import static com.google.common.truth.Truth.assertThat; |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.fail; |
| 21 | + |
| 22 | +import com.google.common.collect.ImmutableList; |
| 23 | +import com.google.common.collect.ImmutableMap; |
| 24 | +import com.google.protobuf.ListValue; |
| 25 | +import com.google.protobuf.NullValue; |
| 26 | +import com.google.protobuf.Struct; |
| 27 | +import com.google.protobuf.Value; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.Map; |
| 30 | +import org.junit.BeforeClass; |
| 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 StructsTest { |
| 37 | + |
| 38 | + private static final Double NUMBER = 42.0; |
| 39 | + private static final String STRING = "string"; |
| 40 | + private static final Boolean BOOLEAN = true; |
| 41 | + private static final ImmutableList<Object> LIST = |
| 42 | + ImmutableList.<Object>of(NUMBER, STRING, BOOLEAN); |
| 43 | + private static final Map<String, Object> INNER_MAP = new HashMap<>(); |
| 44 | + private static final Map<String, Object> MAP = new HashMap<>(); |
| 45 | + private static final Value NULL_VALUE = |
| 46 | + Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build(); |
| 47 | + private static final Value NUMBER_VALUE = Value.newBuilder().setNumberValue(NUMBER).build(); |
| 48 | + private static final Value STRING_VALUE = Value.newBuilder().setStringValue(STRING).build(); |
| 49 | + private static final Value BOOLEAN_VALUE = Value.newBuilder().setBoolValue(BOOLEAN).build(); |
| 50 | + private static final ListValue PROTO_LIST = |
| 51 | + ListValue.newBuilder() |
| 52 | + .addAllValues(ImmutableList.of(NUMBER_VALUE, STRING_VALUE, BOOLEAN_VALUE)) |
| 53 | + .build(); |
| 54 | + private static final Value LIST_VALUE = Value.newBuilder().setListValue(PROTO_LIST).build(); |
| 55 | + private static final Struct INNER_STRUCT = |
| 56 | + Struct.newBuilder() |
| 57 | + .putAllFields( |
| 58 | + ImmutableMap.of( |
| 59 | + "null", NULL_VALUE, |
| 60 | + "number", NUMBER_VALUE, |
| 61 | + "string", STRING_VALUE, |
| 62 | + "boolean", BOOLEAN_VALUE, |
| 63 | + "list", LIST_VALUE)) |
| 64 | + .build(); |
| 65 | + private static final Value STRUCT_VALUE = Value.newBuilder().setStructValue(INNER_STRUCT).build(); |
| 66 | + private static final ImmutableMap<String, Value> VALUE_MAP = |
| 67 | + ImmutableMap.<String, Value>builder() |
| 68 | + .put("null", NULL_VALUE) |
| 69 | + .put("number", NUMBER_VALUE) |
| 70 | + .put("string", STRING_VALUE) |
| 71 | + .put("boolean", BOOLEAN_VALUE) |
| 72 | + .put("list", LIST_VALUE) |
| 73 | + .put("struct", STRUCT_VALUE) |
| 74 | + .buildOrThrow(); |
| 75 | + private static final Struct STRUCT = Struct.newBuilder().putAllFields(VALUE_MAP).build(); |
| 76 | + private static final ImmutableMap<String, Object> EMPTY_MAP = ImmutableMap.of(); |
| 77 | + |
| 78 | + @BeforeClass |
| 79 | + public static void beforeClass() { |
| 80 | + INNER_MAP.put("null", null); |
| 81 | + INNER_MAP.put("number", NUMBER); |
| 82 | + INNER_MAP.put("string", STRING); |
| 83 | + INNER_MAP.put("boolean", BOOLEAN); |
| 84 | + INNER_MAP.put("list", LIST); |
| 85 | + MAP.put("null", null); |
| 86 | + MAP.put("number", NUMBER); |
| 87 | + MAP.put("string", STRING); |
| 88 | + MAP.put("boolean", BOOLEAN); |
| 89 | + MAP.put("list", LIST); |
| 90 | + MAP.put("struct", INNER_MAP); |
| 91 | + } |
| 92 | + |
| 93 | + private <T> void checkMapField(Map<String, T> map, String key, T expected) { |
| 94 | + assertThat(map).containsKey(key); |
| 95 | + assertThat(map).containsEntry(key, expected); |
| 96 | + } |
| 97 | + |
| 98 | + private void checkStructField(Struct struct, String key, Value expected) { |
| 99 | + Map<String, Value> map = struct.getFieldsMap(); |
| 100 | + checkMapField(map, key, expected); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testAsMap() { |
| 105 | + Map<String, Object> map = Structs.asMap(STRUCT); |
| 106 | + checkMapField(map, "null", null); |
| 107 | + checkMapField(map, "number", NUMBER); |
| 108 | + checkMapField(map, "string", STRING); |
| 109 | + checkMapField(map, "boolean", BOOLEAN); |
| 110 | + checkMapField(map, "list", LIST); |
| 111 | + checkMapField(map, "struct", INNER_MAP); |
| 112 | + assertEquals(MAP, map); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testAsMapPut() { |
| 117 | + Map<String, Object> map = Structs.asMap(STRUCT); |
| 118 | + try { |
| 119 | + map.put("key", "value"); |
| 120 | + fail(); |
| 121 | + } catch (UnsupportedOperationException expected) { |
| 122 | + |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testAsMapRemove() { |
| 128 | + Map<String, Object> map = Structs.asMap(STRUCT); |
| 129 | + try { |
| 130 | + map.remove("null"); |
| 131 | + fail(); |
| 132 | + } catch (UnsupportedOperationException expected) { |
| 133 | + |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testAsMapEmpty() { |
| 139 | + Map<String, Object> map = Structs.asMap(Struct.getDefaultInstance()); |
| 140 | + assertThat(map).isEmpty(); |
| 141 | + assertEquals(EMPTY_MAP, map); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testAsMapNull() { |
| 146 | + try { |
| 147 | + Structs.asMap(null); |
| 148 | + fail(); |
| 149 | + } catch (NullPointerException expected) { |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testNewStruct() { |
| 155 | + Struct struct = Structs.newStruct(MAP); |
| 156 | + checkStructField(struct, "null", NULL_VALUE); |
| 157 | + checkStructField(struct, "number", NUMBER_VALUE); |
| 158 | + checkStructField(struct, "string", STRING_VALUE); |
| 159 | + checkStructField(struct, "boolean", BOOLEAN_VALUE); |
| 160 | + checkStructField(struct, "list", LIST_VALUE); |
| 161 | + checkStructField(struct, "struct", STRUCT_VALUE); |
| 162 | + assertEquals(STRUCT, struct); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void testNewStructEmpty() { |
| 167 | + Struct struct = Structs.newStruct(EMPTY_MAP); |
| 168 | + assertThat(struct.getFieldsMap()).isEmpty(); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void testNewStructNull() { |
| 173 | + try { |
| 174 | + Structs.newStruct(null); |
| 175 | + fail(); |
| 176 | + } catch (NullPointerException expected) { |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void testNumbers() { |
| 182 | + int intNumber = Integer.MIN_VALUE; |
| 183 | + long longNumber = Long.MAX_VALUE; |
| 184 | + float floatNumber = Float.MIN_VALUE; |
| 185 | + double doubleNumber = Double.MAX_VALUE; |
| 186 | + ImmutableMap<String, Object> map = |
| 187 | + ImmutableMap.<String, Object>of( |
| 188 | + "int", intNumber, "long", longNumber, "float", floatNumber, "double", doubleNumber); |
| 189 | + Struct struct = Structs.newStruct(map); |
| 190 | + checkStructField(struct, "int", Value.newBuilder().setNumberValue(intNumber).build()); |
| 191 | + checkStructField( |
| 192 | + struct, "long", Value.newBuilder().setNumberValue((double) longNumber).build()); |
| 193 | + checkStructField(struct, "float", Value.newBuilder().setNumberValue(floatNumber).build()); |
| 194 | + checkStructField(struct, "double", Value.newBuilder().setNumberValue(doubleNumber).build()); |
| 195 | + Map<String, Object> convertedMap = Structs.asMap(struct); |
| 196 | + assertThat(convertedMap.get("int")).isInstanceOf(Double.class); |
| 197 | + assertThat(convertedMap.get("long")).isInstanceOf(Double.class); |
| 198 | + assertThat(convertedMap.get("float")).isInstanceOf(Double.class); |
| 199 | + assertThat(convertedMap.get("double")).isInstanceOf(Double.class); |
| 200 | + int convertedInteger = ((Double) convertedMap.get("int")).intValue(); |
| 201 | + long convertedLong = ((Double) convertedMap.get("long")).longValue(); |
| 202 | + float convertedFloat = ((Double) convertedMap.get("float")).floatValue(); |
| 203 | + double convertedDouble = (Double) convertedMap.get("double"); |
| 204 | + assertEquals(intNumber, convertedInteger); |
| 205 | + assertEquals(longNumber, convertedLong); |
| 206 | + assertEquals(floatNumber, convertedFloat, 0); |
| 207 | + assertEquals(doubleNumber, convertedDouble, 0); |
| 208 | + } |
| 209 | +} |
0 commit comments