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.

SvgGeneratorTest.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.server.badge.ws;
  21. import java.io.IOException;
  22. import org.apache.commons.io.IOUtils;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.ExpectedException;
  26. import org.sonar.api.config.internal.MapSettings;
  27. import org.sonar.api.measures.Metric;
  28. import org.sonar.db.DbTester;
  29. import org.sonar.server.tester.UserSessionRule;
  30. import static java.nio.charset.StandardCharsets.UTF_8;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.sonar.api.measures.Metric.Level.WARN;
  33. import static org.sonar.server.badge.ws.SvgGenerator.Color.DEFAULT;
  34. public class SvgGeneratorTest {
  35. @Rule
  36. public ExpectedException expectedException = ExpectedException.none();
  37. @Rule
  38. public UserSessionRule userSession = UserSessionRule.standalone();
  39. @Rule
  40. public DbTester db = DbTester.create();
  41. private MapSettings mapSettings = new MapSettings();
  42. private SvgGenerator underTest;
  43. @Test
  44. public void generate_badge() {
  45. mapSettings.setProperty("sonar.sonarcloud.enabled", false);
  46. initSvgGenerator();
  47. String result = underTest.generateBadge("label", "10", DEFAULT);
  48. checkBadge(result, "label", "10", DEFAULT);
  49. }
  50. @Test
  51. public void generate_quality_gate() {
  52. mapSettings.setProperty("sonar.sonarcloud.enabled", false);
  53. initSvgGenerator();
  54. String result = underTest.generateQualityGate(WARN);
  55. checkQualityGate(result, WARN);
  56. }
  57. @Test
  58. public void generate_error() {
  59. mapSettings.setProperty("sonar.sonarcloud.enabled", false);
  60. initSvgGenerator();
  61. String result = underTest.generateError("Error");
  62. assertThat(result).contains("<text", ">Error</text>");
  63. }
  64. @Test
  65. public void fail_when_unknown_character() {
  66. mapSettings.setProperty("sonar.sonarcloud.enabled", false);
  67. initSvgGenerator();
  68. expectedException.expectMessage("Invalid character 'é'");
  69. underTest.generateError("Méssage with accent");
  70. }
  71. private void initSvgGenerator() {
  72. underTest = new SvgGenerator(mapSettings.asConfig());
  73. }
  74. private void checkBadge(String svg, String expectedLabel, String expectedValue, SvgGenerator.Color expectedColorValue) {
  75. assertThat(svg).contains(
  76. "<text", expectedLabel + "</text>",
  77. "<text", expectedValue + "</text>",
  78. "rect fill=\"" + expectedColorValue.getValue() + "\"");
  79. }
  80. private void checkQualityGate(String response, Metric.Level status) {
  81. switch (status) {
  82. case OK:
  83. assertThat(response).isEqualTo(readTemplate("quality_gate_passed.svg"));
  84. break;
  85. case WARN:
  86. assertThat(response).isEqualTo(readTemplate("quality_gate_warn.svg"));
  87. break;
  88. case ERROR:
  89. assertThat(response).isEqualTo(readTemplate("quality_gate_failed.svg"));
  90. break;
  91. }
  92. }
  93. private String readTemplate(String template) {
  94. try {
  95. return IOUtils.toString(getClass().getResource("templates/sonarqube/" + template), UTF_8);
  96. } catch (IOException e) {
  97. throw new IllegalStateException(String.format("Can't read svg template '%s'", template), e);
  98. }
  99. }
  100. }