Skip to content

Commit 38e31d4

Browse files
committed
9272/added tests to properties classes
1 parent 94d68f6 commit 38e31d4

File tree

6 files changed

+742
-0
lines changed

6 files changed

+742
-0
lines changed

service-api/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,11 @@
194194
<artifactId>mockwebserver</artifactId>
195195
<scope>test</scope>
196196
</dependency>
197+
<dependency>
198+
<groupId>io.github.hakky54</groupId>
199+
<artifactId>logcaptor</artifactId>
200+
<version>2.12.0</version>
201+
<scope>test</scope>
202+
</dependency>
197203
</dependencies>
198204
</project>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 AzurePropertiesTest {
20+
21+
@Mock
22+
private Environment environment;
23+
24+
@InjectMocks
25+
private AzureProperties azureProperties;
26+
27+
private LogCaptor logCaptor;
28+
29+
@BeforeEach
30+
void setUp() {
31+
logCaptor = LogCaptor.forClass(AzureProperties.class);
32+
logCaptor.clearLogs();
33+
}
34+
35+
@Test
36+
void getAzureConnectionString_shouldReturnValue_whenPropertyExists() {
37+
when(environment.getProperty("azure.connection.string"))
38+
.thenReturn("DefaultEndpointsProtocol=https;AccountName=test;");
39+
40+
String result = azureProperties.getAzureConnectionString();
41+
42+
assertEquals("DefaultEndpointsProtocol=https;AccountName=test;", result);
43+
assertTrue(logCaptor.getErrorLogs().isEmpty());
44+
}
45+
46+
@Test
47+
void getAzureConnectionString_shouldThrowException_whenPropertyMissing() {
48+
when(environment.getProperty("azure.connection.string")).thenReturn("");
49+
50+
IllegalStateException ex = assertThrows(IllegalStateException.class,
51+
() -> azureProperties.getAzureConnectionString());
52+
53+
assertEquals(ErrorMessage.AZURE_CONNECTION_STRING_NOT_SET, ex.getMessage());
54+
assertTrue(logCaptor.getErrorLogs()
55+
.contains(ErrorMessage.AZURE_CONNECTION_STRING_NOT_SET));
56+
}
57+
58+
@Test
59+
void getAzureContainerName_shouldReturnValue_whenPropertyExists() {
60+
when(environment.getProperty("azure.container.name"))
61+
.thenReturn("images");
62+
63+
String result = azureProperties.getAzureContainerName();
64+
65+
assertEquals("images", result);
66+
assertTrue(logCaptor.getErrorLogs().isEmpty());
67+
}
68+
69+
@Test
70+
void getAzureContainerName_shouldThrowException_whenPropertyMissing() {
71+
when(environment.getProperty("azure.container.name")).thenReturn("");
72+
73+
IllegalStateException ex = assertThrows(IllegalStateException.class,
74+
() -> azureProperties.getAzureContainerName());
75+
76+
assertEquals(ErrorMessage.AZURE_CONTAINER_NAME_NOT_SET, ex.getMessage());
77+
assertTrue(logCaptor.getErrorLogs()
78+
.contains(ErrorMessage.AZURE_CONTAINER_NAME_NOT_SET));
79+
}
80+
81+
@Test
82+
void validateProperties_shouldLogSuccess_whenAllPropertiesExist() {
83+
when(environment.getProperty("azure.connection.string"))
84+
.thenReturn("connection123");
85+
when(environment.getProperty("azure.container.name"))
86+
.thenReturn("container123");
87+
88+
azureProperties.validateProperties();
89+
90+
assertTrue(logCaptor.getInfoLogs()
91+
.contains("All azure properties validated successfully."));
92+
}
93+
94+
@Test
95+
void validateProperties_shouldThrowException_whenAnyPropertyMissing() {
96+
when(environment.getProperty("azure.connection.string")).thenReturn("");
97+
when(environment.getProperty("azure.container.name")).thenReturn("valid");
98+
99+
assertThrows(IllegalStateException.class,
100+
() -> azureProperties.validateProperties());
101+
102+
assertTrue(logCaptor.getErrorLogs()
103+
.contains(ErrorMessage.AZURE_CONNECTION_STRING_NOT_SET));
104+
}
105+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
.thenReturn("[email protected]");
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+
.thenReturn("[email protected]");
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+
.thenReturn("[email protected]");
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+
.thenReturn("[email protected]");
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+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 GooglePropertiesTest {
20+
21+
@Mock
22+
private Environment environment;
23+
24+
@InjectMocks
25+
private GoogleProperties googleProperties;
26+
27+
private LogCaptor logCaptor;
28+
29+
@BeforeEach
30+
void setUp() {
31+
logCaptor = LogCaptor.forClass(GoogleProperties.class);
32+
logCaptor.clearLogs();
33+
}
34+
35+
@Test
36+
void getGoogleApiKey_shouldReturnValue_whenPropertyExists() {
37+
when(environment.getProperty("external.google.api-key"))
38+
.thenReturn("google-secret-key");
39+
40+
String result = googleProperties.getGoogleApiKey();
41+
42+
assertEquals("google-secret-key", result);
43+
assertTrue(logCaptor.getErrorLogs().isEmpty());
44+
}
45+
46+
@Test
47+
void getGoogleApiKey_shouldThrowException_whenMissing() {
48+
when(environment.getProperty("external.google.api-key")).thenReturn("");
49+
50+
IllegalStateException ex = assertThrows(IllegalStateException.class,
51+
() -> googleProperties.getGoogleApiKey());
52+
53+
assertEquals(ErrorMessage.GOOGLE_API_KEY_NOT_SET, ex.getMessage());
54+
assertTrue(logCaptor.getErrorLogs()
55+
.contains(ErrorMessage.GOOGLE_API_KEY_NOT_SET));
56+
}
57+
58+
@Test
59+
void validateProperties_shouldLogSuccess_whenPropertyValid() {
60+
when(environment.getProperty("external.google.api-key"))
61+
.thenReturn("valid-key");
62+
63+
googleProperties.validateProperties();
64+
65+
assertTrue(logCaptor.getInfoLogs()
66+
.contains("All google properties validated successfully."));
67+
}
68+
69+
@Test
70+
void validateProperties_shouldThrowException_whenPropertyMissing() {
71+
when(environment.getProperty("external.google.api-key")).thenReturn("");
72+
73+
assertThrows(IllegalStateException.class,
74+
() -> googleProperties.validateProperties());
75+
76+
assertTrue(logCaptor.getErrorLogs()
77+
.contains(ErrorMessage.GOOGLE_API_KEY_NOT_SET));
78+
}
79+
}

0 commit comments

Comments
 (0)