Analytics { public void enqueue(MessageBuilder builder) { for (MessageTransformer messageTransformer : messageTransformers) { boolean shouldContinue = messageTransformer.transform(builder); if (!shouldContinue) { log.print(Log.Level.VERBOSE, "Skipping message %s.", builder); return; } } Message message = builder.build(); for (MessageInterceptor messageInterceptor : messageInterceptors) { message = messageInterceptor.intercept(message); if (message == null) { log.print(Log.Level.VERBOSE, "Skipping message %s.", builder); return; } } client.enqueue(message); } Analytics(
      AnalyticsClient client,
      List<MessageTransformer> messageTransformers,
      List<MessageInterceptor> messageInterceptors,
      Log log); static Builder builder(String writeKey); void enqueue(MessageBuilder builder); void flush(); void shutdown();  }
@Test public void enqueueIsDispatched(MessageBuilderTest builder) { MessageBuilder messageBuilder = builder.get().userId("prateek"); Message message = messageBuilder.build(); when(messageTransformer.transform(messageBuilder)).thenReturn(true); when(messageInterceptor.intercept(any(Message.class))).thenReturn(message); analytics.enqueue(messageBuilder); verify(messageTransformer).transform(messageBuilder); verify(messageInterceptor).intercept(any(Message.class)); verify(client).enqueue(message); }
@Test public void doesNotEnqueueWhenTransformerReturnsFalse(MessageBuilderTest builder) { MessageBuilder messageBuilder = builder.get().userId("prateek"); when(messageTransformer.transform(messageBuilder)).thenReturn(false); analytics.enqueue(messageBuilder); verify(messageTransformer).transform(messageBuilder); verify(messageInterceptor, never()).intercept(any(Message.class)); verify(client, never()).enqueue(any(Message.class)); }
@Test public void doesNotEnqueueWhenInterceptorReturnsNull(MessageBuilderTest builder) { MessageBuilder messageBuilder = builder.get().userId("prateek"); when(messageTransformer.transform(messageBuilder)).thenReturn(true); analytics.enqueue(messageBuilder); verify(messageTransformer).transform(messageBuilder); verify(messageInterceptor).intercept(any(Message.class)); verify(client, never()).enqueue(any(Message.class)); }