summaryrefslogtreecommitdiffstats
path: root/javatests/com/google/gerrit/acceptance/rest/binding/PluginProvidedChildRestApiBindingsIT.java
blob: f211366e4492ee824660d8d9a1ad275b1fd9d758 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (C) 2019 The Android Open Source Project
//
// 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.gerrit.acceptance.rest.binding;

import static com.google.gerrit.server.change.RevisionResource.REVISION_KIND;
import static com.google.gerrit.server.change.RobotCommentResource.ROBOT_COMMENT_KIND;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.rest.util.RestApiCallHelper;
import com.google.gerrit.acceptance.rest.util.RestCall;
import com.google.gerrit.entities.Change;
import com.google.gerrit.entities.PatchSet;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.restapi.ChildCollection;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.RestApiModule;
import com.google.gerrit.extensions.restapi.RestCollectionModifyView;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.extensions.restapi.RestResource;
import com.google.gerrit.extensions.restapi.RestView;
import com.google.gerrit.server.change.RevisionResource;
import com.google.gerrit.server.change.RobotCommentResource;
import com.google.gerrit.testing.TestCommentHelper;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import org.junit.Test;

/**
 * Tests for checking plugin-provided REST API bindings nested under a core collection.
 *
 * <p>These tests only verify that the plugin-provided REST endpoints are correctly bound, they do
 * not test the functionality of the plugin REST endpoints.
 */
public class PluginProvidedChildRestApiBindingsIT extends AbstractDaemonTest {
  @Inject private TestCommentHelper testCommentHelper;

  /** Resource to bind a child collection. */
  public static final TypeLiteral<RestView<TestPluginResource>> TEST_KIND = new TypeLiteral<>() {};

  private static final String PLUGIN_NAME = "my-plugin";

  private static final ImmutableSet<RestCall> REVISION_TEST_CALLS =
      ImmutableSet.of(
          // Calls that have the plugin name as part of the collection name
          RestCall.get("/changes/%s/revisions/%s/" + PLUGIN_NAME + "~test-collection/"),
          RestCall.get("/changes/%s/revisions/%s/" + PLUGIN_NAME + "~test-collection/1/detail"),
          RestCall.post("/changes/%s/revisions/%s/" + PLUGIN_NAME + "~test-collection/"),
          RestCall.post("/changes/%s/revisions/%s/" + PLUGIN_NAME + "~test-collection/1/update"),
          // Same tests but without the plugin name as part of the collection name. This works as
          // long as there is no core collection with the same name (which takes precedence) and no
          // other plugin binds a collection with the same name. We highly encourage plugin authors
          // to use the fully qualified collection name instead.
          RestCall.get("/changes/%s/revisions/%s/test-collection/"),
          RestCall.get("/changes/%s/revisions/%s/test-collection/1/detail"),
          RestCall.post("/changes/%s/revisions/%s/test-collection/"),
          RestCall.post("/changes/%s/revisions/%s/test-collection/1/update"));

  private static final ImmutableSet<RestCall> ROBOTCOMMENT_TEST_CALLS =
      ImmutableSet.of(RestCall.delete("/changes/%s/revisions/%s/robotcomments/%s"));

  /**
   * Module for all sys bindings.
   *
   * <p>TODO: This should actually just move into MyPluginHttpModule. However, that doesn't work
   * currently. This TODO is for fixing this bug.
   */
  static class MyPluginSysModule extends AbstractModule {
    @Override
    public void configure() {
      install(
          new RestApiModule() {
            @Override
            public void configure() {
              DynamicMap.mapOf(binder(), TEST_KIND);
              child(REVISION_KIND, "test-collection").to(TestChildCollection.class);

              postOnCollection(TEST_KIND).to(TestPostOnCollection.class);
              post(TEST_KIND, "update").to(TestPost.class);
              get(TEST_KIND, "detail").to(TestGet.class);
              delete(ROBOT_COMMENT_KIND).to(TestDelete.class);
            }
          });
    }
  }

  static class TestPluginResource implements RestResource {}

  @Singleton
  static class TestChildCollection
      implements ChildCollection<RevisionResource, TestPluginResource> {
    private final DynamicMap<RestView<TestPluginResource>> views;

    @Inject
    TestChildCollection(DynamicMap<RestView<TestPluginResource>> views) {
      this.views = views;
    }

    @Override
    public RestView<RevisionResource> list() throws RestApiException {
      return (RestReadView<RevisionResource>)
          resource -> Response.ok(ImmutableList.of("one", "two"));
    }

    @Override
    public TestPluginResource parse(RevisionResource parent, IdString id) throws Exception {
      return new TestPluginResource();
    }

    @Override
    public DynamicMap<RestView<TestPluginResource>> views() {
      return views;
    }
  }

  @Singleton
  static class TestPostOnCollection
      implements RestCollectionModifyView<RevisionResource, TestPluginResource, String> {
    @Override
    public Response<String> apply(RevisionResource parentResource, String input) throws Exception {
      return Response.ok("test");
    }
  }

  @Singleton
  static class TestPost implements RestModifyView<TestPluginResource, String> {
    @Override
    public Response<String> apply(TestPluginResource resource, String input) throws Exception {
      return Response.ok("test");
    }
  }

  @Singleton
  static class TestGet implements RestReadView<TestPluginResource> {
    @Override
    public Response<String> apply(TestPluginResource resource) throws Exception {
      return Response.ok("test");
    }
  }

  @Singleton
  static class TestDelete implements RestModifyView<RobotCommentResource, String> {
    @Override
    public Response<?> apply(RobotCommentResource resource, String input) throws Exception {
      return Response.none();
    }
  }

  @Test
  public void testRevisionEndpoints() throws Exception {
    PatchSet.Id patchSetId = createChange().getPatchSetId();
    try (AutoCloseable ignored = installPlugin(PLUGIN_NAME, MyPluginSysModule.class)) {
      RestApiCallHelper.execute(
          adminRestSession,
          REVISION_TEST_CALLS.asList(),
          String.valueOf(patchSetId.changeId().get()),
          String.valueOf(patchSetId.get()));
    }
  }

  @Test
  public void testRobotCommentEndpoints() throws Exception {
    PatchSet.Id patchSetId = createChange().getPatchSetId();
    String robotCommentUuid = createRobotComment(patchSetId.changeId());
    try (AutoCloseable ignored = installPlugin(PLUGIN_NAME, MyPluginSysModule.class)) {
      RestApiCallHelper.execute(
          adminRestSession,
          ROBOTCOMMENT_TEST_CALLS.asList(),
          String.valueOf(patchSetId.changeId().get()),
          String.valueOf(patchSetId.get()),
          robotCommentUuid);
    }
  }

  private String createRobotComment(Change.Id changeId) throws Exception {
    testCommentHelper.addRobotComment(
        changeId, TestCommentHelper.createRobotCommentInput(PushOneCommit.FILE_NAME));
    return Iterables.getOnlyElement(
            Iterables.getOnlyElement(
                gApi.changes().id(changeId.get()).current().robotComments().values()))
        .id;
  }
}