1+ package greencity .properties ;
2+
3+ import static org .junit .jupiter .api .Assertions .*;
4+ import static org .mockito .Mockito .when ;
5+ import greencity .constant .ErrorMessage ;
6+ import nl .altindag .log .LogCaptor ;
7+ import org .junit .jupiter .api .BeforeEach ;
8+ import org .junit .jupiter .api .Test ;
9+ import org .junit .jupiter .api .extension .ExtendWith ;
10+ import org .mockito .InjectMocks ;
11+ import org .mockito .Mock ;
12+ import org .mockito .junit .jupiter .MockitoExtension ;
13+ import org .mockito .junit .jupiter .MockitoSettings ;
14+ import org .mockito .quality .Strictness ;
15+ import org .springframework .core .env .Environment ;
16+
17+ @ ExtendWith (MockitoExtension .class )
18+ @ MockitoSettings (strictness = Strictness .LENIENT )
19+ class EmailPropertiesTest {
20+
21+ @ Mock
22+ private Environment environment ;
23+
24+ @ InjectMocks
25+ private EmailProperties emailProperties ;
26+
27+ private LogCaptor logCaptor ;
28+
29+ @ BeforeEach
30+ void setUp () {
31+ logCaptor = LogCaptor .forClass (EmailProperties .class );
32+ logCaptor .clearLogs ();
33+ }
34+
35+ @ Test
36+ void getSenderEmailAddress_shouldReturnValue_whenPropertyExists () {
37+ when (environment .getProperty ("contacts.sender.email-address" ))
38+ 39+
40+ String result = emailProperties .getSenderEmailAddress ();
41+
42+ assertEquals (
"[email protected] " ,
result );
43+ assertTrue (logCaptor .getErrorLogs ().isEmpty ());
44+ }
45+
46+ @ Test
47+ void getSenderEmailAddress_shouldThrowException_whenMissing () {
48+ when (environment .getProperty ("contacts.sender.email-address" )).thenReturn ("" );
49+
50+ IllegalStateException ex = assertThrows (IllegalStateException .class ,
51+ () -> emailProperties .getSenderEmailAddress ());
52+
53+ assertEquals (ErrorMessage .SENDER_EMAIL_ADDRESS_NOT_SET , ex .getMessage ());
54+ assertTrue (logCaptor .getErrorLogs ()
55+ .contains (ErrorMessage .SENDER_EMAIL_ADDRESS_NOT_SET ));
56+ }
57+
58+ @ Test
59+ void getGreenCityOfficeEmailAddress_shouldReturnValue_whenPropertyExists () {
60+ when (environment .getProperty ("contacts.greenoffice.email-address" ))
61+ 62+
63+ String result = emailProperties .getGreenCityOfficeEmailAddress ();
64+
65+ assertEquals (
"[email protected] " ,
result );
66+ assertTrue (logCaptor .getErrorLogs ().isEmpty ());
67+ }
68+
69+ @ Test
70+ void getGreenCityOfficeEmailAddress_shouldThrowException_whenMissing () {
71+ when (environment .getProperty ("contacts.greenoffice.email-address" )).thenReturn ("" );
72+
73+ IllegalStateException ex = assertThrows (IllegalStateException .class ,
74+ () -> emailProperties .getGreenCityOfficeEmailAddress ());
75+
76+ assertEquals (ErrorMessage .GREENCITY_OFFICE_EMAIL_ADDRESS_NOT_SET , ex .getMessage ());
77+ assertTrue (logCaptor .getErrorLogs ()
78+ .contains (ErrorMessage .GREENCITY_OFFICE_EMAIL_ADDRESS_NOT_SET ));
79+ }
80+
81+ @ Test
82+ void getTelegramFeedbackEmailAddress_shouldReturnValue_whenPropertyExists () {
83+ when (environment .getProperty ("contacts.tgbot.feedbacks-email-address" ))
84+ 85+
86+ String result = emailProperties .getTelegramFeedbackEmailAddress ();
87+
88+ assertEquals (
"[email protected] " ,
result );
89+ assertTrue (logCaptor .getErrorLogs ().isEmpty ());
90+ }
91+
92+ @ Test
93+ void getTelegramFeedbackEmailAddress_shouldThrowException_whenMissing () {
94+ when (environment .getProperty ("contacts.tgbot.feedbacks-email-address" )).thenReturn ("" );
95+
96+ IllegalStateException ex = assertThrows (IllegalStateException .class ,
97+ () -> emailProperties .getTelegramFeedbackEmailAddress ());
98+
99+ assertEquals (ErrorMessage .TELEGRAM_EMAIL_ADDRESS_NOT_SET , ex .getMessage ());
100+ assertTrue (logCaptor .getErrorLogs ()
101+ .contains (ErrorMessage .TELEGRAM_EMAIL_ADDRESS_NOT_SET ));
102+ }
103+
104+ @ Test
105+ void getSystemEmailAddress_shouldReturnValue_whenPropertyExists () {
106+ when (environment .getProperty ("contacts.authorization.system-email-address" ))
107+ 108+
109+ String result = emailProperties .getSystemEmailAddress ();
110+
111+ assertEquals (
"[email protected] " ,
result );
112+ assertTrue (logCaptor .getErrorLogs ().isEmpty ());
113+ }
114+
115+ @ Test
116+ void getSystemEmailAddress_shouldThrowException_whenMissing () {
117+ when (environment .getProperty ("contacts.authorization.system-email-address" )).thenReturn ("" );
118+
119+ IllegalStateException ex = assertThrows (IllegalStateException .class ,
120+ () -> emailProperties .getSystemEmailAddress ());
121+
122+ assertEquals (ErrorMessage .SYSTEM_EMAIL_ADDRESS_NOT_SET , ex .getMessage ());
123+ assertTrue (logCaptor .getErrorLogs ()
124+ .contains (ErrorMessage .SYSTEM_EMAIL_ADDRESS_NOT_SET ));
125+ }
126+
127+ @ Test
128+ void validateProperties_shouldLogSuccess_whenAllPropertiesValid () {
129+ when (
environment .
getProperty (
"contacts.sender.email-address" )).
thenReturn (
"[email protected] " );
130+ when (
environment .
getProperty (
"contacts.greenoffice.email-address" )).
thenReturn (
"[email protected] " );
131+ when (
environment .
getProperty (
"contacts.tgbot.feedbacks-email-address" )).
thenReturn (
"[email protected] " );
132+ when (
environment .
getProperty (
"contacts.authorization.system-email-address" )).
thenReturn (
"[email protected] " );
133+
134+ emailProperties .validateProperties ();
135+
136+ assertTrue (logCaptor .getInfoLogs ()
137+ .contains ("All Email properties validated successfully." ));
138+ }
139+
140+ @ Test
141+ void validateProperties_shouldThrowException_whenAnyPropertyMissing () {
142+ when (environment .getProperty ("contacts.sender.email-address" )).thenReturn ("" );
143+ when (
environment .
getProperty (
"contacts.greenoffice.email-address" )).
thenReturn (
"[email protected] " );
144+ when (
environment .
getProperty (
"contacts.tgbot.feedbacks-email-address" )).
thenReturn (
"[email protected] " );
145+ when (
environment .
getProperty (
"contacts.authorization.system-email-address" )).
thenReturn (
"[email protected] " );
146+
147+ assertThrows (IllegalStateException .class ,
148+ () -> emailProperties .validateProperties ());
149+
150+ assertTrue (logCaptor .getErrorLogs ()
151+ .contains (ErrorMessage .SENDER_EMAIL_ADDRESS_NOT_SET ));
152+ }
153+ }
0 commit comments