001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.replication;
019
020import static java.util.stream.Collectors.toList;
021import static java.util.stream.Collectors.toSet;
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertNotNull;
024import static org.junit.Assert.assertTrue;
025
026import java.util.HashMap;
027import java.util.Iterator;
028import java.util.List;
029import java.util.Map;
030import java.util.Random;
031import java.util.Set;
032import java.util.stream.Stream;
033import org.apache.hadoop.hbase.TableName;
034
035/**
036 * A helper tool for generating random {@link ReplicationPeerConfig} and do assertion.
037 */
038public final class ReplicationPeerConfigTestUtil {
039
040  // Seed may be set with Random#setSeed
041  private static final Random RNG = new Random();
042
043  private ReplicationPeerConfigTestUtil() {
044  }
045
046  private static Set<String> randNamespaces(Random rand) {
047    return Stream.generate(() -> Long.toHexString(rand.nextLong())).limit(rand.nextInt(5))
048      .collect(toSet());
049  }
050
051  private static Map<TableName, List<String>> randTableCFs(Random rand) {
052    int size = rand.nextInt(5);
053    Map<TableName, List<String>> map = new HashMap<>();
054    for (int i = 0; i < size; i++) {
055      TableName tn = TableName.valueOf(Long.toHexString(rand.nextLong()));
056      List<String> cfs = Stream.generate(() -> Long.toHexString(rand.nextLong()))
057        .limit(rand.nextInt(5)).collect(toList());
058      map.put(tn, cfs);
059    }
060    return map;
061  }
062
063  public static ReplicationPeerConfig getConfig(int seed) {
064    RNG.setSeed(seed);
065    return ReplicationPeerConfig.newBuilder().setClusterKey(Long.toHexString(RNG.nextLong()))
066      .setReplicationEndpointImpl(Long.toHexString(RNG.nextLong()))
067      .setNamespaces(randNamespaces(RNG)).setExcludeNamespaces(randNamespaces(RNG))
068      .setTableCFsMap(randTableCFs(RNG)).setExcludeTableCFsMap(randTableCFs(RNG))
069      .setReplicateAllUserTables(RNG.nextBoolean()).setBandwidth(RNG.nextInt(1000)).build();
070  }
071
072  private static void assertSetEquals(Set<String> expected, Set<String> actual) {
073    if (expected == null || expected.size() == 0) {
074      assertTrue(actual == null || actual.size() == 0);
075      return;
076    }
077    assertEquals(expected.size(), actual.size());
078    expected.forEach(s -> assertTrue(actual.contains(s)));
079  }
080
081  private static void assertMapEquals(Map<TableName, List<String>> expected,
082    Map<TableName, List<String>> actual) {
083    if (expected == null || expected.size() == 0) {
084      assertTrue(actual == null || actual.size() == 0);
085      return;
086    }
087    assertEquals(expected.size(), actual.size());
088    expected.forEach((expectedTn, expectedCFs) -> {
089      List<String> actualCFs = actual.get(expectedTn);
090      if (expectedCFs == null || expectedCFs.size() == 0) {
091        assertTrue(actual.containsKey(expectedTn));
092        assertTrue(actualCFs == null || actualCFs.size() == 0);
093      } else {
094        assertNotNull(actualCFs);
095        assertEquals(expectedCFs.size(), actualCFs.size());
096        for (Iterator<String> expectedIt = expectedCFs.iterator(),
097            actualIt = actualCFs.iterator(); expectedIt.hasNext();) {
098          assertEquals(expectedIt.next(), actualIt.next());
099        }
100      }
101    });
102  }
103
104  public static void assertConfigEquals(ReplicationPeerConfig expected,
105    ReplicationPeerConfig actual) {
106    assertEquals(expected.getClusterKey(), actual.getClusterKey());
107    assertEquals(expected.getReplicationEndpointImpl(), actual.getReplicationEndpointImpl());
108    assertSetEquals(expected.getNamespaces(), actual.getNamespaces());
109    assertSetEquals(expected.getExcludeNamespaces(), actual.getExcludeNamespaces());
110    assertMapEquals(expected.getTableCFsMap(), actual.getTableCFsMap());
111    assertMapEquals(expected.getExcludeTableCFsMap(), actual.getExcludeTableCFsMap());
112    assertEquals(expected.replicateAllUserTables(), actual.replicateAllUserTables());
113    assertEquals(expected.getBandwidth(), actual.getBandwidth());
114  }
115}