Skip to content

Commit 37269be

Browse files
committed
test: refactor and clean up unit tests across multiple classes
1 parent 9d6bacf commit 37269be

6 files changed

Lines changed: 112 additions & 97 deletions

File tree

src/test/java/codes/thischwa/dyndrest/model/HostTest.java renamed to src/test/java/codes/thischwa/dyndrest/model/HostEnrichedTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package codes.thischwa.dyndrest.model;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
44

55
import org.junit.jupiter.api.Test;
66

7-
class HostTest {
7+
class HostEnrichedTest {
88

99
@Test
1010
void testGetFullHost() {
Lines changed: 83 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,104 @@
11
package codes.thischwa.dyndrest.model;
22

3-
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNotNull;
7+
import static org.junit.jupiter.api.Assertions.assertNull;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
49

510
import java.net.Inet4Address;
611
import java.net.Inet6Address;
712
import java.net.InetAddress;
813
import java.net.UnknownHostException;
9-
10-
import static org.junit.jupiter.api.Assertions.*;
14+
import org.junit.jupiter.api.Test;
1115

1216
class IpSettingTest {
1317

14-
@Test
15-
void defaultConstructor_isNotSet() {
16-
IpSetting s = new IpSetting();
17-
assertTrue(s.isNotSet());
18-
assertNull(s.getIpv4());
19-
assertNull(s.getIpv6());
20-
assertNull(s.ipv4ToString());
21-
assertNull(s.ipv6ToString());
22-
}
18+
@Test
19+
void defaultConstructor_isNotSet() {
20+
IpSetting s = new IpSetting();
21+
assertTrue(s.isNotSet());
22+
assertNull(s.getIpv4());
23+
assertNull(s.getIpv6());
24+
assertNull(s.ipv4ToString());
25+
assertNull(s.ipv6ToString());
26+
}
2327

24-
@Test
25-
void stringConstructor_setsIpv4() throws UnknownHostException {
26-
IpSetting s = new IpSetting("192.168.1.10");
27-
assertFalse(s.isNotSet());
28-
assertNotNull(s.getIpv4());
29-
assertNull(s.getIpv6());
30-
assertEquals("192.168.1.10", s.ipv4ToString());
31-
}
28+
@Test
29+
void stringConstructor_setsIpv4() throws UnknownHostException {
30+
IpSetting s = new IpSetting("192.168.1.10");
31+
assertFalse(s.isNotSet());
32+
assertNotNull(s.getIpv4());
33+
assertNull(s.getIpv6());
34+
assertEquals("192.168.1.10", s.ipv4ToString());
35+
}
3236

33-
@Test
34-
void stringConstructor_setsIpv6() throws UnknownHostException {
35-
IpSetting s = new IpSetting("2a03:4000:41:32::2");
36-
assertFalse(s.isNotSet());
37-
assertNull(s.getIpv4());
38-
assertNotNull(s.getIpv6());
39-
assertEquals("2a03:4000:41:32:0:0:0:2", s.ipv6ToString());
40-
}
37+
@Test
38+
void stringConstructor_setsIpv6() throws UnknownHostException {
39+
IpSetting s = new IpSetting("2a03:4000:41:32::2");
40+
assertFalse(s.isNotSet());
41+
assertNull(s.getIpv4());
42+
assertNotNull(s.getIpv6());
43+
assertEquals("2a03:4000:41:32:0:0:0:2", s.ipv6ToString());
44+
}
4145

42-
@Test
43-
void dualStringConstructor_setsBoth_whenValid() throws UnknownHostException {
44-
IpSetting s = new IpSetting("10.0.0.1", "2a03:4000:41:32::20");
45-
assertFalse(s.isNotSet());
46-
assertEquals("10.0.0.1", s.ipv4ToString());
47-
assertEquals("2a03:4000:41:32:0:0:0:20", s.ipv6ToString());
48-
}
46+
@Test
47+
void dualStringConstructor_setsBoth_whenValid() throws UnknownHostException {
48+
IpSetting s = new IpSetting("10.0.0.1", "2a03:4000:41:32::20");
49+
assertFalse(s.isNotSet());
50+
assertEquals("10.0.0.1", s.ipv4ToString());
51+
assertEquals("2a03:4000:41:32:0:0:0:20", s.ipv6ToString());
52+
}
4953

50-
@Test
51-
void dualStringConstructor_handlesNullsIndividually() throws UnknownHostException {
52-
IpSetting onlyV4 = new IpSetting("10.0.0.2", null);
53-
assertEquals("10.0.0.2", onlyV4.ipv4ToString());
54-
assertNull(onlyV4.ipv6ToString());
54+
@Test
55+
void dualStringConstructor_handlesNullsIndividually() throws UnknownHostException {
56+
IpSetting onlyV4 = new IpSetting("10.0.0.2", null);
57+
assertEquals("10.0.0.2", onlyV4.ipv4ToString());
58+
assertNull(onlyV4.ipv6ToString());
5559

56-
IpSetting onlyV6 = new IpSetting(null, "2a03:4000:41:32::21");
57-
assertNull(onlyV6.ipv4ToString());
58-
assertEquals("2a03:4000:41:32:0:0:0:21", onlyV6.ipv6ToString());
59-
}
60+
IpSetting onlyV6 = new IpSetting(null, "2a03:4000:41:32::21");
61+
assertNull(onlyV6.ipv4ToString());
62+
assertEquals("2a03:4000:41:32:0:0:0:21", onlyV6.ipv6ToString());
63+
}
6064

61-
@Test
62-
void inetAddressConstructor_setsOnlyMatchingTypes() throws Exception {
63-
InetAddress v4 = InetAddress.getByName("172.16.0.3");
64-
InetAddress v6 = InetAddress.getByName("2a03:4000:41:32::22");
65-
IpSetting s = new IpSetting(v4, v6);
66-
assertEquals("172.16.0.3", s.ipv4ToString());
67-
assertEquals("2a03:4000:41:32:0:0:0:22", s.ipv6ToString());
65+
@Test
66+
void inetAddressConstructor_setsOnlyMatchingTypes() throws Exception {
67+
InetAddress v4 = InetAddress.getByName("172.16.0.3");
68+
InetAddress v6 = InetAddress.getByName("2a03:4000:41:32::22");
69+
IpSetting s = new IpSetting(v4, v6);
70+
assertEquals("172.16.0.3", s.ipv4ToString());
71+
assertEquals("2a03:4000:41:32:0:0:0:22", s.ipv6ToString());
6872

69-
// Pass swapped types to ensure non-matching are ignored
70-
Inet4Address onlyV4 = (Inet4Address) v4;
71-
Inet6Address onlyV6 = (Inet6Address) v6;
72-
IpSetting s2 = new IpSetting(onlyV6, onlyV4); // wrong order on purpose
73-
// constructor should ignore mismatched types, leaving nulls
74-
assertNull(s2.getIpv4());
75-
assertNull(s2.getIpv6());
76-
}
73+
// Pass swapped types to ensure non-matching are ignored
74+
Inet4Address onlyV4 = (Inet4Address) v4;
75+
Inet6Address onlyV6 = (Inet6Address) v6;
76+
IpSetting s2 = new IpSetting(onlyV6, onlyV4); // wrong order on purpose
77+
// constructor should ignore mismatched types, leaving nulls
78+
assertNull(s2.getIpv4());
79+
assertNull(s2.getIpv6());
80+
}
7781

78-
@Test
79-
void equalsAndHashCode_sameIps_areEqual() throws UnknownHostException {
80-
IpSetting a = new IpSetting("10.0.0.1", "2a03:4000:41:32::23");
81-
IpSetting b = new IpSetting("10.0.0.1", "2a03:4000:41:32::23");
82-
assertEquals(a, b);
83-
assertEquals(a.hashCode(), b.hashCode());
84-
}
82+
@Test
83+
void equalsAndHashCode_sameIps_areEqual() throws UnknownHostException {
84+
IpSetting a = new IpSetting("10.0.0.1", "2a03:4000:41:32::23");
85+
IpSetting b = new IpSetting("10.0.0.1", "2a03:4000:41:32::23");
86+
assertEquals(a, b);
87+
assertEquals(a.hashCode(), b.hashCode());
88+
}
8589

86-
@Test
87-
void equalsAndHashCode_differentIps_notEqual() throws UnknownHostException {
88-
IpSetting a = new IpSetting("10.0.0.1", "2a03:4000:41:32::23");
89-
IpSetting b = new IpSetting("10.0.0.2", "2a03:4000:41:32::23");
90-
assertNotEquals(a, b);
91-
}
90+
@Test
91+
void equalsAndHashCode_differentIps_notEqual() throws UnknownHostException {
92+
IpSetting a = new IpSetting("10.0.0.1", "2a03:4000:41:32::23");
93+
IpSetting b = new IpSetting("10.0.0.2", "2a03:4000:41:32::23");
94+
assertNotEquals(a, b);
95+
}
9296

93-
@Test
94-
void toString_containsAddresses_whenSet() throws UnknownHostException {
95-
IpSetting s = new IpSetting("10.0.0.5", "2a03:4000:41:32::24");
96-
String txt = s.toString();
97-
assertTrue(txt.contains("10.0.0.5"));
98-
assertTrue(txt.contains("2a03:4000:41:32"));
99-
}
97+
@Test
98+
void toString_containsAddresses_whenSet() throws UnknownHostException {
99+
IpSetting s = new IpSetting("10.0.0.5", "2a03:4000:41:32::24");
100+
String txt = s.toString();
101+
assertTrue(txt.contains("10.0.0.5"));
102+
assertTrue(txt.contains("2a03:4000:41:32"));
103+
}
100104
}
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
package codes.thischwa.dyndrest.model;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
45

56
import java.net.UnknownHostException;
67
import java.time.LocalDateTime;
78
import org.junit.jupiter.api.Test;
89

910
class UpdateLogTest {
1011

11-
@Test
12-
void testEquals() throws UnknownHostException {
13-
IpSetting setting = new IpSetting("192.168.1.1");
14-
LocalDateTime now = LocalDateTime.now();
15-
UpdateLog l1 = UpdateLog.getInstance(1, setting, UpdateLog.Status.failed, null, now);
16-
UpdateLog l2 = UpdateLog.getInstance(1, setting, UpdateLog.Status.failed, null, now);
17-
assertEquals(l1, l2);
12+
@Test
13+
void testEquals() throws UnknownHostException {
14+
IpSetting setting = new IpSetting("192.168.1.1");
15+
LocalDateTime now = LocalDateTime.now();
16+
UpdateLog l1 = UpdateLog.getInstance(1, setting, UpdateLog.Status.failed, null, now);
17+
UpdateLog l2 = UpdateLog.getInstance(1, setting, UpdateLog.Status.failed, null, now);
18+
assertEquals(l1, l2);
1819

19-
l2.setStatus(UpdateLog.Status.success);
20-
assertNotEquals(l1, l2);
21-
}
20+
l2.setStatus(UpdateLog.Status.success);
21+
assertNotEquals(l1, l2);
22+
}
2223
}

src/test/java/codes/thischwa/dyndrest/model/config/AppConfigTest.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66

7-
import codes.thischwa.dyndrest.AbstractIntegrationTest;
87
import org.junit.jupiter.api.Test;
98
import org.springframework.beans.factory.annotation.Autowired;
10-
11-
class AppConfigTest extends AbstractIntegrationTest {
9+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.test.context.ActiveProfiles;
13+
14+
@SpringBootTest(classes = AppConfigTest.EmptyConfig.class)
15+
@EnableConfigurationProperties(AppConfig.class)
16+
@ActiveProfiles("test")
17+
class AppConfigTest {
18+
19+
@Configuration
20+
static class EmptyConfig {}
1221

1322
@Autowired
1423
private AppConfig appConfig;

src/test/java/codes/thischwa/dyndrest/model/config/ZoneImportConfigTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package codes.thischwa.dyndrest.model.config;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
45

56
import codes.thischwa.dyndrest.AbstractIntegrationTest;
67
import codes.thischwa.dyndrest.model.HostEnriched;

src/test/java/codes/thischwa/dyndrest/model/converter/StringToEnumConverterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package codes.thischwa.dyndrest.model.converter;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
44

55
import codes.thischwa.dyndrest.model.UpdateLog;
66
import org.junit.jupiter.api.Test;

0 commit comments

Comments
 (0)