Class TestUtils


  • public class TestUtils
    extends java.lang.Object
    General-purpose utility functions for tests.
    • Constructor Detail

      • TestUtils

        public TestUtils()
    • Method Detail

      • dequalifyClassName

        public static java.lang.String dequalifyClassName​(java.lang.String className)
      • waitForNonDeterministicCompletion

        public static void waitForNonDeterministicCompletion​(long timeout,
                                                             java.util.concurrent.TimeUnit timeoutUnit,
                                                             java.util.function.BooleanSupplier condition)
                                                      throws java.lang.AssertionError
        To be used for tests when we need to wait for an async operation to complete. Pass a timeout, and a lambda for checking if the operation is complete.
        Parameters:
        timeout - amount of time to wait
        timeoutUnit - TimeUnit for the {@param timeout}
        condition - A BooleanSupplier which should execute the non-deterministic action and return true if it is successful, false otherwise.
        Throws:
        java.lang.AssertionError
      • updateStoreToHybrid

        public static ControllerResponse updateStoreToHybrid​(java.lang.String storeName,
                                                             ControllerClient parentControllerClient,
                                                             java.util.Optional<java.lang.Boolean> enableNativeReplication,
                                                             java.util.Optional<java.lang.Boolean> enableActiveActiveReplication,
                                                             java.util.Optional<java.lang.Boolean> enableChunking)
      • waitForNonDeterministicAssertion

        public static void waitForNonDeterministicAssertion​(long timeout,
                                                            java.util.concurrent.TimeUnit timeoutUnit,
                                                            TestUtils.NonDeterministicAssertion assertion)
                                                     throws java.lang.AssertionError
        To be used for tests when we need to wait for an async operation to complete. Pass a timeout, and a labmda for checking if the operation is complete. There is an issue within Mockito where it emits VerifyError instead of ArgumentsAreDifferent Exception. Check out "ExceptionFactory#JunitArgsAreDifferent" for details. The workaround here is to catch both assert and verify error. TODO: find a better way resolve it
        Parameters:
        timeout - amount of time to wait
        timeoutUnit - TimeUnit for the {@param timeout}
        assertion - A TestUtils.NonDeterministicAssertion which should simply execute without exception if it is successful, or throw an AssertionError otherwise.
        Throws:
        java.lang.AssertionError - throws the exception thrown by the TestUtils.NonDeterministicAssertion if the maximum wait time has been exceeded.
      • waitForNonDeterministicAssertion

        public static void waitForNonDeterministicAssertion​(long timeout,
                                                            java.util.concurrent.TimeUnit timeoutUnit,
                                                            boolean exponentialBackOff,
                                                            TestUtils.NonDeterministicAssertion assertion)
                                                     throws java.lang.AssertionError
        Throws:
        java.lang.AssertionError
      • waitForNonDeterministicAssertion

        public static void waitForNonDeterministicAssertion​(long timeout,
                                                            java.util.concurrent.TimeUnit timeoutUnit,
                                                            boolean exponentialBackOff,
                                                            boolean retryOnThrowable,
                                                            TestUtils.NonDeterministicAssertion assertion)
                                                     throws java.lang.AssertionError
        Throws:
        java.lang.AssertionError
      • createVersionWithBatchData

        public static VersionCreationResponse createVersionWithBatchData​(ControllerClient controllerClient,
                                                                         java.lang.String storeName,
                                                                         java.lang.String keySchema,
                                                                         java.lang.String valueSchema,
                                                                         java.util.stream.Stream<java.util.Map.Entry> batchData,
                                                                         PubSubProducerAdapterFactory pubSubProducerAdapterFactory,
                                                                         java.util.Map<java.lang.String,​java.lang.String> additionalProperties)
      • createVersionWithBatchData

        public static VersionCreationResponse createVersionWithBatchData​(ControllerClient controllerClient,
                                                                         java.lang.String storeName,
                                                                         java.lang.String keySchema,
                                                                         java.lang.String valueSchema,
                                                                         java.util.stream.Stream<java.util.Map.Entry> batchData,
                                                                         int valueSchemaId,
                                                                         PubSubProducerAdapterFactory pubSubProducerAdapterFactory,
                                                                         java.util.Map<java.lang.String,​java.lang.String> additionalProperties)
      • writeBatchData

        public static void writeBatchData​(VersionCreationResponse response,
                                          java.lang.String keySchema,
                                          java.lang.String valueSchema,
                                          java.util.stream.Stream<java.util.Map.Entry> batchData,
                                          int valueSchemaId,
                                          PubSubProducerAdapterFactory pubSubProducerAdapterFactory,
                                          java.util.Map<java.lang.String,​java.lang.String> additionalProperties)
      • writeBatchData

        public static void writeBatchData​(VersionCreationResponse response,
                                          java.lang.String keySchema,
                                          java.lang.String valueSchema,
                                          java.util.stream.Stream<java.util.Map.Entry> batchData,
                                          int valueSchemaId,
                                          CompressionStrategy compressionStrategy,
                                          java.util.function.Function<java.lang.String,​java.nio.ByteBuffer> compressionDictionaryGenerator,
                                          PubSubProducerAdapterFactory pubSubProducerAdapterFactory,
                                          java.util.Map<java.lang.String,​java.lang.String> additionalProperties)
      • waitForNonDeterministicPushCompletion

        public static void waitForNonDeterministicPushCompletion​(java.lang.String topicName,
                                                                 ControllerClient controllerClient,
                                                                 long timeout,
                                                                 java.util.concurrent.TimeUnit timeoutUnit)
        Wait for the push job for a store version or topic to be completed. The polling will fast fail if the push is found to be in ERROR state.
      • createTestStore

        public static Store createTestStore​(java.lang.String name,
                                            java.lang.String owner,
                                            long createdTime)
      • getParticipant

        @Deprecated
        public static SafeHelixManager getParticipant​(java.lang.String cluster,
                                                      java.lang.String nodeId,
                                                      java.lang.String zkAddress,
                                                      int httpPort,
                                                      java.lang.String stateModelDef)
        Deprecated.
        TODO: migrate to use ServiceFactory for generating a participant
      • getParticipant

        public static SafeHelixManager getParticipant​(java.lang.String cluster,
                                                      java.lang.String nodeId,
                                                      java.lang.String zkAddress,
                                                      int httpPort,
                                                      org.apache.helix.participant.statemachine.StateModelFactory<org.apache.helix.participant.statemachine.StateModel> stateModelFactory,
                                                      java.lang.String stateModelDef)
      • getOffsetRecord

        public static OffsetRecord getOffsetRecord​(long currentOffset)
      • getOffsetRecord

        public static OffsetRecord getOffsetRecord​(long currentOffset,
                                                   boolean complete)
      • getOffsetRecord

        public static OffsetRecord getOffsetRecord​(long currentOffset,
                                                   java.util.Optional<java.lang.Long> endOfPushOffset)
      • getPropertiesForControllerConfig

        public static java.util.Properties getPropertiesForControllerConfig()
      • getClusterToD2String

        public static java.lang.String getClusterToD2String​(java.util.Map<java.lang.String,​java.lang.String> clusterToD2)
      • getRandomStore

        public static Store getRandomStore()
      • assertCommand

        public static <T extends ControllerResponse> T assertCommand​(T response)
      • assertCommand

        public static <T extends ControllerResponse> T assertCommand​(T response,
                                                                     java.lang.String assertionErrorMessage)
      • preventSystemExit

        public static void preventSystemExit()
      • restoreSystemExit

        public static void restoreSystemExit()
      • createAndVerifyStoreInAllRegions

        public static void createAndVerifyStoreInAllRegions​(java.lang.String storeName,
                                                            ControllerClient parentControllerClient,
                                                            java.util.List<ControllerClient> controllerClientList)
      • verifyDCConfigNativeAndActiveRepl

        public static void verifyDCConfigNativeAndActiveRepl​(java.lang.String storeName,
                                                             boolean enabledNR,
                                                             boolean enabledAA,
                                                             ControllerClient... controllerClients)
      • verifyHybridStoreDataReplicationPolicy

        public static void verifyHybridStoreDataReplicationPolicy​(java.lang.String storeName,
                                                                  DataReplicationPolicy dataReplicationPolicy,
                                                                  ControllerClient... controllerClients)
      • generateInput

        public static java.util.Map<byte[],​byte[]> generateInput​(int recordCnt,
                                                                       boolean sorted,
                                                                       int startId,
                                                                       AvroSerializer serializer)
      • shutdownThread

        public static void shutdownThread​(java.lang.Thread thread)
                                   throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • shutdownThread

        public static void shutdownThread​(java.lang.Thread thread,
                                          long timeout,
                                          java.util.concurrent.TimeUnit unit)
                                   throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • shutdownExecutor

        public static void shutdownExecutor​(java.util.concurrent.ExecutorService executor)
                                     throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • shutdownExecutor

        public static void shutdownExecutor​(java.util.concurrent.ExecutorService executor,
                                            long timeout,
                                            java.util.concurrent.TimeUnit unit)
                                     throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • getIngestionIsolationPropertyMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getIngestionIsolationPropertyMap()
      • getUniqueTopicString

        public static java.lang.String getUniqueTopicString​(java.lang.String prefix)
      • getFreePort

        public static int getFreePort()
        WARNING: The code which generates the free port and uses it must always be called within a try/catch and a loop. There is no guarantee that the port returned will still be available at the time it is used. This is best-effort only.
        Returns:
        a free port to be used by tests.
      • mergeConfigs

        public static java.util.Map<java.lang.String,​java.lang.String> mergeConfigs​(java.util.List<java.util.Map<java.lang.String,​java.lang.String>> configMaps)
      • checkMissingFieldInAvroRecord

        public static void checkMissingFieldInAvroRecord​(org.apache.avro.generic.GenericRecord record,
                                                         java.lang.String fieldName)
      • findFoldersWithFileExtension

        public static java.util.List<java.lang.String> findFoldersWithFileExtension​(java.io.File directory,
                                                                                    java.lang.String fileExtension)
      • searchForFileExtension

        public static java.util.List<java.lang.String> searchForFileExtension​(java.io.File directory,
                                                                              java.lang.String fileExtension)
      • directoryContainsFolder

        public static boolean directoryContainsFolder​(java.lang.String directoryPath,
                                                      java.lang.String folderName)
      • deleteDirectory

        public static void deleteDirectory​(java.io.File fileToDelete)