/*
 * 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 alluxio.web;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.URL;
import java.util.Map.Entry;
import java.util.Scanner;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Multimap;

import alluxio.Configuration;
import alluxio.LocalAlluxioClusterResource;
import alluxio.master.LocalAlluxioCluster;
import alluxio.util.network.NetworkAddressUtils;
import alluxio.util.network.NetworkAddressUtils.ServiceType;

/**
 * Tests the web server is up when Alluxio starts.
 */
public class WebServerIntegrationTest {
  private Configuration mMasterConf;
  private Configuration mWorkerConf;

  // Web pages that will be verified.
  private static final Multimap<ServiceType, String> PAGES =
      new ImmutableListMultimap.Builder<ServiceType, String>()
          .putAll(ServiceType.MASTER_WEB, "/home", "/browse", "/configuration", "/workers",
              "/memory", "/browseLogs", "/metricsui")
          .putAll(ServiceType.WORKER_WEB, "/home", "/blockInfo", "/browseLogs", "/metricsui")
          .build();

  @Rule
  public LocalAlluxioClusterResource mLocalAlluxioClusterResource =
      new LocalAlluxioClusterResource();

  @Before
  public final void before() throws Exception {
    LocalAlluxioCluster localAlluxioCluster = mLocalAlluxioClusterResource.get();
    mMasterConf = localAlluxioCluster.getMasterConf();
    mWorkerConf = localAlluxioCluster.getWorkerConf();
  }

  private void verifyWebService(ServiceType serviceType, String path)
      throws IOException {
    Configuration conf =
        serviceType == ServiceType.MASTER_WEB ? mMasterConf : mWorkerConf;

    InetSocketAddress webAddr =
        NetworkAddressUtils.getConnectAddress(serviceType, conf);
    HttpURLConnection webService = (HttpURLConnection) new URL(
        "http://" + webAddr.getAddress().getHostAddress() + ":"
        + webAddr.getPort() + path).openConnection();
    webService.connect();
    Assert.assertEquals(200, webService.getResponseCode());

    Scanner pageScanner = null;
    boolean verified = false;

    try {
      pageScanner = new Scanner(webService.getInputStream());

      while (pageScanner.hasNextLine()) {
        String line = pageScanner.nextLine();
        if (line.equals("<title>Tachyon</title>") || line.equals("<title>Workers</title>")) {
          verified = true;
          break;
        }
      }
    } finally {
      if (pageScanner != null) {
        pageScanner.close();
      }
      webService.disconnect();
    }

    Assert.assertTrue(String.format("%s was started but not successfully verified.",
        serviceType.getServiceName()), verified);
  }

  /**
   * Tests whether the master and worker web homepage is up.
   */
  @Test
  public void serverUpTest() throws Exception {
    for (Entry<ServiceType, String> entry : PAGES.entries()) {
      verifyWebService(entry.getKey(), entry.getValue());
    }
  }
}

@@ -3,227 +3,35 @@
 %0A * 
-Licensed to the University of California, Berkeley under one or more contributor license%0A * agreements. See the NOTICE file distributed with this work for additional information regarding%0A * copyright ownership. The ASF
+The Alluxio Open Foundation
  lic
@@ -45,19 +45,12 @@
 his 
-file to You
+work
  und
@@ -72,17 +72,17 @@
 icense, 
-V
+v
 ersion 2
@@ -87,30 +87,30 @@
  2.0
+%0A *
  (the
-%0A * %22
+ %E2%80%9C
 License
-%22); y
+%E2%80%9D). Y
 ou m
@@ -129,12 +129,12 @@
 his 
-file
+work
  exc
@@ -171,208 +171,90 @@
 ense
-. You may obtain a%0A * copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software distributed under the License%0A *
+, which is%0A * available at www.apache.org/licenses/LICENSE-2.0%0A *%0A * This software
  is 
@@ -283,13 +283,13 @@
 IS%22 
-BASIS
+basis
 , WI
@@ -331,16 +331,19 @@
 NY KIND,
+%0A *
  either 
@@ -349,19 +349,16 @@
  express
-%0A *
  or impl
@@ -364,235 +364,341 @@
 lied
-. See the License for the specific language governing permissions and limitations under%0A * the License.%0A */%0A%0Apackage alluxio.web;%0A%0Aimport java.io.IOException;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.InetSocket
+, as more fully set forth in the License.%0A *%0A * See the NOTICE file distributed with this work for information regarding copyright ownership.%0A */%0A%0Apackage alluxio.web;%0A%0Aimport alluxio.Configuration;%0Aimport alluxio.LocalAlluxioClusterResource;%0Aimport alluxio.master.LocalAlluxioCluster;%0Aimport alluxio.util.network.Network
 Address
+Utils
 ;%0Aim
@@ -706,74 +706,60 @@
 ort 
-java.net.URL;%0Aimport java.util.Map.Entry;%0Aimport java.u
+alluxio.util.network.NetworkAddressU
 til
+s
 .S
-canner
+erviceType
 ;%0A%0Ai
@@ -965,226 +965,172 @@
 ort 
-alluxio.Configuration;%0Aimport alluxio.LocalAlluxioClusterResource;%0Aimport alluxio.master.LocalAlluxioCluster;%0Aimport alluxio.util.network.NetworkAddressUtils;%0Aimport alluxio.util.network.NetworkAddressU
+java.io.IOException;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.InetSocketAddress;%0Aimport java.net.URL;%0Aimport java.util.Map.Entry;%0Aimport java.u
 til
-s
 .S
-erviceType
+canner
 ;%0A%0A/
@@ -2923,15 +2923,15 @@
 tle%3E
-Tachyon
+Alluxio
 %3C/ti

fix checkstyle and license check