Search in sources :

Example 1 with TestEntity

use of org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.TestEntity in project spring-cloud-gcp by spring-cloud.

the class ConverterAwareMappingSpannerEntityWriterTests method writeTest.

@Test
@SuppressWarnings("unchecked")
public void writeTest() {
    TestEntity t = new TestEntity();
    t.id = "key1";
    t.enumField = TestEntity.Color.WHITE;
    // any positive time value will do.
    t.commitTimestamp = Timestamp.ofTimeMicroseconds(1000);
    t.booleanField = true;
    t.intField = 123;
    t.longField = 3L;
    t.doubleField = 3.33;
    t.doubleArray = new double[] { 3.33, 3.33, 3.33 };
    t.doubleList = new ArrayList<>();
    t.doubleList.add(3.33);
    t.stringList = new ArrayList<>();
    t.stringList.add("stringstringstring");
    t.dateField = Date.fromYearMonthDay(2018, 11, 22);
    t.timestampField = Timestamp.ofTimeMicroseconds(333);
    t.bytes = ByteArray.copyFrom("333");
    t.booleanList = new ArrayList<>();
    t.booleanList.add(t.booleanField);
    t.longList = new ArrayList<>();
    t.longList.add(t.longField);
    t.dateList = new ArrayList<>();
    t.dateList.add(t.dateField);
    t.timestampList = new ArrayList<>();
    t.timestampList.add(t.timestampField);
    t.bytesList = new ArrayList<>();
    t.bytesList.add(t.bytes);
    // this property will be ignored in write mapping because it is a child relationship. no
    // exception will result even though it is an unsupported type for writing.
    t.childTestEntities = new ArrayList<>();
    t.childTestEntities.add(new ChildTestEntity());
    t.testEmbeddedColumns = new TestEmbeddedColumns();
    t.testEmbeddedColumns.id2 = "key2";
    t.testEmbeddedColumns.id3 = "key3";
    t.testEmbeddedColumns.intField2 = 123;
    Instant i1 = Instant.ofEpochSecond(111);
    Instant i2 = Instant.ofEpochSecond(222);
    Instant i3 = Instant.ofEpochSecond(333);
    t.momentsInTime = new ArrayList<>();
    t.momentsInTime.add(i1);
    t.momentsInTime.add(i2);
    t.momentsInTime.add(i3);
    Timestamp t1 = Timestamp.ofTimeSecondsAndNanos(111, 0);
    Timestamp t2 = Timestamp.ofTimeSecondsAndNanos(222, 0);
    Timestamp t3 = Timestamp.ofTimeSecondsAndNanos(333, 0);
    List<Timestamp> timestamps = new ArrayList<>();
    timestamps.add(t1);
    timestamps.add(t2);
    timestamps.add(t3);
    t.bigDecimalField = new BigDecimal("1000000.00001");
    t.bigDecimals = Arrays.asList(new BigDecimal("999999999.0025"), BigDecimal.ZERO);
    WriteBuilder writeBuilder = mock(WriteBuilder.class);
    ValueBinder<WriteBuilder> idBinder = mock(ValueBinder.class);
    when(idBinder.to(anyString())).thenReturn(null);
    when(writeBuilder.set(eq("id"))).thenReturn(idBinder);
    ValueBinder<WriteBuilder> id2Binder = mock(ValueBinder.class);
    when(id2Binder.to(anyString())).thenReturn(null);
    when(writeBuilder.set(eq("id2"))).thenReturn(id2Binder);
    ValueBinder<WriteBuilder> id3Binder = mock(ValueBinder.class);
    when(id3Binder.to(anyString())).thenReturn(null);
    when(writeBuilder.set(eq("id3"))).thenReturn(id3Binder);
    ValueBinder<WriteBuilder> id4Binder = mock(ValueBinder.class);
    when(id4Binder.to(anyString())).thenReturn(null);
    when(writeBuilder.set(eq("id4"))).thenReturn(id4Binder);
    ValueBinder<WriteBuilder> stringFieldBinder = mock(ValueBinder.class);
    when(stringFieldBinder.to(anyString())).thenReturn(null);
    when(writeBuilder.set(eq("custom_col"))).thenReturn(stringFieldBinder);
    ValueBinder<WriteBuilder> booleanFieldBinder = mock(ValueBinder.class);
    when(booleanFieldBinder.to((Boolean) any())).thenReturn(null);
    when(writeBuilder.set(eq("booleanField"))).thenReturn(booleanFieldBinder);
    ValueBinder<WriteBuilder> intFieldBinder = mock(ValueBinder.class);
    when(intFieldBinder.to(anyLong())).thenReturn(null);
    when(writeBuilder.set(eq("intField"))).thenReturn(intFieldBinder);
    ValueBinder<WriteBuilder> intField2Binder = mock(ValueBinder.class);
    when(intField2Binder.to(anyLong())).thenReturn(null);
    when(writeBuilder.set(eq("intField2"))).thenReturn(intField2Binder);
    ValueBinder<WriteBuilder> longFieldBinder = mock(ValueBinder.class);
    when(longFieldBinder.to(anyString())).thenReturn(null);
    when(writeBuilder.set(eq("longField"))).thenReturn(longFieldBinder);
    ValueBinder<WriteBuilder> doubleFieldBinder = mock(ValueBinder.class);
    when(doubleFieldBinder.to(anyDouble())).thenReturn(null);
    when(writeBuilder.set(eq("doubleField"))).thenReturn(doubleFieldBinder);
    ValueBinder<WriteBuilder> doubleArrayFieldBinder = mock(ValueBinder.class);
    when(doubleArrayFieldBinder.toStringArray(any())).thenReturn(null);
    when(writeBuilder.set(eq("doubleArray"))).thenReturn(doubleArrayFieldBinder);
    ValueBinder<WriteBuilder> doubleListFieldBinder = mock(ValueBinder.class);
    when(doubleListFieldBinder.toFloat64Array((Iterable<Double>) any())).thenReturn(null);
    when(writeBuilder.set(eq("doubleList"))).thenReturn(doubleListFieldBinder);
    ValueBinder<WriteBuilder> stringListFieldBinder = mock(ValueBinder.class);
    when(stringListFieldBinder.toStringArray(any())).thenReturn(null);
    when(writeBuilder.set(eq("stringList"))).thenReturn(stringListFieldBinder);
    ValueBinder<WriteBuilder> booleanListFieldBinder = mock(ValueBinder.class);
    when(booleanListFieldBinder.toBoolArray((Iterable<Boolean>) any())).thenReturn(null);
    when(writeBuilder.set(eq("booleanList"))).thenReturn(booleanListFieldBinder);
    ValueBinder<WriteBuilder> longListFieldBinder = mock(ValueBinder.class);
    when(longListFieldBinder.toStringArray(any())).thenReturn(null);
    when(writeBuilder.set(eq("longList"))).thenReturn(longListFieldBinder);
    ValueBinder<WriteBuilder> timestampListFieldBinder = mock(ValueBinder.class);
    when(timestampListFieldBinder.toTimestampArray(any())).thenReturn(null);
    when(writeBuilder.set(eq("timestampList"))).thenReturn(timestampListFieldBinder);
    ValueBinder<WriteBuilder> dateListFieldBinder = mock(ValueBinder.class);
    when(dateListFieldBinder.toDateArray(any())).thenReturn(null);
    when(writeBuilder.set(eq("dateList"))).thenReturn(dateListFieldBinder);
    ValueBinder<WriteBuilder> instantListFieldBinder = mock(ValueBinder.class);
    when(instantListFieldBinder.toTimestampArray(any())).thenReturn(null);
    when(writeBuilder.set(eq("momentsInTime"))).thenReturn(instantListFieldBinder);
    ValueBinder<WriteBuilder> bytesListFieldBinder = mock(ValueBinder.class);
    when(bytesListFieldBinder.toDateArray(any())).thenReturn(null);
    when(writeBuilder.set(eq("bytesList"))).thenReturn(bytesListFieldBinder);
    ValueBinder<WriteBuilder> dateFieldBinder = mock(ValueBinder.class);
    when(dateFieldBinder.to((Date) any())).thenReturn(null);
    when(writeBuilder.set(eq("dateField"))).thenReturn(dateFieldBinder);
    ValueBinder<WriteBuilder> timestampFieldBinder = mock(ValueBinder.class);
    when(timestampFieldBinder.to((Timestamp) any())).thenReturn(null);
    when(writeBuilder.set(eq("timestampField"))).thenReturn(timestampFieldBinder);
    ValueBinder<WriteBuilder> bytesFieldBinder = mock(ValueBinder.class);
    when(bytesFieldBinder.to((ByteArray) any())).thenReturn(null);
    when(writeBuilder.set(eq("bytes"))).thenReturn(bytesFieldBinder);
    ValueBinder<WriteBuilder> commitTimestampBinder = mock(ValueBinder.class);
    when(commitTimestampBinder.to((Timestamp) any())).thenReturn(null);
    when(writeBuilder.set(eq("commitTimestamp"))).thenReturn(commitTimestampBinder);
    ValueBinder<WriteBuilder> bigDecimalFieldBinder = mock(ValueBinder.class);
    when(bigDecimalFieldBinder.to((BigDecimal) any())).thenReturn(null);
    when(writeBuilder.set(eq("bigDecimalField"))).thenReturn(bigDecimalFieldBinder);
    ValueBinder<WriteBuilder> bigDecimalsBinder = mock(ValueBinder.class);
    when(bigDecimalsBinder.toNumericArray(any())).thenReturn(null);
    when(writeBuilder.set(eq("bigDecimals"))).thenReturn(bigDecimalsBinder);
    this.spannerEntityWriter.write(t, writeBuilder::set);
    verify(idBinder, times(1)).to(eq(t.id));
    verify(id2Binder, times(1)).to(eq(t.testEmbeddedColumns.id2));
    verify(id3Binder, times(1)).to(eq(t.testEmbeddedColumns.id3));
    verify(stringFieldBinder, times(1)).to(eq(t.enumField.toString()));
    verify(booleanFieldBinder, times(1)).to(eq(Boolean.valueOf(t.booleanField)));
    verify(intFieldBinder, times(1)).to(eq(Long.valueOf(t.intField)));
    verify(intField2Binder, times(1)).to(eq(Long.valueOf(t.testEmbeddedColumns.intField2)));
    verify(longFieldBinder, times(1)).to(eq(String.valueOf(t.longField)));
    verify(doubleFieldBinder, times(1)).to(eq(Double.valueOf(t.doubleField)));
    verify(doubleArrayFieldBinder, times(1)).to("3.33,3.33,3.33");
    verify(doubleListFieldBinder, times(1)).toFloat64Array(eq(t.doubleList));
    verify(stringListFieldBinder, times(1)).toStringArray(eq(t.stringList));
    verify(booleanListFieldBinder, times(1)).toBoolArray(eq(t.booleanList));
    verify(longListFieldBinder, times(1)).toStringArray(any());
    verify(timestampListFieldBinder, times(1)).toTimestampArray(eq(t.timestampList));
    verify(dateListFieldBinder, times(1)).toDateArray(eq(t.dateList));
    verify(bytesListFieldBinder, times(1)).toBytesArray(eq(t.bytesList));
    verify(dateFieldBinder, times(1)).to(eq(t.dateField));
    verify(timestampFieldBinder, times(1)).to(eq(t.timestampField));
    verify(bytesFieldBinder, times(1)).to(eq(t.bytes));
    verify(instantListFieldBinder, times(1)).toTimestampArray(eq(timestamps));
    // the positive value set earlier must not be passed to Spanner. it must be replaced by
    // the dummy value.
    verify(commitTimestampBinder, times(1)).to(eq(Value.COMMIT_TIMESTAMP));
    verify(bigDecimalFieldBinder, times(1)).to(eq(t.bigDecimalField));
    verify(bigDecimalsBinder, times(1)).toNumericArray(eq(t.bigDecimals));
}
Also used : FaultyTestEntity(org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.FaultyTestEntity) ChildTestEntity(org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.ChildTestEntity) TestEntity(org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.TestEntity) Instant(java.time.Instant) ArrayList(java.util.ArrayList) Timestamp(com.google.cloud.Timestamp) ArgumentMatchers.anyDouble(org.mockito.ArgumentMatchers.anyDouble) BigDecimal(java.math.BigDecimal) ChildTestEntity(org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.ChildTestEntity) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) TestEmbeddedColumns(org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.TestEmbeddedColumns) Test(org.junit.Test)

Example 2 with TestEntity

use of org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.TestEntity in project spring-cloud-gcp by spring-cloud.

the class ConverterAwareMappingSpannerEntityWriterTests method writeIncompatibleTypeTest.

@Test
public void writeIncompatibleTypeTest() {
    this.expectedEx.expect(SpannerDataException.class);
    this.expectedEx.expectMessage("Unsupported mapping for type: " + "class org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities$TestEntity");
    FaultyTestEntity ft = new FaultyTestEntity();
    ft.fieldWithUnsupportedType = new TestEntity();
    WriteBuilder writeBuilder = Mutation.newInsertBuilder("faulty_test_table");
    this.spannerEntityWriter.write(ft, writeBuilder::set);
}
Also used : FaultyTestEntity(org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.FaultyTestEntity) ChildTestEntity(org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.ChildTestEntity) TestEntity(org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.TestEntity) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) FaultyTestEntity(org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.FaultyTestEntity) Test(org.junit.Test)

Example 3 with TestEntity

use of org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.TestEntity in project spring-cloud-gcp by spring-cloud.

the class ConverterAwareMappingSpannerEntityProcessorTests method mapToListTest.

@Test
public void mapToListTest() {
    List<Double> doubleList = new ArrayList<>();
    doubleList.add(3.33);
    List<String> stringList = new ArrayList<>();
    stringList.add("string");
    Instant i1 = Instant.ofEpochSecond(111);
    Instant i2 = Instant.ofEpochSecond(222);
    Instant i3 = Instant.ofEpochSecond(333);
    List<Instant> instants = new ArrayList<>();
    instants.add(i1);
    instants.add(i2);
    instants.add(i3);
    Timestamp ts1 = Timestamp.ofTimeSecondsAndNanos(111, 0);
    Timestamp ts2 = Timestamp.ofTimeSecondsAndNanos(222, 0);
    Timestamp ts3 = Timestamp.ofTimeSecondsAndNanos(333, 0);
    List<Timestamp> timestamps = new ArrayList<>();
    timestamps.add(ts1);
    timestamps.add(ts2);
    timestamps.add(ts3);
    Struct struct1 = Struct.newBuilder().set("id").to(Value.string("key1")).set("id2").to(Value.string("key2")).set("id3").to(Value.string("key3")).set("id4").to(Value.string("key4")).set("custom_col").to(Value.string("WHITE")).set("booleanField").to(Value.bool(true)).set("intField").to(Value.int64(123L)).set("intField2").to(Value.int64(333L)).set("longField").to(Value.int64(3L)).set("doubleField").to(Value.float64(3.33)).set("doubleArray").to(Value.float64Array(new double[] { 3.33, 3.33, 3.33 })).set("doubleList").to(Value.float64Array(doubleList)).set("stringList").to(Value.stringArray(stringList)).set("booleanList").to(Value.boolArray(new boolean[] {})).set("longList").to(Value.int64Array(new long[] {})).set("timestampList").to(Value.timestampArray(new ArrayList<>())).set("dateList").to(Value.dateArray(new ArrayList<>())).set("bytesList").to(Value.bytesArray(new ArrayList<>())).set("dateField").to(Value.date(Date.fromYearMonthDay(2018, 11, 22))).set("timestampField").to(Value.timestamp(Timestamp.ofTimeMicroseconds(333))).set("bytes").to(Value.bytes(ByteArray.copyFrom("string1"))).set("momentsInTime").to(Value.timestampArray(timestamps)).set("commitTimestamp").to(Value.timestamp(Timestamp.ofTimeMicroseconds(1))).set("bigDecimalField").to(Value.numeric(BigDecimal.TEN)).set("bigDecimals").to(Value.numericArray(Arrays.asList(BigDecimal.ONE, BigDecimal.ZERO))).build();
    Struct struct2 = Struct.newBuilder().set("id").to(Value.string("key12")).set("id2").to(Value.string("key22")).set("id3").to(Value.string("key32")).set("id4").to(Value.string("key42")).set("custom_col").to(Value.string("BLACK")).set("booleanField").to(Value.bool(true)).set("intField").to(Value.int64(222L)).set("intField2").to(Value.int64(555L)).set("longField").to(Value.int64(5L)).set("doubleField").to(Value.float64(5.55)).set("doubleArray").to(Value.float64Array(new double[] { 5.55, 5.55 })).set("doubleList").to(Value.float64Array(doubleList)).set("stringList").to(Value.stringArray(stringList)).set("booleanList").to(Value.boolArray(new boolean[] {})).set("longList").to(Value.int64Array(new long[] {})).set("timestampList").to(Value.timestampArray(new ArrayList<>())).set("dateList").to(Value.dateArray(new ArrayList<>())).set("bytesList").to(Value.bytesArray(new ArrayList<>())).set("dateField").to(Value.date(Date.fromYearMonthDay(2019, 11, 22))).set("timestampField").to(Value.timestamp(Timestamp.ofTimeMicroseconds(555))).set("momentsInTime").to(Value.timestampArray(timestamps)).set("bytes").to(Value.bytes(ByteArray.copyFrom("string2"))).set("commitTimestamp").to(Value.timestamp(Timestamp.ofTimeMicroseconds(1))).set("bigDecimalField").to(Value.numeric(new BigDecimal("0.0001"))).set("bigDecimals").to(Value.numericArray(Arrays.asList(new BigDecimal("-0.999"), new BigDecimal("10.9001")))).build();
    MockResults mockResults = new MockResults();
    mockResults.structs = Arrays.asList(struct1, struct2);
    ResultSet results = mock(ResultSet.class);
    when(results.next()).thenAnswer((invocation) -> mockResults.next());
    when(results.getCurrentRowAsStruct()).thenAnswer((invocation) -> mockResults.getCurrent());
    List<TestEntity> entities = this.spannerEntityProcessor.mapToList(results, TestEntity.class);
    verify(results, times(1)).close();
    assertThat(entities).hasSize(2);
    TestEntity t1 = entities.get(0);
    TestEntity t2 = entities.get(1);
    assertThat(t1.id).isEqualTo("key1");
    assertThat(t1.testEmbeddedColumns.id2).isEqualTo("key2");
    assertThat(t1.testEmbeddedColumns.id3).isEqualTo("key3");
    assertThat(t1.id4).isEqualTo("key4");
    assertThat(t1.enumField).isEqualTo(TestEntity.Color.WHITE);
    assertThat(t1.booleanField).isEqualTo(true);
    assertThat(t1.intField).isEqualTo(123);
    assertThat(t1.testEmbeddedColumns.intField2).isEqualTo(333);
    assertThat(t1.longField).isEqualTo(3L);
    assertThat(t1.doubleField).isEqualTo(3.33, DELTA);
    assertThat(t1.doubleArray.length).isEqualTo(3);
    assertThat(t1.dateField.getYear()).isEqualTo(2018);
    assertThat(t1.momentsInTime).isEqualTo(instants);
    assertThat(t1.bytes).isEqualTo(ByteArray.copyFrom("string1"));
    assertThat(t1.commitTimestamp).isEqualTo(Timestamp.ofTimeMicroseconds(1));
    assertThat(t1.bigDecimalField).isEqualTo(BigDecimal.TEN);
    assertThat(t1.bigDecimals).containsExactly(BigDecimal.ONE, BigDecimal.ZERO);
    assertThat(t2.id).isEqualTo("key12");
    assertThat(t2.testEmbeddedColumns.id2).isEqualTo("key22");
    assertThat(t2.testEmbeddedColumns.id3).isEqualTo("key32");
    assertThat(t2.id4).isEqualTo("key42");
    assertThat(t2.enumField).isEqualTo(TestEntity.Color.BLACK);
    assertThat(t2.booleanField).isEqualTo(true);
    assertThat(t2.intField).isEqualTo(222);
    assertThat(t2.testEmbeddedColumns.intField2).isEqualTo(555);
    assertThat(t2.longField).isEqualTo(5L);
    assertThat(t2.doubleField).isEqualTo(5.55, DELTA);
    assertThat(t2.doubleArray.length).isEqualTo(2);
    assertThat(t2.dateField.getYear()).isEqualTo(2019);
    assertThat(t2.doubleList).hasSize(1);
    assertThat(t2.doubleList.get(0)).isEqualTo(3.33, DELTA);
    assertThat(t2.momentsInTime).isEqualTo(instants);
    assertThat(t2.stringList).containsExactly("string");
    assertThat(t2.bytes).isEqualTo(ByteArray.copyFrom("string2"));
    assertThat(t2.commitTimestamp).isEqualTo(Timestamp.ofTimeMicroseconds(1));
    assertThat(t2.bigDecimalField).isEqualTo(new BigDecimal("0.0001"));
    assertThat(t2.bigDecimals).containsExactly(new BigDecimal("-0.999"), new BigDecimal("10.9001"));
}
Also used : TestEntity(org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.TestEntity) Instant(java.time.Instant) ArrayList(java.util.ArrayList) Timestamp(com.google.cloud.Timestamp) BigDecimal(java.math.BigDecimal) Struct(com.google.cloud.spanner.Struct) ResultSet(com.google.cloud.spanner.ResultSet) Test(org.junit.Test)

Example 4 with TestEntity

use of org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.TestEntity in project spring-cloud-gcp by spring-cloud.

the class ConverterAwareMappingSpannerEntityProcessorTests method mapToListPartialColumnsTest.

@Test
public void mapToListPartialColumnsTest() {
    List<Double> doubleList = new ArrayList<>();
    doubleList.add(3.33);
    List<String> stringList = new ArrayList<>();
    stringList.add("string");
    Struct struct1 = Struct.newBuilder().set("id").to(Value.string("key1")).set("custom_col").to(Value.string("WHITE")).set("doubleList").to(Value.float64Array(doubleList)).set("stringList").to(Value.stringArray(stringList)).build();
    Struct struct2 = Struct.newBuilder().set("id").to(Value.string("key2")).set("custom_col").to(Value.string("BLACK")).set("doubleList").to(Value.float64Array(doubleList)).set("stringList").to(Value.stringArray(stringList)).build();
    MockResults mockResults = new MockResults();
    mockResults.structs = Arrays.asList(struct1, struct2);
    ResultSet results = mock(ResultSet.class);
    when(results.next()).thenAnswer((invocation) -> mockResults.next());
    when(results.getCurrentRowAsStruct()).thenAnswer((invocation) -> mockResults.getCurrent());
    List<TestEntity> entities = this.spannerEntityProcessor.mapToList(results, TestEntity.class, "id", "custom_col");
    verify(results, times(1)).close();
    assertThat(entities).hasSize(2);
    TestEntity t1 = entities.get(0);
    TestEntity t2 = entities.get(1);
    assertThat(t1.id).isEqualTo("key1");
    assertThat(t1.enumField).isEqualTo(TestEntity.Color.WHITE);
    // This should not have been set
    assertThat(t1.doubleList).isNull();
    assertThat(t2.id).isEqualTo("key2");
    assertThat(t2.enumField).isEqualTo(TestEntity.Color.BLACK);
    // This should not have been set
    assertThat(t2.stringList).isNull();
}
Also used : TestEntity(org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.TestEntity) ArrayList(java.util.ArrayList) ResultSet(com.google.cloud.spanner.ResultSet) Struct(com.google.cloud.spanner.Struct) Test(org.junit.Test)

Example 5 with TestEntity

use of org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.TestEntity in project spring-cloud-gcp by spring-cloud.

the class ConverterAwareMappingSpannerEntityWriterTests method writeSomeColumnsTest.

@Test
@SuppressWarnings("unchecked")
public void writeSomeColumnsTest() {
    TestEntity t = new TestEntity();
    t.id = "key1";
    t.enumField = TestEntity.Color.BLACK;
    WriteBuilder writeBuilder = mock(WriteBuilder.class);
    ValueBinder<WriteBuilder> idBinder = mock(ValueBinder.class);
    when(idBinder.to(anyString())).thenReturn(null);
    when(writeBuilder.set(eq("id"))).thenReturn(idBinder);
    ValueBinder<WriteBuilder> stringFieldBinder = mock(ValueBinder.class);
    when(stringFieldBinder.to(anyString())).thenReturn(null);
    when(writeBuilder.set(eq("custom_col"))).thenReturn(stringFieldBinder);
    ValueBinder<WriteBuilder> booleanFieldBinder = mock(ValueBinder.class);
    when(booleanFieldBinder.to((Boolean) any())).thenReturn(null);
    when(writeBuilder.set(eq("booleanField"))).thenReturn(booleanFieldBinder);
    this.spannerEntityWriter.write(t, writeBuilder::set, new HashSet<>(Arrays.asList("id", "custom_col")));
    verify(idBinder, times(1)).to(eq(t.id));
    verify(stringFieldBinder, times(1)).to(eq(t.enumField.toString()));
    verifyNoInteractions(booleanFieldBinder);
}
Also used : FaultyTestEntity(org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.FaultyTestEntity) ChildTestEntity(org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.ChildTestEntity) TestEntity(org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.TestEntity) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)6 TestEntity (org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.TestEntity)6 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)4 ChildTestEntity (org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.ChildTestEntity)4 FaultyTestEntity (org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.FaultyTestEntity)4 ArrayList (java.util.ArrayList)3 Timestamp (com.google.cloud.Timestamp)2 ResultSet (com.google.cloud.spanner.ResultSet)2 Struct (com.google.cloud.spanner.Struct)2 BigDecimal (java.math.BigDecimal)2 Instant (java.time.Instant)2 ArgumentMatchers.anyDouble (org.mockito.ArgumentMatchers.anyDouble)2 HashSet (java.util.HashSet)1 TestEmbeddedColumns (org.springframework.cloud.gcp.data.spanner.core.convert.TestEntities.TestEmbeddedColumns)1