Search in sources :

Example 16 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.

the class FirebaseChannelTest method sendFirebaseMessage_create.

@Test
public void sendFirebaseMessage_create() throws Exception {
    // Mock out the firebase response. See
    // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    return response;
                }
            };
        }
    });
    FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
    firebaseChannel.sendFirebaseMessage("my_key", new Game());
    verify(mockHttpTransport, times(1)).buildRequest("PATCH", FIREBASE_DB_URL + "/channels/my_key.json");
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 17 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.

the class FirebaseChannelTest method firebasePut.

@Test
public void firebasePut() throws Exception {
    // Mock out the firebase response. See
    // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    return response;
                }
            };
        }
    });
    FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
    Game game = new Game();
    firebaseChannel.firebasePut(FIREBASE_DB_URL + "/my/path", game);
    verify(mockHttpTransport, times(1)).buildRequest("PUT", FIREBASE_DB_URL + "/my/path");
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 18 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.

the class TicTacToeServletTest method doGet_existingGame.

@Test
public void doGet_existingGame() throws Exception {
    // Mock out the firebase response. See
    // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    return response;
                }
            };
        }
    });
    FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
    // Insert a game
    Objectify ofy = ObjectifyService.ofy();
    Game game = new Game("some-other-user-id", null, "         ", true);
    ofy.save().entity(game).now();
    String gameKey = game.getId();
    when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);
    Mockito.doReturn(null).when(servletUnderTest).getServletContext();
    servletUnderTest.doGet(mockRequest, mockResponse);
    // Make sure the game object was updated with the other player
    game = ofy.load().type(Game.class).first().safe();
    assertEquals(game.userX, "some-other-user-id");
    assertEquals(game.userO, USER_ID);
    verify(mockHttpTransport, times(2)).buildRequest(eq("PATCH"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
    verify(requestDispatcher).forward(mockRequest, mockResponse);
    verify(mockRequest).setAttribute(eq("token"), anyString());
    verify(mockRequest).setAttribute("game_key", game.id);
    verify(mockRequest).setAttribute("me", USER_ID);
    verify(mockRequest).setAttribute("channel_id", USER_ID + gameKey);
    verify(mockRequest).setAttribute(eq("initial_message"), anyString());
    verify(mockRequest).setAttribute(eq("game_link"), anyString());
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) Objectify(com.googlecode.objectify.Objectify) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) Mockito.anyString(org.mockito.Mockito.anyString) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 19 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.

the class LocalUrlFetchTest method testMockUrlFetch.

@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);
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 20 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project google-api-java-client by google.

the class MockMetadataServerTransport method buildRequest.

@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
    if (url.equals(METADATA_TOKEN_SERVER_URL)) {
        MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url) {

            @Override
            public LowLevelHttpResponse execute() throws IOException {
                if (tokenRequestStatusCode != null) {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse().setStatusCode(tokenRequestStatusCode).setContent("Token Fetch Error");
                    return response;
                }
                String metadataRequestHeader = getFirstHeaderValue("Metadata-Flavor");
                if (!"Google".equals(metadataRequestHeader)) {
                    throw new IOException("Metadata request header not found.");
                }
                // Create the JSon response
                GenericJson refreshContents = new GenericJson();
                refreshContents.setFactory(JSON_FACTORY);
                refreshContents.put("access_token", accessToken);
                refreshContents.put("expires_in", 3600000);
                refreshContents.put("token_type", "Bearer");
                String refreshText = refreshContents.toPrettyString();
                MockLowLevelHttpResponse response = new MockLowLevelHttpResponse().setContentType(Json.MEDIA_TYPE).setContent(refreshText);
                return response;
            }
        };
        return request;
    } else if (url.equals(METADATA_SERVER_URL)) {
        MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url) {

            @Override
            public LowLevelHttpResponse execute() {
                MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                response.addHeader("Metadata-Flavor", "Google");
                return response;
            }
        };
        return request;
    }
    return super.buildRequest(method, url);
}
Also used : GenericJson(com.google.api.client.json.GenericJson) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest)

Aggregations

LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)24 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)21 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)20 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)18 IOException (java.io.IOException)18 Test (org.junit.Test)16 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)13 GenericJson (com.google.api.client.json.GenericJson)6 Objectify (com.googlecode.objectify.Objectify)5 JsonFactory (com.google.api.client.json.JsonFactory)3 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)3 Storage (com.google.api.services.storage.Storage)3 HttpResponse (com.google.api.client.http.HttpResponse)2 GoogleCloudStorage (com.google.cloud.hadoop.gcsio.GoogleCloudStorage)2 Mockito.anyString (org.mockito.Mockito.anyString)2 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1 JsonWebSignature (com.google.api.client.json.webtoken.JsonWebSignature)1 Get (com.google.api.services.storage.Storage.Objects.Get)1