Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store user details in config objects in mongo #212

Merged
merged 7 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.google.protobuf.Value;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.util.Optional;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import org.hypertrace.config.service.ConfigServiceUtils;
import org.hypertrace.core.documentstore.Document;
Expand All @@ -26,11 +28,14 @@
*/
@lombok.Value
@Slf4j
@Builder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still needed with the latest change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required will remove it

public class ConfigDocument implements Document {

private static final ObjectMapper OBJECT_MAPPER =
new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

public static final String DEFAULT_LATEST_UPDATED_USER_ID = "Unknown";
public static final String DEFAULT_LATEST_UPDATED_USER_EMAIL = "Unknown";
public static final String RESOURCE_FIELD_NAME = "resourceName";
public static final String RESOURCE_NAMESPACE_FIELD_NAME = "resourceNamespace";
public static final String TENANT_ID_FIELD_NAME = "tenantId";
Expand Down Expand Up @@ -91,8 +96,10 @@ public ConfigDocument(
this.tenantId = tenantId;
this.context = context;
this.configVersion = configVersion;
this.lastUpdatedUserId = lastUpdatedUserId;
this.lastUpdatedUserEmail = lastUpdatedUserEmail;
this.lastUpdatedUserId =
Optional.ofNullable(lastUpdatedUserId).orElse(DEFAULT_LATEST_UPDATED_USER_ID);
this.lastUpdatedUserEmail =
Optional.ofNullable(lastUpdatedUserEmail).orElse(DEFAULT_LATEST_UPDATED_USER_EMAIL);
this.config = config;
this.creationTimestamp = creationTimestamp;
this.updateTimestamp = updateTimestamp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.hypertrace.config.service.TestUtils.RESOURCE_NAMESPACE;
import static org.hypertrace.config.service.TestUtils.TENANT_ID;
import static org.hypertrace.config.service.TestUtils.getConfig1;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.google.common.io.Resources;
Expand Down Expand Up @@ -70,4 +71,41 @@ void convertJsonStringWithoutTimestamp() throws IOException {
assertEquals(0, configDocument.getCreationTimestamp());
assertEquals(0, configDocument.getUpdateTimestamp());
}

@Test
@DisplayName("Test compatibility of json string when new field is added to ConfigDocument")
void testConfigDocumentCompatibility() throws IOException {
ConfigDocument configDocument =
new ConfigDocument(
"exampleResourceName",
"exampleResourceNamespace",
"exampleTenantId",
"exampleContext",
1,
null,
null,
Value.newBuilder().build(),
1622547800000L,
1625149800000L);
assertDoesNotThrow(() -> configDocument.toJson());
assertEquals("Unknown", configDocument.getLastUpdatedUserId());
assertEquals("Unknown", configDocument.getLastUpdatedUserEmail());

String jsonString =
"{"
+ "\"resourceName\":\"exampleResourceName\","
+ "\"userId\":\"userId\","
+ "\"resourceNamespace\":\"exampleResourceNamespace\","
+ "\"tenantId\":\"exampleTenantId\","
+ "\"context\":\"exampleContext\","
+ "\"configVersion\":1,"
+ "\"config\":null,"
+ "\"creationTimestamp\":1622547800000,"
+ "\"updateTimestamp\":1625149800000}";

ConfigDocument configDocument1 = ConfigDocument.fromJson(jsonString);

assertEquals("Unknown", configDocument1.getLastUpdatedUserId());
assertEquals("Unknown", configDocument1.getLastUpdatedUserEmail());
}
}
Loading