Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeWang1127 committed Feb 28, 2025
1 parent 2d00d11 commit 08506d3
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public ReadWriteConversions datastoreReadWriteConversions(
@Bean
@ConditionalOnMissingBean
public DatastoreMappingContext datastoreMappingContext(GcpDatastoreProperties gcpDatastoreProperties) {
return new DatastoreMappingContext(gcpDatastoreProperties.isNullValueIgnored());
return new DatastoreMappingContext(gcpDatastoreProperties.isSkipNullValue());
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class GcpDatastoreProperties implements CredentialsSupplier {
private String namespace;

/** Whether skip the insertion if the value is null */
private boolean isNullValueIgnored;
private boolean skipNullValue;

@Override
public Credentials getCredentials() {
Expand Down Expand Up @@ -90,11 +90,11 @@ public void setHost(String host) {
this.host = host;
}

public boolean isNullValueIgnored() {
return isNullValueIgnored;
public boolean isSkipNullValue() {
return skipNullValue;
}

public void setNullValueIgnored(boolean nullValueIgnored) {
isNullValueIgnored = nullValueIgnored;
public void setSkipNullValue(boolean skipNullValue) {
this.skipNullValue = skipNullValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void write(Object source, @NonNull BaseEntity.Builder sink) {
if (persistentProperty.isUnindexed()) {
convertedVal = setExcludeFromIndexes(convertedVal);
}
if (!(persistentProperty.isNullValueIgnored() && convertedVal.getType().equals(NULL))) {
if (!(persistentProperty.isSkipNullValue() && convertedVal.getType().equals(NULL))) {
sink.set(persistentProperty.getFieldName(), convertedVal);
}
} catch (DatastoreDataException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ public class DatastoreMappingContext
// Kind and that are subclasses of the given class.
private static final Map<Class, Set<Class>> discriminationFamilies = new ConcurrentHashMap<>();

private final boolean isSkipEmptyValue;
private final boolean skipNullValue;

public DatastoreMappingContext() {
this(false);
}

public DatastoreMappingContext(boolean isSkipEmptyValue) {
public DatastoreMappingContext(boolean skipNullValue) {
this.setSimpleTypeHolder(DatastoreNativeTypes.HOLDER);
this.isSkipEmptyValue = isSkipEmptyValue;
this.skipNullValue = skipNullValue;
}

@Override
Expand Down Expand Up @@ -125,7 +125,7 @@ protected <T> DatastorePersistentEntity<?> createPersistentEntity(
protected DatastorePersistentProperty createPersistentProperty(
Property property, DatastorePersistentEntity<?> owner, SimpleTypeHolder simpleTypeHolder) {
return new DatastorePersistentPropertyImpl(
property, owner, simpleTypeHolder, FIELD_NAMING_STRATEGY, false);
property, owner, simpleTypeHolder, FIELD_NAMING_STRATEGY, skipNullValue);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public interface DatastorePersistentProperty
boolean isLazyLoaded();

/**
* Return whether to ignore null value, i.e., skip insertion if value is null.
* Return whether to skip null value, i.e., skip insertion if value is null.
*
* @return {@code true} if the null value is ignored. {@code false} otherwise.
*/
boolean isNullValueIgnored();
boolean isSkipNullValue();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class DatastorePersistentPropertyImpl

private final FieldNamingStrategy fieldNamingStrategy;

private final boolean isSkipEmptyValue;
private final boolean isSkipNullValue;

/**
* Constructor.
Expand All @@ -53,13 +53,13 @@ public class DatastorePersistentPropertyImpl
PersistentEntity<?, DatastorePersistentProperty> owner,
SimpleTypeHolder simpleTypeHolder,
FieldNamingStrategy fieldNamingStrategy,
boolean isSkipEmptyValue) {
boolean isSkipNullValue) {
super(property, owner, simpleTypeHolder);
this.fieldNamingStrategy =
(fieldNamingStrategy != null)
? fieldNamingStrategy
: PropertyNameFieldNamingStrategy.INSTANCE;
this.isSkipEmptyValue = isSkipEmptyValue;
this.isSkipNullValue = isSkipNullValue;
verify();
}

Expand Down Expand Up @@ -137,7 +137,7 @@ public boolean isLazyLoaded() {
}

@Override
public boolean isNullValueIgnored() {
return isSkipEmptyValue;
public boolean isSkipNullValue() {
return isSkipNullValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.cloud.datastore.BaseEntity;
import com.google.cloud.datastore.Blob;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreException;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.EntityValue;
import com.google.cloud.datastore.FullEntity;
Expand Down Expand Up @@ -353,6 +354,43 @@ void writeNullTest() {
assertThat(baseEntity).as("validate null field").isEqualTo(new NullValue());
}

@Test
void testWriteEmptyValueSkipped() {
DatastoreMappingContext context = new DatastoreMappingContext(true);
DatastoreEntityConverter skipEmptyValueConverter =
new DefaultDatastoreEntityConverter(
new DatastoreMappingContext(true),
new TwoStepsConversions(
new DatastoreCustomConversions(
Collections.singletonList(
new Converter<HashMap, String>() {
@Nullable
@Override
public String convert(HashMap source) {
return "Map was converted to String";
}
})),
null,
context));
byte[] bytes = {1, 2, 3};
TestDatastoreItem item = new TestDatastoreItem();
item.setStringField(null);
item.setBoolField(true);
item.setDoubleField(3.1415D);
item.setLongField(123L);
item.setLatLngField(LatLng.of(10, 20));
item.setTimestampField(Timestamp.ofTimeSecondsAndNanos(30, 40));
item.setBlobField(Blob.copyFrom(bytes));

Entity.Builder builder = getEntityBuilder();
skipEmptyValueConverter.write(item, builder);

Entity entity = builder.build();
assertThatThrownBy(() -> entity.getValue("stringField"))
.isInstanceOf(DatastoreException.class)
.hasMessage("No such property stringField");
}

@Test
void testUnsupportedTypeWriteException() {

Expand Down

0 comments on commit 08506d3

Please sign in to comment.