/*
 * Licensed to the University of California, Berkeley under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package tachyon.worker.block;

import java.net.InetSocketAddress;
import java.util.LinkedHashMap;
import java.util.Map;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import tachyon.Constants;
import tachyon.conf.TachyonConf;
import tachyon.util.NetworkUtils;

public class BlockWorkerUtilsTest {
  private static Map<String, String> sTestProperties = new LinkedHashMap<String, String>();
  private static Map<String, String> sNullMasterHostNameTestProperties =
      new LinkedHashMap<String, String>();
  private static Map<String, String> sNullMasterPortTestProperties =
      new LinkedHashMap<String, String>();
  private static Map<String, String> sNullTestProperties =
      new LinkedHashMap<String, String>();

  private TachyonConf mCustomPropsTachyonConf;
  private TachyonConf mNullMasterHostNameTachyonConf;
  private TachyonConf mNullMasterPortTachyonConf;
  private TachyonConf mNullTachyonConf;

  @BeforeClass
  public static void beforeClass() {
    // initialize the test properties.
    sTestProperties.put(Constants.MASTER_HOSTNAME, "RemoteMaster1");
    sTestProperties.put(Constants.MASTER_PORT, "10000");
    sTestProperties.put(Constants.WORKER_PORT, "10001");

    // initialize the null master host name test properties.
    sNullMasterHostNameTestProperties.put(Constants.MASTER_PORT, "20000");

    // initialize the null master port test properties.
    sNullMasterPortTestProperties.put(Constants.MASTER_HOSTNAME, "RemoteMaster3");
  }

  @Before
  public void before() {
    // init TachyonConf
    mCustomPropsTachyonConf = new TachyonConf(sTestProperties);
    mNullMasterHostNameTachyonConf = new TachyonConf(sNullMasterHostNameTestProperties);
    mNullMasterPortTachyonConf = new TachyonConf(sNullMasterPortTestProperties);
    mNullTachyonConf = new TachyonConf(sNullTestProperties);
  }

  @Test
  public void getMasterAddressTest() {
    String defaultHostname = NetworkUtils.getLocalHostName(mCustomPropsTachyonConf);
    int defaultPort = Constants.DEFAULT_MASTER_PORT;

    InetSocketAddress masterAddress = BlockWorkerUtils.getMasterAddress(mCustomPropsTachyonConf);
    Assert.assertNotNull(masterAddress);
    Assert.assertEquals(masterAddress, new InetSocketAddress("RemoteMaster1", 10000));

    masterAddress = BlockWorkerUtils.getMasterAddress(mNullMasterHostNameTachyonConf);
    Assert.assertNotNull(masterAddress);
    Assert.assertEquals(masterAddress, new InetSocketAddress(defaultHostname, 20000));

    masterAddress = BlockWorkerUtils.getMasterAddress(mNullMasterPortTachyonConf);
    Assert.assertNotNull(masterAddress);
    Assert.assertEquals(masterAddress, new InetSocketAddress("RemoteMaster3", defaultPort));

    masterAddress = BlockWorkerUtils.getMasterAddress(mNullTachyonConf);
    Assert.assertNotNull(masterAddress);
    Assert.assertEquals(masterAddress, new InetSocketAddress(defaultHostname, defaultPort));
  }

  @Test
  public void getWorkerAddressTest() {
    String defaultHostname = NetworkUtils.getLocalHostName(mCustomPropsTachyonConf);
    int defaultPort = Constants.DEFAULT_WORKER_PORT;

    InetSocketAddress workerAddress = BlockWorkerUtils.getWorkerAddress(mCustomPropsTachyonConf);
    Assert.assertNotNull(workerAddress);
    Assert.assertEquals(workerAddress, new InetSocketAddress(defaultHostname, 10001));

    workerAddress = BlockWorkerUtils.getWorkerAddress(mNullTachyonConf);
    Assert.assertNotNull(workerAddress);
    Assert.assertEquals(workerAddress, new InetSocketAddress(defaultHostname, defaultPort));
  }
}

@@ -3045,39 +3045,24 @@
 ssertEquals(
-masterAddress, 
 new InetSock
@@ -3090,24 +3090,39 @@
 er1%22, 10000)
+, masterAddress
 );%0A%0A    mast
@@ -3261,39 +3261,24 @@
 ssertEquals(
-masterAddress, 
 new InetSock
@@ -3310,16 +3310,31 @@
 , 20000)
+, masterAddress
 );%0A%0A    
@@ -3473,39 +3473,24 @@
 ssertEquals(
-masterAddress, 
 new InetSock
@@ -3528,16 +3528,31 @@
 ultPort)
+, masterAddress
 );%0A%0A    
@@ -3689,23 +3689,8 @@
 als(
-masterAddress, 
 new 
@@ -3732,24 +3732,39 @@
 defaultPort)
+, masterAddress
 );%0A  %7D%0A%0A  @T
@@ -4104,39 +4104,24 @@
 ssertEquals(
-workerAddress, 
 new InetSock
@@ -4153,16 +4153,31 @@
 , 10001)
+, workerAddress
 );%0A%0A    
@@ -4314,23 +4314,8 @@
 als(
-workerAddress, 
 new 
@@ -4361,16 +4361,31 @@
 ultPort)
+, workerAddress
 );%0A  %7D%0A%7D

fix assertEquals