Skip to content

Commit

Permalink
CB-16465 Lazy load safe toString() methods
Browse files Browse the repository at this point in the history
Increment reflections version, as DomainToStringTest was affected by this bug: ronmamo/reflections#273
  • Loading branch information
Bajzathd committed Mar 23, 2022
1 parent 649cc18 commit e380a95
Show file tree
Hide file tree
Showing 60 changed files with 769 additions and 21 deletions.
2 changes: 1 addition & 1 deletion common/build.gradle
Expand Up @@ -40,7 +40,7 @@ dependencies {
implementation group: 'net.sf.json-lib', name: 'json-lib', version: '2.4', classifier: 'jdk15'
api group: 'net.jcip', name: 'jcip-annotations', version: '1.0'
api group: 'com.github.spotbugs', name: 'spotbugs-annotations', version: '4.4.2'
api group: 'org.reflections', name: 'reflections', version: '0.9.11'
api group: 'org.reflections', name: 'reflections', version: reflectionsVersion

implementation group: 'com.github.ben-manes.caffeine', name: 'caffeine', version: caffeineVersion
implementation group: 'org.glassfish.jersey.core', name: 'jersey-client', version: jerseyCoreVersion
Expand Down
Expand Up @@ -38,6 +38,10 @@ public class AnonymizerUtil {
private AnonymizerUtil() {
}

public static String anonymize(Object object) {
return object == null ? null : anonymize(object.toString());
}

public static String anonymize(String content) {
String ret = content;
if (ret != null) {
Expand Down
Expand Up @@ -3,13 +3,17 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.function.Supplier;

import org.hibernate.Hibernate;
import org.postgresql.Driver;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

public class DatabaseUtil {
public static final String DEFAULT_SCHEMA_NAME = "public";

public static final String UNINITIALIZED_TO_STRING = "<uninitialized>";

private DatabaseUtil() {
}

Expand All @@ -24,4 +28,22 @@ public static void createSchemaIfNeeded(String dbType, String dbAddress, String
}
}
}

public static boolean isLazyLoadInitialized(Object o) {
return Hibernate.isInitialized(o);
}

public static String lazyLoadSafeToString(Object o) {
return lazyLoadSafeToString(o, () -> o);
}

public static String lazyLoadSafeToString(Object o, Supplier<Object> toStringSupplier) {
if (o == null) {
return null;
} else if (isLazyLoadInitialized(o)) {
return String.valueOf(toStringSupplier.get());
} else {
return UNINITIALIZED_TO_STRING;
}
}
}
@@ -0,0 +1,106 @@
package com.sequenceiq.cloudbreak.util;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
import org.junit.jupiter.api.Test;

class DatabaseUtilTest {

private static final String INITIALIZED_TO_STRING = "initialized";

private static final String SUPPLIED_TO_STRING = "supplied toString()";

private static final String NOT_PROXY_OBJECT = "not proxy object";

@Test
void isLazyLoadInitializedFalseWhenProxyIsUninitialized() {
Object proxyObject = createProxyObject(false);

boolean result = DatabaseUtil.isLazyLoadInitialized(proxyObject);

assertThat(result).isFalse();
}

@Test
void isLazyLoadInitializedTrueWhenProxyIsInitialized() {
Object proxyObject = createProxyObject(true);

boolean result = DatabaseUtil.isLazyLoadInitialized(proxyObject);

assertThat(result).isTrue();
}

@Test
void isLazyLoadInitializedTrueWhenObjectIsNotProxy() {
boolean result = DatabaseUtil.isLazyLoadInitialized(NOT_PROXY_OBJECT);

assertThat(result).isTrue();
}

@Test
void lazyLoadSafeToStringWhenProxyIsUninitialized() {
Object proxyObject = createProxyObject(false);

String result = DatabaseUtil.lazyLoadSafeToString(proxyObject);

assertThat(result).isEqualTo(DatabaseUtil.UNINITIALIZED_TO_STRING);
}

@Test
void lazyLoadSafeToStringWhenProxyIsInitialized() {
Object proxyObject = createProxyObject(true);

String result = DatabaseUtil.lazyLoadSafeToString(proxyObject);

assertThat(result).isEqualTo(INITIALIZED_TO_STRING);
}

@Test
void lazyLoadSafeToStringWhenObjectIsNotProxy() {
String result = DatabaseUtil.lazyLoadSafeToString(NOT_PROXY_OBJECT);

assertThat(result).isEqualTo((Object) NOT_PROXY_OBJECT);
}

@Test
void lazyLoadSafeToStringWithSupplierWhenProxyIsUninitialized() {
Object proxyObject = createProxyObject(false);

String result = DatabaseUtil.lazyLoadSafeToString(proxyObject, () -> SUPPLIED_TO_STRING);

assertThat(result).isEqualTo(DatabaseUtil.UNINITIALIZED_TO_STRING);
}

@Test
void lazyLoadSafeToStringWithSupplierWhenProxyIsInitialized() {
Object proxyObject = createProxyObject(true);

String result = DatabaseUtil.lazyLoadSafeToString(proxyObject, () -> SUPPLIED_TO_STRING);

assertThat(result).isEqualTo(SUPPLIED_TO_STRING);
}

@Test
void lazyLoadSafeToStringWithSupplierWhenObjectIsNotProxy() {
String result = DatabaseUtil.lazyLoadSafeToString(NOT_PROXY_OBJECT, () -> SUPPLIED_TO_STRING);

assertThat(result).isEqualTo(SUPPLIED_TO_STRING);
}

/**
* @param initialized the expected return value of {@link org.hibernate.Hibernate#isInitialized(Object)} when called on the result {@link Object}
*/
private Object createProxyObject(boolean initialized) {
LazyInitializer lazyInitializer = mock(LazyInitializer.class);
when(lazyInitializer.isUninitialized()).thenReturn(!initialized);
HibernateProxy hibernateProxy = mock(HibernateProxy.class);
when(hibernateProxy.getHibernateLazyInitializer()).thenReturn(lazyInitializer);
when(hibernateProxy.toString()).thenReturn(INITIALIZED_TO_STRING);
return hibernateProxy;
}

}
2 changes: 1 addition & 1 deletion core-model/build.gradle
Expand Up @@ -25,7 +25,7 @@ dependencies {
implementation group: 'net.jcip', name: 'jcip-annotations', version: '1.0'
implementation group: 'com.github.spotbugs', name: 'spotbugs-annotations', version: '4.4.2'
testImplementation group: 'org.hamcrest', name: 'hamcrest', version: hamcrestVersion
testImplementation group: 'org.reflections', name: 'reflections', version: '0.9.11'
testImplementation group: 'org.reflections', name: 'reflections', version: reflectionsVersion
testImplementation group: 'org.assertj', name: 'assertj-core', version: assertjVersion
testImplementation project(path: ':common', configuration: 'tests')
}
Expand Down
Expand Up @@ -76,4 +76,14 @@ public void setCluster(Cluster cluster) {
this.cluster = cluster;
}

@Override
public String toString() {
return "Container{" +
"id=" + id +
", containerId='" + containerId + '\'' +
", name='" + name + '\'' +
", image='" + image + '\'' +
", host='" + host + '\'' +
'}';
}
}
Expand Up @@ -46,4 +46,13 @@ public AdjustmentType getAdjustmentType() {
public void setAdjustmentType(AdjustmentType adjustmentType) {
this.adjustmentType = adjustmentType;
}

@Override
public String toString() {
return "FailurePolicy{" +
"id=" + id +
", threshold=" + threshold +
", adjustmentType=" + adjustmentType +
'}';
}
}
Expand Up @@ -122,4 +122,14 @@ public void setCloudStorage(CloudStorage cloudStorage) {
this.cloudStorage = new Json(cloudStorage);
}
}

@Override
public String toString() {
return "FileSystem{" +
"id=" + id +
", name='" + name + '\'' +
", type=" + type +
", description='" + description + '\'' +
'}';
}
}
Expand Up @@ -188,4 +188,23 @@ public void unsetRelationsToEntitiesToBeDeleted() {
public void setDeletionTimestamp(Long deletionTimestamp) {
this.deletionTimestamp = deletionTimestamp;
}

@Override
public String toString() {
return "Network{" +
"id=" + id +
", name='" + name + '\'' +
", subnetCIDR='" + subnetCIDR + '\'' +
", description='" + description + '\'' +
", status=" + status +
", cloudPlatform='" + cloudPlatform + '\'' +
", attributes=" + attributes +
", outboundInternetTraffic=" + outboundInternetTraffic +
", networkCidrs='" + networkCidrs + '\'' +
", topology=" + topology +
", workspace=" + workspace +
", archived=" + archived +
", deletionTimestamp=" + deletionTimestamp +
'}';
}
}
Expand Up @@ -58,4 +58,13 @@ public Json getAttributes() {
public void setAttributes(Json attributes) {
this.attributes = attributes;
}

@Override
public String toString() {
return "Orchestrator{" +
"id=" + id +
", apiEndpoint='" + apiEndpoint + '\'' +
", type='" + type + '\'' +
'}';
}
}
Expand Up @@ -269,4 +269,18 @@ public Long getDeletionTimestamp() {
public boolean isArchived() {
return archived;
}

@Override
public String toString() {
return "RDSConfig{" +
"id=" + id +
", name='" + name + '\'' +
", creationDate=" + creationDate +
", stackVersion='" + stackVersion + '\'' +
", status=" + status +
", type='" + type + '\'' +
", archived=" + archived +
", deletionTimestamp=" + deletionTimestamp +
'}';
}
}
Expand Up @@ -163,4 +163,17 @@ public String getAvailabilityZone() {
public void setAvailabilityZone(String availabilityZone) {
this.availabilityZone = availabilityZone;
}

@Override
public String toString() {
return "Resource{" +
"id=" + id +
", resourceType=" + resourceType +
", resourceStatus=" + resourceStatus +
", resourceName='" + resourceName + '\'' +
", resourceReference='" + resourceReference + '\'' +
", instanceId='" + instanceId + '\'' +
", availabilityZone='" + availabilityZone + '\'' +
'}';
}
}
Expand Up @@ -134,4 +134,11 @@ public String getSaltSignPublicKey() {
public String getSaltSignPrivateKey() {
return saltSignPrivateKey.getRaw();
}

@Override
public String toString() {
return "SaltSecurityConfig{" +
"id=" + id +
'}';
}
}
Expand Up @@ -16,6 +16,7 @@
import com.sequenceiq.cloudbreak.service.secret.SecretValue;
import com.sequenceiq.cloudbreak.service.secret.domain.Secret;
import com.sequenceiq.cloudbreak.service.secret.domain.SecretToString;
import com.sequenceiq.cloudbreak.util.DatabaseUtil;
import com.sequenceiq.cloudbreak.workspace.model.Workspace;
import com.sequenceiq.cloudbreak.workspace.model.WorkspaceAwareResource;

Expand Down Expand Up @@ -144,4 +145,19 @@ public String getUserFacingKey() {
public void setUserFacingKey(String userFacingKey) {
this.userFacingKey = new Secret(userFacingKey);
}

@Override
public String toString() {
return "SecurityConfig{" +
"id=" + id +
", clientKey=" + clientKey +
", clientCert=" + clientCert +
", stack=" + DatabaseUtil.lazyLoadSafeToString(stack, () -> stack.getResourceCrn()) +
", saltSecurityConfig=" + saltSecurityConfig +
", usePrivateIpToTls=" + usePrivateIpToTls +
", workspace=" + workspace +
", userFacingCert=" + userFacingCert +
", userFacingKey=" + userFacingKey +
'}';
}
}
Expand Up @@ -119,4 +119,15 @@ public String getCloudPlatform() {
public void setCloudPlatform(String cloudPlatform) {
this.cloudPlatform = cloudPlatform;
}

@Override
public String toString() {
return "SecurityGroup{" +
"id=" + id +
", name='" + name + '\'' +
", description='" + description + '\'' +
", status=" + status +
", cloudPlatform='" + cloudPlatform + '\'' +
'}';
}
}
Expand Up @@ -52,4 +52,13 @@ public Boolean isActive() {
public Duration getTimeout() {
return timeout != null ? timeout : Duration.ZERO;
}

@Override
public String toString() {
return "ShowTerminatedClustersPreferences{" +
"id=" + id +
", active=" + active +
", timeout=" + timeout +
'}';
}
}
Expand Up @@ -57,4 +57,14 @@ public String getLoginUserName() {
public void setLoginUserName(String loginUserName) {
this.loginUserName = loginUserName;
}

@Override
public String toString() {
return "StackAuthentication{" +
"id=" + id +
", publicKey='" + publicKey + '\'' +
", publicKeyId='" + publicKeyId + '\'' +
", loginUserName='" + loginUserName + '\'' +
'}';
}
}
Expand Up @@ -133,4 +133,17 @@ public String getResourceCrn() {
public void setResourceCrn(String resourceCrn) {
this.resourceCrn = resourceCrn;
}

@Override
public String toString() {
return "StructuredEventEntity{" +
"id=" + id +
", eventType=" + eventType +
", resourceType='" + resourceType + '\'' +
", resourceId=" + resourceId +
", resourceCrn='" + resourceCrn + '\'' +
", timestamp=" + timestamp +
", structuredEventJson=" + structuredEventJson +
'}';
}
}

0 comments on commit e380a95

Please sign in to comment.