/*
 * Copyright 2013 Google Inc. All Rights Reserved.
 * Licensed 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 com.google.appengine.tck.urlfetch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.jboss.arquillian.junit.Arquillian;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

/**
 * HttpURLConnection tests
 */
@RunWith(Arquillian.class)
public class HttpURLConnectionTest extends URLFetchTestBase {

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void fetchExistingPage() throws Exception {
        String content = fetchUrl("http://www.google.org/", 200);
    }

    @Test
    public void fetchNonExistentPage() throws Exception {
        String content = fetchUrl("http://www.google.com/404", 404);
    }

    @Test
    public void fetchNonExistentSite() throws Exception {
        thrown.expect(IOException.class);
        String content = fetchUrl("http://i.do.not.exist/", 503);
    }

    protected String fetchUrl(String url, int expectedResponseCode) throws IOException {
        URLConnection conn = new URL(url).openConnection();
        Assert.assertTrue("URLConenction is not HttpURLConnection: " + conn, conn instanceof HttpURLConnection);
        HttpURLConnection connection = (HttpURLConnection) conn;
        connection.connect();
        try {
            int responseCode = connection.getResponseCode();
            assertEquals(url, expectedResponseCode, responseCode);
            return getContent(conn);
        } finally {
            connection.disconnect();
        }
    }

    private String getContent(URLConnection connection) throws IOException {
        InputStream stream = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        try {
            String content = "";
            String line;
            while ((line = reader.readLine()) != null) {
                content += line;
            }
            return content;
        } finally {
            reader.close();
        }
    }
}

@@ -950,77 +950,12 @@
 nit.
-Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.ExpectedException
+Test
 ;%0Aim
@@ -1160,81 +1160,45 @@
 e %7B%0A
-%0A    @Rule%0A    public ExpectedException thrown = ExpectedException.none()
+    private static final int OK = 200
 ;%0A%0A 
@@ -1268,33 +1268,16 @@
 %0A       
- String content =
  fetchUr
@@ -1304,19 +1304,18 @@
 .org/%22, 
-200
+OK
 );%0A    %7D
@@ -1391,33 +1391,16 @@
 %0A       
- String content =
  fetchUr
@@ -1444,32 +1444,62 @@
     %7D%0A%0A    @Test
+(expected = IOException.class)
 %0A    public void
@@ -1552,67 +1552,8 @@
     
- thrown.expect(IOException.class);%0A        String content =
  fet
@@ -2114,16 +2114,39 @@
   return
+ (responseCode == OK) ?
  getCont
@@ -2154,16 +2154,23 @@
 nt(conn)
+ : null
 ;%0A      
@@ -2380,16 +2380,21 @@
         
+try (
 Buffered
@@ -2458,29 +2458,17 @@
 stream))
-;%0A        try
+)
  %7B%0A     
@@ -2656,56 +2656,8 @@
 nt;%0A
-        %7D finally %7B%0A            reader.close();%0A
     

Handle non-200 diff.