You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GithubAppConfigurationTest.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.auth.github;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import java.util.Random;
  25. import java.util.stream.Stream;
  26. import javax.annotation.Nullable;
  27. import org.apache.commons.lang3.ArrayUtils;
  28. import org.junit.Test;
  29. import org.junit.runner.RunWith;
  30. import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  33. @RunWith(DataProviderRunner.class)
  34. public class GithubAppConfigurationTest {
  35. @Test
  36. @UseDataProvider("incompleteConfigurationParametersSonarQube")
  37. public void isComplete_returns_false_if_configuration_is_incomplete_on_SonarQube(@Nullable Long applicationId, @Nullable String privateKey, @Nullable String apiEndpoint) {
  38. GithubAppConfiguration underTest = new GithubAppConfiguration(applicationId, privateKey, apiEndpoint);
  39. assertThat(underTest.isComplete()).isFalse();
  40. }
  41. @Test
  42. @UseDataProvider("incompleteConfigurationParametersSonarQube")
  43. public void getId_throws_ISE_if_config_is_incomplete(@Nullable Long applicationId, @Nullable String privateKey, @Nullable String apiEndpoint) {
  44. GithubAppConfiguration underTest = new GithubAppConfiguration(applicationId, privateKey, apiEndpoint);
  45. assertThatThrownBy(underTest::getId)
  46. .isInstanceOf(IllegalStateException.class)
  47. .hasMessageContaining("Configuration is not complete");
  48. }
  49. @Test
  50. public void getId_returns_applicationId_if_configuration_is_valid() {
  51. long applicationId = new Random().nextLong();
  52. GithubAppConfiguration underTest = newValidConfiguration(applicationId);
  53. assertThat(underTest.getId()).isEqualTo(applicationId);
  54. }
  55. @Test
  56. @UseDataProvider("incompleteConfigurationParametersSonarQube")
  57. public void getPrivateKeyFile_throws_ISE_if_config_is_incomplete(@Nullable Long applicationId, @Nullable String privateKey, @Nullable String apiEndpoint) {
  58. GithubAppConfiguration underTest = new GithubAppConfiguration(applicationId, privateKey, apiEndpoint);
  59. assertThatThrownBy(underTest::getPrivateKey)
  60. .isInstanceOf(IllegalStateException.class)
  61. .hasMessageContaining("Configuration is not complete");
  62. }
  63. @DataProvider
  64. public static Object[][] incompleteConfigurationParametersSonarQube() {
  65. long applicationId = new Random().nextLong();
  66. String privateKey = randomAlphabetic(9);
  67. String apiEndpoint = randomAlphabetic(11);
  68. return generateNullCombination(new Object[] {
  69. applicationId,
  70. privateKey,
  71. apiEndpoint
  72. });
  73. }
  74. @Test
  75. public void toString_displays_complete_configuration() {
  76. long id = 34;
  77. String privateKey = randomAlphabetic(3);
  78. String apiEndpoint = randomAlphabetic(7);
  79. GithubAppConfiguration underTest = new GithubAppConfiguration(id, privateKey, apiEndpoint);
  80. assertThat(underTest)
  81. .hasToString(String.format("GithubAppConfiguration{id=%s, privateKey='***(3)***', apiEndpoint='%s'}", id, apiEndpoint));
  82. }
  83. @Test
  84. public void toString_displays_incomplete_configuration() {
  85. GithubAppConfiguration underTest = new GithubAppConfiguration(null, null, null);
  86. assertThat(underTest)
  87. .hasToString("GithubAppConfiguration{id=null, privateKey=null, apiEndpoint=null}");
  88. }
  89. @Test
  90. public void toString_displays_privateKey_as_stars() {
  91. GithubAppConfiguration underTest = new GithubAppConfiguration(null, randomAlphabetic(555), null);
  92. assertThat(underTest)
  93. .hasToString(
  94. "GithubAppConfiguration{id=null, privateKey='***(555)***', apiEndpoint=null}");
  95. }
  96. @Test
  97. public void equals_is_not_implemented() {
  98. long applicationId = new Random().nextLong();
  99. String privateKey = randomAlphabetic(8);
  100. String apiEndpoint = randomAlphabetic(7);
  101. GithubAppConfiguration underTest = new GithubAppConfiguration(applicationId, privateKey, apiEndpoint);
  102. assertThat(underTest)
  103. .isEqualTo(underTest)
  104. .isNotEqualTo(new GithubAppConfiguration(applicationId, privateKey, apiEndpoint));
  105. }
  106. @Test
  107. public void hashcode_is_based_on_all_fields() {
  108. long applicationId = new Random().nextLong();
  109. String privateKey = randomAlphabetic(8);
  110. String apiEndpoint = randomAlphabetic(7);
  111. GithubAppConfiguration underTest = new GithubAppConfiguration(applicationId, privateKey, apiEndpoint);
  112. assertThat(underTest).hasSameHashCodeAs(underTest);
  113. assertThat(underTest.hashCode()).isNotEqualTo(new GithubAppConfiguration(applicationId, privateKey, apiEndpoint));
  114. }
  115. private GithubAppConfiguration newValidConfiguration(long applicationId) {
  116. return new GithubAppConfiguration(applicationId, randomAlphabetic(6), randomAlphabetic(6));
  117. }
  118. private static Object[][] generateNullCombination(Object[] objects) {
  119. Object[][] firstPossibleValues = new Object[][] {
  120. {null},
  121. {objects[0]}
  122. };
  123. if (objects.length == 1) {
  124. return firstPossibleValues;
  125. }
  126. Object[][] subCombinations = generateNullCombination(ArrayUtils.subarray(objects, 1, objects.length));
  127. return Stream.of(subCombinations)
  128. .flatMap(combination -> Stream.of(firstPossibleValues).map(firstValue -> ArrayUtils.addAll(firstValue, combination)))
  129. .filter(array -> ArrayUtils.contains(array, null))
  130. .toArray(Object[][]::new);
  131. }
  132. }