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      .setRemoteWALDir(Long.toHexString(RNG.nextLong())).setNamespaces(randNamespaces(RNG))
068      .setExcludeNamespaces(randNamespaces(RNG)).setTableCFsMap(randTableCFs(RNG))
069      .setExcludeTableCFsMap(randTableCFs(RNG)).setReplicateAllUserTables(RNG.nextBoolean())
070      .setBandwidth(RNG.nextInt(1000)).build();
071  }
072
073  private static void assertSetEquals(Set<String> expected, Set<String> actual) {
074    if (expected == null || expected.size() == 0) {
075      assertTrue(actual == null || actual.size() == 0);
076      return;
077    }
078    assertEquals(expected.size(), actual.size());
079    expected.forEach(s -> assertTrue(actual.contains(s)));
080  }
081
082  private static void assertMapEquals(Map<TableName, List<String>> expected,
083    Map<TableName, List<String>> actual) {
084    if (expected == null || expected.size() == 0) {
085      assertTrue(actual == null || actual.size() == 0);
086      return;
087    }
088    assertEquals(expected.size(), actual.size());
089    expected.forEach((expectedTn, expectedCFs) -> {
090      List<String> actualCFs = actual.get(expectedTn);
091      if (expectedCFs == null || expectedCFs.size() == 0) {
092        assertTrue(actual.containsKey(expectedTn));
093        assertTrue(actualCFs == null || actualCFs.size() == 0);
094      } else {
095        assertNotNull(actualCFs);
096        assertEquals(expectedCFs.size(), actualCFs.size());
097        for (Iterator<String> expectedIt = expectedCFs.iterator(),
098            actualIt = actualCFs.iterator(); expectedIt.hasNext();) {
099          assertEquals(expectedIt.next(), actualIt.next());
100        }
101      }
102    });
103  }
104
105  public static void assertConfigEquals(ReplicationPeerConfig expected,
106    ReplicationPeerConfig actual) {
107    assertEquals(expected.getClusterKey(), actual.getClusterKey());
108    assertEquals(expected.getReplicationEndpointImpl(), actual.getReplicationEndpointImpl());
109    assertSetEquals(expected.getNamespaces(), actual.getNamespaces());
110    assertSetEquals(expected.getExcludeNamespaces(), actual.getExcludeNamespaces());
111    assertMapEquals(expected.getTableCFsMap(), actual.getTableCFsMap());
112    assertMapEquals(expected.getExcludeTableCFsMap(), actual.getExcludeTableCFsMap());
113    assertEquals(expected.replicateAllUserTables(), actual.replicateAllUserTables());
114    assertEquals(expected.getBandwidth(), actual.getBandwidth());
115  }
116}