### Question:
SubscriptionBuilder { @SuppressWarnings("unchecked") public <T> Function<Subscription, T> mapToSubscriptionClass(final Class<T> clazz) { return subscription -> { if (clazz.isAssignableFrom(subscription.getClass())) { return (T) subscription; } else { final String msg = String.format( "%s instance can't be mapped to type %s", subscription.getClass().getSimpleName(), clazz.getSimpleName()); throw new IllegalArgumentException(msg); } }; }  Subscription build(
      final long subscriptionId, final String connectionId, final SubscribeRequest request); @SuppressWarnings("unchecked") Function<Subscription, T> mapToSubscriptionClass(final Class<T> clazz);  }### Answer:
@Test public void shouldReturnLogsSubscriptionWhenMappingLogsSubscription() { final Function<Subscription, LogsSubscription> function = subscriptionBuilder.mapToSubscriptionClass(LogsSubscription.class); final Subscription subscription = new LogsSubscription(1L, CONNECTION_ID, filterParameter()); assertThat(function.apply(subscription)).isInstanceOf(LogsSubscription.class); }
@Test public void shouldReturnNewBlockHeadsSubscriptionWhenMappingNewBlockHeadsSubscription() { final Function<Subscription, NewBlockHeadersSubscription> function = subscriptionBuilder.mapToSubscriptionClass(NewBlockHeadersSubscription.class); final Subscription subscription = new NewBlockHeadersSubscription(1L, CONNECTION_ID, true); assertThat(function.apply(subscription)).isInstanceOf(NewBlockHeadersSubscription.class); }
@Test public void shouldReturnSubscriptionWhenMappingNewPendingTransactionsSubscription() { final Function<Subscription, Subscription> function = subscriptionBuilder.mapToSubscriptionClass(Subscription.class); final Subscription logsSubscription = new Subscription( 1L, CONNECTION_ID, SubscriptionType.NEW_PENDING_TRANSACTIONS, Boolean.FALSE); assertThat(function.apply(logsSubscription)).isInstanceOf(Subscription.class); }
@Test public void shouldReturnSubscriptionWhenMappingSyncingSubscription() { final Function<Subscription, SyncingSubscription> function = subscriptionBuilder.mapToSubscriptionClass(SyncingSubscription.class); final Subscription subscription = new SyncingSubscription(1L, CONNECTION_ID, SubscriptionType.SYNCING); assertThat(function.apply(subscription)).isInstanceOf(SyncingSubscription.class); }
@Test @SuppressWarnings("ReturnValueIgnored") public void shouldThrownIllegalArgumentExceptionWhenMappingWrongSubscriptionType() { final Function<Subscription, LogsSubscription> function = subscriptionBuilder.mapToSubscriptionClass(LogsSubscription.class); final NewBlockHeadersSubscription subscription = new NewBlockHeadersSubscription(1L, CONNECTION_ID, true); final Throwable thrown = catchThrowable(() -> function.apply(subscription)); assertThat(thrown) .hasNoCause() .isInstanceOf(IllegalArgumentException.class) .hasMessage( "NewBlockHeadersSubscription instance can't be mapped to type LogsSubscription"); }