3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.almintegration.validator;
22 import org.junit.Test;
23 import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
24 import org.sonar.db.alm.setting.AlmSettingDto;
26 import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.Mockito.doThrow;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.sonar.db.almsettings.AlmSettingsTesting.newBitbucketAlmSettingDto;
34 public class BitbucketServerSettingsValidatorTest {
36 private final BitbucketServerRestClient bitbucketServerRestClient = mock(BitbucketServerRestClient.class);
37 private final BitbucketServerSettingsValidator underTest = new BitbucketServerSettingsValidator(bitbucketServerRestClient);
40 public void validate_success() {
41 AlmSettingDto almSettingDto = newBitbucketAlmSettingDto()
42 .setUrl("http://abc.com")
43 .setPersonalAccessToken("abc");
45 underTest.validate(almSettingDto);
47 verify(bitbucketServerRestClient, times(1)).validateUrl("http://abc.com");
48 verify(bitbucketServerRestClient, times(1)).validateToken("http://abc.com", "abc");
49 verify(bitbucketServerRestClient, times(1)).validateReadPermission("http://abc.com", "abc");
53 public void validate_failure_on_incomplete_configuration() {
54 AlmSettingDto almSettingDto = newBitbucketAlmSettingDto()
56 .setPersonalAccessToken("abc");
58 assertThatThrownBy(() -> underTest.validate(almSettingDto))
59 .isInstanceOf(IllegalArgumentException.class);
63 public void validate_failure_on_bitbucket_server_api_error() {
64 doThrow(new IllegalArgumentException("error")).when(bitbucketServerRestClient).validateUrl(anyString());
65 AlmSettingDto almSettingDto = newBitbucketAlmSettingDto()
66 .setUrl("http://abc.com")
67 .setPersonalAccessToken("abc");
69 assertThatThrownBy(() -> underTest.validate(almSettingDto))
70 .isInstanceOf(IllegalArgumentException.class);