2626import org .apache .cloudstack .utils .mailing .SMTPMailSender ;
2727import org .apache .logging .log4j .Logger ;
2828import org .junit .Assert ;
29+ import org .junit .Before ;
2930import org .junit .Test ;
3031import org .junit .runner .RunWith ;
32+ import org .mockito .ArgumentCaptor ;
3133import org .mockito .InjectMocks ;
3234import org .mockito .Mock ;
3335import org .mockito .Mockito ;
4850import com .cloud .host .dao .HostDao ;
4951import com .cloud .storage .StorageManager ;
5052
53+ import static org .junit .Assert .assertEquals ;
54+ import static org .junit .Assert .assertNotNull ;
55+ import static org .junit .Assert .assertNull ;
56+ import static org .mockito .ArgumentMatchers .any ;
57+ import static org .mockito .Mockito .verify ;
58+
5159@ RunWith (MockitoJUnitRunner .class )
5260public class AlertManagerImplTest {
5361
@@ -56,7 +64,7 @@ public class AlertManagerImplTest {
5664 AlertManagerImpl alertManagerImplMock ;
5765
5866 @ Mock
59- AlertDao alertDaoMock ;
67+ AlertDao _alertDao ;
6068
6169 @ Mock
6270 private DataCenterDao _dcDao ;
@@ -88,7 +96,16 @@ public class AlertManagerImplTest {
8896 @ Mock
8997 SMTPMailSender mailSenderMock ;
9098
91- private void sendMessage (){
99+ private final String []
recipients =
new String []{
"[email protected] " };
100+ private final String senderAddress =
"[email protected] " ;
101+
102+ @ Before
103+ public void setUp () {
104+ alertManagerImplMock .recipients = recipients ;
105+ alertManagerImplMock .senderAddress = senderAddress ;
106+ }
107+
108+ private void sendMessage () {
92109 try {
93110 DataCenterVO zone = Mockito .mock (DataCenterVO .class );
94111 Mockito .when (zone .getId ()).thenReturn (0L );
@@ -100,47 +117,77 @@ private void sendMessage (){
100117 Mockito .when (cluster .getId ()).thenReturn (1L );
101118 Mockito .when (_clusterDao .findById (1L )).thenReturn (cluster );
102119
103- alertManagerImplMock .sendAlert (AlertManager .AlertType .ALERT_TYPE_CPU , 0 , 1l , 1l , "" , "" );
120+ alertManagerImplMock .sendAlert (AlertManager .AlertType .ALERT_TYPE_CPU , 0 , 1L , 1L , "" , "" );
104121 } catch (UnsupportedEncodingException | MessagingException e ) {
105122 Assert .fail ();
106123 }
107124 }
108125
109126 @ Test
110127 public void sendAlertTestSendMail () {
111- Mockito .doReturn (null ).when (alertDaoMock ).getLastAlert (Mockito .anyShort (), Mockito .anyLong (),
128+ Mockito .doReturn (null ).when (_alertDao ).getLastAlert (Mockito .anyShort (), Mockito .anyLong (),
112129 Mockito .anyLong (), Mockito .anyLong ());
113- Mockito .doReturn (null ).when (alertDaoMock ).persist (Mockito . any ());
114- alertManagerImplMock .recipients = new String [] {"" };
130+ Mockito .doReturn (null ).when (_alertDao ).persist (any ());
131+ alertManagerImplMock .recipients = new String [] {"" };
115132
116133 sendMessage ();
117134
118- Mockito .verify (alertManagerImplMock ).sendMessage (Mockito . any ());
135+ Mockito .verify (alertManagerImplMock ).sendMessage (any ());
119136 }
120137
121138 @ Test
122139 public void sendAlertTestDebugLogging () {
123140 Mockito .doReturn (0 ).when (alertVOMock ).getSentCount ();
124- Mockito .doReturn (alertVOMock ).when (alertDaoMock ).getLastAlert (Mockito .anyShort (), Mockito .anyLong (),
141+ Mockito .doReturn (alertVOMock ).when (_alertDao ).getLastAlert (Mockito .anyShort (), Mockito .anyLong (),
125142 Mockito .anyLong (), Mockito .anyLong ());
126143
127144 sendMessage ();
128145
129146 Mockito .verify (alertManagerImplMock .logger ).debug (Mockito .anyString ());
130- Mockito .verify (alertManagerImplMock , Mockito .never ()).sendMessage (Mockito . any ());
147+ Mockito .verify (alertManagerImplMock , Mockito .never ()).sendMessage (any ());
131148 }
132149
133150 @ Test
134151 public void sendAlertTestWarnLogging () {
135- Mockito .doReturn (null ).when (alertDaoMock ).getLastAlert (Mockito .anyShort (), Mockito .anyLong (),
152+ Mockito .doReturn (null ).when (_alertDao ).getLastAlert (Mockito .anyShort (), Mockito .anyLong (),
136153 Mockito .anyLong (), Mockito .anyLong ());
137- Mockito .doReturn (null ).when (alertDaoMock ).persist (Mockito .any ());
154+ Mockito .doReturn (null ).when (_alertDao ).persist (Mockito .any ());
138155 alertManagerImplMock .recipients = null ;
139156
140157 sendMessage ();
141158
142159 Mockito .verify (alertManagerImplMock .logger , Mockito .times (2 )).warn (Mockito .anyString ());
143- Mockito .verify (alertManagerImplMock , Mockito .never ()).sendMessage (Mockito .any ());
160+ Mockito .verify (alertManagerImplMock , Mockito .never ()).sendMessage (any ());
161+ }
162+
163+ @ Test
164+ public void testSendAlertWithNullParameters () throws MessagingException , UnsupportedEncodingException {
165+ // Given
166+ String subject = "Test Subject" ;
167+ String content = "Test Content" ;
168+ AlertManager .AlertType alertType = AlertManager .AlertType .ALERT_TYPE_MEMORY ;
169+
170+ // When
171+ alertManagerImplMock .sendAlert (alertType , null , null , null , subject , content );
172+
173+ // Then
174+ ArgumentCaptor <AlertVO > alertCaptor = ArgumentCaptor .forClass (AlertVO .class );
175+ verify (_alertDao ).persist (alertCaptor .capture ());
176+
177+ AlertVO capturedAlert = alertCaptor .getValue ();
178+ assertNotNull ("Captured alert should not be null" , capturedAlert );
179+ assertEquals (0L , capturedAlert .getDataCenterId ());
180+ assertNull (capturedAlert .getPodId ());
181+ assertNull (capturedAlert .getClusterId ());
182+ assertEquals (subject , capturedAlert .getSubject ());
183+ assertEquals (content , capturedAlert .getContent ());
184+ assertEquals (alertType .getType (), capturedAlert .getType ());
185+ }
186+
187+ @ Test (expected = NullPointerException .class )
188+ public void testSendAlertWithNullAlertType () throws MessagingException , UnsupportedEncodingException {
189+ // When
190+ alertManagerImplMock .sendAlert (null , 0 , 1L , 1L , "subject" , "content" );
144191 }
145192
146193 @ Test
0 commit comments