Search in sources :

Example 1 with MockRequestAssertion

use of com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion in project copybara by google.

the class GerritDestinationTest method gerritSubmit_noChange.

@Test
public void gerritSubmit_noChange() throws Exception {
    options.gerrit.gerritChangeId = null;
    fetch = "master";
    writeFile(workdir, "file", "some content");
    url = BASE_URL + "/foo/bar";
    repoGitDir = gitUtil.mockRemoteRepo("user:SECRET@copybara-not-real.com/foo/bar").getGitDir();
    gitUtil.mockApi(eq("GET"), startsWith(BASE_URL + "/changes/"), mockResponse("[]"));
    AtomicBoolean submitCalled = new AtomicBoolean(false);
    AtomicBoolean reviewCalled = new AtomicBoolean(false);
    gitUtil.mockApi(eq("POST"), matches(BASE_URL + "/changes/.*/revisions/.*/review"), mockResponseWithStatus("", 204, new MockRequestAssertion("Always true with side-effect", s -> {
        reviewCalled.set(true);
        return true;
    })));
    gitUtil.mockApi(eq("POST"), matches(BASE_URL + "/changes/.*/submit"), mockResponseWithStatus("", 204, new MockRequestAssertion("Always true with side-effect", s -> {
        submitCalled.set(true);
        return true;
    })));
    options.setForce(true);
    DummyRevision originRef = new DummyRevision("origin_ref");
    GerritDestination destination = destination("submit = True", "gerrit_submit = True");
    Glob glob = Glob.createGlob(ImmutableList.of("**"), excludedDestinationPaths);
    WriterContext writerContext = new WriterContext("GerritDestinationTest", "test", false, originRef, Glob.ALL_FILES.roots());
    List<DestinationEffect> result = destination.newWriter(writerContext).write(TransformResults.of(workdir, originRef).withSummary("Test message").withIdentity(originRef.asString()), glob, console);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getErrors()).isEmpty();
    assertThat(submitCalled.get()).isFalse();
    assertThat(reviewCalled.get()).isFalse();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WriterContext(com.google.copybara.WriterContext) MockRequestAssertion(com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion) DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision) Glob(com.google.copybara.util.Glob) Test(org.junit.Test)

Example 2 with MockRequestAssertion

use of com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion in project copybara by google.

the class GerritDestinationTest method gerritSubmit_success.

@Test
public void gerritSubmit_success() throws Exception {
    options.gerrit.gerritChangeId = null;
    fetch = "master";
    writeFile(workdir, "file", "some content");
    url = BASE_URL + "/foo/bar";
    repoGitDir = gitUtil.mockRemoteRepo("user:SECRET@copybara-not-real.com/foo/bar").getGitDir();
    gitUtil.mockApi(eq("GET"), startsWith(BASE_URL + "/changes/"), mockResponse("[" + "{" + "  change_id : \"Iaaaaaaaaaabbbbbbbbbbccccccccccdddddddddd\"," + "  status : \"NEW\"" + "}]"));
    AtomicBoolean submitCalled = new AtomicBoolean(false);
    AtomicBoolean reviewCalled = new AtomicBoolean(false);
    gitUtil.mockApi(eq("POST"), matches(BASE_URL + "/changes/.*/revisions/.*/review"), mockResponseAndValidateRequest("{\"labels\": { \"Code-Review\": 2}}", new MockRequestAssertion("Always true with side-effect", s -> {
        reviewCalled.set(true);
        return true;
    })));
    gitUtil.mockApi(eq("POST"), matches(BASE_URL + "/changes/.*/submit"), mockResponseAndValidateRequest("{" + "  change_id : \"Iaaaaaaaaaabbbbbbbbbbccccccccccdddddddddd\"," + "  status : \"submitted\"" + "}", new MockRequestAssertion("Always true with side-effect", s -> {
        submitCalled.set(true);
        return true;
    })));
    options.setForce(true);
    DummyRevision originRef = new DummyRevision("origin_ref");
    GerritDestination destination = destination("submit = True", "gerrit_submit = True");
    Glob glob = Glob.createGlob(ImmutableList.of("**"), excludedDestinationPaths);
    WriterContext writerContext = new WriterContext("GerritDestinationTest", "test", false, originRef, Glob.ALL_FILES.roots());
    List<DestinationEffect> result = destination.newWriter(writerContext).write(TransformResults.of(workdir, originRef).withSummary("Test message").withIdentity(originRef.asString()), glob, console);
    assertThat(reviewCalled.get()).isTrue();
    assertThat(submitCalled.get()).isTrue();
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getErrors()).isEmpty();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WriterContext(com.google.copybara.WriterContext) MockRequestAssertion(com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion) DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision) Glob(com.google.copybara.util.Glob) Test(org.junit.Test)

Example 3 with MockRequestAssertion

use of com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion in project copybara by google.

the class GerritDestinationTest method gerritSubmit_alreadySubmittable.

@Test
public void gerritSubmit_alreadySubmittable() throws Exception {
    options.gerrit.gerritChangeId = null;
    fetch = "master";
    writeFile(workdir, "file", "some content");
    url = BASE_URL + "/foo/bar";
    repoGitDir = gitUtil.mockRemoteRepo("user:SECRET@copybara-not-real.com/foo/bar").getGitDir();
    gitUtil.mockApi(eq("GET"), startsWith(BASE_URL + "/changes/"), mockResponse("[" + "{" + "  change_id : \"Iaaaaaaaaaabbbbbbbbbbccccccccccdddddddddd\"," + "  status : \"NEW\"," + "  submittable : true" + "}]"));
    AtomicBoolean submitCalled = new AtomicBoolean(false);
    gitUtil.mockApi(eq("POST"), matches(BASE_URL + "/changes/.*/revisions/.*/review"), mockResponseWithStatus("Change should not be voted on", 500));
    gitUtil.mockApi(eq("POST"), matches(BASE_URL + "/changes/.*/submit"), mockResponseAndValidateRequest("{" + "  change_id : \"Iaaaaaaaaaabbbbbbbbbbccccccccccdddddddddd\"," + "  status : \"submitted\"" + "}", new MockRequestAssertion("Always true with side-effect", s -> {
        submitCalled.set(true);
        return true;
    })));
    options.setForce(true);
    DummyRevision originRef = new DummyRevision("origin_ref");
    GerritDestination destination = destination("submit = True", "gerrit_submit = True");
    Glob glob = Glob.createGlob(ImmutableList.of("**"), excludedDestinationPaths);
    WriterContext writerContext = new WriterContext("GerritDestinationTest", "test", false, originRef, Glob.ALL_FILES.roots());
    List<DestinationEffect> result = destination.newWriter(writerContext).write(TransformResults.of(workdir, originRef).withSummary("Test message").withIdentity(originRef.asString()), glob, console);
    assertThat(submitCalled.get()).isTrue();
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getErrors()).isEmpty();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WriterContext(com.google.copybara.WriterContext) MockRequestAssertion(com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion) DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision) Glob(com.google.copybara.util.Glob) Test(org.junit.Test)

Example 4 with MockRequestAssertion

use of com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion in project copybara by google.

the class GerritEndpointTest method deleteVote.

@Test
public void deleteVote() throws Exception {
    AtomicBoolean called = new AtomicBoolean(false);
    gitUtil.mockApi(eq("POST"), startsWith(BASE_URL + "/changes/12345/reviewers/me/votes/Code-Review/delete"), mockResponseWithStatus("", 204, new MockRequestAssertion("Always true with side-effect", s -> {
        called.set(true);
        return true;
    })));
    runFeedback(ImmutableList.<String>builder().add("ctx.destination" + ".delete_vote('12345', 'me', 'Code-Review')").build());
    assertThat(called.get()).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockRequestAssertion(com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion) Test(org.junit.Test)

Example 5 with MockRequestAssertion

use of com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion in project copybara by google.

the class GitHubEndpointTest method testFeedbackCreateStatus.

/**
 * A test that uses feedback.
 *
 * <p>Does not verify all the fields, see {@link #testCreateStatusExhaustive()} for that.
 */
@Test
public void testFeedbackCreateStatus() throws Exception {
    dummyTrigger.addAll("Foo", "Bar");
    Feedback feedback = feedback("" + "def test_action(ctx):\n" + "    ref = 'None'\n" + "    if len(ctx.refs) > 0:\n" + "      ref = ctx.refs[0]\n" + "    \n" + "    for m in ctx.origin.get_messages:\n" + "      status = ctx.destination.create_status(\n" + "        sha = ref,\n" + "        state = 'success',\n" + "        context = 'test',\n" + "        description = 'Observed ' + m,\n" + "      )\n" + "      ctx.console.info('Created status')\n" + "    return ctx.success()\n" + "\n");
    Iterator<String> createValues = ImmutableList.of("Observed Foo", "Observed Bar").iterator();
    gitUtil.mockApi(eq("POST"), contains("/status"), mockResponseAndValidateRequest("{\n" + "    state : 'success',\n" + "    target_url : 'https://github.com/google/example',\n" + "    description : 'Observed foo',\n" + "    context : 'test'\n" + "}", new MockRequestAssertion(String.format("Requests were expected to cycle through the values of %s", createValues), r -> r.contains(createValues.next()))));
    feedback.run(workdir, ImmutableList.of("e597746de9c1704e648ddc3ffa0d2096b146d600"));
    console.assertThat().timesInLog(2, MessageType.INFO, "Created status");
    verify(gitUtil.httpTransport(), times(2)).buildRequest(eq("POST"), contains("/status"));
}
Also used : MockRequestAssertion(com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion) Feedback(com.google.copybara.feedback.Feedback) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

MockRequestAssertion (com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion)8 Test (org.junit.Test)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 WriterContext (com.google.copybara.WriterContext)4 DummyRevision (com.google.copybara.testing.DummyRevision)4 Glob (com.google.copybara.util.Glob)4 DestinationEffect (com.google.copybara.DestinationEffect)3 ValidationException (com.google.copybara.exception.ValidationException)2 Feedback (com.google.copybara.feedback.Feedback)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1