/*
 * 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.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("host=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);
  }

  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);
  }
}

@@ -571,24 +571,72 @@
 sertEquals;%0A
+import static org.junit.Assert.assertNotEquals;%0A
 import stati
@@ -2379,32 +2379,764 @@
 y, other);%0A  %7D%0A%0A
+  @Test%0A  public void locality() %7B%0A    LocalityTier lt1 = new LocalityTier(%22node%22, %22A%22);%0A    LocalityTier lt2 = new LocalityTier(%22node%22, %22A%22);%0A    LocalityTier lt3 = new LocalityTier(%22node%22, %22B%22);%0A    LocalityTier lt4 = new LocalityTier(%22rack%22, %22A%22);%0A    LocalityTier lt5 = new LocalityTier(%22rack%22, %22B%22);%0A    LocalityTier lt6 = new LocalityTier(%22rack%22, %22B%22);%0A    LocalityTier lt7 = new LocalityTier(%22rack%22, %22%22);%0A    LocalityTier lt8 = new LocalityTier(%22node%22, %22A%22);%0A    LocalityTier lt9 = new LocalityTier(%22node%22, %22%22);%0A    assertEquals(lt1, lt1);%0A    assertEquals(lt1, lt2);%0A    assertNotEquals(lt2, lt3);%0A    assertEquals(lt5, lt6);%0A    assertNotEquals(lt4, lt5);%0A    assertNotEquals(lt6, lt7);%0A    assertNotEquals(lt8, lt9);%0A  %7D%0A%0A
   public void ch

Add missing node locality test