public class ListActionTest {
- private static final long NOW = 1_500_000_000L;
- private static final long BEFORE = NOW - 1_000L;
+ private static final long NOW = 1_500_000_000L;
+ private static final long BEFORE = NOW - 1_000L;
- @Rule
- public ExpectedException expectedException = none();
+ @Rule
+ public ExpectedException expectedException = none();
- @Rule
- public UserSessionRule userSession = standalone();
+ @Rule
+ public UserSessionRule userSession = standalone();
- @Rule
- public DbTester db = create();
+ @Rule
+ public DbTester db = create();
- private DbClient dbClient = db.getDbClient();
- private DefaultOrganizationProvider defaultOrganizationProvider = from(db);
- private WebhookSupport webhookSupport = new WebhookSupport(userSession);
- private ListAction underTest = new ListAction(dbClient, userSession, defaultOrganizationProvider, webhookSupport);
+ private DbClient dbClient = db.getDbClient();
+ private DefaultOrganizationProvider defaultOrganizationProvider = from(db);
+ private WebhookSupport webhookSupport = new WebhookSupport(userSession);
+ private ListAction underTest = new ListAction(dbClient, userSession, defaultOrganizationProvider, webhookSupport);
- private ComponentDbTester componentDbTester = db.components();
- private WebhookDbTester webhookDbTester = db.webhooks();
- private WebhookDeliveryDbTester webhookDeliveryDbTester = db.webhookDelivery();
- private OrganizationDbTester organizationDbTester = db.organizations();
- private WsActionTester wsActionTester = new WsActionTester(underTest);
+ private ComponentDbTester componentDbTester = db.components();
+ private WebhookDbTester webhookDbTester = db.webhooks();
+ private WebhookDeliveryDbTester webhookDeliveryDbTester = db.webhookDelivery();
+ private OrganizationDbTester organizationDbTester = db.organizations();
+ private WsActionTester wsActionTester = new WsActionTester(underTest);
- @Test
- public void definition() {
+ @Test
+ public void definition() {
+ WebService.Action action = wsActionTester.getDef();
- WebService.Action action = wsActionTester.getDef();
+ assertThat(action).isNotNull();
+ assertThat(action.isInternal()).isFalse();
+ assertThat(action.isPost()).isFalse();
+ assertThat(action.responseExampleAsString()).isNotEmpty();
+ assertThat(action.params())
+ .extracting(Param::key, Param::isRequired)
+ .containsExactlyInAnyOrder(
+ tuple("organization", false),
+ tuple("project", false));
+
+ }
- assertThat(action).isNotNull();
- assertThat(action.isInternal()).isFalse();
- assertThat(action.isPost()).isFalse();
- assertThat(action.responseExampleAsString()).isNotEmpty();
- assertThat(action.params())
- .extracting(Param::key, Param::isRequired)
- .containsExactlyInAnyOrder(
- tuple("organization", false),
- tuple("project", false));
+ @Test
+ public void list_webhooks_and_their_latest_delivery() {
+ WebhookDto webhook1 = webhookDbTester.insert(newOrganizationWebhook("aaa", defaultOrganizationProvider.get().getUuid()));
+ webhookDeliveryDbTester.insert(newDto("WH1-DELIVERY-1-UUID", webhook1.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE));
+ webhookDeliveryDbTester.insert(newDto("WH1-DELIVERY-2-UUID", webhook1.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW));
+
+ WebhookDto webhook2 = webhookDbTester.insert(newOrganizationWebhook("bbb", defaultOrganizationProvider.get().getUuid()));
+ webhookDeliveryDbTester.insert(newDto("WH2-DELIVERY-1-UUID", webhook2.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE));
+ webhookDeliveryDbTester.insert(newDto("WH2-DELIVERY-2-UUID", webhook2.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW));
+
+ userSession.logIn().addPermission(ADMINISTER, db.getDefaultOrganization().getUuid());
- }
+ ListResponse response = wsActionTester.newRequest().executeProtobuf(ListResponse.class);
+
+ List<Webhooks.ListResponseElement> elements = response.getWebhooksList();
+ assertThat(elements.size()).isEqualTo(2);
+
+ assertThat(elements.get(0)).extracting(Webhooks.ListResponseElement::getKey).containsExactly(webhook1.getUuid());
+ assertThat(elements.get(0)).extracting(Webhooks.ListResponseElement::getName).containsExactly("aaa");
+ assertThat(elements.get(0).getLatestDelivery()).isNotNull();
+ assertThat(elements.get(0).getLatestDelivery()).extracting(Webhooks.LatestDelivery::getId).containsExactly("WH1-DELIVERY-2-UUID");
+
+ assertThat(elements.get(1)).extracting(Webhooks.ListResponseElement::getKey).containsExactly(webhook2.getUuid());
+ assertThat(elements.get(1)).extracting(Webhooks.ListResponseElement::getName).containsExactly("bbb");
+ assertThat(elements.get(1).getLatestDelivery()).isNotNull();
+ assertThat(elements.get(1).getLatestDelivery()).extracting(Webhooks.LatestDelivery::getId).containsExactly("WH2-DELIVERY-2-UUID");
+ }
- @Test
- public void list_webhooks_and_their_latest_delivery() {
- WebhookDto webhook1 = webhookDbTester.insert(newOrganizationWebhook("aaa", defaultOrganizationProvider.get().getUuid()));
- webhookDeliveryDbTester.insert(newDto("WH1-DELIVERY-1-UUID", webhook1.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE));
- webhookDeliveryDbTester.insert(newDto("WH1-DELIVERY-2-UUID", webhook1.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW));
+ @Test
+ public void list_webhooks_when_no_delivery() {
+ WebhookDto webhook1 = webhookDbTester.insert(newOrganizationWebhook("aaa", defaultOrganizationProvider.get().getUuid()));
+ WebhookDto webhook2 = webhookDbTester.insert(newOrganizationWebhook("bbb", defaultOrganizationProvider.get().getUuid()));
+
+ userSession.logIn().addPermission(ADMINISTER, db.getDefaultOrganization().getUuid());
- WebhookDto webhook2 = webhookDbTester.insert(newOrganizationWebhook("bbb", defaultOrganizationProvider.get().getUuid()));
- webhookDeliveryDbTester.insert(newDto("WH2-DELIVERY-1-UUID", webhook2.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE));
- webhookDeliveryDbTester.insert(newDto("WH2-DELIVERY-2-UUID", webhook2.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW));
+ ListResponse response = wsActionTester.newRequest().executeProtobuf(ListResponse.class);
+
+ List<Webhooks.ListResponseElement> elements = response.getWebhooksList();
+ assertThat(elements.size()).isEqualTo(2);
- userSession.logIn().addPermission(ADMINISTER, db.getDefaultOrganization().getUuid());
+ assertThat(elements.get(0)).extracting(Webhooks.ListResponseElement::getKey).containsExactly(webhook1.getUuid());
+ assertThat(elements.get(0)).extracting(Webhooks.ListResponseElement::getName).containsExactly("aaa");
+ assertThat(elements.get(0).hasLatestDelivery()).isFalse();
- ListResponse response = wsActionTester.newRequest().executeProtobuf(ListResponse.class);
+ assertThat(elements.get(1)).extracting(Webhooks.ListResponseElement::getKey).containsExactly(webhook2.getUuid());
+ assertThat(elements.get(1)).extracting(Webhooks.ListResponseElement::getName).containsExactly("bbb");
+ assertThat(elements.get(1).hasLatestDelivery()).isFalse();
+ }
+
+ @Test
+ public void list_global_webhooks() {
+ WebhookDto dto1 = webhookDbTester.insertWebhook(db.getDefaultOrganization());
+ WebhookDto dto2 = webhookDbTester.insertWebhook(db.getDefaultOrganization());
+ userSession.logIn().addPermission(ADMINISTER, db.getDefaultOrganization().getUuid());
- List<Webhooks.ListResponseElement> elements = response.getWebhooksList();
- assertThat(elements.size()).isEqualTo(2);
+ ListResponse response = wsActionTester.newRequest()
+ .executeProtobuf(ListResponse.class);
- assertThat(elements.get(0)).extracting(Webhooks.ListResponseElement::getKey).containsExactly(webhook1.getUuid());
- assertThat(elements.get(0)).extracting(Webhooks.ListResponseElement::getName).containsExactly("aaa");
- assertThat(elements.get(0).getLatestDelivery()).isNotNull();
- assertThat(elements.get(0).getLatestDelivery()).extracting(Webhooks.LatestDelivery::getId).containsExactly("WH1-DELIVERY-2-UUID");
+ assertThat(response.getWebhooksList())
+ .extracting(Webhooks.ListResponseElement::getName, Webhooks.ListResponseElement::getUrl)
+ .contains(tuple(dto1.getName(), dto1.getUrl()),
+ tuple(dto2.getName(), dto2.getUrl()));
- assertThat(elements.get(1)).extracting(Webhooks.ListResponseElement::getKey).containsExactly(webhook2.getUuid());
- assertThat(elements.get(1)).extracting(Webhooks.ListResponseElement::getName).containsExactly("bbb");
- assertThat(elements.get(1).getLatestDelivery()).isNotNull();
- assertThat(elements.get(1).getLatestDelivery()).extracting(Webhooks.LatestDelivery::getId).containsExactly("WH2-DELIVERY-2-UUID");
- }
+ }
- @Test
- public void list_webhooks_when_no_delivery() {
- WebhookDto webhook1 = webhookDbTester.insert(newOrganizationWebhook("aaa", defaultOrganizationProvider.get().getUuid()));
- WebhookDto webhook2 = webhookDbTester.insert(newOrganizationWebhook("bbb", defaultOrganizationProvider.get().getUuid()));
+ @Test
+ public void list_project_webhooks_when_no_organization_is_provided() {
+ ComponentDto project1 = componentDbTester.insertPrivateProject();
+ userSession.logIn().addProjectPermission(ADMIN, project1);
- userSession.logIn().addPermission(ADMINISTER, db.getDefaultOrganization().getUuid());
+ WebhookDto dto1 = webhookDbTester.insertWebhook(project1);
+ WebhookDto dto2 = webhookDbTester.insertWebhook(project1);
- ListResponse response = wsActionTester.newRequest().executeProtobuf(ListResponse.class);
+ ListResponse response = wsActionTester.newRequest()
+ .setParam(PROJECT_KEY_PARAM, project1.getKey())
+ .executeProtobuf(ListResponse.class);
- List<Webhooks.ListResponseElement> elements = response.getWebhooksList();
- assertThat(elements.size()).isEqualTo(2);
+ assertThat(response.getWebhooksList())
+ .extracting(Webhooks.ListResponseElement::getName, Webhooks.ListResponseElement::getUrl)
+ .contains(tuple(dto1.getName(), dto1.getUrl()),
+ tuple(dto2.getName(), dto2.getUrl()));
- assertThat(elements.get(0)).extracting(Webhooks.ListResponseElement::getKey).containsExactly(webhook1.getUuid());
- assertThat(elements.get(0)).extracting(Webhooks.ListResponseElement::getName).containsExactly("aaa");
- assertThat(elements.get(0).hasLatestDelivery()).isFalse();
+ }
- assertThat(elements.get(1)).extracting(Webhooks.ListResponseElement::getKey).containsExactly(webhook2.getUuid());
- assertThat(elements.get(1)).extracting(Webhooks.ListResponseElement::getName).containsExactly("bbb");
- assertThat(elements.get(1).hasLatestDelivery()).isFalse();
- }
+ @Test
+ public void list_organization_webhooks() {
+ OrganizationDto organizationDto = organizationDbTester.insert();
+ WebhookDto dto1 = webhookDbTester.insertWebhook(organizationDto);
+ WebhookDto dto2 = webhookDbTester.insertWebhook(organizationDto);
+ userSession.logIn().addPermission(ADMINISTER, organizationDto.getUuid());
- @Test
- public void list_global_webhooks() {
+ ListResponse response = wsActionTester.newRequest()
+ .setParam(ORGANIZATION_KEY_PARAM, organizationDto.getKey())
+ .executeProtobuf(ListResponse.class);
- WebhookDto dto1 = webhookDbTester.insertWebhook(db.getDefaultOrganization());
- WebhookDto dto2 = webhookDbTester.insertWebhook(db.getDefaultOrganization());
- userSession.logIn().addPermission(ADMINISTER, db.getDefaultOrganization().getUuid());
+ assertThat(response.getWebhooksList())
+ .extracting(Webhooks.ListResponseElement::getName, Webhooks.ListResponseElement::getUrl)
+ .contains(tuple(dto1.getName(), dto1.getUrl()),
+ tuple(dto2.getName(), dto2.getUrl()));
- ListResponse response = wsActionTester.newRequest()
- .executeProtobuf(ListResponse.class);
+ }
- assertThat(response.getWebhooksList())
- .extracting(Webhooks.ListResponseElement::getName, Webhooks.ListResponseElement::getUrl)
- .contains(tuple(dto1.getName(), dto1.getUrl()),
- tuple(dto2.getName(), dto2.getUrl()));
+ @Test
+ public void list_project_webhooks_when_organization_is_provided() {
+ OrganizationDto organization = organizationDbTester.insert();
+ ComponentDto project = componentDbTester.insertPrivateProject(organization);
+ userSession.logIn().addProjectPermission(ADMIN, project);
- }
+ WebhookDto dto1 = webhookDbTester.insertWebhook(project);
+ WebhookDto dto2 = webhookDbTester.insertWebhook(project);
- @Test
- public void list_project_webhooks_when_no_organization_is_provided() {
+ ListResponse response = wsActionTester.newRequest()
+ .setParam(ORGANIZATION_KEY_PARAM, organization.getKey())
+ .setParam(PROJECT_KEY_PARAM, project.getKey())
+ .executeProtobuf(ListResponse.class);
- ComponentDto project1 = componentDbTester.insertPrivateProject();
- userSession.logIn().addProjectPermission(ADMIN, project1);
+ assertThat(response.getWebhooksList())
+ .extracting(Webhooks.ListResponseElement::getName, Webhooks.ListResponseElement::getUrl)
+ .contains(tuple(dto1.getName(), dto1.getUrl()),
+ tuple(dto2.getName(), dto2.getUrl()));
- WebhookDto dto1 = webhookDbTester.insertWebhook(project1);
- WebhookDto dto2 = webhookDbTester.insertWebhook(project1);
+ }
- ListResponse response = wsActionTester.newRequest()
- .setParam(PROJECT_KEY_PARAM, project1.getKey())
- .executeProtobuf(ListResponse.class);
+ @Test
+ public void return_NotFoundException_if_requested_project_is_not_found() {
+ userSession.logIn().setSystemAdministrator();
+ expectedException.expect(NotFoundException.class);
- assertThat(response.getWebhooksList())
- .extracting(Webhooks.ListResponseElement::getName, Webhooks.ListResponseElement::getUrl)
- .contains(tuple(dto1.getName(), dto1.getUrl()),
- tuple(dto2.getName(), dto2.getUrl()));
+ wsActionTester.newRequest()
+ .setParam(PROJECT_KEY_PARAM, "pipo")
+ .executeProtobuf(ListResponse.class);
- }
+ }
- @Test
- public void list_organization_webhooks() {
+ @Test
+ public void return_NotFoundException_if_requested_organization_is_not_found() {
+ userSession.logIn().setSystemAdministrator();
+ expectedException.expect(NotFoundException.class);
- OrganizationDto organizationDto = organizationDbTester.insert();
- WebhookDto dto1 = webhookDbTester.insertWebhook(organizationDto);
- WebhookDto dto2 = webhookDbTester.insertWebhook(organizationDto);
- userSession.logIn().addPermission(ADMINISTER, organizationDto.getUuid());
+ wsActionTester.newRequest()
+ .setParam(ORGANIZATION_KEY_PARAM, "pipo")
+ .executeProtobuf(ListResponse.class);
- ListResponse response = wsActionTester.newRequest()
- .setParam(ORGANIZATION_KEY_PARAM, organizationDto.getKey())
- .executeProtobuf(ListResponse.class);
+ }
- assertThat(response.getWebhooksList())
- .extracting(Webhooks.ListResponseElement::getName, Webhooks.ListResponseElement::getUrl)
- .contains(tuple(dto1.getName(), dto1.getUrl()),
- tuple(dto2.getName(), dto2.getUrl()));
+ @Test
+ public void fail_if_project_exists_but_does_not_belong_to_requested_organization() {
+ OrganizationDto organization = organizationDbTester.insert();
+ ComponentDto project = componentDbTester.insertPrivateProject();
- }
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project '%s' does not belong to organisation '%s'", project.getKey(), organization.getKey()));
- @Test
- public void list_project_webhooks_when_organization_is_provided() {
+ userSession.logIn().addProjectPermission(ADMIN, project);
- OrganizationDto organization = organizationDbTester.insert();
- ComponentDto project = componentDbTester.insertPrivateProject(organization);
- userSession.logIn().addProjectPermission(ADMIN, project);
+ wsActionTester.newRequest()
+ .setParam(ORGANIZATION_KEY_PARAM, organization.getKey())
+ .setParam(PROJECT_KEY_PARAM, project.getKey())
+ .execute();
- WebhookDto dto1 = webhookDbTester.insertWebhook(project);
- WebhookDto dto2 = webhookDbTester.insertWebhook(project);
+ }
- ListResponse response = wsActionTester.newRequest()
- .setParam(ORGANIZATION_KEY_PARAM, organization.getKey())
- .setParam(PROJECT_KEY_PARAM, project.getKey())
- .executeProtobuf(ListResponse.class);
+ @Test
+ public void return_UnauthorizedException_if_not_logged_in() {
+ userSession.anonymous();
+ expectedException.expect(UnauthorizedException.class);
- assertThat(response.getWebhooksList())
- .extracting(Webhooks.ListResponseElement::getName, Webhooks.ListResponseElement::getUrl)
- .contains(tuple(dto1.getName(), dto1.getUrl()),
- tuple(dto2.getName(), dto2.getUrl()));
+ wsActionTester.newRequest()
+ .executeProtobuf(ListResponse.class);
- }
+ }
- @Test
- public void return_NotFoundException_if_requested_project_is_not_found() throws Exception {
+ @Test
+ public void throw_ForbiddenException_if_not_organization_administrator() {
+ userSession.logIn();
- userSession.logIn().setSystemAdministrator();
- expectedException.expect(NotFoundException.class);
+ expectedException.expect(ForbiddenException.class);
+ expectedException.expectMessage("Insufficient privileges");
- wsActionTester.newRequest()
- .setParam(PROJECT_KEY_PARAM, "pipo")
- .executeProtobuf(ListResponse.class);
+ wsActionTester.newRequest()
+ .executeProtobuf(ListResponse.class);
+ }
- }
+ @Test
+ public void throw_ForbiddenException_if_not_project_administrator() {
+ ComponentDto project = componentDbTester.insertPrivateProject();
- @Test
- public void return_NotFoundException_if_requested_organization_is_not_found() throws Exception {
+ userSession.logIn();
- userSession.logIn().setSystemAdministrator();
- expectedException.expect(NotFoundException.class);
+ expectedException.expect(ForbiddenException.class);
+ expectedException.expectMessage("Insufficient privileges");
- wsActionTester.newRequest()
- .setParam(ORGANIZATION_KEY_PARAM, "pipo")
- .executeProtobuf(ListResponse.class);
+ wsActionTester.newRequest()
+ .setParam(PROJECT_KEY_PARAM, project.getKey())
+ .executeProtobuf(ListResponse.class);
- }
-
- @Test
- public void fail_if_project_exists_but_does_not_belong_to_requested_organization() {
-
- OrganizationDto organization = organizationDbTester.insert();
- ComponentDto project = componentDbTester.insertPrivateProject();
-
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage(format("Project '%s' does not belong to organisation '%s'", project.getKey(), organization.getKey()));
-
- userSession.logIn().addProjectPermission(ADMIN, project);
-
- wsActionTester.newRequest()
- .setParam(ORGANIZATION_KEY_PARAM, organization.getKey())
- .setParam(PROJECT_KEY_PARAM, project.getKey())
- .execute();
-
- }
-
- @Test
- public void return_UnauthorizedException_if_not_logged_in() throws Exception {
-
- userSession.anonymous();
- expectedException.expect(UnauthorizedException.class);
-
- wsActionTester.newRequest()
- .executeProtobuf(ListResponse.class);
-
- }
-
- @Test
- public void throw_ForbiddenException_if_not_organization_administrator() {
-
- userSession.logIn();
-
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("Insufficient privileges");
-
- wsActionTester.newRequest()
- .executeProtobuf(ListResponse.class);
- }
-
- @Test
- public void throw_ForbiddenException_if_not_project_administrator() {
-
- ComponentDto project = componentDbTester.insertPrivateProject();
-
- userSession.logIn();
-
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("Insufficient privileges");
-
- wsActionTester.newRequest()
- .setParam(PROJECT_KEY_PARAM, project.getKey())
- .executeProtobuf(ListResponse.class);
-
- }
+ }
}