PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-plugins-webresource-tests/src/test/java/it/com/atlassian/webresource/ProfilingMetricsTest.java

https://bitbucket.org/atlassian/atlassian-plugins-webresource
Java | 164 lines | 136 code | 23 blank | 5 comment | 0 complexity | d16a027fba8f3a7f7c3ecff3f3277973 MD5 | raw file
  1. package it.com.atlassian.webresource;
  2. import io.restassured.RestAssured;
  3. import io.restassured.response.Response;
  4. import io.restassured.specification.RequestSpecification;
  5. import it.com.atlassian.rest.util.AsyncTestUtils;
  6. import it.com.atlassian.rest.util.WaitCondition;
  7. import org.hamcrest.Description;
  8. import org.jetbrains.annotations.NotNull;
  9. import org.junit.Before;
  10. import org.junit.Test;
  11. import java.util.Arrays;
  12. import java.util.List;
  13. import java.util.concurrent.atomic.AtomicReference;
  14. import java.util.function.Supplier;
  15. import java.util.stream.Collectors;
  16. import static io.restassured.http.ContentType.JSON;
  17. import static it.com.atlassian.rest.util.CommonRestTestConstants.getAdminPassword;
  18. import static it.com.atlassian.rest.util.CommonRestTestConstants.getAdminUser;
  19. import static it.com.atlassian.rest.util.CommonRestTestConstants.getRestBaseUrl;
  20. import static java.util.Collections.singletonList;
  21. import static javax.ws.rs.core.Response.Status.NO_CONTENT;
  22. import static org.hamcrest.MatcherAssert.assertThat;
  23. import static org.hamcrest.Matchers.either;
  24. import static org.hamcrest.Matchers.emptyOrNullString;
  25. import static org.hamcrest.Matchers.equalTo;
  26. import static org.hamcrest.Matchers.greaterThan;
  27. import static org.hamcrest.Matchers.is;
  28. import static org.junit.Assume.assumeThat;
  29. public class ProfilingMetricsTest {
  30. @Before
  31. public void setup() {
  32. // These tests only work in the refapp at the moment, as it's the only place where `atlassian-profiling`
  33. // has been configured to make the test work.
  34. assumeThat(getRunningProductInstanceId(), either(equalTo("refapp")).or(is(emptyOrNullString())));
  35. }
  36. @Test
  37. public void whenWebConditionsAreTriggered_thenJmxBeansShouldIncludeTheTimeTakenForConditionShouldDisplayLogic() {
  38. startConditionsSimulationOperation();
  39. AtomicReference<List<String>> emittedConditionBeans = new AtomicReference<>();
  40. waitForAssertionToBeTrue(() -> {
  41. final List<String> mBeanNames = getAllEmittedJmxBeans();
  42. emittedConditionBeans.set(getWebConditionsBeanNames(mBeanNames));
  43. // Wait for the beans and their count to be emitted. The count has a slight delay
  44. return emittedConditionBeans.get().size() >= 2 &&
  45. getBeanCallCount(getDetailedJmxEntry(emittedConditionBeans.get().get(0))) > 0;
  46. });
  47. assertThat("Condition metrics bean have been emitted to JMX", emittedConditionBeans.get().size(), equalTo(2));
  48. emittedConditionBeans.get().forEach(bean -> {
  49. Response jmxDetails = getDetailedJmxEntry(bean);
  50. int callsCount = getBeanCallCount(jmxDetails);
  51. assertThat("Condition calls are counted", callsCount, greaterThan(0));
  52. });
  53. }
  54. private int getBeanCallCount(Response jmxDetails) {
  55. return Integer.parseInt(jmxDetails.jsonPath().get("Count"));
  56. }
  57. private Response getDetailedJmxEntry(String bean) {
  58. return adminJsonRequest()
  59. .then()
  60. .log()
  61. .ifValidationFails()
  62. .statusCode(200)
  63. .contentType(JSON)
  64. .when()
  65. .get(getNoisyNeighbourUrl("/jmx/" + bean));
  66. }
  67. private List<String> getAllEmittedJmxBeans() {
  68. return Arrays.asList(adminJsonRequest()
  69. .then()
  70. .log()
  71. .ifValidationFails()
  72. .statusCode(200)
  73. .contentType(JSON)
  74. .when()
  75. .get(getNoisyNeighbourUrl("/jmx"))
  76. .as(String[].class));
  77. }
  78. private String getRunningProductInstanceId() {
  79. return adminJsonRequest()
  80. .then()
  81. .log()
  82. .ifValidationFails()
  83. .statusCode(200)
  84. .contentType(JSON)
  85. .when()
  86. .get(getQuickreloadUrl("/systemproperties"))
  87. .jsonPath()
  88. .getMap("properties")
  89. .getOrDefault("product", "")
  90. .toString();
  91. }
  92. private void startConditionsSimulationOperation() {
  93. String operationsResourceUrl = getNoisyNeighbourUrl("/admin");
  94. final List<String> operationTask = singletonList("WRM_CONDITION");
  95. adminJsonRequest().given()
  96. .body(operationTask)
  97. .contentType(JSON)
  98. .when()
  99. .post(operationsResourceUrl)
  100. .then()
  101. .log()
  102. .ifValidationFails()
  103. .statusCode(NO_CONTENT.getStatusCode());
  104. }
  105. private String getNoisyNeighbourUrl(String path) {
  106. return getRestBaseUrl() + "/noisyneighbour/latest" + path;
  107. }
  108. private String getQuickreloadUrl(String path) {
  109. return getRestBaseUrl() + "/qr" + path;
  110. }
  111. private RequestSpecification adminJsonRequest() {
  112. return RestAssured.given()
  113. .auth()
  114. .preemptive()
  115. .basic(getAdminUser(), getAdminPassword())
  116. .contentType(JSON);
  117. }
  118. private void waitForAssertionToBeTrue(Supplier<Boolean> assertionFn) {
  119. int retryIntervalMs = 100;
  120. AsyncTestUtils.waitFor(new WaitCondition() {
  121. @Override
  122. public void describeFailure(Description description) {
  123. description.appendText("\nExpected Web conditions beans to be emitted but wasn't");
  124. }
  125. @Override
  126. public boolean test() {
  127. return assertionFn.get();
  128. }
  129. }, 10_000 + retryIntervalMs, retryIntervalMs);
  130. }
  131. @NotNull
  132. private List<String> getWebConditionsBeanNames(List<String> mBeanNames) {
  133. return mBeanNames
  134. .stream()
  135. // Match on the transform task metric
  136. .filter(n -> n.contains("category00=web,category01=resource,name=condition"))
  137. // Match it's from the noisy neighbour plugin' plugin
  138. .filter(n -> n.contains("com.atlassian.diagnostics.noisy-neighbour-plugin"))
  139. .collect(Collectors.toList());
  140. }
  141. }