/sdk/metricsadvisor/azure-ai-metricsadvisor/src/test/java/com/azure/ai/metricsadvisor/NotificationHookTestBase.java

http://github.com/WindowsAzure/azure-sdk-for-java · Java · 151 lines · 128 code · 13 blank · 10 comment · 13 complexity · b0bca24a633e43458e3e822463a1d715 MD5 · raw file

  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. package com.azure.ai.metricsadvisor;
  4. import com.azure.ai.metricsadvisor.administration.models.EmailNotificationHook;
  5. import com.azure.ai.metricsadvisor.administration.models.NotificationHook;
  6. import com.azure.ai.metricsadvisor.administration.models.WebNotificationHook;
  7. import com.azure.core.http.HttpClient;
  8. import com.azure.core.http.HttpHeaders;
  9. import com.azure.core.http.rest.PagedResponse;
  10. import com.azure.core.test.TestMode;
  11. import org.junit.jupiter.api.Assertions;
  12. import org.junit.jupiter.api.Test;
  13. import java.util.ArrayList;
  14. import java.util.HashSet;
  15. import java.util.List;
  16. import java.util.UUID;
  17. public abstract class NotificationHookTestBase extends MetricsAdvisorAdministrationClientTestBase {
  18. @Test
  19. abstract void createEmailHook(HttpClient httpClient, MetricsAdvisorServiceVersion serviceVersion);
  20. protected static class CreateEmailHookInput {
  21. static final CreateEmailHookInput INSTANCE = new CreateEmailHookInput();
  22. String name = UUID.randomUUID().toString();
  23. String email1 = "simpleuser0@hotmail.com";
  24. String email2 = "simpleuser1@hotmail.com";
  25. String description = "alert_us!";
  26. String externalLink = "https://github.com/Azure/azure-sdk-for-java/wiki";
  27. EmailNotificationHook hook = new EmailNotificationHook(name)
  28. .setEmailsToAlert(new ArrayList<String>() {{
  29. add(email1);
  30. add(email2);
  31. }})
  32. .setDescription(description)
  33. .setExternalLink(externalLink);
  34. }
  35. protected void assertCreateEmailHookOutput(NotificationHook notificationHook) {
  36. Assertions.assertNotNull(notificationHook);
  37. Assertions.assertTrue(notificationHook instanceof EmailNotificationHook);
  38. EmailNotificationHook emailHook = (EmailNotificationHook) notificationHook;
  39. Assertions.assertNotNull(emailHook.getId());
  40. Assertions.assertNotNull(emailHook.getAdmins());
  41. Assertions.assertNotNull(emailHook.getName());
  42. Assertions.assertEquals(CreateEmailHookInput.INSTANCE.description, emailHook.getDescription());
  43. Assertions.assertEquals(CreateEmailHookInput.INSTANCE.externalLink, emailHook.getExternalLink());
  44. List<String> emails = emailHook.getEmailsToAlert();
  45. Assertions.assertNotNull(emails);
  46. Assertions.assertEquals(2, emails.size());
  47. HashSet<String> emailSet = new HashSet<>();
  48. emailSet.addAll(emails);
  49. Assertions.assertTrue(emailSet.contains(CreateEmailHookInput.INSTANCE.email1));
  50. Assertions.assertTrue(emailSet.contains(CreateEmailHookInput.INSTANCE.email2));
  51. }
  52. @Test
  53. abstract void createWebHook(HttpClient httpClient, MetricsAdvisorServiceVersion serviceVersion);
  54. protected static class CreateWebHookInput {
  55. static final CreateWebHookInput INSTANCE = new CreateWebHookInput();
  56. String name = UUID.randomUUID().toString();
  57. String endpoint = "https://httpbin.org/post";
  58. String description = "alert_us!";
  59. String externalLink = "https://github.com/Azure/azure-sdk-for-java/wiki";
  60. String userName = "test";
  61. String password = "testpwd!@#";
  62. HttpHeaders httpHeaders = new HttpHeaders()
  63. .put("x-contoso-id", "123")
  64. .put("x-contoso-name", "contoso");
  65. WebNotificationHook hook = new WebNotificationHook(name, endpoint)
  66. .setDescription(description)
  67. .setExternalLink(externalLink)
  68. .setUserCredentials(userName, password)
  69. .setHttpHeaders(httpHeaders);
  70. }
  71. protected void assertCreateWebHookOutput(NotificationHook notificationHook) {
  72. Assertions.assertNotNull(notificationHook);
  73. Assertions.assertTrue(notificationHook instanceof WebNotificationHook);
  74. WebNotificationHook webHook = (WebNotificationHook) notificationHook;
  75. Assertions.assertNotNull(webHook.getId());
  76. Assertions.assertNotNull(webHook.getAdmins());
  77. Assertions.assertNotNull(webHook.getName());
  78. Assertions.assertEquals(CreateWebHookInput.INSTANCE.description, webHook.getDescription());
  79. Assertions.assertEquals(CreateWebHookInput.INSTANCE.externalLink, webHook.getExternalLink());
  80. Assertions.assertNotNull(webHook.getUsername());
  81. Assertions.assertNotNull(webHook.getPassword());
  82. HttpHeaders httpHeaders = webHook.getHttpHeaders();
  83. Assertions.assertNotNull(httpHeaders);
  84. // TODO: BUG: Service is not returning headers.
  85. // Assertions.assertEquals(CreateWebHookInput.INSTANCE.httpHeaders.getSize(), httpHeaders.getSize());
  86. // Map<String, String> headersMap = httpHeaders.toMap();
  87. // for (HttpHeader httpHeader : CreateWebHookInput.INSTANCE.httpHeaders) {
  88. // Assertions.assertTrue(headersMap.containsKey(httpHeader.getName()));
  89. // Assertions.assertEquals(httpHeader.getValue(), headersMap.get(httpHeader.getName()));
  90. // }
  91. }
  92. @Test
  93. abstract void testListHook(HttpClient httpClient, MetricsAdvisorServiceVersion serviceVersion);
  94. protected static class ListHookInput {
  95. static final ListHookInput INSTANCE = new ListHookInput();
  96. EmailNotificationHook emailHook = new EmailNotificationHook("java_test_" + UUID.randomUUID().toString())
  97. .setEmailsToAlert(new ArrayList<String>() {{
  98. add("simpleuser0@hotmail.com");
  99. }});
  100. WebNotificationHook webHook = new WebNotificationHook("java_test_" + UUID.randomUUID().toString(),
  101. "https://httpbin.org/post");
  102. int pageSize = 1;
  103. }
  104. protected void assertListHookOutput(List<NotificationHook> hooksList) {
  105. if (getTestMode() == TestMode.PLAYBACK) {
  106. // assert random generated hook names only when hitting real service.
  107. return;
  108. }
  109. NotificationHook notificationHook1 = null;
  110. NotificationHook notificationHook2 = null;
  111. for (NotificationHook notificationHook : hooksList) {
  112. if (notificationHook1 != null && notificationHook2 != null) {
  113. break;
  114. }
  115. Assertions.assertNotNull(notificationHook.getName());
  116. if (notificationHook.getName().equals(ListHookInput.INSTANCE.emailHook.getName())) {
  117. notificationHook1 = notificationHook;
  118. } else if (notificationHook.getName().equals(ListHookInput.INSTANCE.webHook.getName())) {
  119. notificationHook2 = notificationHook;
  120. }
  121. }
  122. Assertions.assertNotNull(notificationHook1);
  123. Assertions.assertNotNull(notificationHook2);
  124. Assertions.assertTrue(notificationHook1 instanceof EmailNotificationHook);
  125. Assertions.assertTrue(notificationHook2 instanceof WebNotificationHook);
  126. }
  127. protected void assertPagedListHookOutput(List<PagedResponse<NotificationHook>> hookPageList) {
  128. int cnt = 0;
  129. for (PagedResponse<NotificationHook> hookPage : hookPageList) {
  130. Assertions.assertNotNull(hookPage.getValue());
  131. cnt++;
  132. boolean isLastPage = (cnt == hookPageList.size());
  133. if (!isLastPage) {
  134. Assertions.assertEquals(ListHookInput.INSTANCE.pageSize, hookPage.getValue().size());
  135. }
  136. }
  137. }
  138. }