Search in sources :

Example 11 with MockLowLevelHttpRequest

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

the class FirebaseChannelTest method firebasePost.

@Test
public void firebasePost() 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.firebasePost(FIREBASE_DB_URL + "/my/path", game);
    verify(mockHttpTransport, times(1)).buildRequest("POST", 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 12 with MockLowLevelHttpRequest

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

the class MoveServletTest method doPost_myTurn_move.

@Test
public void doPost_myTurn_move() throws Exception {
    // Insert a game
    Objectify ofy = ObjectifyService.ofy();
    Game game = new Game(USER_ID, "my-opponent", "         ", true);
    ofy.save().entity(game).now();
    String gameKey = game.getId();
    when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);
    when(mockRequest.getParameter("cell")).thenReturn("1");
    // 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;
    Mockito.doReturn(null).when(servletUnderTest).getServletContext();
    servletUnderTest.doPost(mockRequest, mockResponse);
    game = ofy.load().type(Game.class).id(gameKey).safe();
    assertEquals(game.board, " X       ");
    verify(mockHttpTransport, times(2)).buildRequest(eq("PATCH"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
}
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) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 13 with MockLowLevelHttpRequest

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

the class OpenedServletTest method doPost_open.

@Test
public void doPost_open() throws Exception {
    // Insert a game
    Objectify ofy = ObjectifyService.ofy();
    Game game = new Game(USER_ID, "my-opponent", "         ", true);
    ofy.save().entity(game).now();
    String gameKey = game.getId();
    when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);
    // 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;
    Mockito.doReturn(null).when(servletUnderTest).getServletContext();
    servletUnderTest.doPost(mockRequest, mockResponse);
    verify(mockHttpTransport, times(2)).buildRequest(eq("PATCH"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
}
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) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 14 with MockLowLevelHttpRequest

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

the class TicTacToeServletTest method doGet_noGameKey.

@Test
public void doGet_noGameKey() 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;
    Mockito.doReturn(null).when(servletUnderTest).getServletContext();
    servletUnderTest.doGet(mockRequest, mockResponse);
    // Make sure the game object was created for a new game
    Objectify ofy = ObjectifyService.ofy();
    Game game = ofy.load().type(Game.class).first().safe();
    assertEquals(game.userX, USER_ID);
    verify(mockHttpTransport, times(1)).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 + game.id);
    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 15 with MockLowLevelHttpRequest

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

the class TextAppTest method setUp.

@Before
public void setUp() throws Exception {
    // Mock out the vision service for unit tests.
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpTransport transport = 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);
                    response.setContentType(Json.MEDIA_TYPE);
                    response.setContent("{\"responses\": [{\"textAnnotations\": []}]}");
                    return response;
                }
            };
        }
    };
    Vision vision = new Vision(transport, jsonFactory, null);
    appUnderTest = new TextApp(vision, null);
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) Vision(com.google.api.services.vision.v1.Vision) JsonFactory(com.google.api.client.json.JsonFactory) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Before(org.junit.Before)

Aggregations

MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)46 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)35 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)33 IOException (java.io.IOException)27 Test (org.junit.Test)25 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)23 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)18 GenericJson (com.google.api.client.json.GenericJson)10 HttpTransport (com.google.api.client.http.HttpTransport)9 JsonFactory (com.google.api.client.json.JsonFactory)8 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)8 GitApiMockHttpTransport (com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport)8 DummyRevision (com.google.copybara.testing.DummyRevision)6 Storage (com.google.api.services.storage.Storage)5 Objectify (com.googlecode.objectify.Objectify)5 HttpRequest (com.google.api.client.http.HttpRequest)4 HttpResponse (com.google.api.client.http.HttpResponse)4 Before (org.junit.Before)4 ErrorInfo (com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo)3 MockGoogleClient (com.google.api.client.googleapis.testing.services.MockGoogleClient)3