/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.wire;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame;

import alluxio.network.TieredIdentityFactory;
import alluxio.util.CommonUtils;
import alluxio.wire.TieredIdentity.LocalityTier;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * Unit tests for {@link TieredIdentity}.
 */
public class TieredIdentityTest {

  @Test
  public void nearest() throws Exception {
    TieredIdentity id1 = TieredIdentityFactory.fromString("node=A,rack=rack1");
    TieredIdentity id2 = TieredIdentityFactory.fromString("node=B,rack=rack2");
    TieredIdentity id3 = TieredIdentityFactory.fromString("node=C,rack=rack2");
    List<TieredIdentity> identities = Arrays.asList(id1, id2, id3);

    assertSame(id1,
        TieredIdentityFactory.fromString("node=D,rack=rack1").nearest(identities).get());
    assertSame(id2,
        TieredIdentityFactory.fromString("node=B,rack=rack2").nearest(identities).get());
    assertSame(id3,
        TieredIdentityFactory.fromString("node=C,rack=rack2").nearest(identities).get());
    assertSame(id1,
        TieredIdentityFactory.fromString("node=D,rack=rack3").nearest(identities).get());
  }

  @Test
  public void json() throws Exception {
    TieredIdentity tieredIdentity = createRandomTieredIdentity();
    ObjectMapper mapper = new ObjectMapper();
    TieredIdentity other =
        mapper.readValue(mapper.writeValueAsBytes(tieredIdentity), TieredIdentity.class);
    checkEquality(tieredIdentity, other);
  }

  @Test
  public void thrift() {
    TieredIdentity tieredIdentity = createRandomTieredIdentity();
    TieredIdentity other = TieredIdentity.fromThrift(tieredIdentity.toThrift());
    checkEquality(tieredIdentity, other);
  }

  @Test
  public void locality() {
    LocalityTier lt1 = new LocalityTier("node", "A");
    LocalityTier lt2 = new LocalityTier("node", "A");
    LocalityTier lt3 = new LocalityTier("node", "B");
    LocalityTier lt4 = new LocalityTier("rack", "A");
    LocalityTier lt5 = new LocalityTier("rack", "B");
    LocalityTier lt6 = new LocalityTier("rack", "B");
    LocalityTier lt7 = new LocalityTier("rack", "");
    LocalityTier lt8 = new LocalityTier("node", "A");
    LocalityTier lt9 = new LocalityTier("node", "");
    assertEquals(lt1, lt1);
    assertEquals(lt1, lt2);
    assertNotEquals(lt2, lt3);
    assertEquals(lt5, lt6);
    assertNotEquals(lt4, lt5);
    assertNotEquals(lt6, lt7);
    assertNotEquals(lt8, lt9);
  }

  public void checkEquality(TieredIdentity a, TieredIdentity b) {
    assertEquals(a.getTiers(), b.getTiers());
    assertEquals(a, b);
  }

  public static TieredIdentity createRandomTieredIdentity() {
    return new TieredIdentity(
        Arrays.asList(createRandomLocalityTier(), createRandomLocalityTier()));
  }

  private static LocalityTier createRandomLocalityTier() {
    Random random = new Random();

    String tier = CommonUtils.randomAlphaNumString(random.nextInt(10) + 1);
    String value = CommonUtils.randomAlphaNumString(random.nextInt(10) + 1);
    return new LocalityTier(tier, value);
  }
}

@@ -3111,32 +3111,267 @@
 lt8, lt9);%0A  %7D%0A%0A
+  public void string() %7B%0A    TieredIdentity identity = new TieredIdentity(%0A        Arrays.asList(new LocalityTier(%22k1%22, %22v1%22), new LocalityTier(%22k2%22, %22v2%22)));%0A    assertEquals(%22TieredIdentity(k1=v1, k2=v2)%22, identity.toString());%0A  %7D%0A%0A
   public void ch

Fix test conflicts