|
| 1 | +package com.linkedin.metadata.utils; |
| 2 | + |
| 3 | +import static com.linkedin.metadata.Constants.DEFAULT_RUN_ID; |
| 4 | +import static org.testng.Assert.*; |
| 5 | + |
| 6 | +import com.linkedin.data.template.StringMap; |
| 7 | +import com.linkedin.mxe.SystemMetadata; |
| 8 | +import org.testng.annotations.Test; |
| 9 | + |
| 10 | +public class SystemMetadataUtilsTest { |
| 11 | + |
| 12 | + @Test |
| 13 | + public void testCreateDefaultSystemMetadata() { |
| 14 | + SystemMetadata metadata = SystemMetadataUtils.createDefaultSystemMetadata(); |
| 15 | + |
| 16 | + assertNotNull(metadata); |
| 17 | + assertEquals(metadata.getRunId(), DEFAULT_RUN_ID); |
| 18 | + assertTrue(metadata.hasLastObserved()); |
| 19 | + assertTrue(metadata.getLastObserved() > 0); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testCreateDefaultSystemMetadataWithRunId() { |
| 24 | + String customRunId = "custom-run-id"; |
| 25 | + SystemMetadata metadata = SystemMetadataUtils.createDefaultSystemMetadata(customRunId); |
| 26 | + |
| 27 | + assertNotNull(metadata); |
| 28 | + assertEquals(metadata.getRunId(), customRunId); |
| 29 | + assertTrue(metadata.hasLastObserved()); |
| 30 | + assertTrue(metadata.getLastObserved() > 0); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testGenerateSystemMetadataIfEmpty() { |
| 35 | + // Test with null input |
| 36 | + SystemMetadata nullMetadata = SystemMetadataUtils.generateSystemMetadataIfEmpty(null); |
| 37 | + assertNotNull(nullMetadata); |
| 38 | + assertEquals(nullMetadata.getRunId(), DEFAULT_RUN_ID); |
| 39 | + assertTrue(nullMetadata.hasLastObserved()); |
| 40 | + |
| 41 | + // Test with existing metadata |
| 42 | + SystemMetadata existingMetadata = |
| 43 | + new SystemMetadata().setRunId("existing-run").setLastObserved(1234567890L); |
| 44 | + SystemMetadata result = SystemMetadataUtils.generateSystemMetadataIfEmpty(existingMetadata); |
| 45 | + |
| 46 | + assertEquals(result.getRunId(), "existing-run"); |
| 47 | + assertEquals(result.getLastObserved(), 1234567890L); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testParseSystemMetadata() { |
| 52 | + // Test null input |
| 53 | + SystemMetadata nullResult = SystemMetadataUtils.parseSystemMetadata(null); |
| 54 | + assertNotNull(nullResult); |
| 55 | + assertEquals(nullResult.getRunId(), DEFAULT_RUN_ID); |
| 56 | + |
| 57 | + // Test empty string input |
| 58 | + SystemMetadata emptyResult = SystemMetadataUtils.parseSystemMetadata(""); |
| 59 | + assertNotNull(emptyResult); |
| 60 | + assertEquals(emptyResult.getRunId(), DEFAULT_RUN_ID); |
| 61 | + |
| 62 | + // Test valid JSON input |
| 63 | + String validJson = "{\"runId\":\"test-run\",\"lastObserved\":1234567890}"; |
| 64 | + SystemMetadata jsonResult = SystemMetadataUtils.parseSystemMetadata(validJson); |
| 65 | + assertNotNull(jsonResult); |
| 66 | + assertEquals(jsonResult.getRunId(), "test-run"); |
| 67 | + assertEquals(jsonResult.getLastObserved(), 1234567890L); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testIsNoOp() { |
| 72 | + // Test null metadata |
| 73 | + assertFalse(SystemMetadataUtils.isNoOp(null)); |
| 74 | + |
| 75 | + // Test metadata without properties |
| 76 | + SystemMetadata emptyMetadata = new SystemMetadata(); |
| 77 | + assertFalse(SystemMetadataUtils.isNoOp(emptyMetadata)); |
| 78 | + |
| 79 | + // Test metadata with isNoOp=true |
| 80 | + SystemMetadata noOpMetadata = new SystemMetadata(); |
| 81 | + StringMap properties = new StringMap(); |
| 82 | + properties.put("isNoOp", "true"); |
| 83 | + noOpMetadata.setProperties(properties); |
| 84 | + assertTrue(SystemMetadataUtils.isNoOp(noOpMetadata)); |
| 85 | + |
| 86 | + // Test metadata with isNoOp=false |
| 87 | + properties.put("isNoOp", "false"); |
| 88 | + assertFalse(SystemMetadataUtils.isNoOp(noOpMetadata)); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testSetNoOp() { |
| 93 | + // Test with null metadata |
| 94 | + assertNull(SystemMetadataUtils.setNoOp(null, true)); |
| 95 | + |
| 96 | + // Test setting noOp to true |
| 97 | + SystemMetadata metadata = new SystemMetadata(); |
| 98 | + SystemMetadata result = SystemMetadataUtils.setNoOp(metadata, true); |
| 99 | + assertNotNull(result); |
| 100 | + assertTrue(result.hasProperties()); |
| 101 | + assertNotNull(result.getProperties()); |
| 102 | + assertEquals(result.getProperties().get("isNoOp"), "true"); |
| 103 | + |
| 104 | + // Test setting noOp to false |
| 105 | + result = SystemMetadataUtils.setNoOp(metadata, false); |
| 106 | + assertNotNull(result); |
| 107 | + assertTrue(result.hasProperties()); |
| 108 | + assertNotNull(result.getProperties()); |
| 109 | + assertEquals(result.getProperties().get("isNoOp"), "false"); |
| 110 | + |
| 111 | + // Test with existing properties |
| 112 | + StringMap existingProps = new StringMap(); |
| 113 | + existingProps.put("otherKey", "value"); |
| 114 | + metadata.setProperties(existingProps); |
| 115 | + result = SystemMetadataUtils.setNoOp(metadata, true); |
| 116 | + assertNotNull(result); |
| 117 | + assertEquals(result.getProperties().get("otherKey"), "value"); |
| 118 | + assertEquals(result.getProperties().get("isNoOp"), "true"); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testGenerateSystemMetadataIfEmpty_NullInput() { |
| 123 | + SystemMetadata result = SystemMetadataUtils.generateSystemMetadataIfEmpty(null); |
| 124 | + |
| 125 | + assertNotNull(result); |
| 126 | + assertEquals(DEFAULT_RUN_ID, result.getRunId()); |
| 127 | + assertNotNull(result.getLastObserved()); |
| 128 | + assertTrue(result.getLastObserved() > 0); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testGenerateSystemMetadataIfEmpty_NoRunId() { |
| 133 | + SystemMetadata input = new SystemMetadata().setLastObserved(1234567890L); |
| 134 | + |
| 135 | + SystemMetadata result = SystemMetadataUtils.generateSystemMetadataIfEmpty(input); |
| 136 | + |
| 137 | + assertNotNull(result); |
| 138 | + assertEquals(DEFAULT_RUN_ID, result.getRunId()); |
| 139 | + assertEquals(1234567890L, result.getLastObserved().longValue()); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testGenerateSystemMetadataIfEmpty_NoLastObserved() { |
| 144 | + SystemMetadata input = new SystemMetadata().setRunId("custom-run-id"); |
| 145 | + |
| 146 | + SystemMetadata result = SystemMetadataUtils.generateSystemMetadataIfEmpty(input); |
| 147 | + |
| 148 | + assertNotNull(result); |
| 149 | + assertEquals("custom-run-id", result.getRunId()); |
| 150 | + assertNotNull(result.getLastObserved()); |
| 151 | + assertTrue(result.getLastObserved() > 0); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void testGenerateSystemMetadataIfEmpty_ZeroLastObserved() { |
| 156 | + SystemMetadata input = new SystemMetadata().setRunId("custom-run-id").setLastObserved(0L); |
| 157 | + |
| 158 | + SystemMetadata result = SystemMetadataUtils.generateSystemMetadataIfEmpty(input); |
| 159 | + |
| 160 | + assertNotNull(result); |
| 161 | + assertEquals("custom-run-id", result.getRunId()); |
| 162 | + assertNotNull(result.getLastObserved()); |
| 163 | + assertTrue(result.getLastObserved() > 0); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void testGenerateSystemMetadataIfEmpty_AllFieldsPopulated() { |
| 168 | + SystemMetadata input = |
| 169 | + new SystemMetadata().setRunId("custom-run-id").setLastObserved(1234567890L); |
| 170 | + |
| 171 | + SystemMetadata result = SystemMetadataUtils.generateSystemMetadataIfEmpty(input); |
| 172 | + |
| 173 | + assertNotNull(result); |
| 174 | + assertEquals("custom-run-id", result.getRunId()); |
| 175 | + assertEquals(1234567890L, result.getLastObserved().longValue()); |
| 176 | + } |
| 177 | +} |
0 commit comments