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

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

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

import tachyon.LocalTachyonClusterResource;
import tachyon.conf.TachyonConf;
import tachyon.master.LocalTachyonCluster;
import tachyon.util.network.NetworkAddressUtils;
import tachyon.util.network.NetworkAddressUtils.ServiceType;

/**
 * Tests the web server is up when Tachyon starts.
 */
public class WebServerIntegrationTest {
  private TachyonConf mMasterTachyonConf;
  private TachyonConf mWorkerTachyonConf;

  @Rule
  public LocalTachyonClusterResource mLocalTachyonClusterResource =
      new LocalTachyonClusterResource();

  @Before
  public final void before() throws Exception {
    LocalTachyonCluster localTachyonCluster = mLocalTachyonClusterResource.get();
    mMasterTachyonConf = localTachyonCluster.getMasterTachyonConf();
    mWorkerTachyonConf = localTachyonCluster.getWorkerTachyonConf();
  }

  private void verifyWebService(ServiceType serviceType, TachyonConf conf) throws IOException {
    InetSocketAddress webAddr =
        NetworkAddressUtils.getConnectAddress(serviceType, conf);
    HttpURLConnection webService = (HttpURLConnection) new URL(
        "http://" + webAddr.getAddress().getHostAddress() + ":"
        + webAddr.getPort() + "/home").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>")) {
          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 {
    verifyWebService(ServiceType.MASTER_WEB, mMasterTachyonConf);
    verifyWebService(ServiceType.WORKER_WEB, mWorkerTachyonConf);
  }
}

@@ -927,16 +927,44 @@
 et.URL;%0A
+import java.util.Map.Entry;%0A
 import j
@@ -1079,16 +1079,116 @@
 .Test;%0A%0A
+import com.google.common.collect.ImmutableListMultimap;%0Aimport com.google.common.collect.Multimap;%0A%0A
 import t
@@ -1594,16 +1594,439 @@
 nConf;%0A%0A
+  // Web pages that will be verified.%0A  private static final Multimap%3CServiceType, String%3E PAGES =%0A      new ImmutableListMultimap.Builder%3CServiceType, String%3E()%0A          .putAll(ServiceType.MASTER_WEB, %22/home%22, %22/browse%22, %22/configuration%22, %22/workers%22,%0A              %22/memory%22, %22/browseLogs%22, %22/metricsui%22)%0A          .putAll(ServiceType.WORKER_WEB, %22/home%22, %22/blockInfo%22, %22/browseLogs%22, %22/metricsui%22)%0A          .build();%0A%0A
   @Rule%0A
@@ -2479,46 +2479,160 @@
 pe, 
-TachyonConf conf) throws IOException %7B
+String path)%0A      throws IOException %7B%0A    TachyonConf conf =%0A        serviceType == ServiceType.MASTER_WEB ? mMasterTachyonConf : mWorkerTachyonConf;%0A
 %0A   
@@ -2884,23 +2884,20 @@
 ort() + 
-%22/home%22
+path
 ).openCo
@@ -2930,24 +2930,265 @@
 .connect();%0A
+    if (webService.getResponseCode() == 500) %7B%0A      System.out.println(path);%0A      System.out.println(webAddr);%0A      try %7B%0A        Thread.sleep(1000000);%0A      %7D catch (InterruptedException e) %7B%0A        e.printStackTrace();%0A      %7D%0A    %7D%0A
     Assert.a
@@ -3509,16 +3509,57 @@
 title%3E%22)
+ %7C%7C line.equals(%22%3Ctitle%3EWorkers%3C/title%3E%22)
 ) %7B%0A    
@@ -4017,135 +4017,130 @@
     
-verifyWebService(ServiceType.MASTER_WEB, mMasterTachyonConf);%0A    verifyWebService(ServiceType.WORKER_WEB, mWorkerTachyonConf);
+for (Entry%3CServiceType, String%3E entry : PAGES.entries()) %7B%0A      verifyWebService(entry.getKey(), entry.getValue());%0A    %7D
 %0A  %7D

make the test cover all pages