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.

MeasureAssert.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.ce.task.projectanalysis.measure;
  21. import java.util.Objects;
  22. import java.util.Optional;
  23. import javax.annotation.Nullable;
  24. import org.assertj.core.api.AbstractAssert;
  25. import org.assertj.core.data.Offset;
  26. import static java.lang.Math.abs;
  27. public class MeasureAssert extends AbstractAssert<MeasureAssert, Measure> {
  28. protected MeasureAssert(@Nullable Measure actual) {
  29. super(actual, MeasureAssert.class);
  30. }
  31. public static MeasureAssert assertThat(Measure actual) {
  32. return new MeasureAssert(actual);
  33. }
  34. public static MeasureAssert assertThat(@Nullable Optional<Measure> actual) {
  35. return new MeasureAssert(actual == null ? null : actual.orElse(null));
  36. }
  37. public MeasureAssert hasValue(int expected) {
  38. isNotNull();
  39. if (actual.getValueType() != Measure.ValueType.INT) {
  40. failWithMessage(
  41. "Expected Measure to have an int value and therefore its ValueType to be <%s> but was <%s>",
  42. Measure.ValueType.INT, actual.getValueType());
  43. }
  44. if (actual.getIntValue() != expected) {
  45. failWithMessage("Expected value of Measure to be <%s> but was <%s>", expected, actual.getIntValue());
  46. }
  47. return this;
  48. }
  49. public MeasureAssert hasValue(long expected) {
  50. isNotNull();
  51. if (actual.getValueType() != Measure.ValueType.LONG) {
  52. failWithMessage(
  53. "Expected Measure to have a long value and therefore its ValueType to be <%s> but was <%s>",
  54. Measure.ValueType.LONG, actual.getValueType());
  55. }
  56. if (actual.getLongValue() != expected) {
  57. failWithMessage("Expected value of Measure to be <%s> but was <%s>", expected, actual.getLongValue());
  58. }
  59. return this;
  60. }
  61. public MeasureAssert hasValue(double expected) {
  62. isNotNull();
  63. if (actual.getValueType() != Measure.ValueType.DOUBLE) {
  64. failWithMessage(
  65. "Expected Measure to have a double value and therefore its ValueType to be <%s> but was <%s>",
  66. Measure.ValueType.DOUBLE, actual.getValueType());
  67. }
  68. if (actual.getDoubleValue() != expected) {
  69. failWithMessage("Expected value of Measure to be <%s> but was <%s>", expected, actual.getDoubleValue());
  70. }
  71. return this;
  72. }
  73. public MeasureAssert hasValue(boolean expected) {
  74. isNotNull();
  75. if (actual.getValueType() != Measure.ValueType.BOOLEAN) {
  76. failWithMessage(
  77. "Expected Measure to have a boolean value and therefore its ValueType to be <%s> but was <%s>",
  78. Measure.ValueType.DOUBLE, actual.getValueType());
  79. }
  80. if (actual.getBooleanValue() != expected) {
  81. failWithMessage("Expected value of Measure to be <%s> but was <%s>", expected, actual.getBooleanValue());
  82. }
  83. return this;
  84. }
  85. public MeasureAssert hasValue(String expected) {
  86. isNotNull();
  87. if (actual.getValueType() != Measure.ValueType.STRING) {
  88. failWithMessage(
  89. "Expected Measure to have a String value and therefore its ValueType to be <%s> but was <%s>",
  90. Measure.ValueType.DOUBLE, actual.getValueType());
  91. }
  92. if (!Objects.equals(actual.getStringValue(), expected)) {
  93. failWithMessage("Expected value of Measure to be <%s> but was <%s>", expected, actual.getStringValue());
  94. }
  95. return this;
  96. }
  97. public MeasureAssert hasValue(Measure.Level expected) {
  98. isNotNull();
  99. if (actual.getValueType() != Measure.ValueType.LEVEL) {
  100. failWithMessage(
  101. "Expected Measure to have a Level value and therefore its ValueType to be <%s> but was <%s>",
  102. Measure.ValueType.DOUBLE, actual.getValueType());
  103. }
  104. if (actual.getLevelValue() != expected) {
  105. failWithMessage("Expected value of Measure to be <%s> but was <%s>", expected, actual.getLevelValue());
  106. }
  107. return this;
  108. }
  109. public MeasureAssert hasNoValue() {
  110. isNotNull();
  111. if (actual.getValueType() != Measure.ValueType.NO_VALUE) {
  112. failWithMessage(
  113. "Expected Measure to have no value and therefore its ValueType to be <%s> but was <%s>",
  114. Measure.ValueType.DOUBLE, actual.getValueType());
  115. }
  116. return this;
  117. }
  118. public MeasureAssert hasData(String expected) {
  119. isNotNull();
  120. if (!Objects.equals(actual.getData(), expected)) {
  121. failWithMessage("Expected data of Measure to be <%s> but was <%s>", expected, actual.getData());
  122. }
  123. return this;
  124. }
  125. public MeasureAssert hasNoData() {
  126. isNotNull();
  127. if (actual.getData() == null) {
  128. failWithMessage("Expected Measure to have no data but was <%s>", actual.getData());
  129. }
  130. return this;
  131. }
  132. public MeasureAssert hasQualityGateLevel(Measure.Level expected) {
  133. isNotNull();
  134. hasQualityGateStatus();
  135. if (actual.getQualityGateStatus().getStatus() != expected) {
  136. failWithMessage("Expected Level of QualityGateStatus of Measure to be <%s> but was <%s>", expected, actual.getQualityGateStatus().getStatus());
  137. }
  138. return this;
  139. }
  140. public MeasureAssert hasQualityGateText(String expected) {
  141. isNotNull();
  142. hasQualityGateStatus();
  143. if (!Objects.equals(actual.getQualityGateStatus().getText(), expected)) {
  144. failWithMessage("Expected text of QualityGateStatus of Measure to be \n<%s>\n but was \n<%s>", expected, actual.getQualityGateStatus().getText());
  145. }
  146. return this;
  147. }
  148. private void hasQualityGateStatus() {
  149. if (!actual.hasQualityGateStatus()) {
  150. failWithMessage("Expected Measure to have a QualityGateStatus but it did not");
  151. }
  152. }
  153. public MeasureAssert hasVariation(double expected) {
  154. isNotNull();
  155. hasVariation();
  156. if (!actual.hasVariation()) {
  157. failWithMessage("Expected Measure to have a variation but it did not");
  158. }
  159. double variation = actual.getVariation();
  160. if (variation != expected) {
  161. failWithMessage("Expected variation of Measure to be <%s> but was <%s>", expected, variation);
  162. }
  163. return this;
  164. }
  165. public MeasureAssert hasVariation(double expected, Offset<Double> offset) {
  166. isNotNull();
  167. hasVariation();
  168. if (!actual.hasVariation()) {
  169. failWithMessage("Expected Measure to have a variation but it did not");
  170. }
  171. double variation = actual.getVariation();
  172. if (abs(expected - variation) > offset.value) {
  173. failWithMessage(
  174. "Expected variation of Measure to be close to <%s> by less than <%s> but was <%s>",
  175. expected, offset.value, variation);
  176. }
  177. return this;
  178. }
  179. private void hasVariation() {
  180. if (!actual.hasVariation()) {
  181. failWithMessage("Expected Measure to have a variation but it did not");
  182. }
  183. }
  184. public void isAbsent() {
  185. if (actual != null) {
  186. failWithMessage("Expected measure to be absent");
  187. }
  188. }
  189. }