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.

GsonEmailsTest.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.bitbucket;
  21. import org.junit.Test;
  22. import static org.assertj.core.api.Assertions.assertThat;
  23. public class GsonEmailsTest {
  24. @Test
  25. public void testParse() {
  26. String json = "{" +
  27. "\"pagelen\": 10," +
  28. "\"values\": [" +
  29. "{" +
  30. "\"is_primary\": true," +
  31. "\"is_confirmed\": true," +
  32. "\"type\": \"email\"," +
  33. "\"email\": \"foo@bar.com\"," +
  34. "\"links\": {" +
  35. "\"self\": {" +
  36. "\"href\": \"https://api.bitbucket.org/2.0/user/emails/foo@bar.com\"" +
  37. "}" +
  38. "}" +
  39. "}" +
  40. "]," +
  41. "\"page\": 1," +
  42. "\"size\": 1" +
  43. "}";
  44. GsonEmails emails = GsonEmails.parse(json);
  45. assertThat(emails.getEmails()).hasSize(1);
  46. assertThat(emails.getEmails().get(0).isPrimary()).isTrue();
  47. assertThat(emails.getEmails().get(0).getEmail()).isEqualTo("foo@bar.com");
  48. }
  49. @Test
  50. public void test_extractPrimaryEmail() {
  51. String json = "{" +
  52. "\"pagelen\": 10," +
  53. "\"values\": [" +
  54. "{" +
  55. "\"is_primary\": false," +
  56. "\"is_confirmed\": true," +
  57. "\"type\": \"email\"," +
  58. "\"email\": \"secondary@bar.com\"," +
  59. "\"links\": {" +
  60. "\"self\": {" +
  61. "\"href\": \"https://api.bitbucket.org/2.0/user/emails/secondary@bar.com\"" +
  62. "}" +
  63. "}" +
  64. "}," +
  65. "{" +
  66. "\"is_primary\": true," +
  67. "\"is_confirmed\": true," +
  68. "\"type\": \"email\"," +
  69. "\"email\": \"primary@bar.com\"," +
  70. "\"links\": {" +
  71. "\"self\": {" +
  72. "\"href\": \"https://api.bitbucket.org/2.0/user/emails/primary@bar.com\"" +
  73. "}" +
  74. "}" +
  75. "}" +
  76. "]," +
  77. "\"page\": 1," +
  78. "\"size\": 2" +
  79. "}";
  80. String email = GsonEmails.parse(json).extractPrimaryEmail();
  81. assertThat(email).isEqualTo("primary@bar.com");
  82. }
  83. @Test
  84. public void test_extractPrimaryEmail_not_found() {
  85. String json = "{" +
  86. "\"pagelen\": 10," +
  87. "\"values\": [" +
  88. "]," +
  89. "\"page\": 1," +
  90. "\"size\": 0" +
  91. "}";
  92. String email = GsonEmails.parse(json).extractPrimaryEmail();
  93. assertThat(email).isNull();
  94. }
  95. }