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.

SentryErrorReporterTest.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.logger;
  23. import com.dmdirc.util.ClientInfo;
  24. import com.getsentry.raven.Raven;
  25. import com.getsentry.raven.RavenFactory;
  26. import com.getsentry.raven.event.Event;
  27. import com.getsentry.raven.event.interfaces.ExceptionInterface;
  28. import com.getsentry.raven.event.interfaces.SentryInterface;
  29. import java.time.LocalDateTime;
  30. import java.time.ZoneOffset;
  31. import java.util.Optional;
  32. import org.junit.Before;
  33. import org.junit.BeforeClass;
  34. import org.junit.Test;
  35. import org.junit.runner.RunWith;
  36. import org.mockito.ArgumentCaptor;
  37. import org.mockito.Captor;
  38. import org.mockito.Mock;
  39. import org.mockito.runners.MockitoJUnitRunner;
  40. import static org.junit.Assert.assertEquals;
  41. import static org.junit.Assert.assertNotNull;
  42. import static org.junit.Assert.assertTrue;
  43. import static org.mockito.ArgumentMatchers.any;
  44. import static org.mockito.Mockito.mock;
  45. import static org.mockito.Mockito.verify;
  46. import static org.mockito.Mockito.when;
  47. @RunWith(MockitoJUnitRunner.class)
  48. public class SentryErrorReporterTest {
  49. private static RavenFactory ravenFactory;
  50. @Mock private Raven raven;
  51. @Mock private ClientInfo clientInfo;
  52. @Captor private ArgumentCaptor<Event> eventCaptor;
  53. private Optional<Throwable> throwable;
  54. private SentryErrorReporter sentryErrorReporter;
  55. @BeforeClass
  56. public static void setUpClass() {
  57. // Static calls are horrible.
  58. ravenFactory = mock(RavenFactory.class);
  59. RavenFactory.registerFactory(ravenFactory);
  60. }
  61. @Before
  62. public void setUp() {
  63. when(ravenFactory.createRavenInstance(any())).thenReturn(raven);
  64. throwable = Optional.of(new IllegalArgumentException());
  65. sentryErrorReporter = new SentryErrorReporter(clientInfo);
  66. }
  67. @Test
  68. public void testSendsMessage() {
  69. sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, LocalDateTime.now(),
  70. throwable);
  71. verify(raven).sendEvent(eventCaptor.capture());
  72. assertEquals("message 123", eventCaptor.getValue().getMessage());
  73. }
  74. @Test
  75. public void testSendsTimestamp() {
  76. final LocalDateTime date = LocalDateTime.ofEpochSecond(56185920L, 0, ZoneOffset.UTC);
  77. sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, date, throwable);
  78. verify(raven).sendEvent(eventCaptor.capture());
  79. assertEquals(56185920000L, eventCaptor.getValue().getTimestamp().getTime());
  80. }
  81. @Test
  82. public void testSendsLowLevelAsInfo() {
  83. sentryErrorReporter.sendException("message 123", ErrorLevel.LOW, LocalDateTime.now(),
  84. throwable);
  85. verify(raven).sendEvent(eventCaptor.capture());
  86. assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
  87. }
  88. @Test
  89. public void testSendsMediumLevelAsWarning() {
  90. sentryErrorReporter.sendException("message 123", ErrorLevel.MEDIUM, LocalDateTime.now(),
  91. throwable);
  92. verify(raven).sendEvent(eventCaptor.capture());
  93. assertEquals(Event.Level.WARNING, eventCaptor.getValue().getLevel());
  94. }
  95. @Test
  96. public void testSendsHighLevelAsError() {
  97. sentryErrorReporter.sendException("message 123", ErrorLevel.HIGH, LocalDateTime.now(),
  98. throwable);
  99. verify(raven).sendEvent(eventCaptor.capture());
  100. assertEquals(Event.Level.ERROR, eventCaptor.getValue().getLevel());
  101. }
  102. @Test
  103. public void testSendsFatalLevelAsFatal() {
  104. sentryErrorReporter.sendException("message 123", ErrorLevel.FATAL, LocalDateTime.now(),
  105. throwable);
  106. verify(raven).sendEvent(eventCaptor.capture());
  107. assertEquals(Event.Level.FATAL, eventCaptor.getValue().getLevel());
  108. }
  109. @Test
  110. public void testSendsUnknownLevelAsInfo() {
  111. sentryErrorReporter.sendException("message 123", ErrorLevel.UNKNOWN, LocalDateTime.now(),
  112. throwable);
  113. verify(raven).sendEvent(eventCaptor.capture());
  114. assertEquals(Event.Level.INFO, eventCaptor.getValue().getLevel());
  115. }
  116. @Test
  117. public void testAddsExceptionInterface() {
  118. final Exception exception = new IndexOutOfBoundsException("Message blah");
  119. sentryErrorReporter.sendException("message 123", ErrorLevel.UNKNOWN, LocalDateTime.now(),
  120. Optional.of(exception));
  121. verify(raven).sendEvent(eventCaptor.capture());
  122. final SentryInterface iface = eventCaptor.getValue().getSentryInterfaces()
  123. .get(ExceptionInterface.EXCEPTION_INTERFACE);
  124. assertNotNull(iface);
  125. assertTrue(iface instanceof ExceptionInterface);
  126. assertEquals(exception.getMessage(),
  127. ((ExceptionInterface) iface).getExceptions().getFirst().getExceptionMessage());
  128. }
  129. }