Search in sources :

Example 6 with MockLowLevelHttpRequest

use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project copybara by google.

the class GerritApiTest method setUp.

@Before
public void setUp() throws Exception {
    OptionsBuilder options = new OptionsBuilder().setWorkdirToRealTempDir().setEnvironment(GitTestUtil.getGitEnv()).setOutputRootToTmpDir();
    credentialsFile = Files.createTempFile("credentials", "test");
    Files.write(credentialsFile, "https://user:SECRET@copybara-not-real.com".getBytes(UTF_8));
    GitRepository repo = newBareRepo(Files.createTempDirectory("test_repo"), getGitEnv(), /*verbose=*/
    true).init().withCredentialHelper("store --file=" + credentialsFile);
    httpTransport = new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            String requestString = method + " " + url;
            MockLowLevelHttpRequest request = new MockLowLevelHttpRequest();
            MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
            request.setResponse(response);
            for (Entry<Predicate<String>, byte[]> entry : requestToResponse.entrySet()) {
                if (entry.getKey().test(requestString)) {
                    byte[] content = entry.getValue();
                    assertWithMessage("'" + method + " " + url + "'").that(content).isNotNull();
                    response.setContent(content);
                    return request;
                }
            }
            response.setStatusCode(404);
            response.setContent(("NO URL MATCHED! (Returning 404) REQUEST: " + requestString));
            return request;
        }
    };
    GerritOptions gerritOptions = new GerritOptions(() -> options.general, options.git) {

        @Override
        protected HttpTransport getHttpTransport() {
            return httpTransport;
        }

        @Override
        protected GitRepository getCredentialsRepo() throws RepoException {
            return repo;
        }
    };
    gerritApi = gerritOptions.newGerritApi(getHost() + "/foo/bar/baz");
}
Also used : GitRepository(com.google.copybara.git.GitRepository) Entry(java.util.Map.Entry) 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) GerritOptions(com.google.copybara.git.GerritOptions) IOException(java.io.IOException) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Before(org.junit.Before)

Example 7 with MockLowLevelHttpRequest

use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project copybara by google.

the class GithubApiTest method getTransport.

@Override
public GitHubApiTransport getTransport() throws Exception {
    credentialsFile = Files.createTempFile("credentials", "test");
    Files.write(credentialsFile, "https://user:SECRET@github.com".getBytes(UTF_8));
    GitRepository repo = newBareRepo(Files.createTempDirectory("test_repo"), getGitEnv(), /*verbose=*/
    true).init().withCredentialHelper("store --file=" + credentialsFile);
    requestToResponse = new HashMap<>();
    requestValidators = new HashMap<>();
    httpTransport = new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            String requestString = method + " " + url;
            MockLowLevelHttpRequest request = new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    Predicate<String> validator = requestValidators.get(method + " " + url);
                    if (validator != null) {
                        assertWithMessage("Request content did not match expected values.").that(validator.test(getContentAsString())).isTrue();
                    }
                    return super.execute();
                }
            };
            byte[] content = requestToResponse.get(requestString);
            MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
            if (content == null) {
                response.setContent(String.format("{ 'message' : 'This is not the repo you are looking for! %s %s'," + " 'documentation_url' : 'http://github.com/some_url'}", method, url));
                response.setStatusCode(404);
            } else {
                response.setContent(content);
            }
            request.setResponse(response);
            return request;
        }
    };
    return new GitHubApiTransportImpl(repo, httpTransport, "some_storage_file", new TestingConsole());
}
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) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Predicate(java.util.function.Predicate) GitRepository(com.google.copybara.git.GitRepository) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse)

Example 8 with MockLowLevelHttpRequest

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

the class FirebaseChannelTest method firebaseDelete.

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

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

the class FirebaseChannelTest method firebaseGet.

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

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

the class FirebaseChannelTest method firebasePatch.

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

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