Skip to content

Commit ec9150b

Browse files
committed
Updated Test Coverage
1 parent 70dd35b commit ec9150b

File tree

2 files changed

+209
-6
lines changed

2 files changed

+209
-6
lines changed

src/test/java/io/nats/NatsClusterTest.java

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
import org.junit.jupiter.api.Test;
1717
import org.junit.jupiter.api.parallel.Isolated;
1818

19+
import java.nio.file.Paths;
1920
import java.util.Collections;
2021
import java.util.List;
2122

2223
import static io.nats.NatsRunnerUtils.*;
23-
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
import static org.junit.jupiter.api.Assertions.*;
2425

2526
@Isolated
2627
public class NatsClusterTest extends TestBase {
@@ -75,4 +76,102 @@ private void _testCreateCluster(List<ClusterInsert> clusterInserts, boolean js)
7576
}
7677
}
7778
}
79+
80+
@Test
81+
public void testClusterNodeConstruction() {
82+
ClusterNode cn = new ClusterNode("name", "server", 1234, 5678);
83+
assertEquals("name", cn.clusterName);
84+
assertEquals("server", cn.serverName);
85+
assertNull(cn.host);
86+
assertEquals(1234, cn.port);
87+
assertEquals(5678, cn.listen);
88+
assertNull(cn.monitor);
89+
assertNull(cn.jsStoreDir);
90+
91+
cn = new ClusterNode("name", "server", 1234, 5678, 9999);
92+
assertEquals("name", cn.clusterName);
93+
assertEquals("server", cn.serverName);
94+
assertNull(cn.host);
95+
assertEquals(1234, cn.port);
96+
assertEquals(5678, cn.listen);
97+
assertNotNull(cn.monitor);
98+
assertEquals(9999, cn.monitor);
99+
assertNull(cn.jsStoreDir);
100+
101+
cn = new ClusterNode("name", "server", 1234, 5678, Paths.get("path"));
102+
assertEquals("name", cn.clusterName);
103+
assertEquals("server", cn.serverName);
104+
assertNull(cn.host);
105+
assertEquals(1234, cn.port);
106+
assertEquals(5678, cn.listen);
107+
assertNull(cn.monitor);
108+
assertNotNull(cn.jsStoreDir);
109+
assertEquals("path", cn.jsStoreDir.toString());
110+
111+
cn = new ClusterNode("name", "server", "host", 1234, 5678, 9999, Paths.get("path"));
112+
assertEquals("name", cn.clusterName);
113+
assertEquals("server", cn.serverName);
114+
assertEquals("host", cn.host);
115+
assertEquals(1234, cn.port);
116+
assertEquals(5678, cn.listen);
117+
assertNotNull(cn.monitor);
118+
assertEquals(9999, cn.monitor);
119+
assertNotNull(cn.jsStoreDir);
120+
assertEquals("path", cn.jsStoreDir.toString());
121+
122+
cn = ClusterNode.builder()
123+
.clusterName("name")
124+
.serverName("server")
125+
.host("host")
126+
.port(1234)
127+
.listen(5678)
128+
.monitor(9999)
129+
.jsStoreDir(Paths.get("path"))
130+
.build()
131+
;
132+
assertEquals("name", cn.clusterName);
133+
assertEquals("server", cn.serverName);
134+
assertEquals("host", cn.host);
135+
assertEquals(1234, cn.port);
136+
assertEquals(5678, cn.listen);
137+
assertNotNull(cn.monitor);
138+
assertEquals(9999, cn.monitor);
139+
assertNotNull(cn.jsStoreDir);
140+
assertEquals("path", cn.jsStoreDir.toString());
141+
}
142+
143+
@Test
144+
public void testClusterInsertCoverage() {
145+
146+
ClusterNode cn = ClusterNode.builder()
147+
.clusterName("name")
148+
.serverName("server")
149+
.host("host")
150+
.port(1234)
151+
.listen(5678)
152+
.monitor(9999)
153+
.jsStoreDir(Paths.get("path"))
154+
.build()
155+
;
156+
157+
ClusterInsert ci = new ClusterInsert(cn, null);
158+
assertEquals("name", ci.node.clusterName);
159+
assertEquals("server", ci.node.serverName);
160+
assertEquals("host", ci.node.host);
161+
assertEquals(1234, ci.node.port);
162+
assertEquals(5678, ci.node.listen);
163+
assertNotNull(ci.node.monitor);
164+
assertEquals(9999, ci.node.monitor);
165+
assertNotNull(ci.node.jsStoreDir);
166+
assertEquals("path", ci.node.jsStoreDir.toString());
167+
168+
assertNull(ci.configInserts);
169+
170+
ci = new ClusterInsert(cn, new String[0]);
171+
assertNull(ci.configInserts);
172+
173+
ci = new ClusterInsert(cn, new String[]{"insert"});
174+
assertNotNull(ci.configInserts);
175+
assertEquals("insert", ci.configInserts[0]);
176+
}
78177
}

src/test/java/io/nats/NatsRunnerUtilsTest.java

Lines changed: 109 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,46 @@
1515

1616
import org.junit.jupiter.api.Test;
1717

18-
import static io.nats.NatsRunnerUtils.DEFAULT_NATS_SERVER;
19-
import static io.nats.NatsRunnerUtils.getResolvedServerPath;
18+
import java.io.IOException;
19+
import java.nio.file.Path;
20+
import java.nio.file.Paths;
21+
import java.util.List;
22+
23+
import static io.nats.NatsRunnerUtils.*;
2024
import static org.junit.jupiter.api.Assertions.*;
2125

2226
public class NatsRunnerUtilsTest extends TestBase {
2327

2428
@Test
25-
public void testGetUriForPort() throws Exception {
29+
public void testGetUriForPort() {
2630
//noinspection deprecation
2731
assertEquals("nats://localhost:1234", NatsRunnerUtils.getURIForPort(1234));
2832
}
2933

3034
@Test
31-
public void testGetNatsServerVersionString() throws Exception {
35+
public void testGetNatsLocalhostUri() {
36+
assertEquals("nats://localhost:1234", NatsRunnerUtils.getNatsLocalhostUri(1234));
37+
}
38+
39+
@Test
40+
public void testGetNatsUri() {
41+
assertEquals("nats://host:1234", NatsRunnerUtils.getNatsUri("host", 1234));
42+
}
43+
44+
@Test
45+
public void testGetLocalhostUri() {
46+
assertEquals("schema://localhost:1234", NatsRunnerUtils.getLocalhostUri("schema", 1234));
47+
}
48+
49+
@Test
50+
public void testGetNatsServerVersionString() {
3251
String v = NatsRunnerUtils.getNatsServerVersionString();
3352
assertNotNull(v);
3453
assertTrue(v.startsWith("nats-server: v"));
3554
}
3655

3756
@Test
38-
public void testGetResolvedServerPath() throws Exception {
57+
public void testGetResolvedServerPath() {
3958
assertEquals(DEFAULT_NATS_SERVER, getResolvedServerPath());
4059

4160
//noinspection deprecation
@@ -46,4 +65,89 @@ public void testGetResolvedServerPath() throws Exception {
4665
NatsRunnerUtils.clearServerPath();
4766
assertEquals(DEFAULT_NATS_SERVER, getResolvedServerPath());
4867
}
68+
69+
@Test
70+
public void testCreateClusterInserts() throws IOException {
71+
Path t = Paths.get("path");
72+
int p = 4220;
73+
int l = 4230;
74+
int m = 4280;
75+
String c = "cluster";
76+
String s = "server";
77+
String h = "127.0.0.1";
78+
79+
validateClusterInserts(3, p, l, c, s, h, null, null, createClusterInserts());
80+
validateClusterInserts(2, p, l, c, s, h, null, null, createClusterInserts(2));
81+
validateClusterInserts(3, p, l, c, s, h, null, t, createClusterInserts(t));
82+
validateClusterInserts(2, p, l, c, s, h, null, t, createClusterInserts(2, t));
83+
84+
c = "clstr";
85+
s = "srvr";
86+
validateClusterInserts(2, p, l, c, s, h, null, null, createClusterInserts(2, c, s));
87+
validateClusterInserts(2, p, l, c, s, h, null, t, createClusterInserts(2, c, s, t));
88+
validateClusterInserts(2, p, l, c, s, h, null, null, createClusterInserts(2, c, s, false));
89+
validateClusterInserts(2, p, l, c, s, h, m, null, createClusterInserts(2, c, s, true));
90+
validateClusterInserts(2, p, l, c, s, h, null, t, createClusterInserts(2, c, s, false, t));
91+
validateClusterInserts(2, p, l, c, s, h, m, t, createClusterInserts(2, c, s, true, t));
92+
93+
p = 5220;
94+
l = 5230;
95+
m = 5280;
96+
h = "host";
97+
c = "cluster";
98+
s = "server";
99+
100+
defaultHost(h);
101+
defaultPortStart(p);
102+
defaultListenStart(l);
103+
defaultMonitorStart(m);
104+
105+
validateClusterInserts(3, p, l, c, s, h, null, null, createClusterInserts());
106+
validateClusterInserts(2, p, l, c, s, h, null, null, createClusterInserts(2));
107+
validateClusterInserts(3, p, l, c, s, h, null, t, createClusterInserts(t));
108+
validateClusterInserts(2, p, l, c, s, h, null, t, createClusterInserts(2, t));
109+
110+
c = "clstr";
111+
s = "srvr";
112+
validateClusterInserts(2, p, l, c, s, h, null, null, createClusterInserts(2, c, s));
113+
validateClusterInserts(2, p, l, c, s, h, null, t, createClusterInserts(2, c, s, t));
114+
validateClusterInserts(2, p, l, c, s, h, null, null, createClusterInserts(2, c, s, false));
115+
validateClusterInserts(2, p, l, c, s, h, m, null, createClusterInserts(2, c, s, true));
116+
validateClusterInserts(2, p, l, c, s, h, null, t, createClusterInserts(2, c, s, false, t));
117+
validateClusterInserts(2, p, l, c, s, h, m, t, createClusterInserts(2, c, s, true, t));
118+
}
119+
120+
private static void validateClusterInserts(int count,
121+
int basePort,
122+
int baseListen,
123+
String cluster,
124+
String server,
125+
String host,
126+
Integer baseMonitor,
127+
Path jsStoreDir,
128+
List<ClusterInsert> list) {
129+
assertEquals(count, list.size());
130+
for (int x = 0; x < list.size(); x++) {
131+
int port = basePort + x;
132+
int listen = baseListen + x;
133+
ClusterInsert ci = list.get(x);
134+
assertEquals(cluster, ci.node.clusterName);
135+
assertEquals(server + x, ci.node.serverName);
136+
assertEquals(port, ci.node.port);
137+
assertEquals(listen, ci.node.listen);
138+
assertEquals(host, ci.node.host);
139+
if (baseMonitor == null) {
140+
assertNull(ci.node.monitor);
141+
}
142+
else {
143+
assertEquals(baseMonitor + x, ci.node.monitor);
144+
}
145+
if (jsStoreDir == null) {
146+
assertNull(ci.node.jsStoreDir);
147+
}
148+
else {
149+
assertEquals(jsStoreDir + "\\" + port, ci.node.jsStoreDir.toString());
150+
}
151+
}
152+
}
49153
}

0 commit comments

Comments
 (0)