55import com .google .api .gax .grpc .ChannelPoolSettings ;
66import com .google .cloud .bigtable .gaxx .grpc .BigtableChannelPoolSettings ;
77import java .lang .reflect .Modifier ;
8- import java .util .List ;
8+ import java .util .Set ;
9+ import com .google .common .collect .ImmutableSet ;
910import java .lang .reflect .Method ;
1011import java .util .Arrays ;
1112import java .util .stream .Collectors ;
@@ -32,60 +33,48 @@ public void testToBigtableChannelPoolSettingsAllFieldsSetCopiesCorrectly() throw
3233 .setPreemptiveRefreshEnabled (true )
3334 .build ();
3435
35- BigtableChannelPoolSettings copiedSettings =
36- ChannelPoolSettingsCopier .toBigtableChannelPoolSettings (originalSettings );
36+ BigtableChannelPoolSettings copiedSettings = BigtableChannelPoolSettings .copyFrom (originalSettings );
3737 assertSettingsCopiedCorrectly (originalSettings , copiedSettings );
3838 }
3939
4040 @ Test
4141 public void testToBigtableChannelPoolSettingsDefaultValuesCopiesCorrectly () throws Exception {
42- // 1. Arrange
4342 ChannelPoolSettings originalSettings = ChannelPoolSettings .builder ().build ();
44-
45- // 2. Act
46- BigtableChannelPoolSettings copiedSettings =
47- ChannelPoolSettingsCopier .toBigtableChannelPoolSettings (originalSettings );
48-
49- // 3. Assert
43+ BigtableChannelPoolSettings copiedSettings = BigtableChannelPoolSettings .copyFrom (originalSettings );
5044 assertSettingsCopiedCorrectly (originalSettings , copiedSettings );
5145 }
5246
53- @ Test
54- public void testToBigtableChannelPoolSettingsNullInputReturnsNull () {
55- ChannelPoolSettings originalSettings = null ;
56- BigtableChannelPoolSettings copiedSettings =
57- ChannelPoolSettingsCopier .toBigtableChannelPoolSettings (originalSettings );
58- assertThat (copiedSettings ).isNull ();
59- }
47+ // @Test
48+ // public void testToBigtableChannelPoolSettingsNullInputReturnsNull() {
49+ // ChannelPoolSettings originalSettings = null;
50+ // BigtableChannelPoolSettings copiedSettings =
51+ // ChannelPoolSettingsCopier.toBigtableChannelPoolSettings(originalSettings);
52+ // assertThat(copiedSettings).isNull();
53+ // }
6054
6155 private void assertSettingsCopiedCorrectly (
6256 ChannelPoolSettings originalSettings , BigtableChannelPoolSettings copiedSettings )
6357 throws Exception {
64- Class <ChannelPoolSettings > originalSettingsClass = ChannelPoolSettings .class ;
6558
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 ())
59+ Set < String > supportedGetters = ImmutableSet . of ( "getMinRpcsPerChannel" , "getMaxRpcsPerChannel" , "getMinChannelCount" , "getMaxChannelCount" , "getInitialChannelCount" , "isPreemptiveRefreshEnabled" , "isStaticSize" );
60+
61+ Set < String > actualGetters = Arrays .stream (ChannelPoolSettings .class .getDeclaredMethods ())
6962 .filter (method -> Modifier .isPublic (method .getModifiers ())
7063 && 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 ());
64+ && (method .getName ().startsWith ("get" ) || method .getName ().startsWith ("is" )))
65+ . map ( Method :: getName )
66+ .collect (Collectors .toSet ());
7467
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 );
68+ // If this fails then we need to add support for the additional attributes on the gax ChannelPool
69+ // Relevant things to update the copier and the other tests in this file
70+ assertThat (supportedGetters ).containsAtLeastElementsIn (actualGetters );
8071
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- }
72+ assertThat (originalSettings .getInitialChannelCount ()).isEqualTo (copiedSettings .getInitialChannelCount ());
73+ assertThat (originalSettings .getMaxChannelCount ()).isEqualTo (copiedSettings .getMaxChannelCount ());
74+ assertThat (originalSettings .getMinChannelCount ()).isEqualTo (copiedSettings .getMinChannelCount ());
75+ assertThat (originalSettings .getMaxRpcsPerChannel ()).isEqualTo (copiedSettings .getMaxRpcsPerChannel ());
76+ assertThat (originalSettings .getMinRpcsPerChannel ()).isEqualTo (copiedSettings .getMinRpcsPerChannel ());
77+ assertThat (originalSettings .getInitialChannelCount ()).isEqualTo (copiedSettings .getInitialChannelCount ());
78+ assertThat (originalSettings .isPreemptiveRefreshEnabled ()).isEqualTo (copiedSettings .isPreemptiveRefreshEnabled ());
9079 }
9180}
0 commit comments