DatabaseUtil { public static String findConnectionUrl(DbmsHandlerComponent dbmsHandlerComponent, Dbms dbms) { final DbmsType type = findDbmsType(dbmsHandlerComponent, dbms); return dbms.getConnectionUrl().orElseGet(() -> type.getConnectionUrlGenerator().from(dbms)); } private  DatabaseUtil(); static DbmsType dbmsTypeOf(DbmsHandlerComponent handler, Dbms dbms); static String findConnectionUrl(DbmsHandlerComponent dbmsHandlerComponent, Dbms dbms); static DbmsType findDbmsType(DbmsHandlerComponent dbmsHandlerComponent, Dbms dbms);  }
@Test void findConnectionUrl() { final Dbms dbms = mock(Dbms.class); when(dbms.getTypeName()).thenReturn("dbms_type_name"); when(dbms.getConnectionUrl()).thenReturn(Optional.of("connection.url")); final Dbms dbms2 = mock(Dbms.class); when(dbms2.getTypeName()).thenReturn("another_dbms_type_name"); when(dbms2.getConnectionUrl()).thenReturn(Optional.empty()); final DbmsType dbmsType = mock(DbmsType.class); when(dbmsType.getName()).thenReturn("dbms_type_name"); final DbmsType dbmsType2 = mock(DbmsType.class, RETURNS_DEEP_STUBS); when(dbmsType2.getName()).thenReturn("another_dbms_type_name"); when(dbmsType2.getConnectionUrlGenerator().from(dbms2)).thenReturn("another.connection.url"); final DbmsHandlerComponent dbmsHandlerComponent = mock(DbmsHandlerComponent.class); when(dbmsHandlerComponent.findByName("dbms_type_name")).thenReturn(Optional.of(dbmsType)); when(dbmsHandlerComponent.findByName("another_dbms_type_name")).thenReturn(Optional.of(dbmsType2)); assertEquals("connection.url", DatabaseUtil.findConnectionUrl(dbmsHandlerComponent, dbms)); assertEquals("another.connection.url", DatabaseUtil.findConnectionUrl(dbmsHandlerComponent, dbms2)); }