1+ package com .google .cloud .bigtable .gaxx .utils ;
2+
3+ import static com .google .common .truth .Truth .assertThat ;
4+
5+ import com .google .api .gax .grpc .ChannelPoolSettings ;
6+ import com .google .cloud .bigtable .gaxx .grpc .BigtableChannelPoolSettings ;
7+ import java .lang .reflect .Modifier ;
8+ import java .util .List ;
9+ import java .lang .reflect .Method ;
10+ import java .util .Arrays ;
11+ import java .util .stream .Collectors ;
12+ import org .junit .Rule ;
13+ import org .junit .Test ;
14+ import org .junit .runner .RunWith ;
15+ import org .junit .runners .JUnit4 ;
16+ import org .mockito .junit .MockitoJUnit ;
17+ import org .mockito .junit .MockitoRule ;
18+
19+ @ RunWith (JUnit4 .class )
20+ public class ChannelPoolCopyTest {
21+ @ Rule public MockitoRule mockitoRule = MockitoJUnit .rule ();
22+
23+ @ Test
24+ public void testToBigtableChannelPoolSettingsAllFieldsSetCopiesCorrectly () throws Exception {
25+ ChannelPoolSettings originalSettings =
26+ ChannelPoolSettings .builder ()
27+ .setMinRpcsPerChannel (10 )
28+ .setMaxRpcsPerChannel (50 )
29+ .setMinChannelCount (5 )
30+ .setMaxChannelCount (100 )
31+ .setInitialChannelCount (20 )
32+ .setPreemptiveRefreshEnabled (true )
33+ .build ();
34+
35+ BigtableChannelPoolSettings copiedSettings =
36+ ChannelPoolSettingsCopier .toBigtableChannelPoolSettings (originalSettings );
37+ assertSettingsCopiedCorrectly (originalSettings , copiedSettings );
38+ }
39+
40+ @ Test
41+ public void testToBigtableChannelPoolSettingsDefaultValuesCopiesCorrectly () throws Exception {
42+ // 1. Arrange
43+ ChannelPoolSettings originalSettings = ChannelPoolSettings .builder ().build ();
44+
45+ // 2. Act
46+ BigtableChannelPoolSettings copiedSettings =
47+ ChannelPoolSettingsCopier .toBigtableChannelPoolSettings (originalSettings );
48+
49+ // 3. Assert
50+ assertSettingsCopiedCorrectly (originalSettings , copiedSettings );
51+ }
52+
53+ @ Test
54+ public void testToBigtableChannelPoolSettingsNullInputReturnsNull () {
55+ ChannelPoolSettings originalSettings = null ;
56+ BigtableChannelPoolSettings copiedSettings =
57+ ChannelPoolSettingsCopier .toBigtableChannelPoolSettings (originalSettings );
58+ assertThat (copiedSettings ).isNull ();
59+ }
60+
61+ private void assertSettingsCopiedCorrectly (
62+ ChannelPoolSettings originalSettings , BigtableChannelPoolSettings copiedSettings )
63+ throws Exception {
64+ Class <ChannelPoolSettings > originalSettingsClass = ChannelPoolSettings .class ;
65+
66+ // Get all public abstract methods from ChannelPoolSettings that start with "get" or "is"
67+ // and exclude methods from Object.class
68+ List <Method > originalPoolGetters = Arrays .stream (ChannelPoolSettings .class .getMethods ())
69+ .filter (method -> Modifier .isPublic (method .getModifiers ())
70+ && Modifier .isAbstract (method .getModifiers ())
71+ && (method .getName ().startsWith ("get" ) || method .getName ().startsWith ("is" ))
72+ && method .getDeclaringClass () != Object .class ) // Filter out java.lang.Object.getClass()
73+ .collect (Collectors .toList ());
74+
75+ for (Method getterMethod : originalPoolGetters ) {
76+ try {
77+ Object originalValue = getterMethod .invoke (originalSettings );
78+ Method correspondingMethod = BigtableChannelPoolSettings .class .getMethod (getterMethod .getName ());
79+ Object copiedValue = correspondingMethod .invoke (copiedSettings );
80+
81+ assertThat (copiedValue ).isEqualTo (originalValue );
82+ } catch (ReflectiveOperationException e ) {
83+ throw new AssertionError (
84+ String .format ("Reflection error accessing method '%s': %s" , getterMethod .getName (), e .getMessage ()), e );
85+ } catch (Exception e ) { // Catch any other unexpected exceptions
86+ throw new Exception (
87+ String .format ("Unexpected error during comparison for method '%s': %s" , getterMethod .getName (), e .getMessage ()), e );
88+ }
89+ }
90+ }
91+ }
0 commit comments