Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arjun4084346 committed Nov 22, 2024
1 parent 114803d commit 5484ab5
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.linkedin.venice.utils;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.expectThrows;
import static org.testng.Assert.fail;
import static org.mockito.Mockito.*;
import static org.testng.Assert.*;

import com.linkedin.venice.exceptions.VeniceException;
import com.linkedin.venice.meta.HybridStoreConfig;
import com.linkedin.venice.meta.Store;
import com.linkedin.venice.meta.StoreInfo;
import com.linkedin.venice.meta.Version;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand All @@ -19,6 +22,7 @@
import java.util.TreeMap;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.collections.Lists;


/**
Expand Down Expand Up @@ -229,4 +233,110 @@ public void testResolveKafkaUrlForSepTopic() {
Assert.assertEquals(Utils.resolveKafkaUrlForSepTopic(originalKafkaUrlForSep), originalKafkaUrl);
Assert.assertEquals(Utils.resolveKafkaUrlForSepTopic(originalKafkaUrl), originalKafkaUrl);
}

@Test
void testGetRealTimeTopicNameWithStore() {
Store mockStore = mock(Store.class);
List<Version> mockVersions = Collections.singletonList(mock(Version.class));
HybridStoreConfig mockHybridConfig = mock(HybridStoreConfig.class);

when(mockStore.getName()).thenReturn("TestStore");
when(mockStore.getVersions()).thenReturn(mockVersions);
when(mockStore.getCurrentVersion()).thenReturn(1);
when(mockStore.getHybridStoreConfig()).thenReturn(mockHybridConfig);

when(mockHybridConfig.getRealTimeTopicName()).thenReturn("RealTimeTopic");

String result = Utils.getRealTimeTopicName(mockStore);
assertEquals("RealTimeTopic", result);
}

@Test
void testGetRealTimeTopicNameWithStoreInfo() {
StoreInfo mockStoreInfo = mock(StoreInfo.class);
List<Version> mockVersions = Collections.singletonList(mock(Version.class));
HybridStoreConfig mockHybridConfig = mock(HybridStoreConfig.class);

when(mockStoreInfo.getName()).thenReturn("TestStore");
when(mockStoreInfo.getVersions()).thenReturn(mockVersions);
when(mockStoreInfo.getCurrentVersion()).thenReturn(1);
when(mockStoreInfo.getHybridStoreConfig()).thenReturn(mockHybridConfig);

when(mockHybridConfig.getRealTimeTopicName()).thenReturn("RealTimeTopic");

String result = Utils.getRealTimeTopicName(mockStoreInfo);
assertEquals("RealTimeTopic", result);
}

@Test
void testGetRealTimeTopicNameWithHybridConfig() {
HybridStoreConfig mockHybridConfig = mock(HybridStoreConfig.class);

when(mockHybridConfig.getRealTimeTopicName()).thenReturn("RealTimeTopic");
String result = Utils.getRealTimeTopicName("TestStore", Collections.EMPTY_LIST, 1, mockHybridConfig);

assertEquals("RealTimeTopic", result);
}

@Test
void testGetRealTimeTopicNameWithoutHybridConfig() {
String result = Utils.getRealTimeTopicName("TestStore", Collections.EMPTY_LIST, 0, null);
assertEquals("TestStore" + Version.REAL_TIME_TOPIC_SUFFIX, result);
}

@Test
void testGetRealTimeTopicNameWithConflictingVersions() {
Version mockVersion1 = mock(Version.class);
Version mockVersion2 = mock(Version.class);
HybridStoreConfig mockConfig1 = mock(HybridStoreConfig.class);
HybridStoreConfig mockConfig2 = mock(HybridStoreConfig.class);

when(mockVersion1.isHybrid()).thenReturn(true);
when(mockVersion2.isHybrid()).thenReturn(true);
when(mockVersion1.getHybridStoreConfig()).thenReturn(mockConfig1);
when(mockVersion2.getHybridStoreConfig()).thenReturn(mockConfig2);
when(mockConfig1.getRealTimeTopicName()).thenReturn("RealTimeTopic1");
when(mockConfig2.getRealTimeTopicName()).thenReturn("RealTimeTopic2");

String result = Utils.getRealTimeTopicName("TestStore", Lists.newArrayList(mockVersion1, mockVersion2), 1, null);
assertTrue(result.equals("RealTimeTopic1") || result.equals("RealTimeTopic2"));
}

@Test
void testGetRealTimeTopicNameWithExceptionHandling() {
Version mockVersion1 = mock(Version.class);
Version mockVersion2 = mock(Version.class);

when(mockVersion1.isHybrid()).thenReturn(true);
when(mockVersion1.getHybridStoreConfig()).thenThrow(new VeniceException("Test Exception"));

when(mockVersion2.isHybrid()).thenReturn(false);

String result = Utils.getRealTimeTopicName("TestStore", Lists.newArrayList(mockVersion1, mockVersion2), 1, null);
assertEquals("TestStore" + Version.REAL_TIME_TOPIC_SUFFIX, result);
}

@Test
void testGetRealTimeTopicNameWithVersion() {
Version mockVersion = mock(Version.class);
HybridStoreConfig mockHybridConfig = mock(HybridStoreConfig.class);

when(mockVersion.getHybridStoreConfig()).thenReturn(mockHybridConfig);
when(mockVersion.getStoreName()).thenReturn("TestStore");
when(mockHybridConfig.getRealTimeTopicName()).thenReturn("RealTimeTopic");

String result = Utils.getRealTimeTopicName(mockVersion);
assertEquals("RealTimeTopic", result);
}

@Test(expectedExceptions = VeniceException.class)
void testGetRealTimeTopicNameWithNonHybridVersion() {
// Mocking the Version object
Version mockVersion = mock(Version.class);

// Mock setup to trigger the exception path
when(mockVersion.getHybridStoreConfig()).thenReturn(null);

Utils.getRealTimeTopicName(mockVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ protected void checkWhetherToStartBufferReplayForHybrid(OfflinePushStatus offlin
try {
String newStatusDetails;
realTimeTopicSwitcher.switchToRealTimeTopic(
Utils.getRealTimeTopicName(version),
Utils.getRealTimeTopicName(store),
offlinePushStatus.getKafkaTopic(),
store,
aggregateRealTimeSourceKafkaUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ public void testSendVersionSwap() {

Store mockStore = mock(Store.class);
when(mockStore.getName()).thenReturn(storeName);
String realTimeTopicName = Utils.getRealTimeTopicName(mockStore);
Version version1 = new VersionImpl(storeName, 1, "push1");
Version version2 = new VersionImpl(storeName, 2, "push2");
version2.setViewConfigs(viewConfigs);
Expand Down

0 comments on commit 5484ab5

Please sign in to comment.