/*
 * Copyright 2016 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.samples;

import static org.junit.Assert.assertEquals;

import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalURLFetchServiceTestConfig;
import com.google.api.client.http.LowLevelHttpRequest;
import com.google.api.client.http.LowLevelHttpResponse;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
import com.google.api.client.testing.http.MockLowLevelHttpResponse;

import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpResponse;

public class LocalUrlFetchTest {
  private final LocalServiceTestHelper helper =
      new LocalServiceTestHelper(new LocalURLFetchServiceTestConfig());

  @Before
  public void setUp() {
    helper.setUp();
  }

  @After
  public void tearDown() {
    helper.tearDown();
  }

  @Test
  public void testMockUrlFetch() throws IOException {
    // See http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = new MockHttpTransport() {
      @Override
      public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
        assertEquals(method, "GET");
        assertEquals(url, "http://foo.bar");

        return new MockLowLevelHttpRequest() {
          @Override
          public LowLevelHttpResponse execute() throws IOException {
            MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
            response.setStatusCode(234);
            return response;
          }
        };
      }
    };

    HttpRequestFactory requestFactory = mockHttpTransport.createRequestFactory();
    HttpResponse response = requestFactory.buildGetRequest(new GenericUrl("http://foo.bar"))
        .execute();
    assertEquals(response.getStatusCode(), 234);
  }
}

@@ -715,64 +715,32 @@
 e.ap
-pengine.tools.development.testing.LocalS
+i.client.http.Gen
 er
-v
 ic
-eTestHelper
+Url
 ;%0Aim
@@ -761,72 +761,88 @@
 e.ap
-pengine.tools.development.testing.LocalURLFetchServiceTestConfig
+i.client.http.HttpRequestFactory;%0Aimport com.google.api.client.http.HttpResponse
 ;%0Aim
@@ -1150,256 +1150,272 @@
 se;%0A
-%0A
 import 
-java.io.IOException;%0A%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport com.google.api.client.http.HttpRequestFactory;%0Aimport com.google.api.client.http.GenericUrl;%0Aimport com.google.api.client.http.HttpResponse
+com.google.appengine.tools.development.testing.LocalServiceTestHelper;%0Aimport com.google.appengine.tools.development.testing.LocalURLFetchServiceTestConfig;%0A%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0A%0Aimport java.io.IOException
 ;%0A%0Ap

Fix style errors.